#!/usr/bin/python

from __future__ import with_statement
from contextlib import contextmanager

import commands
import ConfigParser
import signal
import os

# top level directory where all packages live (svn checkout of trunk) 
pkg_root = "/home/hisham/code/e17.svn/e/trunk"

pkgs = [
	"imlib2",
	"eina",
	"eet",
	"evas",
	"ecore",
	"embryo",
	"edje",
	"efreet",
	"etk",
	"epsilon",
	"exml",
	"enhance",
	"e_dbus",
	"exhibit",
	"estickies",
	"enity",
	"emprint",
	"e"
]

@contextmanager
def pushd(dir):
	old_dir = os.getcwd()
	os.chdir(dir)
	yield
	os.chdir(old_dir)

class Package:

	"Represents an installable package."

	prefix = "/usr"
	processes = 4

	def __init__(self, pkg_name):
		self.pkg_name = pkg_name
	
	def configure(self, prefix = prefix):
		status = commands.getstatusoutput("./autogen.sh --prefix=" + prefix)
		return not status[0]

	def compile(self, processes = processes):
		status = commands.getstatusoutput("make -j" + `processes`)
		return not status[0]

	def install(self):
		status = commands.getstatusoutput("sudo make install")
		return not status[0]

class Config:

	"Simple wrapper to ConfigParser that allows us to resume state and set config params."

	def __init__(self, filename):
		self.filename = filename
		self.config = ConfigParser.SafeConfigParser()
		self.config.read(os.path.expanduser(filename))

	def get_resume_pkg(self):
		if self.config.has_section('Resume'):
			if self.config.has_option('Resume', 'pkg'):
				pkg = self.config.get('Resume', 'pkg')
				return pkg
		return None

	def set_resume_pkg(self, pkg):
		if not self.config.has_section('Resume'):
			self.config.add_section('Resume')
	
		self.config.set('Resume', 'pkg', pkg)

	def clear_resume_pkg(self):
		if self.config.has_section('Resume'):
			self.config.remove_section('Resume')
	
	def save(self):
		with open(os.path.expanduser(self.filename), 'wb') as configfile:
			self.config.write(configfile)

def update(dir):
	with pushd(dir):
		status = commands.getstatusoutput("svn up")
		return not status[0]

def get_resume_index(pkg, pkgs):
	try:
		index = pkgs.index(pkg)
	except ValueError:		
		index = -1
	return index

def install(pkgs, config, index = 0):
	with pushd(pkg_root):
		for i in range(index, len(pkgs)):
			pkg_name = pkgs[i]
			with pushd(pkg_name):
				print "Compiling " + pkg_name
				pkg = Package(pkg_name)
				if not pkg.configure() or not pkg.compile() or not pkg.install():
					print "Problem compiling " + pkg_name
					config.set_resume_pkg(pkg_name)
					return
	config.clear_resume_pkg()

def handler(signum, frame):
	print "Aborting and saving resume state."

signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGABRT, handler)

config = Config('~/.e/builder.cfg')
pkg = config.get_resume_pkg()
if pkg != None:
	print "Trying to resume from " + pkg
	index = get_resume_index(pkg, pkgs)
	if index == -1:
		print "Invalid package name encountered, starting over..."
		index = 0
else:
	index = 0

print "Updating packages..."
update(pkg_root)

install(pkgs, config, index)

config.save()

print "Welcome to the dark side..."


