inherit package
DEPENDS_prepend="${@["ipkg-utils-native ", ""][(oe.data.getVar('PACKAGES', d, 1) == '')]}"
BOOTSTRAP_EXTRA_RDEPENDS += "ipkg-collateral ipkg ipkg-link"
BOOTSTRAP_EXTRA_DEPENDS  += "ipkg-collateral ipkg ipkg-link"

python package_ipk_fn () {
	from oe import data
	oe.data.setVar('PKGFN', oe.data.getVar('PKG',d), d)
}

python package_ipk_install () {
	import os, sys
	pkg = oe.data.getVar('PKG', d, 1)
	pkgfn = oe.data.getVar('PKGFN', d, 1)
	rootfs = oe.data.getVar('IMAGE_ROOTFS', d, 1)
	ipkdir = oe.data.getVar('DEPLOY_DIR_IPK', d, 1)
	stagingdir = oe.data.getVar('STAGING_DIR', d, 1)
	tmpdir = oe.data.getVar('TMPDIR', d, 1)

	if None in (pkg,pkgfn,rootfs):
		raise oe.build.FuncFailed("missing variables (one or more of PKG, PKGFN, IMAGEROOTFS)")
	try:
		oe.mkdirhier(rootfs)
		os.chdir(rootfs)
	except OSError:
		(type, value, traceback) = sys.exc_info()
		print value
		raise oe.build.FuncFailed

	# Generate ipk.conf if it or the stamp doesnt exist
	conffile = os.path.join(stagingdir,"ipkg.conf")
	if not  os.access(conffile, os.R_OK):
		ipkg_archs = oe.data.getVar('IPKG_ARCHS',d)
		if ipkg_archs is None:
			oe.error("IPKG_ARCHS missing")
			raise FuncFailed
		ipkg_archs = ipkg_archs.split()
		arch_priority = 1

		f = open(conffile,"w")
		for arch in ipkg_archs:
			f.write("arch %s %s\n" % ( arch, arch_priority ))
			arch_priority += 1
		f.write("src local file:%s" % ipkdir)
		f.close()


	if (not os.access(os.path.join(ipkdir,"Packages"), os.R_OK) or
		not os.access(os.path.join(os.path.join(tmpdir, "stamps"),"do_packages"),os.R_OK)):
		ret = os.system('ipkg-make-index -p %s %s ' % (os.path.join(ipkdir, "Packages"), ipkdir))
		if (ret != 0 ):
			raise oe.build.FuncFailed
		f=open(os.path.join(os.path.join(tmpdir, "stamps"),"do_packages"),"w")
		f.close()

	ret = os.system('ipkg-cl  -o %s -f %s update' % (rootfs, conffile))
	ret = os.system('ipkg-cl  -o %s -f %s install %s' % (rootfs, conffile, pkgfn))
	if (ret != 0 ):
		raise oe.build.FuncFailed
}

python package_ipk_do_package_ipk () {
	import copy # to back up env data
	import sys

	workdir = oe.data.getVar('WORKDIR', d, 1)
	if not workdir:
		oe.error("WORKDIR not defined, unable to package")
		return

	import os # path manipulations
	outdir = oe.data.getVar('DEPLOY_DIR_IPK', d, 1)
	if not outdir:
		oe.error("DEPLOY_DIR_IPK not defined, unable to package")
		return
	oe.mkdirhier(outdir)

	dvar = oe.data.getVar('D', d, 1)
	if not dvar:
		oe.error("D not defined, unable to package")
		return
	oe.mkdirhier(dvar)

	packages = oe.data.getVar('PACKAGES', d, 1)
	if not packages:
		oe.debug(1, "PACKAGES not defined, nothing to package")
		return

	tmpdir = oe.data.getVar('TMPDIR', d, 1)
	# Invalidate the packages file
	if os.access(os.path.join(os.path.join(tmpdir, "stamps"),"do_packages"),os.R_OK):
		os.unlink(os.path.join(os.path.join(tmpdir, "stamps"),"do_packages"))

	if packages == []:
		oe.debug(1, "No packages; nothing to do")
		return

	for pkg in packages.split():
		from copy import deepcopy
		localdata = deepcopy(d)
		root = "%s/install/%s" % (workdir, pkg)

		oe.data.setVar('ROOT', '', localdata)
		oe.data.setVar('ROOT_%s' % pkg, root, localdata)
		pkgname = oe.data.getVar('PKG_%s' % pkg, localdata, 1)
		if not pkgname:
			pkgname = pkg
		oe.data.setVar('PKG', pkgname, localdata)

		overrides = oe.data.getVar('OVERRIDES', localdata)
		if not overrides:
			raise oe.build.FuncFailed('OVERRIDES not defined')
		overrides = oe.data.expand(overrides, localdata)
		oe.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)

		oe.data.update_data(localdata)
		basedir = os.path.join(os.path.dirname(root))
		pkgoutdir = outdir
		oe.mkdirhier(pkgoutdir)
		os.chdir(root)
		from glob import glob
		g = glob('*')
		try:
			del g[g.index('CONTROL')]
			del g[g.index('./CONTROL')]
		except ValueError:
			pass
		if not g and not oe.data.getVar('ALLOW_EMPTY', localdata):
			from oe import note
			note("Not creating empty archive for %s-%s-%s" % (pkg, oe.data.getVar('PV', localdata, 1), oe.data.getVar('PR', localdata, 1)))
			continue
		controldir = os.path.join(root, 'CONTROL')
		oe.mkdirhier(controldir)
		try:
			ctrlfile = file(os.path.join(controldir, 'control'), 'w')
		except OSError:
			raise oe.build.FuncFailed("unable to open control file for writing.")

		fields = []
		fields.append(["Version: %s-%s\n", ['PV', 'PR']])
		fields.append(["Description: %s\n", ['DESCRIPTION']])
		fields.append(["Section: %s\n", ['SECTION']])
		fields.append(["Priority: %s\n", ['PRIORITY']])
		fields.append(["Maintainer: %s\n", ['MAINTAINER']])
		fields.append(["Architecture: %s\n", ['PACKAGE_ARCH']])
		fields.append(["Source: %s\n", ['SRC_URI']])

		def pullData(l, d):
			l2 = []
			for i in l:
				l2.append(oe.data.getVar(i, d, 1))
			return l2

		ctrlfile.write("Package: %s\n" % pkgname)
		# check for required fields
		try:
			for (c, fs) in fields:
				for f in fs:
					if oe.data.getVar(f, localdata) is None:
						raise KeyError(f)
				ctrlfile.write(c % tuple(pullData(fs, localdata)))
		except KeyError:
			(type, value, traceback) = sys.exc_info()
			ctrlfile.close()
			raise oe.build.FuncFailed("Missing field for ipk generation: %s" % value)
		# more fields
		rdepends = explode_deps(oe.data.getVar("RDEPENDS", localdata, 1) or "")
		rsuggests = (oe.data.getVar("RSUGGESTS", localdata, 1) or "").split()
		rrecommends = (oe.data.getVar("RRECOMMENDS", localdata, 1) or "").split()
		rprovides = (oe.data.getVar("RPROVIDES", localdata, 1) or "").split()
		if rdepends:
			ctrlfile.write("Depends: " + ", ".join(rdepends) + "\n")
		if rsuggests:
			ctrlfile.write("Suggests: " + ", ".join(rsuggests) + "\n")
		if rrecommends:
			ctrlfile.write("Recommends: " + ", ".join(rrecommends) + "\n")
		if rprovides:
			ctrlfile.write("Provides: " + ", ".join(rprovides) + "\n")
		ctrlfile.close()

		for script in ["preinst", "postinst", "prerm", "postrm"]:
			scriptvar = oe.data.getVar('pkg_%s' % script, localdata, 1)
			if not scriptvar:
				continue
			try:
				scriptfile = file(os.path.join(controldir, script), 'w')
			except OSError:
				raise oe.build.FuncFailed("unable to open %s script file for writing." % script)
			scriptfile.write(scriptvar)
			scriptfile.close()
			os.chmod(os.path.join(controldir, script), 0755)

		conffiles_str = oe.data.getVar("CONFFILES", localdata, 1)
		if conffiles_str:
			try:
				conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
			except OSError:
				raise oe.build.FuncFailed("unable to open conffiles for writing.")
			for f in conffiles_str.split():
				conffiles.write('%s\n' % f)
			conffiles.close()

		os.chdir(basedir)
		ret = os.system("PATH=\"%s\" ipkg-build -o 0 -g 0 %s %s" % (oe.data.getVar("PATH", localdata, 1), pkg, pkgoutdir))
		if ret != 0:
			raise oe.build.FuncFailed("ipkg-build execution failed")

		for script in ["preinst", "postinst", "prerm", "postrm", "control" ]:
			scriptfile = os.path.join(controldir, script)
			try:
				os.remove(scriptfile)
			except OSError:
				pass
		try:
			os.rmdir(controldir)
		except OSError:
			pass
		del localdata
}

EXPORT_FUNCTIONS do_package_ipk

addtask package_ipk after do_package before do_build