#!/bin/bash
# Copyright (C) 2001 - Terry Chan
# Copyright 2002-2003 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /home/cvsroot/gentoo-src/portage/bin/quickpkg,v 1.8 2003/07/22 16:54:10 vapier Exp $

# This script tries to quickly create a Gentoo binary package using the
# /var/db/pkg/category/pkg/*  files
#
# Resulting tbz2 file will be created in ${PKGDIR} ...
# default is /usr/portage/packages/All/

if [ "`whoami`" != "root" ] ; then
	echo "You must run this as root"
	exit 1
fi
if [ -z $1 ] ; then
	echo "QUICKPKG ver 1.2"
	echo "USAGE: quickpkg <list of pkgs>"
	echo "    a pkg can be of the form:"
	echo "        - /var/db/pkg/<CATEGORY>/<PKG-VERSION>/"
	echo "        - single depend-type atom ..."
	echo "              if portage can emerge it, quickpkg can make a package"
	echo "              for exact definitions of depend atoms, see ebuild(5)"
	echo
	echo "EXAMPLE:"
	echo "    quickpkg /var/db/pkg/net-www/apache-1.3.27-r1"
	echo "        package up apache, just version 1.3.27-r1"
	echo "    quickpkg apache"
	echo "        package up apache, all versions of apache installed"
	echo "    quickpkg =apache-1.3.27-r1"
	echo "        package up apache, just version 1.3.27-r1"
	exit 1
fi

export PKGDIR="`portageq envvar PKGDIR`"
export PORTAGE_TMPDIR="`portageq envvar PORTAGE_TMPDIR`"

source /sbin/functions.sh

# here we make a package given a little info
# $1 = package-name w/version
# $2 = category
do_pkg() {
	MYDIR="${PORTAGE_TMPDIR}/portage-pkg/$1"
	SRCDIR="/var/db/pkg/$2/$1"

	ebegin "Building package for $1"
	(
		# clean up temp directory
		rm -rf ${MYDIR}

		# get pkg info files
		mkdir -p ${MYDIR}/temp
		cp ${SRCDIR}/* ${MYDIR}/temp/

		# create filelist and a basic tbz2
		cut -f 2 -d " " ${SRCDIR}/CONTENTS >${MYDIR}/filelist
		tar -vjcf ${MYDIR}/bin.tar.bz2 --files-from=${MYDIR}/filelist --no-recursion

		# join together the basic tbz2 and the pkg info files
		xpak ${MYDIR}/temp ${MYDIR}/inf.xpak
		tbz2tool join ${MYDIR}/bin.tar.bz2 ${MYDIR}/inf.xpak ${MYDIR}/$1.tbz2

		# move the final binary package to PKGDIR
		[ -d ${PKGDIR}/All ] ||  mkdir -p ${PKGDIR}/All
		[ -d ${PKGDIR}/$2 ] || mkdir -p ${PKGDIR}/$2
		mv ${MYDIR}/$1.tbz2 ${PKGDIR}/All
		( cd ${PKGDIR}/$2 && ln -s ../All/$1.tbz2 )

		# cleanup again
		rm -rf ${MYDIR}
	) >& ${PORTAGE_TMPDIR}/quickpkglog

	if [ -e ${PKGDIR}/All/$1.tbz2 ] ; then
		rm -f ${PORTAGE_TMPDIR}/quickpkglog
		PKGSTATS="${PKGSTATS}"$'\n'"$(einfo $1: `ls -alh ${PKGDIR}/All/$1.tbz2 | awk '{print $5}'`)"
		eend 0
	else
		cat ${PORTAGE_TMPDIR}/quickpkglog
		PKGSTATS="${PKGSTATS}"$'\n'"$(ewarn $1: not created)"
		eend 1
	fi
}

# here we parse the parameters given to use on the cmdline
export PKGSTATS=""
for x in $@ ; do

	# they gave us full path
	if [ -e ${x}/CONTENTS ] ; then
		pkg="`echo ${x} | cut -d/ -f6`"
		cat="`echo ${x} | cut -d/ -f5`"
		do_pkg ${pkg} ${cat}

	# lets figure out what they want
	else
		DIRLIST="`portageq match / ${x}`"
		if [ -z "${DIRLIST}" ] ; then
			eerror "could not find anything to match ${x}"
			exit 1
		fi

		for d in ${DIRLIST} ; do
			pkg="`echo ${d} | cut -d/ -f2`"
			cat="`echo ${d} | cut -d/ -f1`"
			do_pkg ${pkg} ${cat}
		done
	fi

done

[ -z "${PKGSTATS}" ] \
	&& eerror "No packages found" \
	|| echo $'\n'"$(einfo Packages now in ${PKGDIR}:)${PKGSTATS}"
