#!/bin/bash
#
#   repo-add - add a package to a given repo database file
#   repo-remove - remove a package entry from a given repo database file
#   @configure_input@
#
#   Copyright (c) 2006-2008 Aaron Griffin <aaron@archlinux.org>
#   Copyright (c) 2007-2008 Dan McGee <dan@archlinux.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

# gettext initialization
export TEXTDOMAIN='pacman'
export TEXTDOMAINDIR='@localedir@'

myver='@PACKAGE_VERSION@'
confdir='@sysconfdir@'

QUIET=0
REPO_DB_FILE=""

# ensure we have a sane umask set
umask 0022

msg() {
	local mesg=$1; shift
	printf "==> ${mesg}\n" "$@" >&1
}

msg2() {
	[ $QUIET -ne 0 ] && return
	local mesg=$1; shift
	printf "  -> ${mesg}\n" "$@" >&1
}

warning() {
	local mesg=$1; shift
	printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
}

error() {
	local mesg=$1; shift
	printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
}

# print usage instructions
usage() {
	printf "repo-add, repo-remove (pacman) %s\n\n" "$myver"
	printf "$(gettext "Usage: repo-add [-q] <path-to-db> <package> ...\n")"
	printf "$(gettext "Usage: repo-remove [-q] <path-to-db> <packagename> ...\n\n")"
	printf "$(gettext "\
repo-add will update a package database by reading a package file.\n\
Multiple packages to add can be specified on the command line.\n\n")"
	printf "$(gettext "\
repo-remove will update a package database by removing the package name\n\
specified on the command line from the given repo database. Multiple\n\
packages to remove can be specified on the command line.\n\n")"
	printf "$(gettext "\
Use the -q/--quiet flag to minimize output to basic messages, warnings,\n\
and errors\n\n")"
	echo "$(gettext "Example:  repo-add /path/to/repo.db.tar.gz pacman-3.0.0.pkg.tar.gz")"
	echo "$(gettext "Example:  repo-remove /path/to/repo.db.tar.gz kernel26")"
}

version() {
	printf "repo-add, repo-remove (pacman) %s\n\n" "$myver"
	printf "$(gettext "\
Copyright (C) 2006-2008 Aaron Griffin <aaron@archlinux.org>.\n\
Copyright (c) 2007-2008 Dan McGee <dan@archlinux.org>.\n\n\
This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n")"
}

# test if a file is a repository DB
test_repo_db_file () {
	if bsdtar -tf "$REPO_DB_FILE" | grep -q "/desc"; then
		return 0 # YES
	else
		return 1 # NO
	fi
}

# write a list entry
#		arg1 - Entry name
#		arg2 - List
#		arg3 - File to write to
write_list_entry() {
	if [ -n "$2" ]; then
		echo "%$1%" >>$3
		echo -e $2 >>$3
	fi
}

# write a delta entry to the pacman database
#   arg1 - path to delta
db_write_delta()
{
	# blank out all variables and set deltafile to absolute path
	local deltafile=$($realpath "$1")
	local filename=$(basename "$deltafile")
	local deltavars pkgname fromver tover arch csize md5sum

	# format of the delta filename:
	# (package)-(fromver)_to_(tover)-(arch).delta
	deltavars=( $(echo "$filename" | sed -e 's/\(.*\)-\(.*-.*\)_to_\(.*-.*\)-\(.*\).delta/\1 \2 \3 \4/') )
	pkgname=${deltavars[0]}
	fromver=${deltavars[1]}
	tover=${deltavars[2]}
	arch=${deltavars[3]}

	# get md5sum and size of delta
	md5sum="$(openssl dgst -md5 "$deltafile" | awk '{print $NF}')"
	csize=$(@SIZECMD@ "$deltafile")

	# ensure variables were found
	if [ -z "$pkgname" -o -z "$fromver" -o -z "$tover" -o -z "$arch" ]; then
		return 1
	fi

	# add the entry for this delta file
	echo -e "$fromver $tover $csize $filename $md5sum" >>deltas
} # end db_write_delta


