#!/usr/bin/python -tt

import pakrat
import os, sys
from optparse import OptionParser

parser = OptionParser(version='pakrat %s' % pakrat.__version__,
    usage='\n'.join([
        'pakrat [options]\n',
        'Repositories are declared using two options: name and',
        'url. The "--name" and "--url" options may be repeated',
        'to declare multiple repositories. Repository names and',
        'URLs are matched up according to their position on the',
        'command line.'
    ]))
parser.add_option('--repoversion', default=None,
    help=('The version of the repository to create. By default, the '
	  'repositories will not be versioned.'))
parser.add_option('--name', action='append',
    help='The name of a YUM repository. (repeatable)') 
parser.add_option('--url', action='append',
    help='The source URL to a repository (repeatable)')
parser.add_option('--outdir', type='string', default=os.getcwd(),
    help='The root output directory to store repository data. Default '
         'is the current working directory.')
parser.add_option('--repofile', default=[], action='append',
    help=('The path to a YUM repository configuration file. '
         '(repeatable)'))
parser.add_option('--repodir', default=[], action='append',
    help=('The path to a YUM repos.d-style directory of configs '
          '(repeatable)'))
parser.add_option('--delete', action='store_true', default=False,
    help=('Delete packages that are no longer at the remote source. '
          'This option does nothing if --repoversion is passed.'))

options, args = parser.parse_args()

repos = []

if options.name and options.url:
    if len(options.name) != len(options.url):
        print 'Each repository must have a name and URL.'
        sys.exit(1)
    for repo in zip(options.name, options.url):
        name, url = repo
        repos.append(pakrat.repo(name=name, baseurls=[url]))

if len(repos) < 1 and len(options.repofile) < 1 and len(options.repodir) < 1:
    parser.print_help()
    sys.exit(1)

pakrat.sync(
    basedir=options.outdir,
    repoversion=options.repoversion,
    repos=repos,
    repofiles=options.repofile,
    repodirs=options.repodir,
    delete=options.delete
)
