#!/bin/sh
# @vasm: XICONSET
# @level: user
# @description: Select icon theme for various window managers
# 
# (c) Kocil, 2003
# (c) Vector Linux, 2003
#
# BUGS : It will be broken if there is a M$ SPACE in the file name


if [ ! $DISPLAY = "" ]; then
  DCMD="Xdialog --wrap" 
  CMD="rxvt -e "
  BACKTITLE=''  
else
  DCMD="dialog"
  CMD=""
  BACKTITLE='Vector Linux ... at the speed of light'
fi

ICON_GLOBAL="/usr/share/icons"
ICON_HOME="$HOME/.icons"
ICON_DIRS="$ICON_HOME $ICON_GLOBAL"
ROX_ICONS="$HOME/.Choices/Icons"
XFCE_ICONS="$HOME/.xfce4/icons"
ICEWM_ICONS="$HOME/.icewm/icons"
KDE_ICONS="$HOME/.kde/share/icons"

fmenu=`mktemp -q /tmp/menu.XXXXXX` && freply=`mktemp -q /tmp/reply.XXXXXX`
if [ $? != 0 ]; then
   echo Can not create temp file 
   exit 1
fi

# just a debugging aid.
dbug() {
  echo $* > /dev/null
}


#################################################
#
no_icon()
{
DIMENSION="14 70"
[ "$CMD" ] && DIMENSION="15 70"
$DCMD "$BACKTITLE" --title " NO ICON " --msgbox \
"\nI could not find any Icon at $ICON_GLOBAL\n
as well as at $ICON_HOME.\n
Please install some Icon themes first." $DIMENSION
}

##################################################
# Scan KDE's Icons
menu_kde() {
echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
ii=0
for ICON_DIR in $ICON_DIRS; do
  find $ICON_DIR -maxdepth 2 -name index.desktop -print > $freply 2> /dev/null 
  for LINE in `cat $freply` ; do
    dbug $LINE
    grep -e "^[KDE Icon Theme]" $LINE > /dev/null
    if [ $? = 0 ]; then
      IDIR=`dirname $LINE`
      IBASE=`basename $IDIR`
      INAME=`grep -e "^Name=" $LINE | cut -d = -f 2`
      [ -z "$INAME" ] && INAME=$IBASE
      echo "$IBASE \"$INAME\" \\" >> $fmenu
      let ii=ii+1
    fi
  done
done
echo ' 2> $freply ' >> $fmenu
if [ $ii = 0 ]; then
  no_icon
  return 1 
else
  . $fmenu
fi
}

###################################################
# SCAN IceWN Icons
menu_icewm() {

echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
ii=0
for ICON_DIR in $ICON_DIRS ; do
  find $ICON_DIR -maxdepth 2 -name index.icewm -print > $freply 2> /dev/null
  for LINE in `cat $freply` ; do
    dbug $LINE
    IDIR=`dirname $LINE`
    IBASE=`basename $IDIR`
    INAME=`grep -e "Comment=" $LINE | cut -d = -f 2`
    [ -z "$INAME" ] && INAME=$IBASE
    echo "$IBASE \"$INAME\" \\" >> $fmenu
    let ii=ii+1
  done
done
echo ' 2> $freply ' >> $fmenu

if [ $ii = 0 ]; then
  no_icon
  return 1 
else
  . $fmenu
fi
}

##################################################
# Scan KDE's Icons
check_kde() {

#TITLE="SELECT ICON THEME"
echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --checklist "$TEXT" $DIMENSION \' > $fmenu
ii=0
for ICON_DIR in $ICON_DIRS; do
  find $ICON_DIR -maxdepth 2 -name index.desktop -print > $freply 2> /dev/null
  for LINE in `cat $freply`; do
    dbug $LINE
    grep -e "^[KDE Icon Theme]" $LINE > /dev/null
    if [ $? = 0 ]; then
      IDIR=`dirname $LINE`
      IBASE=`basename $IDIR`
      INAME=`grep -e "Name=" $LINE | cut -d = -f 2`
      [ -z "$INAME" ] && INAME=$IBASE
      echo "$IBASE \"$INAME\" on \\" >> $fmenu
      let ii=ii+1
    fi
  done
done
echo ' 2> $freply ' >> $fmenu
if [ $ii = 0 ]; then
  no_icon
  return 1 
else
  . $fmenu
fi
}

