#!/bin/bash -e
#
#   abs - download a PKGBUILD tree from a CVS repository
#
#   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/>.
#

##
# Constants
##
ABS_VERSION="%%ABS_VERSION%%"
CONFDIR="%%CONF_DIR%%"
SYNCCMD='rsync'
SYNCARGS='-mrtv --no-motd --delete-after --delete-excluded'
BUG_REPORT_EMAIL=pacman-dev@archlinux.org

##
# Script Exit Reasons
# -------------------
#              E_OK : Everything worked :)
# E_MISSING_PROGRAM : A program the script depends on is not installed.
#    E_CONFIG_ERROR : Missing/incorrect configuration.
#  E_INVALID_OPTION : User has passed unknown/invalid option to script.
##
_E_OK=0
_E_MISSING_PROGRAM=1
_E_CONFIG_ERROR=2
_E_INVALID_OPTION=3

##
# Consistent messaging format
##
msg() {
	local mesg=$1; shift
	printf "==> ${mesg}\n" "$@" >&2
}

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

##
# Source configuration
##
if [ -f "$CONFDIR/abs.conf" ]; then
	source "$CONFDIR/abs.conf"
else
	error "Could not find configuration file $CONFDIR/abs.conf"
	exit $_E_CONFIG_ERROR
fi

##
# User based overrides
##
[ -f ~/.abs.conf ] && source ~/.abs.conf

##
# Helper functions
##
usage() {
	echo "Arch Build System $ABS_VERSION -- synchronization utility"
	echo "usage: $0 [options] [repository1 [repository2 ...]]"
	echo
	echo "Options:"
	echo "  -h, --help     Display this help message then exit."
	echo "  -V, --version  Display version information then exit."
	echo
	echo "abs will synchronize PKGBUILD scripts from the Arch Linux"
	echo "repository into $ABSROOT via rsync. If no argument is given, abs"
	echo "will synchronize the repositories specified in ${CONFDIR}abs.conf."
	echo
	echo "Report bugs to $BUG_REPORT_EMAIL with [ABS] in the subject"
}

version() {
	echo "abs $ABS_VERSION"
	echo
	echo "Copyright (C) 2002-2008 Judd Vinet <jvinet@zeroflux.org>."
	echo
	echo "This is free software; see the source for copying conditions."
  echo "There is NO WARRANTY, to the extent permitted by law."
}

update() {
	cd "$ABSROOT"

	EXCLUDE="--filter=P_/local/ --filter=P_/README"

	# I want to ensure we only exclude the top-level repo directories
	for repo in "${REPOS[@]}"; do
		if [ "$repo" != "${repo#!}" ]; then
			EXCLUDE="${EXCLUDE} --filter=-_/${repo#!}/"
		fi
	done

	msg "Starting ABS sync..."
	$SYNCCMD $SYNCARGS $EXCLUDE ${SYNCSERVER}::abs/${ARCH}/ $ABSROOT
} 

##
# Signal Traps
##
trap 'error "TERM signal caught. Exiting..."; exit 1' TERM HUP QUIT
trap 'error "Aborted by user! Exiting..."; exit 1' INT
trap 'error "An unknown error has occured. Exiting..."; exit 1' ERR

##
# Parse Options
##
OPT_SHORT="hV"
OPT_LONG="help,version"
OPT_TEMP="$(getopt -o "$OPT_SHORT" -l "$OPT_LONG" -n "$(basename "$0")" -- "$@" || echo 'GETOPT GO BANG!')"
if echo "$OPT_TEMP" | grep -q 'GETOPT GO BANG!'; then
	# This is a small hack to stop the script bailing with 'set -e'
	echo; usage; exit $_E_INVALID_OPTION;
fi
eval set -- "$OPT_TEMP"
unset OPT_SHORT OPT_LONG OPT_TEMP

while true; do
	case "$1" in
		-h|--help)     usage; exit $_E_OK;;
		-V|--version)  version; exit $_E_OK;;

		--)            OPT_IND=0; shift; break;;
		*)             usage; exit $_E_INVALID_OPTION;; 
	esac
	shift
done

if [ "$#" -gt "0" ]; then
	REPOS=("$@")
fi

## 
# Verify Config
##
if [ ! -d "$ABSROOT" ]; then
	error "$ABSROOT does not exist (or is not a directory)"
	exit $_E_CONFIG_ERROR
fi

if [ ! -w "$ABSROOT" ]; then
	error "no write permissions in $ABSROOT"
	exit $_E_CONFIG_ERROR 
fi

if [ ! "$(type -p rsync)" ]; then
	error "missing rsync synchronization utility.  Install rsync."
	exit $_E_MISSING_PROGRAM
fi

##
# Go-go Update ABS tree!
##
update

exit $_E_OK

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