#!/bin/sh

prgname=`/bin/basename $0`

if [ "$KDEDIR" != "" ]
then
  DIR=$KDEDIR
elif [ -d /opt/kde ]
then
  DIR=/opt/kde
elif [ -d /usr/share/mimelnk ]
then
  DIR=/usr
elif [ -d /usr/local/share/mimelnk ]
then
  DIR=/usr/local
else
  echo "Cannot find directory to $prgname KDE files."
  exit 0
fi

if [ "$1" = "magic" ]
then
  /bin/grep "Chemical MIME" $DIR/share/mimelnk/magic > /dev/null 2> /dev/null
  ret=$?
  if [ $ret -eq 1 ]
  then
    /bin/cat /usr/local/lib/viewmol/kde/mimelnk/magic >> $DIR/share/mimelnk/magic
  fi
  /bin/grep "Chemical MIME" $DIR/share/magic > /dev/null 2> /dev/null
  ret=$?
  if [ $ret -eq 1 ]
  then
    /bin/cat /usr/local/lib/viewmol/kde/mimelnk/magic >> $DIR/share/magic
    /usr/bin/file -C -m $DIR/share/magic
  fi
  exit 0
fi

echo -n "KDE files will be ${prgname}ed in $DIR ..."
umask 022
if [ "$prgname" = "install" ]
then
  /bin/cp -r mimelnk/chemical $DIR/share/mimelnk
else
  /bin/rm -f $DIR/share/mimelnk/chemical/x-dmol.desktop $DIR/share/mimelnk/chemical/x-gamess-output.desktop \
             $DIR/share/mimelnk/chemical/x-gaussian-log.desktop $DIR/share/mimelnk/chemical/x-gulp.desktop \
             $DIR/share/mimelnk/chemical/x-mopac.desktop $DIR/share/mimelnk/chemical/x-msi-*.desktop \
             $DIR/share/mimelnk/chemical/x-pdb.desktop $DIR/share/mimelnk/chemical/x-pqs-output.desktop \
             $DIR/share/mimelnk/chemical/x-turbomole-*.desktop
  /bin/rmdir $DIR/share/mimelnk/chemical
fi
echo -n "."
if [ "$prgname" = "install" ]
then
  /bin/cp icons/32x32/mimetypes/*.png $DIR/share/icons/default.kde/32x32/mimetypes
  /bin/cp icons/32x32/apps/*.png $DIR/share/icons/default.kde/32x32/apps
  echo -n "."
  /bin/cp icons/48x48/mimetypes/*.png $DIR/share/icons/default.kde/48x48/mimetypes
  /bin/cp icons/48x48/apps/*.png $DIR/share/icons/default.kde/48x48/apps
  echo -n "."
  /bin/cp icons/64x64/mimetypes/*.png $DIR/share/icons/default.kde/64x64/mimetypes
  /bin/cp icons/64x64/apps/*.png $DIR/share/icons/default.kde/64x64/apps
  echo -n "."
  /bin/cp icons/128x128/mimetypes/*.png $DIR/share/icons/default.kde/128x128/mimetypes
  /bin/cp icons/128x128/apps/*.png $DIR/share/icons/default.kde/128x128/apps
  echo -n "."
else
  for i in 128x128 64x64 48x48 32x32
  do
    /bin/rm -f $DIR/share/icons/default.kde/${i}/mimetypes/solid.png \
               $DIR/share/icons/default.kde/${i}/mimetypes/molecule.png \
               $DIR/share/icons/default.kde/${i}/mimetypes/qc.png \
               $DIR/share/icons/default.kde/${i}/apps/viewmol.png
  done
fi
echo -n "."
export APPLNK=`/bin/ls $DIR/share | /bin/grep applnk`
if [ "$prgname" = "install" ]
then
  /bin/cp applications/viewmol.desktop $DIR/share/applications
  /bin/ln -s $DIR/share/applications/viewmol.desktop $DIR/share/$APPLNK/Graphics/viewmol.desktop
else
  /bin/rm -f $DIR/share/applications/viewmol.desktop
  /bin/rm -f $DIR/share/$APPLNK/Graphics/viewmol.desktop
fi
echo -n "."
if [ "$prgname" = "install" ]
then
  /bin/mkdir -p /usr/local/lib/viewmol/kde/mimelnk
  /bin/cp ./install /usr/local/lib/viewmol/kde/uninstall
  /bin/cp mimelnk/magic /usr/local/lib/viewmol/kde/mimelnk
fi
echo " done."
