#!/bin/bash

PM_CMD="emerge"
EXTRA_ARGS="-1"

while (( $# != 0 )); do
	case $1 in
	--help)
		echo -e "usage: gir-rebuilder [-P PM] [-- PM_ARGS]\nPM in \"portage paludis\"\nPM_ARGS in your PM supported args..."
		exit 0
		;;
	-P)
		shift
		case $1 in
		portage)
			PM_CMD="emerge"
			;;
		paludis)
			which cave &>/dev/null && PM_CMD="cave resolve -xz" || PM_CMD="paludis -i"
			;;
		*)
			echo "Did not understand package manager: $1"
			exit -1
			;;
		esac
		;;
	--)
		shift
		while (( $# != 0 )); do
			EXTRA_ARGS+=" $1"
			shift
		done
		;;
	*)
		echo "Did not understand: $1"
		exit -2
		;;
	esac
	shift
done

exec ${PM_CMD} ${EXTRA_ARGS} $(grep -r gobject-introspection /var/db/pkg | cut -d ':' -f1 | cut -d '/' -f 5-6 | sed 's/^/=/' | sort -u | grep -Ev '(gobject-introspection|glib)')