#!/bin/bash
#
#   gensync
#
#   Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.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/>.
#

myver='3.1.1'

# functions

usage() {
	printf "gensync (pacman) %s\n\n" "$myver"
	printf "Usage: %s <root> <destfile> [package_directory]\n\n" "$0"
	printf "\
gensync will generate a sync database by reading all PKGBUILD files\n\
from <root>. gensync builds the database in a temporary directory\n\
and then compresses it to <destfile>.\n\n"
	printf "\
gensync will calculate md5sums of packages in the same directory as\n\
<destfile>, unless an alternate [package_directory] is specified.\n\n"
	printf "\
note: The <destfile> name is important. It must be of the form\n\
      {treename}.db.tar.gz where {treename} is the name of the custom\n\
      package repository you configured in /etc/pacman.conf. The\n\
      generated database must reside in the same directory as your\n\
      custom packages (also configured in /etc/pacman.conf)\n\n"
	echo "Example:  gensync /var/abs/local /home/mypkgs/custom.db.tar.gz"
}

version() {
	printf "gensync (pacman) %s\n" "$myver"
	printf "\
Copyright (C) 2002-2007 Judd Vinet <jvinet@zeroflux.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"
}

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

die () {
	error $*
	exit 1
}

# PROGRAM START

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

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

if [ $# -lt 2 ]; then
	usage
	exit 1
fi

# source system and user makepkg.conf
if [ -r /etc/makepkg.conf ]; then
	source /etc/makepkg.conf
else
	die "/etc/makepkg.conf not found. Cannot continue."
fi

if [ -r ~/.makepkg.conf ]; then
	source ~/.makepkg.conf
fi


d=$(dirname $1)
rootdir="$(cd $d && pwd)/$(basename $1)"
d="$(dirname $2)"
destdir="$(cd $d && pwd)"
destfile="$destdir/$(basename $2)"
pkgdir=""
if [ "$3" != "" ]; then
	pkgdir="$3"
fi

[ ! -d "$rootdir" ] && die "invalid root dir: $rootdir"

echo "gensync: building database entries, generating md5sums..." >&2
cd "$destdir"

pkgs=""

for file in $(find "$rootdir"/* -name "$BUILDSCRIPT"); do
	unset pkgname pkgver pkgrel options

	source $file || die "failed to parse $file"
	if [ "$arch" = 'any' ]; then
		CARCH='any'
	fi
	if [ "$pkgdir" != "" ]; then
		pkgfile="$pkgdir/$pkgname-$pkgver-$pkgrel-${CARCH}${PKGEXT}"
	else
		pkgfile="$destdir/$pkgname-$pkgver-$pkgrel-${CARCH}${PKGEXT}"
	fi

	if [ ! -f "$pkgfile" ]; then
		error "could not find %s-%s-%s-%s%s - skipping" $pkgname $pkgver $pkgrel $CARCH $PKGEXT
	else
		pkgs="$pkgs $pkgfile"
	fi
done

echo "creating repo DB..."

# we'll trim the output just a tad, as gensync may be used on large repos
repo-add $destfile $pkgs \
		| grep -e "package" -e "database"

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