#!/sbin/sh

#########################################################################
# 									
#   File:	 customize
#   @(#) customize $Revision: 1.10 $ $Date: 93/02/23 12:09:18 $
#   Description: /etc/update customization script for HPXT-XDM fileset
# 
#########################################################################
#

umask 022
set -a
PATH="/bin:/usr/bin:/etc:/usr/bin/X11/700X/admin"

FILESET=HPXT-XDM

# Get environment variables as setup earlier in prerequisite fileset    
#
ENVARDIRS="/opt/hpxt/enware/lbin /usr/bin/X11/700X/admin /tmp /usr/tmp last"
for ENVARDIR in $ENVARDIRS
do
  if [ $ENVARDIR = "last" ]
  then
    echo "WARNING: cannot find suitable env.vars file"
    exit 0
  fi

  if [ -f $ENVARDIR/env.vars ]
  then
    . $ENVARDIR/env.vars
    break
  fi
done


if [ "$OS" = "hp10" ]
then
  if [ -f /usr/bin/dcnodes -o -f /usr/sbin/dcnodes ]
  then
    dcnodes -q
    if [ $? -eq 2 ]
    then
        exit 0
    fi
  fi

  CONTROL_UTILS=/usr/lbin/sw/control_utils
  if [ ! -f $CONTROL_UTILS ]
  then
    echo "ERROR:   Cannot find $UTILS"
    exit 1
  fi
  set +u
  . $CONTROL_UTILS
  set -u
  PATH=$SW_PATH:/opt/hpxt/enware/lbin
fi


if [ "$OS" = "hp10" ]
then
  NEWXDMLIB=${INSTALLDIR}/xdm
  XDMFILES="Xaccess Xresources Xservers xdm-config \
            Xsession Xstartup Xreset sys.xsession"

  for FILE in $XDMFILES
  do
    if [ -f $NEWXDMLIB/$FILE ]
    then
      cmp -s ${INSTALLDIR}/newconfig/$FILE $NEWXDMLIB/$FILE
      if [ $? -ne 0 ]
      then
        cp $NEWXDMLIB/$FILE $NEWXDMLIB/$FILE.old.$$
        xtutil_add-file $NEWXDMLIB/$FILE.old.$$
        echo "INFO:    existing $FILE seems customized."
        echo "         .. file saved as $NEWXDMLIB/$FILE.old.$$"
      fi
    fi
    cp ${INSTALLDIR}/newconfig/$FILE $NEWXDMLIB
    xtutil_add-file $NEWXDMLIB/$FILE
  done


  ############################################################
  # modify xdm-config depending on where xdm lives
  # xdm assumes its config files are in $XDMDIR
  # config files in turn have hard coded paths also to $XDMDIR
  # These are here changed to ${TOPDIR}${XDMDIR}
  ############################################################

  FILES="xdm-config Xsession"
  XDMDIR=/usr/lib/X11/xdm

  for i in $FILES
  do
   if [ -f $NEWXDMLIB/$i ]
   then
     egrep "$NEWXDMLIB" $NEWXDMLIB/$i > /dev/null
     if [ $? -ne 0 ]
     then
       sed "s!$XDMDIR!$NEWXDMLIB!g" $NEWXDMLIB/$i > /tmp/$i.$$
       if [ -s /tmp/$i.$$ ]
       then
         cp /tmp/$i.$$ $NEWXDMLIB/$i
         rm /tmp/$i.$$
       fi
     fi
   fi
  done


  ################################################################
  # do the same for xdm-config: /usr/bin/X11(sessreg)(chooser)
  ################################################################

  egrep "/usr/lib/X11/chooser" $NEWXDMLIB/xdm-config > /dev/null
  if [ $? -eq 0 ]
  then
    sed -e "s!/usr/bin/X11/chooser!$NEWXDMLIB/chooser!g" \
	-e "s!/usr/bin/X11/sessreg!$NEWXDMLIB/sessreg!g" \
	$NEWXDMLIB/xdm-config  > /tmp/xdm-config.$$
    if [ -s /tmp/xdm-config.$$ ]
    then
      cp /tmp/xdm-config.$$ $NEWXDMLIB/xdm-config
      rm /tmp/xdm-config.$$
    else
      echo "WARNING: edit of $NEWXDMLIB/xdm-config failed"
    fi
  fi

  #########################################################################
  # for sys.xsession, replace /usr/bin/X11/700X with ${INSTALLDIR}/bin 
  # also replace /usr/lib/X11/700X with ${INSTALLDIR}/xthome 
  #########################################################################

  egrep 'XTBINDIR=unknown' $NEWXDMLIB/sys.xsession > /dev/null
  if [ $? -eq 0 ]
  then
    sed -e "s!XTBINDIR=unknown!XTBINDIR=${INSTALLDIR}/bin!g" \
	-e "s!XTHOME=unknown!XTHOME=${INSTALLDIR}/xthome!g" \
		$NEWXDMLIB/sys.xsession  > /tmp/sys.xsession.$$
    if [ -s /tmp/sys.xsession.$$ ]
    then
      cp /tmp/sys.xsession.$$ $NEWXDMLIB/sys.xsession
      rm /tmp/sys.xsession.$$
    else
      echo "WARNING: edit of $NEWXDMLIB/sys.xsession failed"
    fi
  fi

  #########################################################################
  # for Xstartup/Xreset, replace /usr/bin/X11/700X/admin with ${INSTALLDIR}/lbin 
  #########################################################################
  XDMSCRIPTS="Xstartup Xreset"
  OLDADMIN=/usr/bin/X11/700X/admin
  NEWADMIN=${INSTALLDIR}/lbin

  for SCRIPT in $XDMSCRIPTS
  do
    egrep "$OLDADMIN" $NEWXDMLIB/$SCRIPT > /dev/null
    if [ $? -eq 0 ]
    then
      sed -e "s!${OLDADMIN}!${NEWADMIN}!g" \
	$NEWXDMLIB/$SCRIPT  > /tmp/$SCRIPT.$$
      if [ -s /tmp/$SCRIPT.$$ ]
      then
        cp /tmp/$SCRIPT.$$ $NEWXDMLIB/$SCRIPT
        rm /tmp/$SCRIPT.$$
      else
        echo "WARNING: edit of $NEWXDMLIB/$SCRIPT failed" >&2
      fi
    fi
  done


  #echo SW_SESSION_OPTIONS=$SW_SESSION_OPTIONS 
  #echo $SW_SESSION_OPTIONS | egrep 'ENWARE.HPXT.FLOPPY' > /dev/null

  if [ -f ${INSTALLDIR}/xthome/bin/Floppy ]
  then
    VUEDIR=/etc/vue/config
    if [ -f $VUEDIR/Xstartup -a -f $VUEDIR/Xreset ]
    then
      cat $VUEDIR/Xstartup $VUEDIR/Xreset | egrep -v '^#' \
	| egrep '[a-z]' > /dev/null
      if [ $? -ne 0 ]
      then
        cp $VUEDIR/Xstartup $VUEDIR/Xstartup.orig.$$
        cp $VUEDIR/Xreset $VUEDIR/Xreset.orig.$$

        cat ${INSTALLDIR}/xdm/Xstartup \
	  | sed '/^sessreg/s/sessreg/#sessreg/'  > $VUEDIR/Xstartup
        cat ${INSTALLDIR}/xdm/Xreset   \
	  | sed '/^sessreg/s/sessreg/#sessreg/'  > $VUEDIR/Xreset
      fi
    fi
  fi

  #-----------------------------------------------------------------#
  # If xdm does not exist (in the traditional place)
  # then create links from those into our version in /opt/hpxt/enware
  #-----------------------------------------------------------------#
 
  if [ ! -d /usr/lib/X11/xdm ]
  then
    if [ ! -f /usr/bin/X11/xdm ]
    then
      ln -s /opt/hpxt/enware/xdm     /usr/lib/X11/xdm
      ln -s /opt/hpxt/enware/xdm/xdm /usr/bin/X11/xdm
    fi
  fi


  exit 0