# write an entry to the pacman database
#   arg1 - path to package
db_write_entry()
{
	# blank out all variables and set pkgfile to an absolute path
	local pkgfile=$($realpath "$1")
	local pkgname pkgver pkgdesc url builddate packager csize size \
		group depend backup license replaces provides conflict force \
		_groups _depends _backups _licenses _replaces _provides _conflicts \
		startdir optdepend _optdepends

	local OLDIFS="$IFS"
	# IFS (field separator) is only the newline character
	IFS="
"

	# read info from the zipped package
	local line
	for line in $(bsdtar -xOf "$pkgfile" .PKGINFO | \
		grep -v "^#" | sed 's|\(\w*\)\s*=\s*\(.*\)|\1="\2"|'); do
		eval "$line"
		case "$line" in
			group=*)    _groups="$_groups$group\n" ;;
			depend=*)   _depends="$_depends$depend\n" ;;
			backup=*)   _backups="$_backups$backup\n" ;;
			license=*)  _licenses="$_licenses$license\n" ;;
			replaces=*) _replaces="$_replaces$replaces\n" ;;
			provides=*) _provides="$_provides$provides\n" ;;
			conflict=*) _conflicts="$_conflicts$conflict\n" ;;
			optdepend=*) _optdepends="$_optdepends$optdepend\n" ;;
		esac
	done

	IFS=$OLDIFS

	# get compressed size of package
	csize=$(@SIZECMD@ "$pkgfile")

	startdir=$(pwd)
	pushd "$gstmpdir" 2>&1 >/dev/null

	# ensure $pkgname and $pkgver variables were found
	if [ -z "$pkgname" -o -z "$pkgver" ]; then
		error "$(gettext "Invalid package file '%s'.")" "$pkgfile"
		popd 2>&1 >/dev/null
		return 1
	fi

	# remove an existing entry if it exists, ignore failures
	db_remove_entry "$pkgname"

	# create package directory
	mkdir "$pkgname-$pkgver"
	cd "$pkgname-$pkgver"

	# create desc entry
	msg2 "$(gettext "Creating 'desc' db entry...")"
	echo -e "%FILENAME%\n$(basename "$1")\n" >>desc
	echo -e "%NAME%\n$pkgname\n" >>desc
	echo -e "%VERSION%\n$pkgver\n" >>desc
	[ -n "$pkgdesc" ] && echo -e "%DESC%\n$pkgdesc\n" >>desc
	write_list_entry "GROUPS" "$_groups" "desc"
	[ -n $csize ] && echo -e "%CSIZE%\n$csize\n" >>desc
	[ -n $size ] && echo -e "%ISIZE%\n$size\n" >>desc

	# compute checksums
	msg2 "$(gettext "Computing md5 checksums...")"
	echo -e "%MD5SUM%\n$(openssl dgst -md5 "$pkgfile" | awk '{print $NF}')\n" >>desc

	[ -n "$url" ] && echo -e "%URL%\n$url\n" >>desc
	write_list_entry "LICENSE" "$_licenses" "desc"
	[ -n "$arch" ] && echo -e "%ARCH%\n$arch\n" >>desc
	[ -n "$builddate" ] && echo -e "%BUILDDATE%\n$builddate\n" >>desc
	[ -n "$packager" ] && echo -e "%PACKAGER%\n$packager\n" >>desc
	write_list_entry "REPLACES" "$_replaces" "desc"
	[ -n "$force" ] && echo -e "%FORCE%\n" >>desc

	# create depends entry
	msg2 "$(gettext "Creating 'depends' db entry...")"
	write_list_entry "DEPENDS" "$_depends" "depends"
	write_list_entry "CONFLICTS" "$_conflicts" "depends"
	write_list_entry "PROVIDES" "$_provides" "depends"
	write_list_entry "OPTDEPENDS" "$_optdepends" "depends"

	# create deltas entry if there are delta files
	for delta in $startdir/$pkgname-*-*_to_*-*-$arch.delta; do
		# This for loop also pulls in all files that start with the current package
		# name and are followed by a -whatever. For instance, running this loop for
		# gcc would also grab gcc-libs. To guard against this, compare the package
		# name of the delta to the current package name.
		local filename=$(basename "$delta")
		local dpkgname="$(echo "$filename" | sed -e 's/\(.*\)-.*-.*_to_.*-.*-.*.delta/\1/')"
		if [ "$pkgname" = "$dpkgname" -a -f "$delta" ]; then
			# create deltas file if it does not already exist
			if [ ! -f "deltas" ]; then
				msg2 "$(gettext "Creating 'deltas' db entry...")"
				echo -e "%DELTAS%" >>deltas
			fi

			# write this delta entry
			if db_write_delta "$delta"; then
				msg2 "$(gettext "Added delta '%s'")" "$(basename "$delta")"
			else
				warning "$(gettext "Could not add delta '%s'")" "$(basename "$delta")"
			fi
		fi
	done
	# add the final newline
	[ -f "deltas" ] && echo -e "" >>deltas

	# preserve the modification time
	touch -r "$pkgfile" desc depends
	[ -f "deltas" ] && touch -r "$pkgfile" deltas

	popd 2>&1 >/dev/null
} # end db_write_entry

# remove existing entries from the DB
#   arg1 - package name
db_remove_entry() {
	pushd "$gstmpdir" 2>&1 >/dev/null

	# remove any other package in the DB with same name
	local existing
	for existing in *; do
		if [ "${existing%-*-*}" = "$1" ]; then
			msg2 "$(gettext "Removing existing package '%s'...")" "$existing"
			rm -rf "$existing"
		fi
	done

	popd 2>&1 >/dev/null
} # end db_remove_entry

