#!/bin/sh
# vim:ts=4
#----------------------------------------------------------------------
# pacoball - Creates binary .tar.bz2 or .tar.gz packages from already
#            installed (and logged by paco) packages.
#----------------------------------------------------------------------
# pacoball is part of paco-1.10.2.
# Copyright (C) 2004-2005 David Rosal <david.rosal@upf.edu>
# For more information visit http://paco.sourceforge.net
#----------------------------------------------------------------------

#----------------------------#
# Bourne shell compatibility #
#----------------------------#

if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
	emulate sh
elif test -n "${BASH_VERSION+set}" && (set -o posix) >/dev/null 2>&1; then
	set -o posix
fi


#---------------------------------------#
# Initialize and parse the command line #
#---------------------------------------#

# Name of the program
case $0 in
	/bin/*sh|/sbin/*sh) PROGRAM=`basename $1`;;
	*) PROGRAM=`basename $0`;;
esac

# Temporary file
tmp=`mktemp -t ${PROGRAM}XXXXXX 2>/dev/null || echo /tmp/$PROGRAM.$$`

# Trap EXIT to remove tmp files before exit
trap 'rm -f ${tmp}*' 0

# Defaults
DIR=.
PKGS=
VERBOSE=
EXPAND=
ZIP=bzip2
LEVEL=-9


while test -n "$1"; do
	
	case $1 in

		-V|--version)
			echo "$PROGRAM (paco-1.10.2)"
			echo "Copyright (C) 2004-2005 David Rosal <david.rosal@upf.edu>"
			exit 0
			;;

		-h|--help)
cat << EOF
$PROGRAM - Creates binary packages from installed (and logged by paco) packages

Usage:
  $PROGRAM [OPTIONS] <packages>

Options:
  -a, --all            Create a pacoball for each logged package.
      --directory=DIR  Create the pacoballs in directory DIR (default is '.').
  -b, --bzip2          Compress with bzip2 (default).
  -g, --gzip           Compress with gzip.
  -1, --fast           Compress faster.
  -9, --best           Compress better (default).
  -x, --expand         Expand the command line package names (as in paco).
  -v, --verbose        Increase output messages.
  -V, --version        Display version information.
  -h, --help           Display this help message.

Note:
  Leading '/' are stripped from the filenames in the pacoballs. Thus they can be
  extracted using the -C tar option.
  
Contact:
  David Rosal <david.rosal@upf.edu>
EOF
			exit 0
			;;

		-a|--all)
			PKGS="$PKGS --all"
			;;
			
		-v|--verbose)
			VERBOSE=--verbose
			;;
			
		-x|--expand)
			EXPAND=--expand
			;;
			
		-b|--bzip2|--bzip|--bz)
			ZIP=bzip2
			;;
			
		-g|--gzip|--gz)
			ZIP=gzip
			;;
		
		--directory|--dir|--directory=*|--dir=*)
			DIR=`expr $1 : '[^=]*=\(.*\)'` || {
				echo "$PROGRAM: Option '$1' requires an argument" >&2
				echo "Try '$PROGRAM --help' for more information" >&2
				exit 1
			}
			;;
	
		-1|-2|-3|-4|-5|-6|-7|-8|-9)
			LEVEL=$1
			;;

		--fast)
			LEVEL=-1
			;;

		--best)
			LEVEL=-9
			;;
			
		-*)
			echo "$PROGRAM: Unrecognized option: '$1'" >&2
			echo "Try '$PROGRAM --help' for more information" >&2
			exit 1
			;;
		
		*)	# Non option argument = Name of a package
			PKGS="$PKGS $1"
			;;
			
	esac
	shift
done

# Solaris tar is very different from GNU's
(tar --help 2>&1 | grep -- '--files-from' >/dev/null 2>&1) \
  && files_from="--files-from=" || files_from="-I "

paco --version >/dev/null || {
	echo "$PROGRAM: Can't find paco executable"
	exit 1
}

# "Absolutize" DIR, and remove trailing and duplicate slashes
expr "$DIR" : '/' >/dev/null || DIR=`pwd`/$DIR
DIR=`expr "$DIR" : '\(.*[^/]\)' '|' '/' | tr -s '/'`

if test -f $DIR; then
	echo "$PROGRAM: $DIR: Not a directory" >&2
	exit 1
fi

if test -z "$PKGS"; then
	echo "$PROGRAM: No input packages" >&2
	echo "Try '$PROGRAM --help' for more information" >&2
	exit 1
fi

PKGS=`paco --verbose $EXPAND $PKGS 2>$tmp.err`
if test -z "$PKGS"; then
	cat $tmp.err >&2
	exit 1
fi
paco --update $VERBOSE $PKGS 2>/dev/null

test -d $DIR || install -d $DIR


for pkg in $PKGS; do

	rm -f $tmp $tmp.err
	
	paco --files --no-package-name $pkg >$tmp 2>$tmp.err || {
		cat $tmp.err >&2
		continue
	}
	rm -f $tmp.err
	test -s $tmp || {
		echo "$pkg: No files logged" >&2
		continue;
	}
	
	tar $VERBOSE cf $DIR/$pkg-paco.tar ${files_from}$tmp 2>$tmp.err || {
		cat $tmp.err >&2
		continue
	}
	$ZIP $LEVEL $VERBOSE $DIR/$pkg-paco.tar
	
done


# Nice job PacoBall!
exit 0


