#!/bin/sh 

PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/IBM/bin 

home=$HOME/IBM

if [ -f /IBM/linuxDistribution ]; then
   version=`cat /IBM/linuxDistribution | head -1 | cut -d' ' -f1`
fi

if [ -f $home/IBM/IBMSetupInformationBlock ]; then
   . /root/IBM/IBMSetupInformationBlock

   # configure screensaver
   if [ $screensaver = "enabled" ]; then
      if [ $screensaver_image = "default" ]; then
         ln -sf /IBM/IBMWIZARD.GIF /IBM/screensaver.img
      else
         ln -sf $screensaver_image /IBM/screensaver.img
      fi
   fi
   if [ "$defaultprinter" != "" ]; then
      export PRINTER=$defaultprinter
   fi
fi

RES=`cat /proc/cmdline | awk '{print $3}' | cut -d'=' -f2`
GEOMETRY="-geometry $RES+0+0"

##########################################
# generate icewm menu file
##########################################

if [ -f $home/WM_Config ]; then
   CONFIGFILE=$home/WM_Config
elif [ $USER = "root" -a -f /etc/sysconfig/WM_Config ]; then
   CONFIGFILE=/etc/sysconfig/WM_Config
else
   CONFIGFILE=/etc/sysconfig/WM_Config.system
   if [ -d /etc/sysconfig/ICAClient -a $USER != "root" ]; then

      if [ -d $HOME/.ICAClient ]; then
         rm -Rf $HOME/.ICAClient
      fi
      mkdir $HOME/.ICAClient
      cp -f /etc/sysconfig/ICAClient/* $HOME/.ICAClient
   fi 
fi

XCLIENTS=$HOME/.Xclients

if [ ! -f $HOME/.icewm ]; then
   mkdir -p $HOME/.icewm
fi

if [ -f $CONFIGFILE ]; then
   MODE=`sed -n '/^#MODE/p' $CONFIGFILE | cut -d' ' -f2`
   if [ $MODE = "0" ]; then
      cp /etc/X11/icewm/* ~/.icewm
      ICEMENU=$HOME/.icewm/menu
      ICETOOL=$HOME/.icewm/toolbar
      rm -f $ICEMENU
      rm -f $XCLIENTS
      if [ $USER = "root" ]; then
         # add some default menu entries for root
         if [ $version = "TurboLinux" ]; then
            echo "prog \"User Config\" app xterm -e /usr/sbin/turbousercfg   # ADMIN Config" > $ICEMENU
            echo "prog \"Print Config\" app xterm -e /usr/sbin/turboprintcfg   # ADMIN Config" >> $ICEMENU
            echo "prog \"Time Config\" app xterm -e /usr/sbin/turbotimecfg   # ADMIN Config" >> $ICEMENU
         else 
            echo "prog \"User Config\" app kuser # ADMIN Config" > $ICEMENU
            echo "prog \"Print Config\" app printtool # ADMIN Config" >> $ICEMENU
         fi
      fi

      # proces the config file line by line looking for n+ or y+
      # n+ add to menu, do not autostart
      # y+ add to menu and autostart
      exec < $CONFIGFILE
      while read line
      do
        if echo $line | grep "Exec" > /dev/null 2>&1
        then
          NAME=`echo $line | sed -n 's/.*\(\".*\"\).*/\1/p'`
          EXEC=`echo $line | sed -n 's/.*Exec \(.*\)[yn]$/\1/p'`
          case "$NAME" in
             \"IBMSetup\") 
                ICON=idf
   	     ;;
	     \"3270\ Emulator\")
   	        ICON=app
   	     ;;       
             \"VT220\ Emulator\" | \"Xterm\")
                ICON=xterm
             ;;
             \"ICA\ Client\" | \"ICA\ Remote\ Application\ Manager\")
                ICON=ica
             ;;
             \"Navigator\ Browser\")
                ICON=navigator
             ;;
             *) 
                ICON=app
          esac

          echo "prog $NAME $ICON $EXEC" >> $ICEMENU
       fi    
        if echo $line | grep "y$" > /dev/null 2>&1
        then
           if [ ! -f $XCLIENTS ]; then
               echo "#!/bin/sh" > $XCLIENTS
               chmod 755 $XCLIENTS 
 
           fi
           echo "$EXEC &" >> $XCLIENTS
        fi
      done
      if [ $USER != "root" ]; then
         cp $ICEMENU $ICETOOL
      else
         grep -v "ADMIN Config" $ICEMENU > $ICETOOL
      fi
   else 
      cp /IBM/Custom/Flash/etc/X11/icewm/kiosk/* $HOME/.icewm
      rm -f $XCLIENTS
      echo "#!/bin/sh" > $XCLIENTS
      echo "while true; do" >> $XCLIENTS

      if [ $MODE = "1" ]; then
         # full screen netscape kiosk mode
         echo "   netscape $GEOMETRY" >> $XCLIENTS
      elif [ $MODE = "2" ]; then
         # full screen kiosk mode ICA remote manager
         cp /IBM/Custom/Flash/etc/X11/icewm/kiosk/preferences.ica $HOME/.icewm/preferences
         echo "   /usr/bin/wfcmgr" >> $XCLIENTS
      elif [ $MODE = "3" ]; then
         # full screen kiosk mode ICA client
         cp /IBM/Custom/Flash/etc/X11/icewm/kiosk/preferences.ica $HOME/.icewm/preferences
         exec < $CONFIGFILE
         while read line
         do
            if echo $line | grep "Exec" > /dev/null 2>&1
            then
               EXEC=`echo $line | sed -n 's/.*Exec \(.*\)[yn]$/\1/p'`
               echo "$EXEC " >> $XCLIENTS
            fi
         done
      elif [ $MODE = "4" ]; then 
         # custom application 
         EXEC=`grep Exec $CONFIGFILE | sed -n 's/.*Exec \(.*\)[yn]$/\1/p'`
         echo "$EXEC " >> $XCLIENTS 
      fi
      echo "done" >> $XCLIENTS
      chmod 755 $XCLIENTS
   fi
fi