# PROGRAM START

# determine whether we have gettext; make it a no-op if we do not
if [ ! $(type -t gettext) ]; then
	gettext() {
		echo "$@"
	}
fi

# check for help flags
if [ "$1" = "-h" -o "$1" = "--help" ]; then
	usage
	exit 0
fi

# check for version flags
if [ "$1" = "-V" -o "$1" = "--version" ]; then
	version
	exit 0
fi

# check for correct number of args
if [ $# -lt 2 ]; then
	usage
	exit 1
fi

# check for and store the name of a realpath-like program
if [ $(type -t realpath) ]; then
	realpath='realpath'
elif [ $(type -t readlink) ]; then
	realpath='readlink -f'
else
	error "$(gettext "Either realpath or readlink are required by repo-add.")"
	exit 1 # $E_MISSING_PROGRAM
fi

# main routine
gstmpdir=$(mktemp -d /tmp/repo-tools.XXXXXXXXXX) || (\
	error "$(gettext "Cannot create temp directory for database building.")"; \
	exit 1)

# figure out what program we are
cmd="$(basename $0)"
if [ "$cmd" != "repo-add" -a "$cmd" != "repo-remove" ]; then
	error "$(gettext "Invalid command name '%s' specified.")" "$cmd"
	exit 1
fi

success=0
# parse arguments
for arg in "$@"; do
	if [ "$arg" == "--force" -o "$arg" == "-f" ]; then
		warning "$(gettext "the -f and --force options are no longer recognized")"
		msg2 "$(gettext "use options=(force) in the PKGBUILD instead")"
	elif [ "$arg" == "--quiet" -o "$arg" == "-q" ]; then
		QUIET=1
	elif [ -z "$REPO_DB_FILE" ]; then
		# store absolute path to repo DB
		REPO_DB_FILE=$($realpath "$arg")
		if [ -f "$REPO_DB_FILE" ]; then
			if ! test_repo_db_file $cmd; then
				error "$(gettext "Repository file '%s' is not a proper pacman database.")" "$REPO_DB_FILE"
				exit 1
			fi
			msg "$(gettext "Extracting database to a temporary location...")"
			bsdtar -xf "$REPO_DB_FILE" -C "$gstmpdir"
		elif [ "$cmd" == "repo-remove" ]; then
			error "$(gettext "Repository file '%s' was not found.")" "$REPO_DB_FILE"
			exit 1
		fi
	else
		if [ "$cmd" == "repo-add" ]; then
			if [ -f "$arg" ]; then
				if ! bsdtar -tf "$arg" .PKGINFO 2>&1 >/dev/null; then
					error "$(gettext "'%s' is not a package file, skipping")" "$arg"
				else
					msg "$(gettext "Adding package '%s'")" "$arg"

					if db_write_entry "$arg"; then
						success=1
					fi
				fi
			else
				error "$(gettext "Package '%s' not found.")" "$arg"
			fi
		elif [ "$cmd" == "repo-remove" ]; then
			msg "$(gettext "Searching for package '%s'...")" "$arg"

			if db_remove_entry "$arg"; then
				success=1
			else
				error "$(gettext "Package matching '%s' not found.")" "$arg"
			fi
		fi
	fi
done

# if all operations were a success, re-zip database
if [ $success -eq 1 ]; then
	msg "$(gettext "Creating updated database file '%s'")" "$REPO_DB_FILE"
	pushd "$gstmpdir" 2>&1 >/dev/null

	if [ -n "$(ls)" ]; then
		[ -f "${REPO_DB_FILE}.old" ] && rm "${REPO_DB_FILE}.old"
		[ -f "$REPO_DB_FILE" ] && mv "$REPO_DB_FILE" "${REPO_DB_FILE}.old"

		case "$REPO_DB_FILE" in
			*tar.gz)  TAR_OPT="z" ;;
			*tar.bz2) TAR_OPT="j" ;;
			*) warning "$(gettext "'%s' does not have a valid archive extension.")" \
			           "$REPO_DB_FILE" ;;
		esac

		bsdtar -c${TAR_OPT}f "$REPO_DB_FILE" *
	else
		# we should only end up with an empty db after a remove of the last package in the database
		error "$(gettext "All packages have been removed from the database. Deleting '%s'.")" "$REPO_DB_FILE"
		rm "$REPO_DB_FILE"
	fi

	popd 2>&1 >/dev/null
else
	msg "$(gettext "No packages modified, nothing to do.")"
fi

# remove the temp directory used to unzip
[ -d "$gstmpdir" ] && rm -rf "$gstmpdir"

# vim: set ts=2 sw=2 noet: