#!/bin/sh
#@(#)HP X Station Admin Scripts
#
# makexsession
#


if [ $OS = "hp" ]
then 
  DMS="xdm"
elif [ $OS = "sco" ]
then
  DMS="scologin"
else
  DMS="xdm"
  OH=$OPENWINHOME
fi

A="a"
echo ""

	#	
	#	Ask if installer wants a .xsession file placed in  
	# 	a user home directory.	
	#	
	#	

while [ 0 ]
do
  if [ $OS = "sun4" -o $OS = "sun5" -a -f $OPENWINHOME/lib/Xinitrc ]
  then
    if [ -f $ADMINDIR/XSESSIONCHOICE ]
    then
      XSESSION=`cat $ADMINDIR/XSESSIONCHOICE`
      if [ $XSESSION = ".xsession" ]
      then
        SYSXSESSION=$XDMDIR/sys.xsession
      else
        SYSXSESSION=$OPENWINHOME/lib/Xinitrc
      fi
    else
      XSESSION=".xsession or .xinitrc"
    fi
  else
    SYSXSESSION=$XDMDIR/sys.xsession 
    XSESSION=".xsession"
  fi

  echo
  $ECHO "Copy $XSESSION script to $A user home directory? [y|n|?] (def=y): $NL"
  read SEL 
  echo ""
  case $SEL in
  y|Y|YES|yes|"")
     if [ "$XSESSION" = ".xsession or .xinitrc" ]
     then
     while true 
     do
	echo ""
	echo "You can choose between having users excute"
	echo " 1. the xinitrc script"
	echo " or "
	echo " 2. the xsession script"
	echo "when they log in through $DMS"
        $ECHO "Please enter selection [1|2|q|?]   (default=1) $NL"
       REPLY=`line`
       case $REPLY in
         q|Q|quit|QUIT)
	   exit
	   ;;
         1|"")
           SYSXSESSION=$OPENWINHOME/lib/Xinitrc
           XSESSION=.xinitrc
	   echo "$XSESSION" > $ADMINDIR/XSESSIONCHOICE

	   egrep "^[^#].*cmdtool.*-C" \
		$OPENWINHOME/lib/openwin-init > /dev/null
           if [ $? -eq 0 ]
	   then
	     cat <<EOF

WARNING:
  .xinitrc normally invokes $OPENWINHOME/lib/openwin-init
  Your $OPENWINHOME/lib/openwin-init seems to invoke cmdtool with the -C option
  This may remove control of the console from user that currently has it.
  It is recommended that you edit openwin-init and remove this option.
EOF
             while true
	     do
	       echo ""
	       echo "(openwin-init will be saved before any changes)"
               $ECHO "do you want script to edit/remove option? [y|n]  (default=y) $NL"
               read REPLY  
	       case $REPLY in 
                 y|Y|YES|yes|"")
		   cp $OH/lib/openwin-init  $OH/lib/openwin-init.$$
                   echo "existing version saved as $OH/lib/openwin-init.$$"
		   sed '/cmdtool/s/-C//g' $OH/lib/openwin-init > tmp.$$
		   cp tmp.$$		  $OH/lib/openwin-init
		   rm tmp.$$
		   echo "option removed"
		   break
		   ;;
                 n|N|NO|no)
		   echo "$OH/lib/openwin-init unchanged"
		   break
		   ;;
                 *)
		   ;;
               esac
             done
	     echo
           fi


	   break
	   ;;
         2)
           SYSXSESSION=$XDMDIR/sys.xsession
           XSESSION=.xsession
	   echo "$XSESSION" > $ADMINDIR/XSESSIONCHOICE
	   break
	   ;;
         \?)
	   cat <<EOF

When a user logs in through $DMS, it will attempt to start X clients/programs
specified in the $XSESSION script in user's home directory.
This selection will copy either 

1) $OPENWINHOME/Xinitrc to home_dir/.xinitrc 
or 
2) $XDMDIR/sys.xsession to home_dir/.xsession 

The .xsession script will start up olwm, cmdtool, filemgr, and xterm.
Some olwm functionality (eg "Save Workspace") may be missing.
Choosing .xsession makes it possible to maintain a session script
different from the .xinitrc script executed by $OPENWINHOME/bin/openwin.
.xinitrc, on some systems, may contain "sunview'isms"/"NeWS'isms". 

