#!/opt/stack/bin/python

import subprocess
import shutil
import os
import time

file = open('/proc/cmdline', 'r')
args = file.readline().split()
file.close()

if 'boss' in args:
	#
	# we are a boss
	#

	#
	# restart lighttpd 
	#
	if os.path.exists('/run/install/tmp/stack.conf'):
		shutil.copyfile('/run/install/tmp/stack.conf',
			'/tmp/stack.conf')
	else:
		#
		# create a dummy stack.conf
		#
		file = open('/tmp/stack.conf', 'w')
		file.write('var.trackers = ""\n')
		file.write('var.pkgservers = ""\n')
		file.close()

	subprocess.call([ '/opt/lighttpd/sbin/lighttpd', '-f', 
		'/opt/lighttpd/conf/lighttpd.conf', '-m',
		'/opt/lighttpd/lib/' ])

#
# call the real anaconda
#

s = subprocess.Popen(['/usr/sbin/anaconda.real'])
s.wait()

if 'boss' in args:
	#
	# get the repos configured
	#
	# first move all the existing repo files out of the way
	#
	os.system('mv /etc/anaconda.repos.d/*.repo /tmp/')

	#
	# now build a new repo file with all the selected pallets
	#
	import stack.roll

	g = stack.roll.Generator()

	if os.path.exists('/tmp/rolls.xml'):
		g.parse('/tmp/rolls.xml')
	elif os.path.exists('/tmp/pallets.xml'):
		g.parse('/tmp/pallets.xml')

	file = open('/etc/anaconda.repos.d/stacki.repo', 'w')

	baserepo = None
	for pallet in g.rolls:
		(pname, pver, prel, parch, purl, pdiskid) = pallet

		file.write('[%s-%s]\n' % (pname, pver))
		file.write('name=%s %s\n' % (pname, pver))
		file.write('baseurl=http://localhost/install/pallets/%s/%s/redhat/x86_64\n' % (pname, pver))
		file.write('assumeyes=1\n')

		#
		# need to make a "base" repo address
		#
		if pname in [ 'CentOS', 'RHEL', 'os' ]:
			baserepo = 'http://localhost/install/pallets/%s/%s/redhat/x86_64' % (pname, pver)
			
	file.close()

	#
	# we are a boss
	#
	if 'stack-debug' in args:
		#
		# debug development code
		#
		file = open('/tmp/wait', 'w')
		file.write('/usr/sbin/anaconda.real --repo=%s\n' % baserepo)
		file.close()
		while os.path.exists('/tmp/wait'):
			time.sleep(5)

	#
	#
	# kill all instances of 'anaconda.real' and 'Xorg'
	#
	os.system('killall -9 anaconda.real')
	os.system('killall -9 Xorg')

	#
	# if we are a boss, then restart anaconda
	#
	s = subprocess.Popen(['/usr/sbin/anaconda.real', '--repo=%s' % baserepo])
	s.wait()

	if 'stack-debug' in args:
		#
		# debug development code
		#
		file = open('/tmp/wait', 'w')
		file.close()
		while os.path.exists('/tmp/wait'):
			time.sleep(5)
