#!/usr/bin/python
# Copyright 2006, CRIM, Revolution Linux Inc.
#
# $Id: mille-xterm-bootserver 2076 2006-08-11 14:00:03Z francis $
#
# Authors : Francis Giraldeau <francis.giraldeau@revolutionlinux.com>
#
# This file is part of the MILLE-XTERM project.
#
#			   http://www.revolutionlinux.com/mille-xterm/
#			   http://www.mille.ca/
#
# This program is covered by the GNU General Public License.
# See the COPYING file in the top-level MILLE-XTERM directory.
#
# -------------------------------------------------------------------------


""" 
Command line interface to mille-xterm-bootserver utilities
"""
import os
import sys
import string
try : from inisettings import *
except: from mxterm.inisettings import *


# mxterm is installed in the site-package directory of python 2.4.
# To make shure that it will work with newer version of python
# we add the path before the import : 
if not '/usr/lib/python2.7/site-packages' in sys.path:
	sys.path.insert(0, '/usr/lib/python2.7/site-packages') 

try : import xtermroot, initrd, autoconfig, utils
except: from mxterm import xtermroot, initrd, autoconfig, utils

__author__="Francis Giraldeau <francis.giraldeau@revolutionlinux.com>"

def build_all():
	print "Starting build all"
	build_xtermroot()
	build_initrd()

def build_initrd():
	print "Starting build initrd"
	ini_settings = IniSettings('/etc/mille-xterm/bootserver.ini')
	i = initrd.Initrd(ini_settings)
	i.buildInitrd()

	# Now, copy the kernel and the ramdisk into tftpboot
	#shutil.copy2(i.getInitrdImgPath(),"/tftpboot")
	#sh(["gzip","/tftpboot/initrd.img"])

def build_xtermroot():
	print "Starting build xtermroot"
	xtermroot.build()

def autoconfigure():
	autoconfig.main()

def sync():
	ini_settings = IniSettings('/etc/mille-xterm/bootserver.ini')
	bootserver_master = ini_settings.settings['global']['master_url']
	module = bootserver_master + '::mxboot-server-master'
	cmd = ['rsync','-avzq','--delete',module,'/opt/xtermroot']
	print string.join(cmd)
	utils.execute_cmd(cmd)

def wizard():
	autoconfig.wizard()

def usage(switches):
	print __doc__
	s = "usage : " + sys.argv[0] + " [ " + string.join(switches.keys()," | ") + " ]\n\n"
	for i in switches.keys():
		s += "\t " + i + "  :\t" + switches[i] + "\n"
	print s

if __name__ == "__main__":

	# Verify that we are root
	if os.getuid() != 0:
		print ("Please restart %s with root permissions!") % (sys.argv[0])
		sys.exit(10)

	# Possibles options : build_all, build_initrd, build_xtermroot
	switches = {"build_all":"Build xtermroot and initrd (master and standalone)",
		    "build_initrd":"Build only the initrd (master and standalone)",
		    "build_xtermroot":"Build only the xtermroot (master and standalone)",
		    "sync":"Get the xtermroot from the master (slave)",
		    "autoconfigure":"Configure automaticaly parameters for this bootserver (start here)"}

	# FIXME : should handle args better
	if len(sys.argv)>1:
		if sys.argv[1] in switches.keys():
			eval(sys.argv[1])()
		else : 
			print usage(switches)
	else : 
		print usage(switches)

#	try:
#		runWizardGui()
#	except KeyboardInterrupt, e:
#		pass