iconset_rox()
{
TITLE="SET ROX-Pinboard ICONS"
TEXT="\nThis configurator setup an icon theme for ROX-Pinboard.\n
1. It creates a symlink $ROX_ICONS to an icons theme.\n
2. You should use those icons for your pins.\n
3. Next time simply use this menu again to change the theme.\n\n
Cheap trick eh ? Whatever ... \n
Just select your theme now or Cancel if you don't want to."

DIMENSION="22 70 6"
[ "$CMD" ] && DIMENSION="24 70 6"

menu_kde "\nThis program set the icon theme for Rox-Pinboard.\n
Please select the theme you wish:"

[ ! $? = 0 ] && return

ICHOICE=`cat $freply`
rm $freply
BASE=`basename $ROX_ICONS`
mkdir -p $BASE
for ICON_DIR in $ICON_DIRS; do
  if [ -d "$ICON_DIR/$ICHOICE" ]; then
    if [ -L $ROX_ICONS ]; then
      rm $ROX_ICONS
    elif [ -d $ROX_ICONS ]; then
      rm -rf $ROX_ICONS
    fi
    dbug ln -s $ICON_DIR/$ICHOICE/48x48 $ROX_ICONS
    ln -s $ICON_DIR/$ICHOICE/48x48 $ROX_ICONS
    break
  fi
done

DIMENSION="14 70"
[ "$CMD" ] && DIMENSION="15 70"
$DCMD "$BACKTITLE" --title " DONE " --msgbox \
"\nThe icon theme for Rox-Pinboard has been setup.\n
Please gently move your mouse over the desktop icons.\n
For the Rox-Filer itself, use the Options menu to select the icon theme.\n
If you think it should be better than this ...\n
please send a feature request to Rox developers" $DIMENSION
}

#######################################################################
# XFCE
iconset_xfce() {

TITLE="SET XFCE PANEL ICON"
TEXT="\nThis configurator set up your XFCE4 panel icons:\n.
1. It creates a symlink $XFCE_ICONS to an icons theme.\n
2. You should modify XFCE launchers to use those 'other' icons.\n
3. Next time you want a different theme, use this menu again\n\n
Another cheap trick, I know.\n
Cancel now or be quiet and select your theme:"
DIMENSION="22 70 6"

[ "$CMD" ] && DIMENSION="24 70 6"

menu_kde

[ ! $? = 0 ] && return

ICHOICE=`cat $freply`
BASE=`basename $XFCE_ICONS`
mkdir -p $BASE

for ICON_DIR in $ICON_DIRS; do
  if [ -d $ICON_DIR/$ICHOICE ]; then
    if [ -L $XFCE_ICONS ]; then
      rm $XFCE_ICONS
    elif [ -d $XFCE_ICONS ]; then
      rm -rf $XFCE_ICONS
    fi
    dbug "ln -s $ICON_DIR/$ICHOICE/48x48 $XFCE_ICONS"
    ln -s $ICON_DIR/$ICHOICE/48x48 $XFCE_ICONS
    break
  fi
done

DIMENSION="14 70"
[ "$CMD" ] && DIMENSION="15 70"
$DCMD "$BACKTITLE" --title " DONE " --msgbox \
"\nThe icon theme for XFCE4 has been setup.
However, this is only a symlink trick so you need to restart the XFCE4.\n
Your 'other' icons should be changed. \n
For the stock icons, you may change it from the panel settings.\n 
Hopefully, XFCE will support full icon themes in the next release.\n
" $DIMENSION
}

