#! /bin/sh

. $TS_GLOBAL

case "$1" in
init)

 PACKAGE=`basename $0`

 if [ "`make_caps $CUSTOM_CONFIG`" = "ON" ] ; then
   let y=0
   cat $REPLIMENU/customize.tpl > $REPLIMENU/customize.menu
   (set | grep -e "|" -e "*" | grep -v NET_HOSTNAME ) |
   while read variable
   do
      # Grab out variables
      options=""
      name=`echo "$variable" | cut -f 1 -d=`
      list=`echo "$variable" | cut -f 2 -d= | cut -f 2 -d"'"`
      default=`echo "$list" | cut -f 1 -d"|"`
      if [ "`echo $name | cut -f 1 -d_`" = "SESSION" ] ; then
          shortname=`echo "$name" | cut -f 3- -d_`
      else
	  shortname="$name"
      fi

      if [ "$list" != "*" ] ; then
        echo "$name=$default" >> $TS_USER
	echo "$list" | sed -e 's/|/\
/g' > /tmp/options

	# Build Customize menu
	let y=y+1
	ITEM="begin item\n\
            name = item$y\n\
	    label = $shortname\n\
	    bullet =\n\
	    command = replimenu -c $MENUCOLOUR -f $REPLIMENU/$name.menu \n\
	    end"
	echo -e $ITEM >> $REPLIMENU/customize.menu
	# Build Options Menus
	let x=0
      	cat $REPLIMENU/options.tpl > $REPLIMENU/$name.menu
	(cat /tmp/options) |
	while read line
	do
	   let x=x+1
	   if [ "$line" = "*" ] ; then
	     echo -e "begin item\n\
               name = item$x\n\
	       label = USER DEFINED\n\
	       bullet =\n\
	       type = input | notempty $options \n\
	       command = echo "$name=\\\"\$RM_INPUT\\\"" >> $TS_USER \n\
	       end" >> $REPLIMENU/$name.menu
           else
	     echo -e "begin item\n\
               name = item$x\n\
	       label = $line\n\
	       bullet =\n\
	       type = ask \n\
	       command = echo \"$name='"$line"'\" >> $TS_USER \n\
	       end" >> $REPLIMENU/$name.menu
           fi
	done
      else
	  if echo $name | grep PASSWORD > /dev/null ; then
		options=" | password"
	  fi
	  echo -e "begin item\n\
		name = item$y\n\
		label = $shortname \n\
		bullet =\n\
		type = input | notempty $options \n\
		command = echo "$name=\\\"\$RM_INPUT\\\"" >> $TS_USER \n\
		end" >> $REPLIMENU/customize.menu

      fi	
   done

   echo -e "br ---------------\n\
        begin item\n\
	name = QUIT\n\
	label = Continue Boot\n\
	bullet =\n\
	command = exit 0 \n\
	end" >> $REPLIMENU/customize.menu

   rm /tmp/options

   replimenu -c $MENUCOLOUR -f $REPLIMENU/customize.menu < /dev/tty0 > /dev/tty0

   echo "CUSTOM_CONFIG='Off'" >> $TS_USER
 else
   let x=0
   while [ -n "`eval echo '$SESSION_'$x'_CUSTOM_CONFIG'`" ] ; do
	custom=`eval echo '$SESSION_'$x'_CUSTOM_CONFIG'`
	if [ "`make_caps $custom`" = "OFF" ] ; then
   	  (set | grep SESSION_"$x"_ | grep "|" ) |
	  while read variable
	  do
    	    # Grab out variables
     	    name=`echo "$variable" | cut -f 1 -d=`
      	    list=`echo "$variable" | cut -f 2 -d= | cut -f 2 -d"'"`
     	    default=`echo "$list" | cut -f 1 -d"|"`
     	    echo "$name=$default" >> $TS_RUNTIME
 	  done
	fi
	let x=x+1
   done

   (set | grep -v SESSION_ | grep "|" ) |
   while read variable
   do
       # Grab out variables
       name=`echo "$variable" | cut -f 1 -d=`
       list=`echo "$variable" | cut -f 2 -d= | cut -f 2 -d"'"`
       default=`echo "$list" | cut -f 1 -d"|"`
       echo "$name=$default" >> $TS_RUNTIME
   done
 fi
 ;;
help)
 echo "Usage: $0 init"
 ;;
*)
 exit 1
 ;;
esac

exit 0