The .xinitrc script will normally start up olwm, cmdtool, filemgr and others.
Choosing .xinitrc makes it possible to maintain a session script
identical to the one normally executed by $OPENWINHOME/bin/openwin.
Normally this will work fine. On some systems, however, .xinitrc
may contain "sunview'isms"and/or "NeWS'isms". This will not work with 
the HP X Station. 
If uncustomized, .xinitrc will excute "cmdtool -C" which may remove control 
of the console from user that currently has it. If you choose to use .xinitrc
this script may assist you in correcting this.
EOF
             $ECHO "enter <return> to continue: $NL"
             read SEL 
	   ;;
         *)
	   continue
           ;; 
       esac
    done
    fi

       A="another"

     $ECHO "Please enter user login name or q to quit and continue: (q) $NL"
     JOE=`line`
     export JOE
     case $JOE in
       q|Q|quit|QUIT|"")
	 break
	 ;;
       *)
                #
		#  Try to make sure that specified user name
		#  seems to exist and that a home directory is there
		#

	 HOMEDIR=`echo /bin/ls -d "~$JOE" | /bin/csh 2> /dev/null`
	 echo $HOMEDIR | grep '/' > /dev/null 2>&1
	 if [ $? -ne 0 ]
	 then
	   echo "Could not find home directory of user $JOE"
	   continue
         fi

	 echo "/bin/ls"  | su $JOE 2>&1 > /dev/null 2>&1
	 if [ $? -ne 0 ]
	 then
	   echo "user $JOE does not seem to exist"
	   continue
         fi

                #
		#  Don't overwrite existing .xsession files 
		#  unless user OKs it.
		#

	 echo "/bin/ls $HOMEDIR/$XSESSION"  | su $JOE 2>&1 \
		| egrep -i '(not found)|(no such)' > /dev/null 2>&1
	 if [ $? -eq 0 ]
	 then
	   echo "/bin/cp $SYSXSESSION $HOMEDIR/$XSESSION" | su $JOE 2>&1 > /dev/null 2>&1
	   echo "/bin/chmod 755  $HOMEDIR/$XSESSION" | su $JOE 2>&1  > /dev/null 2>&1
	   echo "copied a sample $XSESSION file to home directory of $JOE"
           #
           # Sneak in an openwin-init without "cmdtool -C", just "cmdtool"
           #
           if [ ! -f $HOMEDIR/.openwin-init -a "$XSESSION" = ".xinitrc" ]
           then
             OI="$ADMINDIR/openwin-init"
             echo "/bin/cp $OI $HOMEDIR/.openwin-init" | su $JOE 2>&1 > /dev/null 2>&1
	     echo "/bin/chmod 755  $HOMEDIR/.openwin-init" | su $JOE 2>&1 > /dev/null 2>&1
           fi  
	 else
	   echo "There's already a $XSESSION file in home directory of $JOE"
           $ECHO "do you want it to be replaced with the new version? : (default=y) $NL"
	   REPLY=`line`
           case $REPLY in
            y|Y|yes|YES|yy|"")
	      echo "/bin/chmod u+w $HOMEDIR/$XSESSION" | su $JOE 2>&1 > /dev/null 2>&1
	      echo "/bin/rm -f $HOMEDIR/$XSESSION" | su $JOE 2>&1 > /dev/null 2>&1
	      echo "/bin/cp $SYSXSESSION $HOMEDIR/$XSESSION" | su $JOE 2>&1 > /dev/null 2>&1
	      echo "/bin/chmod 755  $HOMEDIR/$XSESSION" | su $JOE 2>&1 > /dev/null 2>&1
	      echo "replaced existing $XSESSION file with a new version"
          
              #
              # Sneak in an openwin-init without "cmdtool -C", just "cmdtool"
              #
              if [ "$XSESSION" = ".xinitrc" ]
              then
                if [ -f $HOMEDIR/.openwin-init ]
                then
                  echo "cp $HOMEDIR/.openwin-init $HOMEDIR/.openwin-init.orig.$$" | su $JOE 2>&1 > /dev/null 2>&1
                  echo HOME/.openwin-init saved as HOME/.openwin-init.orig.$$
                fi

                OI="$ADMINDIR/openwin-init"
                echo "/bin/cp $OI $HOMEDIR/.openwin-init" | su $JOE 2>&1 > /dev/null 2>&1
	        echo "/bin/chmod 755  $HOMEDIR/.openwin-init" | su $JOE 2>&1 > /dev/null 2>&1
                echo created a new HOME/.openwin-init 
              fi  

	      ;;
            *)
	      echo " .. existing $XSESSION file left unchanged"
	     ;;
           esac
         fi

         #
	 #  warn c-shell users 
	 #

	 if [ -f $HOMEDIR/.cshrc ]
	 then
	   XTBINDIR="/usr/bin/X11/700X"
	   grep "$XTBINDIR" $HOMEDIR/.cshrc > /dev/null
	   if [ $? -ne 0 ]
	   then
	     echo
	     echo "WARNING: The path setting in $HOMEDIR/.cshrc does not"
	     echo "         seem to include directory $XTBINDIR."
	     echo "         Please edit and include $XTBINDIR in path."
	     echo
             $ECHO "enter <return> to continue: $NL"
             read REPLY
	   fi
	 fi

         #
	 #  Do the .xinitrc stuff only if on a Sun host
	 #

	 if [ $OS != "sun4" -a $OS != "sun5" -a "$XSESSION" != ".xinitrc" ]
	 then
	   continue
         fi

	 if [ "$XSESSION" = ".xinitrc" ]
	 then
	   continue
         fi

                #
		#  Again only  overwrite existing .xinitrc file 
		#  if user OKs it.
		#

	 echo "/bin/ls $HOMEDIR/.xinitrc" | su $JOE 2>&1 \
		| egrep -i '(not found)|(no such)' > /dev/null 2>&1
	 if [ $? -eq 0 ]
	 then
	   echo "/bin/ln -s $HOMEDIR/$XSESSION $HOMEDIR/.xinitrc" | su $JOE 2>&1 > /dev/null 2>&1
	   echo "/bin/ls $HOMEDIR/.xinitrc"  | su $JOE 2>&1 \
		| egrep -i '(not found)|(no such)' > /dev/null 2>&1
	   if [ $? -ne 0 ]
	   then
	     echo "linked .xinitrc to $XSESSION"
	   else
	     echo "linking of .xinitrc to $XSESSION failed"
           fi
         else
	   echo "There's already a .xinitrc file or link in home directory of $JOE"
           $ECHO "do you want it to be replaced with a link to new $XSESSION file? : (y) $NL"
	   REPLY=`line`
           case $REPLY in
            y|Y|yes|YES|yy*|"")
	      echo "/bin/chmod u+w $HOMEDIR/.xinitrc" | su $JOE  2>&1 > /dev/null 2>&1
	      echo "/bin/rm -f $HOMEDIR/.xinitrc" | su $JOE 2>&1 > /dev/null 2>&1
	      echo "/bin/ln -s $HOMEDIR/$XSESSION $HOMEDIR/.xinitrc" | su $JOE 2>&1 > /dev/null 2>&1
	      echo "replaced existing .xinitrc with a link to new $XSESSION file"
	      ;;
            *)
	      echo " .. existing .xinitrc left unchanged"
	      continue
	     ;;
           esac
	 fi
	 ;;
     esac
     ;;
  \?)	 
	cat <<EOF

