#!/bin/bash
# Created By Tony Brijeski (C) 2004
# Released under GPL2 license
#
# Script to cleanup KDE menu entries after install 
# if the apps are not there
# Makes it easier to develop for install purposes

# add path, to make sure
PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/games"
PATH="$PATH:/usr/X11R6/bin:/opt/bin/:/opt/kde/bin:/opt/xfce4/bin:/opt/gnome/bin:/opt/java/bin"
export PATH

# If no kde, quit
if [ ! /etc/skel/.kde/share/applnk/ ]; then
    exit 0
fi

# use /etc/skel as the template base
cd /etc/skel/.kde/share/applnk/

# make the checklist
rm /tmp/kmenucleanup.lst
grep "Exec" */*.desktop >/tmp/kmenucleanup.lst
grep "Exec" */*/*.desktop >>/tmp/kmenucleanup.lst
grep "Exec" */*/*/*.desktop >>/tmp/kmenucleanup.lst

# routine to read through the checklist and test the apps
cat /tmp/kmenucleanup.lst | while read LINE; do

# get the KDE *.desktop filename with folder name
klaunch=`echo $LINE | cut -f1 -d ":"`

# get the executable name
kexe=`echo $LINE | cut -f1 -d " " | cut -f2 -d "="`

# test to see if there is an executable name found
if [ "$kexe" != "" ]; then

   tst=`which $kexe 2>/dev/null`

   # if it isn't found, remove it 
   if [ "$tst" = "" ]; then
      echo "$kexe not found so $klaunch should be removed"
      rm -f $klaunch
      rm -f /root/.kde/share/applnk/$klaunch
      rm -f /home/*/.kde/share/applnk/$klaunch
   else
      echo "$kexe found so $klaunch is good"
   fi
fi


done

# remove the temp checklist file
rm -f /tmp/kmenucleanup.lst
