#!/bin/sh 

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

# Source the profile functions.
. /IBM/bin/profile.functions

echo_env () {
# remove the name, "=" and the start and end single quote of (name='value')
   set | sed -n /^$*=\'/s/^$*=\'//p | sed s/\'$//
}




IBMSetupInformationBlock=`profile_file IBMSetupInformationBlock`
if [ -n "$IBMSetupInformationBlock" ]; then
   . $IBMSetupInformationBlock
fi

if [ -f /tmp/profile/user/IBMSetupIcewmInfo ]; then
   . /tmp/profile/user/IBMSetupIcewmInfo
fi

# configure screensaver
if [ "$screensaver" = "enabled" ]; then
   mkdir -p /tmp/$USER
   xscreensaverResources=`profile_file xscreensaverResources`
   if [ -n "$xscreensaverResources" ]; then
      xrdb -nocpp -merge $xscreensaverResources
   else
      xrdb -nocpp -merge /IBM/bin/xscreensaverResources
   fi

   screensaver_image=`ls /tmp/profile/user/screensaver.*`
   screensaver_image=`echo $screensaver_image | cut -d' ' -f1`
   screensaver_image=${screensaver_image:-/IBM/IBMWIZARD.GIF}
   ln -sf $screensaver_image /tmp/profile/user/xscreensaver.img
fi

# if screensaver is not enabled, don't use X-Windows saver
if [ "$screensaver" != "enabled" ]; then
   xset s off
fi

#---------------------------------------------------------------------
# Fix netscape files.
#---------------------------------------------------------------------
if [ -f "/tmp/profile/user/home/.netscape/preferences.js" ]; then
   cat /tmp/profile/user/home/.netscape/preferences.js > /tmp/profile/user/preferences.js

   if grep "browser.startup.license_accepted" /tmp/profile/user/preferences.js; then
      {
         grep -v "browser.startup.license_accepted" /tmp/profile/user/preferences.js
         echo "user_pref(\"browser.startup.license_accepted\", \"$UID 4.78\");"
      } >> /tmp/profile/user/preferences.js.tmp
      mv /tmp/profile/user/preferences.js.tmp /tmp/profile/user/preferences.js
   fi
else
   touch /tmp/profile/user/preferences.js
   mkdir -p /tmp/profile/user/home/.netscape
fi

cat /tmp/profile/user/home/.netscape/preferences.js | sed s/user\\/\\//user\\//g | sed s/mytmpdir/profile/g >/tmp/profile/user/preferences.js

cp /tmp/profile/user/preferences.js /tmp/profile/user/home/.netscape/preferences.js
rm -f /tmp/profile/user/preferences.js

rm -rf /tmp/profile/user/home/.netscape/cache
rm -rf /tmp/profile/user/home/.netscape/archive
rm -rf /tmp/profile/user/home/.netscape/history.dat
mkdir -p /tmp/profile/user/.netscape/cache
mkdir -p /tmp/profile/user/.netscape/archive
mkdir -p /tmp/profile/user/home/.netscape/archive
ln -sf /tmp/profile/user/.netscape/history.dat /tmp/profile/user/home/.netscape/history.dat
#---------------------------------------------------------------------
# Fix netscape files.
#---------------------------------------------------------------------


if [ "$defaultprinter" != "" ]; then
   export PRINTER=$defaultprinter
fi


IBMSetupAudioInfo=`profile_file IBMSetupAudioInfo`
if [ -n "$IBMSetupAudioInfo" ]; then
   . $IBMSetupAudioInfo
   if [ "$soundmaster" != "" ]; then
      /IBM/bin/volume $soundmaster
   fi
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

if [ "$MODEL_TYPE" = "BIOS" ]; then
  /IBM/bin/IBMSetup.sh &
else
  if [ -f /bin/nice ]; then
    /bin/nice -n +5 /IBM/bin/IBMSetup &
  else
    /IBM/bin/IBMSetup &
  fi
fi

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

WM_Config=`profile_file WM_Config`
CONFIGFILE=${WM_Config:-/dev/null}
OLD_CONFIG="0"

