#!/bin/sh
# Merge KDE's icon with another default dir
# Only 48x48 icons are used

# Global variables
if [ "$UID" = "0" ]; then
  BASEDIR="/usr/share/icons"
else
  BASEDIR="$HOME/.icons"
fi

show_help() {
echo "Usage: iconclear [icon_dir]"
echo "CLear the softlinks in the icon_dir"
echo 
echo "Example"
echo "iconclear"
echo "   clear all $BASEDIR/*"
echo
echo "iconclear Icon"
echo "   clear $BASEDIR/Icon"
echo
echo "iconclear /path_to/Mine"
echo "   clear /path_to/Icon"
exit 0
}

clear_theme()
{
   BASENAME=`basename $THEMEDIR`
   if [ -L $THEMEDIR -a "$BASENAME" = "default" ]; then
     echo Skipping default
     return 0  
   fi
   if [ -O $THEMEDIR ]; then
      echo Clearing $THEMEDIR
      find $THEMEDIR -type l -exec rm {} \;
   else
      echo $THEMEDIR is not owned by $USER
   fi
}


######################################################################
# Main program

[ "$1" == "-h" ] && show_help
[ "$1" == "--help" ] && show_help
if [ "$1" ]; then
   if [ "`dirname $1`" ]; then
      THEMEDIR=$1
   else
      THEMEDIR=$BASEDIR/$1
   fi
   grep -e "^[KDE Icon Theme]" $THEMEDIR/index.desktop > /dev/null
   if [ $? -gt 0 ]; then
      echo "$THEMEDIR is not a KDE icon theme"
      exit 1
   fi
   clear_theme
   exit 0   
fi

# Clear all
find $BASEDIR -maxdepth 2 -name index.desktop -print | while read LINE; do
  grep -e "^[KDE Icon Theme]" $LINE > /dev/null
  if [ $? = 0 ]; then
    THEMEDIR=`dirname $LINE`
    clear_theme
  fi
done