iconset_icewm()
{
TITLE="SET ICEWM ICON"
TEXT="\nThis configurator set up your IceWM icons:\n.
Please select your theme:"
DIMENSION="15 70 6"
[ "$CMD" ] && DIMENSION="18 70 6"

menu_icewm

[ ! $? = 0 ] && return

BASE=`basename $ICEWM_ICONS`
mkdir -p $BASE

ICHOICE=`cat $freply`
for ICON_DIR in $ICON_DIRS; do
  if [ -d $ICON_DIR/$ICHOICE ]; then
    if [ -L $ICEWM_ICONS ]; then
      rm $ICEWM_ICONS
    elif [ -d $ICEWM_ICONS ]; then
      rm -rf $ICEWM_ICONS
    fi
    dbug "ln -sf $ICON_DIR/$ICHOICE $ICEWM_ICONS"
    ln -sf $ICON_DIR/$ICHOICE $ICEWM_ICONS
    break
  fi
done

DIMENSION="14 70"
[ "$CMD" ] && DIMENSION="15 70"
$DCMD "$BACKTITLE" --title " DONE " --msgbox \
"\nThe icon theme for IceWM has been setup.
Please restart IceWM to activate it" $DIMENSION
}

iconset_kde()
{
TITLE="REGISTER KDE ICON"
TEXT="\nThis configurator registers the Icon so you may use it for KDE.\n
Please select your theme:"
DIMENSION="22 70 6"
[ "$CMD" ] && DIMENSION="24 70 6"

check_kde

[ ! $? = 0 ] && return

mkdir -p $KDE_ICONS

if [ "$DCMD" = "dialog" ]; then
  ICHOICES=`cat $freply | sed -e 's/"/ /g' -e 's/  / /g'`
else
  ICHOICES=`cat $freply | sed -e 's!/! !g' -e 's!  ! !g'`
fi

# remove old links
find $KDE_ICONS -maxdepth 1 -type l -print 2> /dev/null | while read LINE; do
  IBASE=`basename $LINE`
  for ICON_DIR in $ICON_DIRS; do
     if [ -d $ICON_DIR/$IBASE ]; then
        rm $LINE
     fi  
  done
done

for ICHOICE in $ICHOICES; do
  for ICON_DIR in $ICON_DIRS; do
    if [ -d $ICON_DIR/$ICHOICE ]; then
      if [ -L $KDE_ICONS/$ICHOICE ]; then
        rm $KDE_ICONS/$ICHOICE
      elif [ -d $KDE_ICONS/$ICHOICE ]; then
        rm -rf $KDE_ICONS/$ICHOICE
      fi
      dbug "ln -s $ICON_DIR/$ICHOICE $KDE_ICONS/$ICHOICE"
      ln -s "$ICON_DIR/$ICHOICE" $KDE_ICONS/$ICHOICE
      break
    fi
  done
done

DIMENSION="14 70"
[ "$CMD" ] && DIMENSION="15 70"
$DCMD "$BACKTITLE" --title " DONE " --msgbox \
"\nThe selected themes has been registered.\n
They should appear to be chosen in the KDE icon theme 
configurator (control center)" $DIMENSION
}


########################################################
# MAIN PROGRAM

echo Vector Linux Icon configurator is starting ...

while [ 1 ]; do

DIMENSION="18 60 4"
[ "$CMD" ] && DIMENSION="18 60 4"

$DCMD --backtitle "$BACKTITLE" --title "ICON CONFIGURATOR" --menu \
"\nThis program help you to set the icon theme for several \n
supported Windows Managers. You should have prepared some \n
themes at $ICON_HOME and $ICON_GLOBAL \n\n
Please select the menu:" \
$DIMENSION \
PINBOARD "Set the icon for Rox-Pinboard" \
ICEWM    "Set the icon for IceWM" \
XFCE4    "Set the icon for XFCE4-Panel" \
KDE      "Register icon themes for KDE" 2> $freply
[ ! $? = 0 ] && exit 1
ICHOICE=`cat $freply`
dbug "Menu = $ICHOICE"
case $ICHOICE in
"PINBOARD")
  iconset_rox
  ;;
"ICEWM")
  iconset_icewm
  ;;
"XFCE4")
  iconset_xfce
  ;;
"KDE")
  iconset_kde
  ;;
"INSTALL")
  iconset_install
  ;;
esac
[ -f $fmenu ] && rm $fmenu
[ -f $freply ] && rm $freply
done
 
