#!/bin/sh 

#
# KAppFinder
# 
# This scripts installes the kdelnk files in the hierarchy under 
#
# apps
#
# if the executable in each kdelnk file is installed on the system.
# If necessary, the icons in 
#
# pics   and   pics/mini
#
# are installed, too.
#
#
# written by Matthias Hoelzer (hoelzer@physik.uni-wuerzburg.de)
#


# test the arguments
if [ $# -ne 1 ]; then
  INSTPATH=$HOME/.kde
  kde_appsdir=$INSTPATH/share/applnk
  kde_icondir=$INSTPATH/share/icons
  kde_minidir=$INSTPATH/share/icons/mini
else
  INSTPATH=$1 # ignored the more or the less
  kde_icondir=${install_root}/usr/share/icons
  kde_appsdir=${install_root}/usr/share/applnk
  kde_minidir=${install_root}/usr/share/icons/mini
fi

OLDDIR=`pwd`
cd ${install_root}/usr/share/apps/kappfinder

echo "Installing to $INSTPATH"

# iterate over all kdelnk files
for file in `find apps -name "*.kdelnk"` 
do
  # extract name of the executable
  filename=`grep "^Exec=" $file | sed "s/Exec= *//" | sed "s/ .*//"`

  # test if this executable is installed (in path!)
  executable=`which $filename`
  if [ -x "$executable" ]; then

    echo "Looking for $filename: found in $executable"

    # create the directory
    pathname=`echo $file | sed "s/\/[^\/]*kdelnk$//"`    
    mkdir -m 755 -p $kde_appsdir/$pathname

    # copy the kdelnk file
    cp $file $kde_appsdir/$pathname

    # copy the icon file
    picname=`grep "^Icon=" $file | sed "s/Icon= *//" | sed "s/ .*//"`
    if [ -f "pics/$picname" ]; then
      cp pics/$picname $kde_icondir
    fi

    # copy the mini icon file
    minipic=`grep "^MiniIcon=" $file | sed "s/Icon= *//" | sed "s/ .*//"`
    if [ ! "$minifile" ]; then
      minipic=$picname
    fi
    if [ -f "pics/mini/$minipic" ]; then
      cp pics/mini/$minipic $kde_minidir
    fi

  else  
    echo "Looking for $filename: not found"
  fi
done


# now decorate the directories
for file in `find apps -name ".directory"` 
do

  # copy the .directory file
  cp $file $kde_appsdir/$file 2>/dev/null
  
  # continue, if the file was not installed
  if [ ! -f $kde_appsdir/$file ]; then
    continue
  fi

  # copy the icon
  picname=`grep "^Icon=" $file | sed "s/Icon= *//" | sed "s/ .*//"`
  if [ -f "pics/$picname" ]; then
    cp pics/$picname $kde_icondir
  fi

  # copy the mini icon file
  minipic=`grep "^MiniIcon=" $file | sed "s/Icon= *//" | sed "s/ .*//"`
  if [ ! "$minifile" ]; then
    minipic=$picname
  fi
  if [ -f "pics/mini/$minipic" ]; then
    cp pics/mini/$minipic $kde_minidir
  fi

done


cd $OLDPATH


# exit with success
# NB: if some subdirectory is not installed, copying the .directory file
#     fails, but this is no error
exit 0
