#!/bin/sh
#
# This trigger converts virtualpkg conf files to the XBPS 0.33 format.
#
# Arguments:	$ACTION = [run/targets]
#		$TARGET	= [post-install/pre-remove]
#		$PKGNAME
#		$VERSION
#		$UPDATE = [yes/no]
#
ACTION="$1"
TARGET="$2"
PKGNAME="$3"
VERSION="$4"
UPDATE="$5"
CONF_FILE="$6"

export PATH="$PATH:/usr/local/bin"

case "$ACTION" in
targets)
	echo "post-install"
	;;
run)
	for f in etc/xbps/virtualpkg.d/*.conf; do
		file=$(basename $f)
		file=${file%.conf}
		if [ -f etc/xbps/virtualpkg.d/${file}.vpkg ]; then
			continue
		fi
		echo "Generating virtualpkg configuration file for: ${file} ..."
		cat "$f" | while read line; do
			pkg=$(echo "$line"|awk '{print $2}')
			vpkg=$(echo "$line"|awk '{print $6}')
			vpkg="$(echo $vpkg|sed 's,",,g')"
			if [ "$pkg" = "Virtual" ]; then
				echo "$line" > etc/xbps/virtualpkg.d/${file}.vpkg
			else
				echo "virtualpkg=${vpkg}:${pkg}" >> etc/xbps/virtualpkg.d/${file}.vpkg
			fi
		done
	done
	;;
*)
	exit 1
	;;
esac

exit 0
