#!/bin/sh -e
aurdir="$DATA/1-projects/aur"
commands="<origin|clone|create|commit|push|clean>"
if test $# -eq 0
then echo "Usage: $0 $commands"
	 case "$PWD" in ($aurdir*) exit 0;; (*) cd "$aurdir"; exec $SHELL;; esac
fi
command=$1
shift
url="ssh://aur@aur.archlinux.org/${1:-$(basename $PWD)}.git"
case "$command" in
	(origin)
		test "$(git rev-parse --git-dir)" = ".git" && git remote set-url origin "$url"
		git remote -v;;
	(clone)
		cd "$aurdir"
		git -c init.defaultBranch=master clone "$url"
		cd "$1"
		test -f PKGBUILD || sed "s/PKG/${1%%-git}/" ../PKGBUILD > PKGBUILD
		test -f .gitignore || echo '*' > .gitignore
		exec $SHELL;;
	(create)
		git add -f .gitignore PKGBUILD
		git commit -m "Create Package" "$@"
		git aur push --amend;;
	(commit)
		makepkg --printsrcinfo > .SRCINFO
		git add -f .SRCINFO
		git commit -v "$@";;
	(push)
		grep -q SKIP PKGBUILD || updpkgsums
		git add -f *.install 2>/dev/null || true
		git aur commit -a "$@"
		git push;;
	(clean)
		find "$aurdir" -mindepth 2 -maxdepth 2 \( -iname "*.pkg.tar.*" -o -iname "*.zip" -o -name "*.tar.gz" -o -type d -not -name ".*" \) \
			-print -exec sudo rm -rI {} +;;
	(*) echo "Unknown command! Available: $commands"; exit 3;;
esac
