#!/usr/bin/python
#
# nrh-ctrl options:
#   -d [directory name]
#    This option indicates where the repository is located.
#    All the work will be done under the specified directory.
#    If not specified, it will assume current directory
#
#   -t [time format]
#    This option explicitly tells nrh-ctrl to use specified time.
#    If this is specified with -e, then nrh-ctrl will delete only
#    relevant files with such time stamp.  And if -t is specified
#    with -g, nrh-ctrl will ignore -t all together.
# 
#   -c [channel name]
#    This tells nrh-ctrl which channel to generate or to erase
#
#   -e
#    This option tells nrh-ctrl to erase current repository
#    If -t option is not given, nrh-ctrl will assume all repository
#    information.
#
#   -g
#    this option tells nrh-ctrl to generate a fresh repository
#    under the directory specified by -d or current directory.
#
#   -h
#    displays help
#
#  NOTE: -g and -e cannot be specified together
#
#   -p
#    This option puts nrh-ctrl in "prep" mode.  The following long options
#    are supported under this mode.
#
#   --gen-header
#    This option tells nrh-ctrl to generate headers for the RPM(s) specified
#    in the argument

import sys
import os
import os.path
import glob
sys.path.append("/usr/share/nrh-up2date/modules/")
import getopt
import time
try:
    import rpm404
    rpm=rpm404
except:
    import rpm
from nrh_pkglist_op import *
from nrh_repository_update import *
from nrh_header_lib import *
from nrh_rpm_lib import *

def usage():
	print '\nChannel update commands :\n'
	print '	-l : find the latest package list and use is as parameter for other operations'
	print '	-u : update the channel'
	print '	-c : cleanup channel directory\n'
	print '	--partial : valid with -u, will not warn about missing rpms'
	print '	--yes : valid with -c, will assume files can be deleted without confirmation\n'
	print 'Unless -l is used, must receive a packagelist filename argument.\n'
	print 'Examples :\n'
	print '	nrh_ctrl -uc --partial --yes redhat-linux-i386-7.3.20030626135559'
	print '	nrh_ctrl -luc --yes\n'
	print 'Notes :\n'
	print '	All processing is ran in the current directory.'
	print '	If \'-uc\' are used together, channel will first be updated, then cleaned up.\n'
	print "Other options include :\n"
	print "-g for generating repository package list"
	print "	--channel specifies the channel name (i.e. redhat-linux-i386-7.3)\n"
	print "-p puts nrh-ctrl in prep mode (Below options can be used with -p option)"
	print "	--gen-header <rpm file name>"

def genHeader(rpmfile):
	arg=[]
	arg=arg+rpmfile
	rpmHeader().Create(arg)

# Default values for directory and currenttime 
DIRECTORY="."
TIMETOUSE=0

# The following three values are used to determine the nrh-ctrl mode
# Whether to generate or erase or erase then generate
GENERATE=0
CHANNEL=""
PREP=0
HEADER=0
USE_LATEST_PKGLIST=0
UPDATE_REP=0
CLEAN_REP=0

if not len(sys.argv) > 1:
	usage()
	sys.exit(1)

try:
	optlist,args=getopt.getopt(sys.argv[1:],"d:t:r:gpluc",['help','partial','yes','gen-header=','channel='])
except getopt.error, msg:
	print 'Error while parsing command line :',msg
	usage()
	sys.exit(1)

for i in optlist:
	if i[0] == "-d":
		DIRECTORY=i[1]
	if i[0] == "-t":
		TIMETOUSE=i[1]
	if i[0] == "--channel":
		CHANNEL=i[1]
	if i[0] == "-g":
		GENERATE=1
	if i[0] == "-p":
		PREP=1
	if i[0] == '-l':
		USE_LATEST_PKGLIST=1
	if i[0] == '-u':
		UPDATE_REP=1
	if i[0] == '-c':
		CLEAN_REP=1
	if i[0] == "--gen-header":
		HEADER=1
		rpmname=args
		rpmname.insert(0,"%s"%i[1])
	if i[0] == "--help":
		usage()
		sys.exit(0)

# FIXME: prep mode supercedes other modes.
#	 make sure to reflect that here
if PREP:
	#FIXME:  I have to handle the case where a user passes in RPM names as well as
	#        a directory name that contains RPM files.
	if HEADER:
		genHeader(rpmname)
	else:
		usage()
		sys.exit(1)
else:
	if GENERATE:
		if not CHANNEL:
			print "Cannot generate package list without channel name, please use --channel=channelname"
			usage()
			sys.exit(1)

		currentTime=generate_pkglist().run(CHANNEL)

	if USE_LATEST_PKGLIST:
		locate_latest_pkglist().run(optlist,args)

	if UPDATE_REP:
		update_repository().run(optlist,args)

	if CLEAN_REP:
		clean_repository().run(optlist,args)
