#!/bin/sh
#set -e

. $KUMATUXBIN/kumatux.org-vars

APPDIR=`$KUMATUXBASENAME $1`

for DIR in $KUMATUXETCDIRSNOTHIRD
do
    if [ -e $KUMATUXETC/$DIR/$APPDIR.conf ]
    then
        rm $KUMATUXETC/$DIR/$APPDIR.conf
	echo "Deleted $KUMATUXETC/$DIR/$APPDIR.conf"
	DELETED=1
    fi
done

for DIR in $KUMATUXPYTHONVERSIONS
do
    if [ -e $KUMATUXETC/$KUMATUXETCPYTHON/$DIR/$APPDIR.conf ]
    then
        rm $KUMATUXETC/$KUMATUXETCPYTHON/$DIR/$APPDIR.conf
	echo "Deleted $KUMATUXETC/$KUMATUXETCPYTHON/$DIR/$APPDIR.conf"
	DELETED=1
    fi
done

for LINKDIR in $KUMATUXROOTDIRS
do
    for FILE in `ls -l $KUMATUXROOT/$LINKDIR/ | grep $APPDIR | sed 's/ -> .*//' | sed 's/.* //'`
    do
	rm $KUMATUXROOT/$LINKDIR/$FILE
	echo "Deleted $KUMATUXROOT/$LINKDIR/$FILE"
	DELETED=1
    done
done

for LINKDIR in $KUMATUXPYTHONVERSIONS
do
    for FILE in `ls -l $KUMATUXROOT/$KUMATUXROOTPYTHON/$LINKDIR/ | grep $APPDIR | sed 's/ -> .*//' | sed 's/.* //'`
    do
	rm $KUMATUXROOT/$KUMATUXROOTPYTHON/$LINKDIR/$FILE
	echo "Deleted $KUMATUXROOT/$KUMATUXROOTPYTHON/$LINKDIR/$FILE"
	DELETED=1
    done
done

if [ $DELETED ]
then
    APPNAME=`$KUMATUXBASENAME $1 | sed 's/_.*//'`
    if [ $APPNAME = "automake" ]
    then
	echo "Restoring $1/share/aclocal/dirlist..."
        if [ -e $1/share/aclocal/dirlist.bak ]
	then
	    cp $1/share/aclocal/dirlist.bak $1/share/aclocal/dirlist
        else
	    if [ -e $1/share/aclocal/dirlist ]
	    then
		rm $1/share/aclocal/dirlist
	    fi
        fi
	echo "Restored."
    elif [ $APPNAME = "perl" ]
    then
        for FILE in `ls -l $KUMATUXBIN/perl | grep $APPDIR/bin/perl | sed 's/ -> .*//' | sed 's/.* //'`
	do
	    echo "Removing $KUMATUXBIN/perl symlink..."
	    rm $FILE
	    echo "Deleted."
	done
    elif [ $APPNAME = "python" ]
    then
        for FILE in `ls -l $KUMATUXBIN/python | grep $APPDIR/bin/python | sed 's/ -> .*//' | sed 's/.* //'`
	do
	    echo "Removing $KUMATUXBIN/python symlink..."
	    rm $FILE
	    echo "Deleted."
	done
    elif [ $APPNAME = "ruby" ]
    then
        for FILE in `ls -l $KUMATUXBIN/ruby | grep $APPDIR/bin/ruby | sed 's/ -> .*//' | sed 's/.* //'`
	do
	    echo "Removing $KUMATUXBIN/ruby symlink..."
	    rm $FILE
	    echo "Deleted."
	done
    fi
    echo "Deleted kumatux.org links for $1"
else
    echo "No kumatux.org links for $1 deleted, as not installed."
fi
