#!/bin/bash

[ -f /etc/abs/abs.conf ] && source /etc/abs/abs.conf

usage() {
	echo "Arch Build System -- synchronization utility"
	echo "usage: $0 [-p] [repository1 [repository2 ...]]"
	echo
	echo "abs will synchronize PKGBUILD scripts from the CVS repository"
	echo "into $ABSROOT.  You can follow different package trees by"
	echo "editing /etc/abs/supfile.* files.  If no argument is given, abs "
	echo "will synchronize from supfiles specified in /etc/abs/abs.conf."
	echo "If -p is specified, the connection is opened in passive mode."
}

update() {
	cd $ABSROOT
	for sup in "${SUPFILES[@]}"; do
		if [ "$sup" != "testing" ]; then
			if [ "$sup" = "${sup#!}" ]; then
				cvsup -L 1 -r 0 -g -b $ABSROOT -c .sup /etc/abs/supfile.$sup
			fi
		elif [ "$sup" = "testing" ]; then
			if [ ! -d /var/abs/testing ]; then
				mkdir /var/abs/testing;
			fi
			cd $ABSROOT/testing
			cvsup -L 1 -r 0 -g -b $ABSROOT/testing -c .sup /etc/abs/supfile.testing
			cd $ABSROOT
		fi
	done
} 


update() {
	cd $ABSROOT
	for sup in "${SUPFILES[@]}"; do
		if [ "$sup" = "${sup#!}" ]; then
			$CVSUP -L 1 -r 0 -g -b $ABSROOT -P $CONNMODE -c .sup /etc/abs/supfile.$sup
		fi
	done		
}

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

if [ ! -d $ABSROOT ]; then
	echo "abs: $ABSROOT does not exist (or is not a directory)"
	exit 1
fi
if [ ! -w $ABSROOT ]; then
	echo "abs: no write permissions in $ABSROOT"
	exit 1
fi

if [ "`type -p cvsup`" ]; then
	CVSUP="cvsup"
elif [ "`type -p csup`" ]; then
	CVSUP="csup"
else
	echo "abs: missing CVS synchronization utility.  Install cvsup or csup."
	exit 1
fi

if [ ! -d "$ABSROOT" ]; then
	echo "abs: directory $ABSROOT does not exist"
	exit 1
fi

if [ "$1" = "-p" ] || [ "$1" = "--passive" ]; then
	CONNMODE="-"
	shift
else
	CONNMODE="m"
	shift
fi

if [ "$1" = "-p" ] || [ "$1" = "--passive" ]; then
	CONNMODE="-"
	shift
else
	CONNMODE="m"
	shift
fi

if [ "$#" -ne "0" ]; then
	SUPFILES=("$@")
fi

update

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