#!/usr/bin/python
import argparse
import getpass
import os

from os import mkdir, path
from torrentcatcher import torrentcatcher


def setup(tc):
    config = tc.configreader()
    print "Starting setup..."
    hostname = raw_input("Transmission-remote host [localhost]: ")
    port = raw_input("Transmission-remote port [9091]: ")
    auth = raw_input("Requires authentication [y/N]: ")
    if auth.lower().strip() == "y":
        user = raw_input("Username: ")
        password = getpass.getpass("Password: ")
    downloads = raw_input("Download directory: ")
    new_feed = raw_input("Add a feed now? [y/N]: ")
    if new_feed.lower().strip() == "y":
        new_feed = True
        feed_name = raw_input("Enter name for new feed: ")
        feed_url = raw_input("URL for first feed: ")
    if hostname:
        config['hostname'] = hostname
    if port:
        config['port'] = port
    if auth:
        config['auth'] = True
        config['username'] = user
        config['password'] = password
    print "Saving configuration..."
    if new_feed:
        tc.addfeed(feed_name, feed_url)
    print "Setup complete!"


def main():
    # Sets the location of torrentcatcher files
    homeFolder = os.environ['HOME']
    dataPath = path.join(homeFolder, '.torrentcatcher')
    log = path.join(dataPath, 'torrentcatcher.log')
    config = path.join(dataPath, 'trconfig')
    database = path.join(dataPath, 'torcatch.db')
    # Creates data directory for config file, database, and log file
    if not path.isdir(dataPath):
        mkdir(dataPath)
    # Parsing out arguments for command line input
    parser = argparse.ArgumentParser(prog='torrentcatcher')
    parser.add_argument(
        '-a',
        '--archive',
        nargs='+',
        metavar=('all', 'ID'),
        help=("Moves selected torrents to the archive. Using the argument "
              "'all' will move all currently queued torrents to the archive. "
              "Use the '--list' option to see IDs.")
    )
    parser.add_argument(
        '-C',
        nargs=1,
        metavar='<path to trconfig file>',
        help="Override default config file location."
    )
    parser.add_argument(
        '-d',
        '--download',
        nargs='+',
        metavar=('all', 'ID'),
        help=("Moves selected torrents to Transmission. Using the argument "
              "'all' will move all currently queued torrents to Transmission. "
              "Use the '--list' option to see IDs.")
    )
    parser.add_argument(
        '-D',
        nargs=1,
        metavar='<path to database>',
        help="Overrides default database location."
    )
    parser.add_argument(
        '-f',
        '--add-feed',
        nargs=2,
        metavar=('<name>', '<url>'),
        help="Adds given feed to the database."
    )
    parser.add_argument(
        '-F',
        '--feed',
        help=("Checks all feeds for new torrents to add to the queue. DOES "
              "NOT SEND TO TRANSMISSION."),
        action="store_true"
    )
    parser.add_argument(
        '-l',
        '--list',
        nargs=1,
        choices=['queue', 'archive', 'feeds'],
        help="Lists all items for given category."
    )
    parser.add_argument(
        '-L',
        nargs=1,
        metavar='<path to log file>',
        help="Choose location for log output."
    )
    parser.add_argument(
        '-q',
        '--queue',
        nargs='+',
        metavar=('all', 'ID'),
        help=("Moves selected torrents to the queue. Using the argument 'all' "
              "will move all archived torrents to the queue. Use the "
              "'--list' option to see IDs.")
    )
    parser.add_argument(
        '-Q',
        '--quiet',
        help="Suppresses output.",
        action="store_true"
    )
    parser.add_argument(
        '--search',
        nargs=1,
        choices=['name', 'source', 'id'],
        help=("Search archive and queue for given query. Can search by name, "
              "source, or ID number.")
    )
    parser.add_argument(
        '--setup',
        help="Sets up the database and config in the default location.",
        action="store_true"
    )
    parser.add_argument(
        '--version',
        action='version',
        version='%(prog)s 2.1.0'
    )
    args = parser.parse_args()
    # Override default file locations
    if args.C:
        config = args.C
    if args.D:
        database = args.D
    if args.L:
        database = args.L
    # Turns quiet mode off and on
    quiet = args.quiet
    # Initialize Torrentcatcher class
    tc = torrentcatcher.TorrentCatcher(
        trconf=config,
        trlog=log,
        trquiet=quiet,
        trdb=database
    )
    # Create the configuration file if it does not exist
    tc.configreader()
    # Interprets arguments to their respective functions
    argument = False
    if args.archive:
        argument = True
        tc.archive(args.archive)
    if args.download:
        argument = True
        tc.download(args.download)
    if args.add_feed:
        argument = True
        tc.addfeed(args.add_feed[0], args.add_feed[1])
    if args.feed:
        argument = True
        tc.logger.info(
            '[FEED ONLY] Checking feeds for new torrents to queue')
        tc.feeder()
    if args.list:
        argument = True
        tc.lister(args.list[0])
    if args.queue:
        argument = True
        tc.queue(args.queue)
    if args.search:
        argument = True
        query = raw_input('Enter query: ')
        tc.torsearch(args.search[0], query)
    if args.setup:
        argument = True
        print 'Setting up File locations...'
        setup(tc)
    if not argument:
        tc.torrentcatcher()

if __name__ == '__main__':
    main()