fi


if [ "$OS" = "hp" ]
then
  ARCH=$1
  ARCHEXT=""
  case $ARCH in
    HP-MC68*)
	if [ ! hp9000s300 ]
	then
	    ARCHEXT="+/$ARCH"
	fi
	;;
  esac
fi

if [ -x /bin/getcontext ]
then
    CONTEXT=`/bin/getcontext | /usr/bin/awk '{print $1}'`
else
    CONTEXT=standalone
fi

# create xdm directory if necessary
if [ ! -d /usr/lib/X11/xdm ]
then
   /bin/mkdir /usr/lib/X11/xdm 2> /dev/null
   if [ $? -ne 0 ]
   then
      echo "ERROR:   Cannot create directory /usr/lib/X11/xdm." >&2
   fi
   if [ "$CONTEXT" != "standalone" ]
   then
      /usr/bin/makecdf -d $CONTEXT /usr/lib/X11/xdm 2> /dev/null
      if [ $? -ne 0 ]
      then
         echo "ERROR:   Cannot make CDF from /usr/lib/X11/xdm." >&2
      fi
   fi
fi

# conditionally copy config files to xdm config dir
if [ "$CONTEXT" = "standalone" ]
then
   dest="/usr/lib/X11/xdm"
   /system/$FILESET/xdm_config /etc/newconfig/xdm $dest
   CNODES="standalone"
else

   CNODES=`egrep -v '^#' /etc/clusterconf | /usr/bin/awk -F: '{if ($4 == "r" || $4 == "c") {print $3}}'`
   for CNODE in $CNODES
   do
      dest="/usr/lib/X11/xdm+/${CNODE}"
      /system${ARCHEXT}/$FILESET/xdm_config /etc/newconfig${ARCHEXT}/xdm $dest
   done
fi

# check if .greetless was used
if [ -s /etc/.greetless ]
then
	cat <<EOF
WARNING: It appears that you were using the greetless feature of xdm.
         If you still intend to run xdm instead of the default vuelogin,
         you will need to convert the lines in your "/etc/.greetless" file
         to BYPASS_LOGIN lines in "/usr/lib/X11/xdm/Xaccess".
EOF
fi


XDMPPID=""

for CNODE in $CNODES
do
  if [ "$CNODE" = "standalone" -a "$CONTEXT" = "standalone" ]
  then
    XDMDIR="/usr/lib/X11/xdm"
  else  
    XDMDIR="/usr/lib/X11/xdm+/$CNODE"
  fi

done

XDMPPID=`ps -ef | awk '$3 != "1"' | egrep '/xdm$' | grep -v grep | awk '{print $3}' | head -1`

if [ "$XDMPPID" != "" ]
then
  kill -1 $XDMPPID
  echo ""
  echo "NOTE:  sent -1 signal to xdm to make it re-read xdm-config"
fi

# remove old 700X fileset
/bin/rm -rf /system${ARCHEXT}/XDM 2>/dev/null
/bin/rm -f /etc/filesets${ARCHEXT}/XDM 2>/dev/null

# remove old 700RX fileset
/bin/rm -rf /system${ARCHEXT}/700RX-XDM 2>/dev/null
/bin/rm -f /etc/filesets${ARCHEXT}/700RX-XDM 2>/dev/null

exit 0
