#!/bin/sh
#
# http://www.pdaXrom.org 2004
# package uninstaller
# ipkg-uninstall package1[ package2[.. packageN]]
#
IPKG_CONF='/etc/ipkg.conf'

ipkg_protect_slashes() {
	sed -e 's/\//\\\//g'
}

ipkg_dests(){
	local destre=`echo '.*' | ipkg_protect_slashes`
	sed -ne "/^dest[[:space:]]\+$destre/{
s/^dest[[:space:]]\+[^[:space:]]\+[[:space:]]\+//
s/^/$IPKG_OFFLINE_ROOT/
p
}" < $IPKG_CONF
}


PREFIXES=`ipkg_dests`

findpackage () {
	echo "*** Locating package"
	# Does the list file exist?

	for PREFIX in $PREFIXES ; do
	    if [ -e "$PREFIX/usr/lib/ipkg/info/$PACKAGE.list" ]; then
		echo "*** Found $PACKAGE on $PREFIX"
		return 0
	    fi
	done

	echo "Package \"$PACKAGE\" not found on external storages."
	return 1
}

for PACKAGE in $* ; do
    findpackage && (
    ipkg -d $PREFIX remove $PACKAGE
    )
done
