#!/usr/bin/python
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright 2004, 2005 Red Hat, Inc.
#
# Author: Phil Knirsch
#

import sys, os.path, getopt

PYRPMDIR = "/usr/share/pyrpm"
if not PYRPMDIR in sys.path:
    sys.path.append(PYRPMDIR)
from pyrpm import *
from pyrpm import __version__
from pyrpm.database import getRpmDBFactory

def usage():
    print """
INSTALLING, UPGRADING, AND REMOVING PACKAGES:
    pyrpminstall {-i|--install} [install-options] PACKAGE_FILE ...
    pyrpminstall {-U|--upgrade} [install-options] PACKAGE_FILE ...
    pyrpminstall {-F|--freshen} [install-options] PACKAGE_FILE ...
    pyrpminstall {-e|--erase} [install-options] PACKAGE_FILE ...

general options:
    [-?, --help] [--version]
    [--quiet] [-v, --verbose] [-w, warning] [-d, --debug]
    [--dbpath DIRECTORY] [-r, --root DIRECTORY]

install-options:
    [-h, --hash] [--force] [--oldpackage] [--justdb] [--test]
    [--ignoresize] [--ignorearch]
    [--nodeps] [--nosignature]
    [--noorder] [--noscripts] [--notriggers]
"""


def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "iUFe?vwdhr:", ["install", "upgrade", "freshen", "erase", "help", "verbose", "warning", "debug", "hash", "version", "quiet", "dbpath=", "root=", "force", "ignoresize", "ignorearch", "justdb", "nodeps", "nodigest", "nosignature", "noorder", "noscripts", "notriggers", "oldpackage", "test"])
    except getopt.error, e:
        print "Error parsing command list arguments: %s" % e
        usage()
        return 0

    rpmconfig.nosignature = 0   # By default, rpm checks signatures

    operation = None
    for (opt, val) in opts:
        if opt in ["-i", "--install"]:
            if operation != None:
                print "Only one operation at a time is allowed."
                usage()
                return 0
            operation = OP_INSTALL
        elif opt in ["-U", "--upgrade"]:
            if operation != None:
                print "Only one operation at a time is allowed."
                usage()
                return 0
            operation = OP_UPDATE
        elif opt in ["-F", "--freshen"]:
            if operation != None:
                print "Only one operation at a time is allowed."
                usage()
                return 0
            operation = OP_FRESHEN
        elif opt in ["-e", "--erase"]:
            if operation != None:
                print "Only one operation at a time is allowed."
                usage()
                return 0
            operation = OP_ERASE
        elif opt in ['-?', "--help"]:
            usage()
            return 1
        elif opt in ["-v", "--verbose"]:
            rpmconfig.verbose += 1
        elif opt in ["-w", "--warning"]:
            rpmconfig.warning += 1
        elif opt in ["-d", "--debug"]:
            rpmconfig.debug += 1
        elif opt in ["-r", "--root"]:
            rpmconfig.buildroot = val
        elif opt == "--quiet":
            rpmconfig.debug = 0
            rpmconfig.warning = 0
            rpmconfig.verbose = 0
        elif opt == "--version":
            print "pyrpminstall", __version__
            return 1
        elif opt == "--dbpath":
            rpmconfig.dbpath = val
        elif opt == "--force":
            rpmconfig.force = 1
        elif opt in ["-h", "--hash"]:
            rpmconfig.printhash = 1
        elif opt == "--oldpackage":
            rpmconfig.oldpackage = 1
        elif opt == "--justdb":
            rpmconfig.justdb = 1
            rpmconfig.noscripts = 1
            rpmconfig.notriggers = 1
        elif opt == "--test":
            rpmconfig.test = 1
            rpmconfig.timer = 1
            rpmconfig.noscripts = 1
            rpmconfig.notriggers = 1
        elif opt == "--ignoresize":
            rpmconfig.ignoresize = 1
        elif opt == "--ignorearch":
            rpmconfig.ignorearch = 1
        elif opt == "--nodeps":
            rpmconfig.nodeps = 1
        elif opt == "--nodigest":
            rpmconfig.nodigest = 1
        elif opt == "--nosignature":
            rpmconfig.nosignature = 1
        elif opt == "--noorder":
            rpmconfig.noorder = 1
        elif opt == "--noscripts":
            rpmconfig.noscripts = 1
        elif opt == "--notriggers":
            rpmconfig.notriggers = 1

    if not args:
        print "Error no packages to install"
        usage()
        return 0

    pydb = database.getRpmDBFactory(rpmconfig, rpmconfig.dbpath,
                                          rpmconfig.buildroot)
    control = RpmController(rpmconfig, operation, pydb)
    control.handleFiles(args)
    ops = control.getOperations()
    if ops == None:
        return 0
    if rpmconfig.test:
        rpmconfig.printError("test run stopped")
    else:
        result = control.runOperations(ops)
        pydb.close()
        return result
    return 1

if __name__ == '__main__':
    if not run_main(main):
        sys.exit(1)

# vim:ts=4:sw=4:showmatch:expandtab