When a user logs in through $DMS (or vuelogin in "xdm" mode) it will attempt
to start X clients specified in $XSESSION script in user's home
directory. This selection will copy a sample $XSESSION script
to users home directory, so that you will have a personal version that you 
can customize if you so wish.  The script contains commands, which 
will start up a window manager and some useful X clients automatically, 
whenever a user logs in through $DMS. 
The .xsession script contains useful comments regarding start-up 
of X clients and window managers running locally on the HP X Station 
and other customization information. 
Once you have managed to log in using $DMS you may want to customize your 
environment by editing the $XSESSION script in your home directory.
EOF
	if [ $OS = "sun4" -o $OS = "sun5" ]
	then
	  cat <<EOF

If you are using Sun's $DMS (from \$OPENWINHOME/bin/xdm) clients will normally
be started from from ~/.xinitrc.
To change this you may want to edit file \$OPENWINHOME/lib/xdm/Xsession or 
you can make ~/.xinitrc a link pointing to ~/.xsession. 

If you want to alternate between .xsession and .xinitrc, you can choose
menu item "5) XDM Administration" and then "3) Create script .."

EOF
          $ECHO "enter <return> to continue: $NL"
          read REPLY
        fi
	;;
  n|N|NO|no)
    break
    ;;
  *)	 
    continue
    ;;
  esac
done
