#!/usr/bin/env python
# coding: utf-8

import argparse
import sys

from feed2maildir import VERSION
from feed2maildir.converter import Converter
from feed2maildir.loader import Loader
from feed2maildir.reader import Reader

def main():
    parser = argparse.ArgumentParser(description='Convert Feeds to a maildir',
                                     epilog='Feed2Maildir '+ VERSION)
    parser.add_argument('-c', metavar='<file>',
                        help='override the config file location (~/.f2mrc)')
    parser.add_argument('-d', metavar='<file>',
                        help='override the database file location (~/.f2mdb)')
    parser.add_argument('-m', metavar='<dir>',
                        help='override the maildir location (None)')
    parser.add_argument('-s', action='store_true',
                        help='strip HTML from the feed content')
    parser.add_argument('-l', action='store_true',
                        help='just write the links without the content')
    parser.add_argument('-S', metavar='<program>',
                        dest='strip_program', default=None,
                        help='strip HTML from the feed content using an external program')
    args = vars(parser.parse_args())

    if args['c']:
        loader = Loader(config=args['c'])
    else:
        loader = Loader()
    config = loader.config
    reader = Reader(config['feeds'])

    if args['d']:
        db = args['d']
    else:
        db = config['db']
    if args['m']:
        maildir = args['m']
    else:
        maildir = config['maildir']

    strip = args['s']
    if args['strip_program']:
        strip = True

    converter = Converter(db=db, maildir=maildir, strip=strip,
                          strip_program=args['strip_program'],
                          links=args['l'])
    converter.load(reader.feeds)
    converter.run()

if __name__ == '__main__':
    main()

