#!/bin/bash

abort() {
	echo ${1:-'archrelease: Cancelled'}
	exit 1
}

if [ "$1" = '' ]; then
	abort 'Usage: archrelease <repo>'
fi

if [ ! -f PKGBUILD ]; then
	abort 'archrelease: PKGBUILD not found'
fi

trunk=$(basename $(pwd))

if [ "$(basename $(dirname $(pwd)))" == "repos" ]; then
	abort 'archrelease: Not in a package trunk dir'
fi

if [ ! -z "$(svn status -q)" ]; then
	abort 'archrelease: You have not committed your changes yet!'
fi

echo -n "releasing package to ${1}..."
pushd .. >/dev/null
if [ -d "repos/${1}" ]; then
	svn rm --force -q "repos/${1}"
	svn commit -q -m "archrelease: remove ${1}" || abort
fi
if [ ! -d repos ]; then
	mkdir repos
	svn add repos
fi
svn copy -q -r HEAD "${trunk}" "repos/${1}"
svn commit -q -m "archrelease: copy ${trunk} to ${1}" || abort
popd >/dev/null
echo 'done'