#!/bin/sh
if [ $(id -u) != 0 ]; then
 echo Error: You must be root to run this script!
 exit 1
fi
for link in $(find /usr/share/applnk -name *.desktop -type f); do
 #echo testing....... $link
 appl=$(perl -pe '($_)=/Exec=(.*)/' "$link")
 first=$(echo $appl|awk '{print $1}')
 last=$(echo $appl|awk '{print $NF}')
 nr=$(echo $appl|awk '{print NF}')
 if [ "$appl" ]; then
  if [ "$first" == "kdesu" ]; then
   file=$(echo $appl|perl -pe '($_)=/--\s+(\S*)/')
   [ "$file" ] || file=$(echo $appl|perl -pe '($_)=/kdesu\s+(\S*)/')
   case $file in
    rxvt) file=$last;;
    sudo) file=$(echo $appl|perl -pe '($_)=/sudo\s+(\S*)/');; 
   esac
  else
   file=$first
  fi
  [ "$file" == "-s" ] && file=sudo
  if [ "$first" == "kdesu" ]; then
   if ! which $file >/dev/null; then
    echo link.......... $link
    echo application... $appl
    echo first......... $first
    echo last.......... $last
    echo file.......... $file
    echo erasing.......  $link
    rm -i $link
   fi
  else  
   if ! which $file >/dev/null; then
    echo erasing.......  $link
    rm -f $link
   fi
  fi
 else
  if ! dpkg -S $link &>/dev/null; then
   echo erasing.......  $link
   rm -f $link
  fi
 fi
done
