#!/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"
ICEWM_ICONS="$HOME/.icewm/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 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 'Default "Stock Icons" 2> $freply ' >> $fmenu

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

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

while [ 0 ]; do
menu_icewm

[ ! $? = 0 ] && return

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

ICHOICE=`cat $freply`
for ICON_DIR in $ICON_DIRS; do
  if [ $ICHOICE = "Default" ]; then
    rm -rf $ICEWM_ICONS 
    break
  elif [ -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

skill -HUP icewm
if [ ! $? = 0 ]; then
[ "$CMD" ] && DIMENSION="15 70"
$DCMD "$BACKTITLE" --title " DONE " --msgbox \
"\nThe icon theme $ICHOICE for IceWM has been setup.
It should be activated the next time you start IceWM" 0 0
fi
done

}

iconset_icewm

