#!/bin/sh
# vim:ts=4
#----------------------------------------------------------------------
# a2paco - Build paco logs from a local dpkg or RPM database.
#----------------------------------------------------------------------
# a2paco 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


# Defaults
DPKGDIR=/var/lib/dpkg
ALL=no
STRIP=no
QUIET=no
PACKAGES=
PM=

# Parse the command line arguments
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 - Create paco logs from a local dpkg or rpm database.

Usage:
  $PROGRAM [OPTIONS] <packages>

Options [default values listed in brackets]:
  -a, --all            Convert the whole database.
      --dpkg-dir=DIR   Dpkg database directory  [$DPKGDIR].
  -q, --quiet          Be silent, except for errors.
  -r, --rpm            Force reading rpm database.
  -d, --dpkg           Force reading dpkg database.
  -s, --strip-release  Remove the release suffix from the name of the
                        packages (e.g. boo-2.0.2-1 --> boo-2.0.1).
  -v, --version        Display version information and exit.
  -h, --help           Display this help message and exit.

Author:
  David Rosal  <david.rosal@upf.edu>
EOF
exit 0;;

		-a|--all)
			ALL=yes;;
			
		-q|--quiet)
			QUIET=yes;;
			
		-r|--rpm)
			PM=rpm;;
			
		-d|--dpkg)
			PM=dpkg;;
			
		-s|--strip-release|--strip)
			STRIP=yes;;
		
		--dpkg-dir)
			if test -z "$2"; then
				echo "$PROGRAM: Option '$1' requires an argument" >&2
				echo "Try '$PROGRAM --help' for more information" >&2
				exit 1
			fi
			DPKGDIR=$2
			shift;;

		--dpkg-dir=*)
			DPKGDIR=`echo $1 | sed -e 's@^[^=]*=@@'`
			if test -z "$DPKGDIR"; then
				echo "$PROGRAM: Option '$1' requires an argument" >&2
				echo "Try '$PROGRAM --help' for more information" >&2
				exit 1
			fi;;
		
		-*)
			echo "$PROGRAM: Unrecognized option: '$1'" >&2
			echo "Try '$PROGRAM --help' for more information" >&2
			exit 1;;
		
		*)	# Non option argument = Name of a package
			PACKAGES="$PACKAGES $1";;
	esac
	shift
done


#-----------#
# Checkings #
#-----------#

# Remove repeated package names
if test -n "$PACKAGES"; then
	for pkg in $PACKAGES; do echo $pkg; done | sort -u
elif test $ALL = no; then
	echo "$PROGRAM: No input packages" >&2
	echo "Try '$PROGRAM --help' for more information" >&2
	exit 1
fi

type dpkg >/dev/null 2>&1 && have_dpkg=yes || have_dpkg=no
type rpm >/dev/null 2>&1 && have_rpm=yes || have_rpm=no
paco --version >/dev/null 2>&1 || {
	echo "$PROGRAM: Can't find paco executable" >&2
	exit 1
}

if test $have_dpkg = no && test $have_rpm = no; then
	echo "$PROGRAM: Can't find dpkg or rpm executables" >&2
	exit 1
elif test -z "$PM"; then
	test $have_dpkg = yes && PM=dpkg || PM=rpm
elif test $PM = dpkg -a $have_dpkg = no || test $PM = rpm -a $have_rpm = no; then
	echo "$PROGRAM: Can't find $PM executable" >&2
	exit 1
fi	

if test $PM = dpkg; then

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

	if test -f $DPKGDIR; then
		echo "$PROGRAM: $DPKGDIR: Not a directory" >&2
		exit 1
	fi
	for dir in $DPKGDIR $DPKGDIR/info; do
		test -d $dir || {
			echo "$PROGRAM: $dir: No such directory" >&2
			exit 1
		}
	done
fi


#-----------------#
# Create the logs #
#-----------------#

# Get the list of all packages logged in the database
if test $PM = dpkg; then
	logged=`(cd $DPKGDIR/info; ls *.list 2>/dev/null) | sed 's/\.list$//'`
else
	logged=`rpm -qa 2>/dev/null`
fi
if test -z "$logged"; then
	echo "$PROGRAM: The $PM database is empty" >&2
	exit 1
fi

# Get the list of packages to be processed
if test $ALL = yes; then
	pkglist=$logged
else
	logged=" $logged "
	pkglist=
	for pkg in $PACKAGES; do
		expr "$logged" : ".* $pkg " >/dev/null \
		  && pkglist="$pkglist $pkg" \
		  || echo "$PROGRAM: package $pkg is not installed" >&2
	done
	test -z "$pkglist" && exit 1
fi

# Process the packages
for pkg in $pkglist; do
	
	name=$pkg
	if test $PM = dpkg; then
		version=`sed -ne \
			"/^Package: *$pkg$/,/^$/{/[Vv]ersion:/s@^[^:]*: *@@p}" \
			$DPKGDIR/status`
		test -n "$version" && name=$name-$version
	fi
	
	# STRIP the release suffix, if needed.
	if test $STRIP = yes; then
		name=`echo $name | sed -e 's@\(-[0-9\.-]*\)-.*@\1@'`
	fi
	
	# List of files
	if test $QUIET = no; then
		printf "Processing package $name... "
	fi
	if test $PM = dpkg; then
		cat $DPKGDIR/info/$pkg.list | paco -lp+ $name
	else
		rpm -ql $pkg | paco -lp+ $name
	fi
	test $QUIET = no && echo ok
done

exit 0


