#!/usr/bin/python

import getopt,sys
sys.path = ["/home/baz/"]+sys.path
import binpkgmain

help='''

binpkgmain: wrapper script to access functions in binpkgmain.py that are
used to maintain a repository of binary gentoo packages.  Compares the 
packages to the contents of the current portage tree and indicates packages
that have dropped out of portage and need upgrading.  Can optionally move 
redundant packages to another directory and/or advise if there is a newer
version of a package in portage.  Can also be used to explicitly generate
a new metadata.idx file in the binary repository.

Useage: binpkgmain [-m | -u | -g | -h | -d | -t]

-m, --move-redundant : move redundant packages
-u, --show-upgrades : show packages for which there is a newer version
-g, --gen-metadata : regenerate metadata.idx file
-h, --help : show this help
-b, --bin-dir : specify binary directory
-d, --debug : show debug
-t, --test-pkg : check to see its safe to remove a package

'''

#
# Globals
#

# Default options
move_red = 0
show_upg = 0
gen_met = 0
bin_dir = 0
test_pkg = ""


#
# Main
#


try:
    opt, args = getopt.getopt( sys.argv[1:], "mughdb:t:", [ "move-redundant", "show-upgrades", "gen-metadata", "help", "debug", "bin-dir=", "test-pkg=" ] )
except getopt.GetoptError:
    print help
    sys.exit(1)

for o,a in opt:
    if o in ( "-h", "--help"):
        print help
        sys.exit()
    if o in ( "-m", "--move-redundant" ):
        move_red = 1
    if o in ( "-u", "--show-upgrades" ):
        show_upg = 1
    if o in ( "-b", "--bin-dir" ):
        bin_dir = a
    if o in ( "-g", "--gen-metadata" ):
    	gen_met = 1
    if o in ( "-d", "--debug" ):
        binpkgmain.DEBUG = 1
    if o in ( "-t", "--test-pkg" ):
        test_pkg = a

# See if we need to generate metadata
if gen_met:
    if bin_dir:
        out = binpkgmain.gen_metadata(a)
    else:
        out = binpkgmain.gen_metadata()
    sys.exit(0)


# Check individual package for removal.
if test_pkg:
    if bin_dir:
        out = binpkgmain.check_indiv_pkg(test_pkg, move_red, bin_dir)
    else:
        out = binpkgmain.check_indiv_pkg(test_pkg, move_red)
    sys.exit(0)

# Invoke check_versions function from binpkgmain with user supplied options.
if bin_dir:
    out = binpkgmain.check_versions( move_red, show_upg, bin_dir )
else:
    out = binpkgmain.check_versions( move_red, show_upg )