XCLIENTS=/tmp/profile/user/.Xclients


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

   rm -f $XCLIENTS
   echo "#!/bin/sh" > $XCLIENTS

   # 12/04/01 - exclude xscreensaver from launch.   Somewhere down the line
   # this is causing X-server to crash after 40 minutes of inactivity.
   # so...  dont do that.   Check against specific model, other BIOS models
   # may not be affected.

      if [ "$screensaver" = "enabled" -a "$screensaver_lock" = "disabled" ]; then
           # lets not use the xservers screensaver now
           xset s off
           echo "/usr/X11R6/bin/xscreensaver -no-install -no-splash -cycle 0 -no-lock-mode -timeout ${screensaver_time:-5} &" >> $XCLIENTS
      elif [ "$screensaver" = "enabled" -a "$screensaver_lock" = "enabled" ]; then

        # Read the authentication configuration.
        if [ -f /tmp/profile/machine/IBMSetupLogin ]; then
           . /tmp/profile/machine/IBMSetupLogin
        fi
             echo $AutomaticLoginEnable
         if [ "$AutomaticLoginEnable" = "false" ]; then
           # lets not use the xservers screensaver now
           xset s off
           echo "/usr/X11R6/bin/xscreensaver -no-install -no-splash -cycle 0 -lock-mode -lock-timeout 0 -timeout ${screensaver_time:-5} &" >> $XCLIENTS
         else
           # lets not use the xservers screensaver now
           xset s off
           echo "/usr/X11R6/bin/xscreensaver -no-install -no-splash -cycle 0 -no-lock-mode -timeout ${screensaver_time:-5} &" >> $XCLIENTS
         fi
      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

      cat $CONFIGFILE |
      {

         while read line
         do
   
             TYPE=`echo "$line" | awk '{print $1}'`
             if [ $TYPE = "DestroyMenu" -o $TYPE = "AddToMenu" ]; then
                OLD_CONFIG="1"
                line=`echo "$line" | sed -n 's/.*8080^\"\(.*\)/\1/p'`
             fi
             if [ $TYPE = "+" ]; then
                TYPE="MENU_ENTRY"
             fi

             NAME=`echo "$line" | sed -n 's/[^"]*\(\"[^"]*\"\).*/\1/p'`
             if echo $TYPE | grep "MODE" > /dev/null 2>&1
             then
                TYPE="SKIP_LINE"
             else
                if [ $OLD_CONFIG = "0" ]; then
                  EXEC=`echo "$line" | sed -n 's/.*Exec \(.*\) [yn]$/\1/p'`
                  INSTANCES=`echo "$line" | sed -n 's/.*" \([0-9]\) Exec .*$/\1/p'`
                  CUSTOM_ICON=`echo "$line" | sed -n 's/.* -Icon \([^ ]*\) .*$/\1/p'`
                else
                  if echo $line | grep "Exec" > /dev/null 2>&1
                  then
                    EXEC=`echo $line | sed -n 's/.*Exec \(.*\)[yn]$/\1/p'`
                    INSTANCES="1"
                    CUSTOM_ICON=""
                    TYPE="MENU_ENTRY"
                  else
                    TYPE="SKIP_LINE"
                  fi
                fi
                PROGNAME=`echo "$EXEC" | awk '{print $1}'`
                REST_OF_EXEC=`echo "$EXEC" | sed -n 's/^[^ ]* \(.*\)$/\1/p'`
             fi

             if [ $TYPE = "MENU_ENTRY" ]; then
               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
                     if echo $REST_OF_EXEC | grep "VT220" > /dev/null 2>&1
                     then
                        ICON=vt220
                     fi
                  ;;
                  IBM3270)
                     ICON=ibm3270
                  ;;
                  setup3270)
                     ICON=setup3270
                  ;;
                  IBM5250)
                     ICON=ibm5250
                  ;;
                  setup5250)
                     ICON=setup5250
                  ;;
                  wfica | wfcmgr)
                     ICON=ica
                  ;;
                  netscape)
                     ICON=navigator
                  ;;
                  IBMSetup)
                     ICON=ibmsetup
                  ;;
                  ibmterm)
                     ICON=IBM-Term
                     EXEC=`eval echo $EXEC`
                  ;;
                  *) 
                     ICON=app
               esac

               if echo $line | grep "y$" > /dev/null 2>&1
               then
                  while [ $INSTANCES -ne 0 ]
                  do
                     echo "$EXEC &" >> $XCLIENTS
                     let INSTANCES-=1
                  done
               fi
               add_to_menu
             fi
             if [ $TYPE = "START_FOLDER" ]; then
               ICON=folder
               start_folder_in_menu
             fi
             if [ $TYPE = "END_FOLDER" ]; then
                end_folder_in_menu
             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


if [ -x $XCLIENTS ]; then
   $XCLIENTS &
fi


