#!/usr/bin/env python

import argparse
import os

from xdg.BaseDirectory import xdg_config_home

from yanktv import __version__, load_config, yank_episodes


DEFAULT_CONFIG_FILE = os.path.join(xdg_config_home, '.yanktvrc')


def main():
    # Parse command line options
    parser = argparse.ArgumentParser(
        description='Yank torrents for the latest episodes of your tv shows.',
    )
    parser.add_argument(
        '-v',
        '--version',
        action='version',
        version='yanktv v{VERSION}'.format(VERSION=__version__),
    )
    parser.add_argument(
        '-c',
        '--config',
        help='path to config file (defaults to XDG_CONFIG_HOME: {0})'.format(DEFAULT_CONFIG_FILE),
        default=DEFAULT_CONFIG_FILE,
        metavar='PATH',
    )
    parser.add_argument(
        '-i',
        '--init',
        help='initiate the database and fill it with episode data, but does NOT download torrents',
        action='store_true',
        default=False,
    )
    args = parser.parse_args()

    # Try to load the config file
    config = load_config(args.config)
    config.INITIATE_DB = args.init

    # Yank all new episodes
    yank_episodes(config)

################################################################################

if __name__ == '__main__':
    main()
