#!/bin/bash

#
# A script to rebuild packages as needed on gentoo linux.
# 
# Copyright (C) 2011 Emil Karlson <jekarlson@gmail.com>
# Freely distributable.
#

tempfile=`mktemp`

trap "rm ${tempfile}" SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM

case $1 in
	get_api)
		echo 2.0 > "$2"
		;;
	list_actions)
		echo get_api list_actions find_broken_packages > "$2"
		;;

	find_broken_packages)
		perl-cleaner -p --all --package-manager-command "/usr/libexec/write_to_file.sh ${tempfile}" --reinstall-identical-versions >> /dev/null 2>&1
		for i in $(cat "$tempfile"| sed 's/\(^\| \)-[^ ]*//g'); do echo "$i" | sed 's/^=//'; done > ${2}
		;;

	*)
		echo "Unknown action:" "$1" > "$3"
		;;
esac
		
rm "${tempfile}"

