#!/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
DEFAULT="/usr/share/icons/basic"
SIZES="48x48"

# current dirs
CWD=`pwd`
CICON=""
CDEFAULT=""

# merge icons
# usage: micon the_icon subs1 [subs2 [ ...] ] 
# -- = subtitute icons from default dir
# vars: CICON CDEFAULT
micon() {

   ICON="$1.png"
   # delete softlink
   if [ -L "$ICON" ]; then
      rm $ICON
   fi
   # don't do anything if already there
   if [ -f $ICON ]; then
      # echo $ICON = original
      echo -n "*"
      return 0
   fi 
   # try substitutions
   shift
   while [ "$1" ]; do 
      if [ "$1" = "--" ]; then # replace with the default
        if [ -f $CDEFAULT/$ICON ]; then
           ln -sf $CDEFAULT/$ICON $ICON
           echo -n "-" 
	   #echo $ICON = default
           return 0
        fi
      else
        if [ ! -L $1.png ]; then
           if [ -f $1.png ]; then
             ln -sf $1.png $ICON
             #echo $ICON -\> $1.png
	     echo -n "+"
	     return 0
	   fi
        fi
      fi
      shift
   done
   # unsuccess
   # Try the last attemp
   if [ -f $CDEFAULT/$ICON ]; then
       ln -sf $CDEFAULT/$ICON $ICON
       # echo $ICON = default
       echo "-"
       return 0
   fi
   echo -n "?"
   echo =$ICON 
   return 1     
}


# Substitute icons under a size (48x48, 32x32, ...)
# Manual LOL, you need to edit it a LOT
# vars: THEMEDIR, DEFAULT, SIZE
#
msize() {

CICON=$THEMEDIR/$SIZE/apps
CDEFAULT=$DEFAULT/$SIZE/apps
cd $CICON > /dev/null 2>&1
if [ $? -gt 0 ]; then
   echo ERROR: $SIZE is not exist
   return 1
fi

micon terminal console display konsole
micon xterm console terminal display
micon rox mycomputer ../filesystems/folder_penguin ../filesystems/folder_crystal kfm
micon xfe kdisknav 
micon gentoo kdisknav ../filesystems/folder
micon xnc kdisknav ../filesystems/folder_tar ../filesystems/folder
micon xfsamba samba -- ../filesystems/network_local linneighborhood
micon linneighborhood samba ../filesystems/network_local

micon explorer kdisknav kfm mycomputer ../filesystem/folder
micon browser package_network ../filesystems/www firebird galeon netscape 

micon firebird phoenix browser ../filesystems/www
micon evolution xfmail kmail email mail
micon sylpheed email xfmail -- mail kmail
micon gftp ../filesystems/ftp kget
micon d4x kget ../filesystems/ft
micon gaim aim kmess -- kuser personal
micon xchat ktalkd personal -- kuser
micon gfax kfax kdeprintfax fileshare
micon gkdial ../devices/modem kppp 
micon xisp kppp ../devices/modem
micon adsl ../filesystems/socket ../devices/modem kppp

micon abiword -- wp kwrite
micon bluefish -- babelfish quanta
micon gnumeric -- kspre4ad xcalc kcalc
micon oleo xcalc kcalc 
micon gnucash -- xcalc kcalc
micon ghostview kghostview
micon xpdf kpdf acroread
micon blackbook kaddressbook  
micon scribus wp kwrite
micon plan kplan

micon gqview kview ../filesystems/folder_image icons
micon gimp xpaint kpaint
micon sodipodi kontour
micon dia kivio
micon xsane ../devices/scanner
micon flphoto camera ../devices/camera ../filesystems/camera ../devices/cam ../devices/digicam

micon audacity kaboodle kmix 
micon mplayer video multimedia
micon xine -- video multimedia
micon xmms -- audio multimedia
micon ripperx ../devices/cdaudio_mount kscd multimedia
micon xcdroast cdbakeoven ../devices/cdwriter_mount 
micon realplayer -- multimedia
micon freevo multimedia
micon divx multimedia

micon gkrellm hwinfo
micon xscreensaver kscreensaver background desktop
micon vasm kcontrol package_settings
micon vecpkg kpackage package kicker

micon cups ../devices/printer2 ../devices/printer1 ../devices/printer

CICON=$THEMEDIR/$SIZE/devices
CDEFAULT=$DEFAULT/$SIZE/devices
cd $CICON > /dev/null 2>&1
if [ $? -gt 0 ]; then
   echo ERROR: $SIZE is not exist
   return 1
fi

micon floppy_mount 5floppy_mount 3floppy_mount 2floppy_mount
micon floppy_unmount 5floppy_unmount 3floppy_unmount 2floppy_unmount
micon cdrom_mount 3cdrom_mount
micon cdrom_unmount 3cdrom_unmount
micon cdwriter_mount 3cdwriter_mount
micon cdwriter_unmount 3cdwriter_unmount

micon camera gtkcam_camera
micon printer printer1 printer2
micon scanner

cd $CWD
}

# substitute a theme
# vars: THEMEDIR, DEFAULT, SIZES 
mtheme()
{
  BASENAME=`basename $THEMEDIR`
  if [ -L $THEMEDIR -a "$BASENAME" = "default" ]; then
     echo Skipping default
     return 0  
  fi
  if [ ! -O $THEMEDIR ]; then
     echo $THEME is not owned by $USER
     return  
  fi
  for SIZE in $SIZES ; do
     echo
     echo Merging $THEMEDIR/$SIZE with $DEFAULT/$SIZE ...
     msize
     echo
  done
}

show_help() {
echo "Usage: iconmerge [[merged_dir] [default_dir]"
echo "Substitute icons in the merged_dir with the default_dir"
echo "merge_dir and default_dir must be a valid KDE's icons theme"
echo 
echo "Example"
echo "mergeicon"
echo "   merge all $BASEDIR/* with $DEFAULT."
echo
echo "mergeicon Noia"
echo "   merge $BASEDIR/Noia with $DEFAULT"
echo
echo "mergeicon Noia Crystal"
echo "   merge $BASEDIR/Noia with $DEFAULT"
echo
echo "mergeicon ~/.icons/Mine"
echo "   merge Mine with $DEFAULT"
exit 0
}

######################################################################
# 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
   if [ "$2" ]; then
      if [ "`dirname $2`" ]; then
        DEFAULT=$2
      else
        DEFAULT=$BASEDIR/$2
      fi
   fi
   mtheme
   exit 0   
fi

# Merge 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`
    mtheme
  fi
done
