#!/bin/bash

# where are the arch scripts located?
ARCHDIR="/arch"

#All this fun stuff used to be in the db-(whatever) files
# Let's make it cleaner
export CARCH="$arch"
ftppath="/home/ftp/$reponame/os/$arch/"
svnpath="/home/svn-packages"
svnrepo="$reponame-$arch"

#Hacky for now
if [ "$arch" = "x86_64" ]; then
	stagedir="$HOME/staging/${reponame}64"
else
	stagedir="$HOME/staging/$reponame"
fi

WORKDIR="~/.dbscripts"
CHECKOUTDIR="$WORKDIR/checkout"
LOCKFILE="/tmp/.repolck.$arch.$repoid"
DBFILE="$WORKDIR/$reponame.db.tar.gz"

if [ ! `type -p fakeroot` ]; then
		echo "error: fakeroot is missing" >&2
		exit 1
fi

if [ ! -d $stagedir ]; then
		echo "error: staging directory missing: $stagedir" >&2
		exit 1
fi

# Get the package name from the filename
# hackish, but should work for now
getpkgname() {
		local tmp

		tmp=${1##*/}
		tmp=${tmp%.pkg.tar.gz}
		tmp=${tmp%-i686}
		tmp=${tmp%-x86_64}
		echo ${tmp%-*-*}
}

cleanup() {
		# unlock
		rm -f "$LOCKFILE"
		[ "$1" ] && exit $1
}

ctrl_c() {
		echo "Interrupted" >&2
		cleanup 0
}

die() {
		echo "$*" >&2
		cleanup 1
}

# check for locks
if [ -f "$LOCKFILE" ]; then
	owner=$(/bin/ls -l $LOCKFILE | /bin/awk '{print $3}')
	echo "error: db generation is already in progress (started by $owner)"
	exit 1
fi

# catch ^C breaks
trap ctrl_c SIGINT
# lock
touch "$LOCKFILE"

if [ -d $CHECKOUTDIR ]; then
	cd $CHECKOUTDIR
	svn update
	if [ $? -gt 0 ]; then
		die "==> SVN update failed, aborting!"
	fi
else
	echo "==> Checking out repo: $svnrepo ($arch) - Please be patient"
	svn checkout file://$svnpath $CHECKOUTDIR
	if [ $? -gt 0 ]; then
		die "==> SVN checkout failed, aborting!"
	fi
fi

cd $CHECKOUTDIR

# Checkout the SVN module if we need to
updatelists=
if [ "`ls $stagedir/add 2>/dev/null`" -o "`ls $stagedir/del 2>/dev/null`" ]; then
	updatelists=1
else
	echo "No files to process"
	cleanup 0
fi

# Right-O, now we look through the "add" and "del" subdirectories of
# $stagedir and process the packages there accordingly -- all packages
# in the "add" dir are added/updated, all packages in the "del" dir
# are removed.
#
# This means the sync db could actually be unpacked/repacked twice in
# one db-* invocation, but it's not a huge performance hit.

if [ -d $stagedir/add -a "`ls $stagedir/add`" ]; then
		echo "==> Processing new/updated packages for repository '$reponame'..." >&2

		# copy the db file into our working area
		cp $ftppath/$reponame.db.tar.gz $DBFILE

		cd $stagedir/add
		# run it thru fakeroot make sure everything is owned by root.root
		echo "$ARCHDIR/updatesync-many add $DBFILE $CHECKOUTDIR $svnrepo" | fakeroot

		if [ $? -ne 0 ]; then
				die "==> Error returned from updatesync-many"
		fi

		cp $DBFILE $ftppath

		# only for i686 (for now)
		if [ "$arch" = "i686" ]; then
				echo "==> Scanning for New/Updated packages..." >&2
				cd $stagedir/add
				$ARCHDIR/pkgdb1 $CHECKOUTDIR $svnrepo | $ARCHDIR/pkgdb2-add $repoid $stagedir/add
		fi

		# move the package files into the ftp directory
		mv -f $stagedir/add/*.pkg.tar.gz $ftppath
fi

if [ -d $stagedir/del -a "`ls $stagedir/del`" ]; then
		echo "==> Processing deleted packages for repository '$reponame'..." >&2

		# copy the db file into our working area
		cp $ftppath/$reponame.db.tar.gz $DBFILE

		cd $stagedir/del
		# run it thru fakeroot make sure everything is owned by root.root
		echo "$ARCHDIR/updatesync-many del $DBFILE NOT-USED ZOMGWOO" \
		| fakeroot

		if [ $? -ne 0 ]; then
				die "==> Error returned from updatesync-many"
		fi

		cp $DBFILE $ftppath

		# only for i686 (for now)
		if [ "$arch" = "i686" ]; then
				echo "==> Scanning for Deleted packages..." >&2
				cd $stagedir/del
				(
				for i in *.pkg.tar.gz; do
						pkgname=$(getpkgname $i)
						echo $pkgname
				done
				) | $ARCHDIR/pkgdb2-del $repoid $stagedir/del
		fi

		# remove the package files
		rm -f $stagedir/del/*.pkg.tar.gz
fi

if [ "$updatelists" ]; then
		echo "==> Generating Text Package List..." >&2
		cd $CHECKOUTDIR
		$ARCHDIR/genpkglist $ftppath $svnrepo
		if [ -f packages.txt ]; then
			mv packages.txt $ftppath/packages.txt
		fi
fi

cleanup

# vim: set ts=4 sw=4 noet ft=sh: