#!/usr/bin/env python

#
# Xorg recipe generator.
# Lucas C. Villa Real <lucasvr@gobolinux.org>
# 

import os
import sys
import time
import pycurl

class MetaRecipe :
	def __init__(self, name, version) :
		self.package = name
		self.version = version
		self.latestTarballs = None

	def checkout(self) :
		cmd = "EditRecipe -E %s %s" %(self.package, self.version)
		os.system(cmd)

	def update(self, latestTarballs) :
		self.latestTarballs = latestTarballs
		for arch in [ "i686" ] :
			recipe = "/Files/Compile/LocalRecipes/%s/%s/%s/Recipe" %(self.package, self.version, arch)
			if os.path.exists(recipe) :
				self._updateRecipe(recipe)

	def _updateRecipe(self, recipe) :
		f = open(recipe, "r")
		newf = open(recipe + ".new", "w")
		for line in f.readlines() :
			if line.find("--") > 0 :
				pkgName = line.strip().split("--")[0]
				pkgVersion = line.strip().split("--")[1]
				padding = line[0:line.find(pkgName)]
				if self.latestTarballs.has_key(pkgName) :
					newVersion = self.latestTarballs[pkgName][0]
					newf.write("%s%s--%s\n" %(padding, pkgName, newVersion))
				else :
					newf.write(line)
			else :
				newf.write(line)
		newf.close()
		f.close()
		os.rename(recipe + ".new", recipe)


latestTarballs = {
	"xf86-input-acecad": [ "1.4.2", "http://foo.com/package-1.4.1.tar.gz" ],
    "xf86-input-aiptek": [ "1.3.2", "http://foo.com/package-1.3.2.tar.gz" ],
    "xf86-input-evdev": [ "2.5.0", "http://foo.com/package-2.5.0.tar.gz" ],
    "xf86-input-joystick": [ "1.5.0", "http://foo.com/package-1.5.0.tar.gz" ],
    "xf86-input-keyboard": [ "1.5.0", "http://foo.com/package-1.5.0.tar.gz" ],
    "xf86-input-mouse": [ "1.6.0", "http://foo.com/package-1.6.0.tar.gz" ],
    "xf86-input-synaptics": [ "1.3.2", "http://foo.com/package-1.3.2.tar.gz" ],
    "xf86-input-vmmouse": [ "12.6.10", "http://foo.com/package-12.6.10.tar.gz" ],
    "xf86-input-void": [ "1.3.2", "http://foo.com/package-1.3.2.tar.gz" ],
    "xf86-video-apm": [ "1.2.3", "http://foo.com/package-1.2.3.tar.gz" ],
    "xf86-video-ark": [ "0.7.3", "http://foo.com/package-0.7.3.tar.gz" ],
    "xf86-video-ast": [ "0.91.10", "http://foo.com/package-0.91.10.tar.gz" ],
    "xf86-video-ati": [ "6.13.2", "http://foo.com/package-6.13.2.tar.gz" ],
    "xf86-video-chips": [ "1.2.3", "http://foo.com/package-1.2.3.tar.gz" ],
    "xf86-video-cirrus": [ "1.3.2", "http://foo.com/package-1.3.2.tar.gz" ],
    "xf86-video-dummy": [ "0.3.4", "http://foo.com/package-0.3.4.tar.gz" ],
    "xf86-video-fbdev": [ "0.4.2", "http://foo.com/package-0.4.2.tar.gz" ],
    "xf86-video-geode": [ "2.11.10", "http://foo.com/package-2.11.10.tar.gz" ],
    "xf86-video-glint": [ "1.2.6", "http://foo.com/package-1.2.5.tar.gz" ],
    "xf86-video-i128": [ "1.3.4", "http://foo.com/package-1.3.4.tar.gz" ],
    "xf86-video-i740": [ "1.3.2", "http://foo.com/package-1.3.2.tar.gz" ],
    "xf86-video-intel": [ "2.13.0", "http://foo.com/package-2.13.0.tar.gz" ],
    "xf86-video-mach64": [ "6.8.2", "http://foo.com/package-6.8.2.tar.gz" ],
    "xf86-video-mga": [ "1.4.13", "http://foo.com/package-1.4.13.tar.gz" ],
    "xf86-video-neomagic": [ "1.2.6", "http://foo.com/package-1.2.5.tar.gz" ],
    "xf86-video-newport": [ "0.2.3", "http://foo.com/package-0.2.3.tar.gz" ],
    "xf86-video-nv": [ "2.1.18", "http://foo.com/package-2.1.18.tar.gz" ],
    "xf86-video-r128": [ "6.8.2", "http://foo.com/package-6.8.1.tar.gz" ],
    "xf86-video-rendition": [ "4.2.4", "http://foo.com/package-4.2.4.tar.gz" ],
    "xf86-video-s3": [ "0.6.3", "http://foo.com/package-0.6.3.tar.gz" ],
    "xf86-video-s3virge": [ "1.10.4", "http://foo.com/package-1.10.4.tar.gz" ],
    "xf86-video-savage": [ "2.3.2", "http://foo.com/package-2.3.1.tar.gz" ],
    "xf86-video-siliconmotion": [ "1.7.4", "http://foo.com/package-1.7.4.tar.gz" ],
    "xf86-video-sis": [ "0.10.3", "http://foo.com/package-0.10.3.tar.gz" ],
    "xf86-video-sisusb": [ "0.9.4", "http://foo.com/package-0.9.4.tar.gz" ],
    "xf86-video-suncg3": [ "1.1.2", "http://foo.com/package-1.1.1.tar.gz" ],
    "xf86-video-suncg6": [ "1.1.2", "http://foo.com/package-1.1.1.tar.gz" ],
    "xf86-video-suncg14": [ "1.1.2", "http://foo.com/package-1.1.1.tar.gz" ],
    "xf86-video-sunffb": [ "1.2.2", "http://foo.com/package-1.2.1.tar.gz" ],
    "xf86-video-sunleo": [ "1.2.0", "http://foo.com/package-1.2.0.tar.gz" ],
    "xf86-video-suntcx": [ "1.1.2", "http://foo.com/package-1.1.1.tar.gz" ],
    "xf86-video-tdfx": [ "1.4.3", "http://foo.com/package-1.4.3.tar.gz" ],
    "xf86-video-tga": [ "1.2.2", "http://foo.com/package-1.2.1.tar.gz" ],
    "xf86-video-trident": [ "1.3.4", "http://foo.com/package-1.3.4.tar.gz" ],
    "xf86-video-tseng": [ "1.2.4", "http://foo.com/package-1.2.4.tar.gz" ],
    "xf86-video-v4l": [ "0.2.0", "http://foo.com/package-0.2.0.tar.gz" ],
    "xf86-video-vesa": [ "2.3.0", "http://foo.com/package-2.3.0.tar.gz" ],
    "xf86-video-vmware": [ "11.0.3", "http://foo.com/package-11.0.3.tar.gz" ],
    "xf86-video-voodoo": [ "1.2.4", "http://foo.com/package-1.2.4.tar.gz" ],
    "xf86-video-xgi": [ "1.6.0", "http://foo.com/package-1.6.0.tar.gz" ],
    "xf86-video-xgixp": [ "1.8.0", "http://foo.com/package-1.8.0.tar.gz" ]
}

m = MetaRecipe("Xorg-Driver", "7.6")
m.checkout()
m.update(latestTarballs)
