#!/bin/sh

# script that makes it easier to bump split packages
# (and perhaps other fun stuff)

if [ -e /sabayon/bin/buildrc ]; then
	. /sabayon/bin/buildrc || exit 2
else
	echo "Warning: buildrc not found" >&2
fi

list="git [sub]version [tra]nsmission [qua]ssel [pop]pler [lig]htdm [pin]entry [ava]hi"
cmd="emerge -av"
default_overlay="sabayon-distro"
overlay=

if [ "$1" = "--repo" ]; then
	overlay=$2
	if [ -z "$overlay" ]; then
		echo "--repo requires an argument" >&2
		exit 1
	fi
	shift; shift
fi

if [ "$1" = "--print" ]; then
	cmd="echo $cmd"
	shift
fi

overlay=${overlay:-$default_overlay}

what=$1

if [ -z "$what" ]; then
	echo "What to compile? [ $list ]"
	read what
fi

get_list() {
	local base=$1
	shift
	local item result
	for item in "$@"; do
		result="$result$base$item::$overlay "
	done
	result="$result $base::$overlay"
	echo "$result"
}

case $what in
git)
	$cmd $(get_list dev-vcs/git -cvs -gui-tools -subversion) www-apps/gitweb
	;;
subversion|sub)
	$cmd $(get_list dev-vcs/subversion -java) www-apache/mod_dav_svn
	;;
transmission|tra)
	$cmd $(get_list net-p2p/transmission -base -cli -daemon -gtk -qt5)
	;;
quassel|qua)
	$cmd $(get_list net-irc/quassel -client -common -core)
	;;
poppler|pop)
	$cmd $(get_list app-text/poppler -base -glib -qt5)
	;;
lightdm|lig)
	$cmd $(get_list x11-misc/lightdm -base -qt5)
	;;
pinentry|pin)
	$cmd $(get_list app-crypt/pinentry -base -gnome -gtk2 -qt5)
	;;
avahi|ava)
	$cmd $(get_list net-dns/avahi -base -gtk -gtk3 -mono)
	;;
	# add new elements to $list, too
*)
	echo "Excuse me, but I non capisco." >&2
	echo "Use --repo <repository> to use different overlay (default: $default_overlay)." >&2
	echo "Use --print just to print what would be done."
	[ -n "$1" ] && echo "Run without any parameter for interactive use." >&2
	exit 1
esac
