#!/bin/sh 

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

echo_env () {
   set | sed -n /^$*=\'/s/^$*=\'//p | sed s/\'$//
}

home=$HOME/IBM

if [ -f $HOME/IBM/IBMSetupInformationBlock ]; then
   . $HOME/IBM/IBMSetupInformationBlock
elif [ -f /etc/sysconfig/IBMSetupInformationBlock ]; then
   . /etc/sysconfig/IBMSetupInformationBlock
fi

screensaver=${screensaver:-disabled}

# configure screensaver
if [ $screensaver = "enabled" ]; then
   if [ -f $HOME/IBM/xscreensaverResources ]; then
      xrdb -nocpp -merge $HOME/IBM/xscreensaverResources
   elif [ -f /IBM/bin/xscreensaverResources ]; then
      xrdb -nocpp -merge /IBM/bin/xscreensaverResources
   fi
   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

if [ "$soundmaster" != "" ]; then
   /IBM/bin/volume $soundmaster
fi

RES=`cat /proc/cmdline | sed -e s/.*V=/V=/ -e s/[\ ].*// | cut -d= -f2`
ADAPTER=`cat /proc/cmdline | sed -e s/.*ip=/ip=/ -e s/[\ ].*// | cut -d: -f6`
GEOMETRY="-geometry $RES+0+0"

# Load the WindowManager-specific functions.
if [ -r /IBM/bin/desktopFunctions.$WM ]; then
   . /IBM/bin/desktopFunctions.$WM
fi

# ----------------------------------------------------------
# Start processing the WM config files
# ----------------------------------------------------------

if [ -f $home/WM_Config ]; then
   CONFIGFILE=$home/WM_Config

   # Remove link to system ICA settings, if present.
   if [ -L /home/KIOSK/.ICAClient ]; then
      rm -f /home/KIOSK/.ICAClient
   fi
elif [ -f /etc/sysconfig/WM_Config ]; then
   CONFIGFILE=/etc/sysconfig/WM_Config

   # Use system ICA settings.
   if [ -d /home/system-default/KIOSK/.ICAClient ]; then
      rm -Rf /home/KIOSK/.ICAClient
      ln -sf /home/system-default/KIOSK/.ICAClient /home/KIOSK/
   fi
else
   CONFIGFILE=/dev/null
fi

XCLIENTS=$HOME/.Xclients


if [ -r $CONFIGFILE ]; then
   MODELINE=`sed -n '/^#MODE/p' $CONFIGFILE`
   MODE=`echo $MODELINE | cut -d' ' -f2`

   rm -f $XCLIENTS
   echo "#!/bin/sh" > $XCLIENTS
   if [ "$screensaver" = "enabled" ]; then
      echo "/usr/X11R6/bin/xscreensaver -no-install -no-splash -no-lock-mode -timeout $screensaver_time &" >> $XCLIENTS
   fi
   chmod 755 $XCLIENTS

   if [ "$MODE" = "" ]; then

      # No configuration file was specified. This is the first bringup after an install.

      mode_start init

      mode_end init

   elif [ $MODE = "0" ]; then

      mode_start $MODE

      # 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
      sed -n /Exec/s/[\ +]\ //p $CONFIGFILE |
      {

         while read line
         do
           if echo $line | grep "Exec" > /dev/null 2>&1
           then
   
             echo_env line
   
             NAME=`echo_env line | sed -n 's/[^"]*\(\"[^"]*\"\).*/\1/p'`
             EXEC=`echo_env line | sed -n 's/.*Exec \(.*\)[yn]$/\1/p'`
             PROGNAME=`echo_env EXEC | awk '{print $1}'`
 
             if [ $PROGNAME = "setup3270" ]; then
                DESCNAME=`echo $EXEC | awk '{print $2}'`
                if [ "$DESCNAME" = "-desc" ]; then
                   PROGNAME="IBM3270"
                fi
             fi
             if [ $PROGNAME = "setup5250" ]; then
                DESCNAME=`echo $EXEC | awk '{print $2}'`
                if [ "$DESCNAME" = "-desc" ]; then
                   PROGNAME="IBM5250"
                fi
             fi

             case "$PROGNAME" in
                xterm)
                   ICON=xterm
                ;;
                IBM3270)
                   ICON=ibm3270
                ;;
                setup3270)
                   ICON=setup3270
                ;;
                IBM5250)
                   ICON=ibm5250
                ;;
                setup5250)
                   ICON=setup5250
                ;;
                wfica | wfcmgr)
                   ICON=ica
                ;;
                netscape)
                   ICON=navigator
                ;;
                *) 
                   ICON=app
             esac

             add_to_menu

           fi    
           if echo $line | grep "y$" > /dev/null 2>&1
           then
              echo "$EXEC &" >> $XCLIENTS
           fi
         done
      }

      mode_end $MODE 

   else 

      if [ $MODE = "1" ]; then
         # full screen netscape kiosk mode

         mode_start $MODE

         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 "( while true" >> $XCLIENTS
               echo "  do" >> $XCLIENTS
               echo "    $EXEC $GEOMETRY" >> $XCLIENTS
               echo "  done ) &" >> $XCLIENTS
            fi
         done

         mode_end $MODE

      elif [ $MODE = "2" -o $MODE = "3" -o $MODE = "4" ]; then 
         # full screen kiosk mode ICA remote manager - or -
         # full screen kiosk mode ICA client - or -
         # custom application

         mode_start $MODE

         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 "( while true" >> $XCLIENTS
               echo "  do" >> $XCLIENTS
               echo "    $EXEC " >> $XCLIENTS
               echo "  done ) &" >> $XCLIENTS
            fi
         done

         mode_end $MODE

      elif [ $MODE = "5" -o $MODE = "6" ]; then 
         # mode 5 == 3270 ... mode 6 == 5250 

         mode_start $MODE

         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

         mode_end $MODE

      fi
   fi
fi


process_desktop_preferences



