#!/bin/ash
# James Budiono (C) 2013, 2014
# License: GPL Version 3 or later
# Convert paco logs into slackware pkgtool logs and tarball

### config
ARCH=${ARCH:-x86_64}      # arch
VERSION=${VERSION:-1paco} # version number
PKG_TYPE=${PKG_TYPE:-txz} # or tgz or txz

DESC_SRC=${DESC_SRC:-/archive/slack-desc} # location for slack-desc, slack-required, slack-inst ...
OUT_DIR=${OUT_DIR:-/archive/repo} # where the tarball will be kept
ADM_DIR=$ROOT/var/log/packages

###
# get_pkg_name - convert pkgname to its canonical version
# $1 - pkg name
# returns: pkgfull, pkgfullv, pkgversion, pkgshort
get_pkg_name() {
	read pkgfull pkgversion << EOF
$(paco -i $1 | sed -n '2H; /Version:/ {s/Version://;H}; ${g; s/[ \t\n]/ /g; p}')	
EOF
	pkgshort=${pkgfull%-$pkgversion}
	pkgfullv=$pkgfull-$ARCH-$VERSION
}

###
# gen_pkg - generate pkgtool's log and tarball
# $1 paco pkg name
gen_pkg() {
	echo converting $1
	# 1. get pkg details
	get_pkg_name $1	
	pkgsize=$(paco -sk $1 | awk '{print $1}')
	{	echo "FILE LIST:"
		echo "./"
		paco -fz $1 | sed 's|/||'
	} > /tmp/pkg.$$
	
	# 2. prepare metadata
	rm -rf /install 2> /dev/null
	mkdir /install
	touch /install/slack-desc
	[ -e $DESC_SRC/$pkgshort-desc ] && slackdesc $pkgshort $pkgversion \
	"($(head -n1 $DESC_SRC/$pkgshort-desc))" "$(tail -n +2 $DESC_SRC/$pkgshort-desc)" "" > /install/slack-desc
	[ -e $DESC_SRC/$pkgshort-required ] && cp $DESC_SRC/$pkgshort-required /install/slack-required
	[ -e $DESC_SRC/$pkgshort-inst ] && cp $DESC_SRC/$pkgshort-inst /install/doinst.sh && chmod +x /install/doinst.sh
	
	# 3. convert log
	echo create log for $1	
	cat - /install/slack-desc /tmp/pkg.$$ > $ADM_DIR/$pkgfullv << EOF
PACKAGE NAME:     $pkgfullv
COMPRESSED PACKAGE SIZE:     ${pkgsize}K
UNCOMPRESSED PACKAGE SIZE:     ${pkgsize}K
PACKAGE LOCATION: /${pkgfullv}.$PKG_TYPE
PACKAGE DESCRIPTION:
EOF

	# 4. make tarball
	echo create tarball for $1
	case $PKG_TYPE in
		tgz) COMPRESSOR=gzip ;;
		tbz) COMPRESSOR=bzip2 ;;
		txz) COMPRESSOR=xz ;;
	esac
	{	tail -n +2 /tmp/pkg.$$
		find /install
	} | tar --no-recursion -C / -T - -c 2> /dev/null | 
	    $COMPRESSOR -c > $OUT_DIR/$pkgfullv.$PKG_TYPE 
	
	# 5. cleanup
	rm -rf /tmp/pkg.$$
	rm -rf /install
}

###
# gen_desc_template
# create blank slack-desc name
# $1 - paco pkg name
gen_desc_template() {
	echo Creating template for $1
	get_pkg_name $1
	touch $DESC_SRC/$pkgshort-desc
}

###
# gen_dep
# generate slapt-get's slack-required package from ldd 
# note: this isn't too smart
# $1 - paco pkg name
gen_dep() {
	echo calculating dependency for $1
	get_pkg_name $1
	{
		paco -fz $pkgfull | 
		grep -E "/lib/|/bin/|/sbin/" | 
		xargs ldd 2>/dev/null | 
		sed -n '/=>/ {s/.*=>//;s/(.*//;p}' |
		sort | uniq |
		while read -r p; do
			grep -l $p /var/log/paco/*
		done | sed 's|.*/||' | 
		sort | uniq |
		while read -r p; do
			pp=$(sed -n '/Version:/ {s/.*Version://;s/ //g;p}' /var/log/paco/$p)
			echo ${p%-$pp}
		done | grep -vE "glibc|gcc|$pkgshort"
		paco -fz $pkgfull | grep -q "/lib/python" && echo Python
		paco -fz $pkgfull | grep -q "/lib/perl" && echo perl		 
	} | sort | uniq > $DESC_SRC/$pkgshort-required
}

#### main ####
case $1 in
	--help|help|-h|"")
		echo "paco2pkg - (C) James Budiono 2013, GPL Version 3 or later."
		echo "Convert paco logs to slackware pkgtool log and tarball" 
		echo "Read this script to see what environment variables you can set."
		echo
		echo "Usage: paco2pkg --help|desc|all-desc|pkg|all-pkg|dep|all-dep [\$2 ...]"
		echo "  desc \$2 - create a slack-desc template for pkg \$2"
		echo "  dep \$2 - create a slack-required dependency for pkg \$2"		
		echo "  pkg \$2- create log and tarball for pkg \$2"		
		echo "  \$1 - create log and tarball for pkg \$1"
		echo "  all-desc - create slack-desc templates for all installed pkgs"
		echo "  all-dep - create slack-required dependency for all installed pkgs"				
		echo "  all-pkg - create pkg and tarball for all installed paco pkgs"
		echo 
		;;
		
	desc|-desc|--desc)
		# generate desc template for one pkg
		mkdir -p $DESC_SRC
		while [ "$2" ]; do
			gen_desc_template $2
			shift
		done
		;;
		
	alldesc|all-desc|--all-desc|--alldesc)
		# generate desc template for all pkg
		mkdir -p $DESC_SRC
		rm $DESC_SRC/*-desc
		paco -a1 | while read p; do
			gen_desc_template $p
		done		
		;;
		
	allpkg|all-pkg|--all-pkg|--allpkg)
		# convert all pkg
		mkdir -p $ADM_DIR $OUT_DIR	
		paco -a1 | while read p; do
			gen_pkg $p
		done
		;;
	
	dep|-dep|--dep)
		# generate dependency for one pkg
		mkdir -p $DESC_SRC
		while [ "$2" ]; do
			gen_dep $2
			shift
		done
		;;
		
	all-dep|alldep|--alldep|--all-dep)
		# generate dependency for all pkg
		mkdir -p $DESC_SRC
		paco -a1 | while read p; do
			gen_dep $p
		done
		;;
	
	*)	# convert one package
		mkdir -p $OUT_DIR
		case $1 in
			pkg|-pkg|--pkg) shift ;;
		esac
		
		while [ "$1" ]; do 
			gen_pkg $1
			shift
		done
		;;
esac
