#!/usr/bin/env python

#
# Copyright (C) 2002 Manuel Estrada Sainz <ranty@debian.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import twisted
import apt_proxy
from twisted.python import usage
import sys, pwd, os

from apt_proxy.apt_proxy_conf import apConfig
from apt_proxy import packages
from apt_proxy.apt_proxy import Factory

packages.aptpkg_dir = '.apt-proxy-import'

class MyOptions(usage.Options):
    optFlags = [
        ['version', 'V', 'print version and quit'],
        ['verbose', 'v', 'give verbose output'],
        ['debug', 'd', 'debug output'],
        ['quiet', 'q', "try not to write messages to stdout"],
        ['recursive', 'r', 'recurse into subdirectories'],
        ['help', 'h'],
        ]
    optParameters = [
        ['config-file', 'c', None, "Configuration file"],
        ]
    longdesc="apt-proxy-import imports .deb files into the apt-proxy cache."

    def __init__(self):
        usage.Options.__init__(self)
    def getSynopsis(self):
        return "Usage: %s [options] <filename> ..." % (os.path.basename(sys.argv[0])) + \
               "\n       %s -r [options] <directory> ..." % (os.path.basename(sys.argv[0]))

    def parseArgs(self, *args):
        if len(args)==0:
            print self.getSynopsis()
            raise usage.UsageError("Specify files to import")
        self.importargs = args
    def opt_version(self):
        print "apt-proxy-import 1.9.x"
        sys.exit(0)

try:
    config = MyOptions()
    config.parseOptions()
except usage.UsageError, ue:
    print '%s: %s' % (sys.argv[0], ue)
    sys.exit(1)

apiConfig = apConfig(config.opts['config-file'])

uid,gid = pwd.getpwnam(apiConfig.username)[2:4]
should_be_uid = pwd.getpwnam(apiConfig.username)[2]
if os.getuid() != should_be_uid:
    try:
        os.setuid(should_be_uid)
    except OSError:
        print "Error, couldn't change to user %s."%(apiConfig.username)
        sys.exit(1)

twisted.python.log.startLogging(sys.stdout)

if config.opts['debug']:
    print 'debug'
    apiConfig.debug='all:9'
    apiConfig.setDebug()
elif config.opts['verbose']:
    print "verbose"
    apiConfig.debug='all:5'
    apiConfig.setDebug()
if config.opts['quiet']:
    apiConfig.debug='all:0'
    apiConfig.setDebug()

factory=Factory(apiConfig)
factory.configurationChanged() # Load backends
for import_dir in config.importargs:
    packages.import_directory(factory, import_dir, config.opts['recursive'])
