#!/bin/ksh
# *                                                                      *
# * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
# * (c) Copyright 1993, 1994 International Business Machines Corp.       *
# * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
# * (c) Copyright 1993, 1994 Novell, Inc.                                *
# *
#
# @(#)dtconfig.src 1.4 94/07/26 
#
# This version of the CDE dtconfig script is for use in the Solaris(TM)
# operating environment.
#
#  Common Desktop Environment
#
#  (c) Copyright 1993, 1994 Hewlett-Packard Company
#  (c) Copyright 1993, 1994 International Business Machines Corp.
#  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
#  (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of
#      Novell, Inc.
#
# This script can be run after CDE installation to configure CDE within
# the host platforms operating environment.  For example, when auto-start 
# is selected, the CDE desktop login window will be displayed at end of the
# workstation's power up boot cycle.
#
# Location:	/usr/dt/bin/dtconfig 

mode=$1
arg_count=$#
prg_name=$0

Message()
{
# Message() - display message from dtconfig.cat
#   $1 - message id
#   $2 - fallback message text

  if [ -x $DTDSPMSG ]; then
	$DTDSPMSG -s 20 dtconfig $1 "$2" "$3"
	echo " "
  else
	echo "$2"
  fi
}

usage_error() {
    echo " "
    Message 1 " CDE configuration utility"
    echo " "
    Message 2  "  $prg_name -d        (disable auto-start)" $prg_name
    Message 3  "  $prg_name -e        (enable auto-start)" $prg_name
    Message 4  "  $prg_name -kill     (kill dtlogin)" $prg_name
    Message 5  "  $prg_name -reset    (reset dtlogin)" $prg_name
    Message 6  "  $prg_name -p        (printer action update)" $prg_name
    Message 25 "  $prg_name -inetd    (inetd.conf /usr/dt daemons)" $prg_name
    Message 26 "  $prg_name -inetd.ow (inetd.conf /usr/openwin daemons)" $prg_name
    echo " "
}

#
# Allow for configured and factory versions of dtlogin.rc
#

if [ -f /etc/dt/config/dtlogin.rc ]
then
  RC_MASTER=/etc/dt/config/dtlogin.rc
elif [ -f /usr/dt/config/dtlogin.rc ]
then
  RC_MASTER=/usr/dt/config/dtlogin.rc
fi

RC_COPY=/etc/init.d/dtlogin

LEVEL_0_KILL=/etc/rc0.d/K10dtlogin
LEVEL_1_KILL=/etc/rc1.d/K10dtlogin
LEVEL_2_START=/etc/rc2.d/S99dtlogin

DTDSPMSG=/usr/dt/bin/dtdspmsg

NAWK=/usr/bin/nawk


set `/bin/id`
if [ $1 != "uid=0(root)" ]; then
        Message 7 "$prg_name: this script must be run as root" $prg_name
        exit 1
fi


if [ $arg_count -ne 1 ]; then
	echo " "
#
# Display error message for too many arguments
#
        if [ $arg_count -gt 1 ]; then
                Message 8 "Usage: Too many arguments"
 
#
# Display error message for NULL arguments passed
#
        elif [ $arg_count -eq 0 ]; then
                Message 9 "Usage: Needs one argument"
 
        fi
	usage_error
        exit 1
fi


rc_master_check() {
	if [ ! -x $RC_MASTER ]; then
	   Message 10 "option failed"
	   Message 11 "could not execute $RC_MASTER" $RC_MASTER
	   exit 1
	fi
}

nawk_check() {
	if [ ! -x $NAWK ]; then
	   Message 10 "option failed"
	   Message 11 "could not execute $NAWK" $NAWK
	   exit 1
	fi
}


# Following inetdSP() procedure is from Sun CDE SI post_install
# configMin file.  It adds the Subprocess Control daemon inetd.conf
# line so it points to /usr/dt/bin/dtspcd.

inetdSP()
{
FILE=/etc/inetd.conf
TMPFILE=/tmp/inetd.conf

#
# check for existence of /etc/inetd.conf
#

    if [ ! -f $FILE ]   # highly unlikely
    then
        echo "" >$FILE
    fi

#
# see if it already exists
#

    $NAWK '{if ($1 == "dtspc")
                print $0 > "/tmp/dtspc-already-there"
         }' $FILE >/dev/null

    DTSPCD=/usr/dt/bin/dtspcd

    if [ ! -f /tmp/dtspc-already-there ]
    then
        echo "dtspc stream tcp nowait root $DTSPCD $DTSPCD" >>$FILE
    else
        rm /tmp/dtspc-already-there
    fi

#
# remove legacy entries like dtspcd
#

    $NAWK '{if ($1 == "dtspcd" || $1 == "#dtspcd")
                ;
          else
                print $0
         }' $FILE >$TMPFILE

    cp $TMPFILE $FILE
    rm $TMPFILE

FILE=/etc/services
TMPFILE=/tmp/services

#
# check for existence of /etc/services
#

    if [ ! -f $FILE ]   # highly unlikely
    then
        echo "" >$FILE
    fi

#
# see if it already exists
#
    $NAWK '{if ($1 == "dtspc")
                print $0 > "/tmp/dtspc-already-there"
         }' $FILE >/dev/null

    if [ ! -f /tmp/dtspc-already-there ]
    then
        echo "dtspc\t6112/tcp\t#subprocess control" >>$FILE
    else
        rm /tmp/dtspc-already-there
    fi

#
# remove legacy entries like dtspcd
#

    $NAWK '{if ($1 == "dtspcd" || $1 == "#dtspcd")
                ;
          else
                print $0
         }' $FILE >$TMPFILE

    cp $TMPFILE $FILE
    rm $TMPFILE
}


# Following inetdTT() procedure code from Sun CDE SI post_install
# configTT file.  It updates the ToolTalk daemon inetd.conf
# line so it points to /usr/dt/bin/rpc.ttdbserverd.

inetdTT()
{
FILE=/etc/inetd.conf
TMPFILE=/tmp/inetd.conf
TTDBSERVERD=/usr/dt/bin/rpc.ttdbserverd

#
#  remove any non-dt ttdbserverd line
#

    $NAWK -v ttdb=$TTDBSERVERD \
        '{if ($1 == "100083/1" && $6 != ttdb)
                ;
          else
                print $0
         }' $FILE >$TMPFILE

    cp $TMPFILE $FILE
    rm $TMPFILE

#
#  add dt ttdbserverd line if its not already there
#

    $NAWK '{if ($1 == "100083/1")
                print $0 > "/tmp/tt-already-there"
         }' $FILE >/dev/null

    if [ ! -f /tmp/tt-already-there ]
    then
        echo "100083/1 stream rpc/tcp wait root $TTDBSERVERD $TTDBSERVERD" \
                 >>$FILE
    else
        rm /tmp/tt-already-there
    fi
}


# Following owTT() procedure updates the ToolTalk daemon inetd.conf
# line so it points to /usr/openwin/bin/rpc.ttdbserverd.

owTT()
{
FILE=/etc/inetd.conf
TMPFILE=/tmp/inetd.conf
TTDBSERVERD=/usr/openwin/bin/rpc.ttdbserverd

#
# remove any non-ow ttdbserverd line
#

    $NAWK -v ttdb=$TTDBSERVERD \
        '{if ($1 == "100083/1" && $6 != ttdb)
                ;
          else
                print $0
         }' $FILE >$TMPFILE

    cp $TMPFILE $FILE
    rm $TMPFILE

#
# add ow ttdbserverd line if its not already there
#

    $NAWK '{if ($1 == "100083/1")
                print $0 > "/tmp/tt-already-there"
         }' $FILE >/dev/null

    if [ ! -f /tmp/tt-already-there ]
    then
        echo "100083/1 stream rpc/tcp wait root $TTDBSERVERD $TTDBSERVERD" \
                 >>$FILE
    else
        rm /tmp/tt-already-there
    fi
}


# Following inetdCM() procedure is from Sun CDE SI post_install
# configRun file.  It updates the Calendar Manager daemon inetd.conf
# line so it points to /usr/dt/bin/rpc.cmsd.

inetdCM()
{
FILE=/etc/inetd.conf
TMPFILE=/tmp/inetd.conf
CMSD=/usr/dt/bin/rpc.cmsd

#
# remove any non-dt rpc.cmsd line
#

    $NAWK -v cmsd=$CMSD \
        '{if ( ($1 == "100068/2-4" || $1 == "100068/2-5") && $6 != cmsd)
                ;
          else
                print $0
         }' $FILE >$TMPFILE

    cp $TMPFILE $FILE
    rm $TMPFILE

# 
# remove possible old dt cmsd level 2-4 entry
#

    $NAWK -v cmsd=$CMSD \
        '{if ($1 == "100068/2-4" && $6 == cmsd)
                ;
          else
                print $0
         }' $FILE >$TMPFILE

    cp $TMPFILE $FILE
    rm $TMPFILE

#
# add dt cmsd line in needed 
#

    $NAWK -v cmsd=$CMSD \
        '{if ($6 == cmsd && $1 == "100068/2-5")
                print $0 > "/tmp/cmsd-already-there"
         }' $FILE >/dev/null

    if [ ! -f /tmp/cmsd-already-there ]
    then
        echo "100068/2-5 dgram rpc/udp wait root $CMSD rpc.cmsd" >>$FILE
    else
        rm /tmp/cmsd-already-there
    fi
}


# Following owCM() procedure updates the Calendar Manager daemon inetd.conf
# line so it points to /usr/openwin/bin/cmsd.

owCM()
{
FILE=/etc/inetd.conf
TMPFILE=/tmp/inetd.conf
CMSD=/usr/openwin/bin/rpc.cmsd

#
# remove any non-ow rpc.cmsd line
#

    $NAWK -v cmsd=$CMSD \
        '{if ( ($1 == "100068/2-4" || $1 == "100068/2-5") && $6 != cmsd )
                ;
          else
                print $0
         }' $FILE >$TMPFILE

    cp $TMPFILE $FILE
    rm $TMPFILE

#
#  add openwin rpc.cmsd line if not already there 
#

    $NAWK -v cmsd=$CMSD \
        '{if ( ($1 == "100068/2-4" || $1 == "100068/2-5") && $6 == cmsd )
                print $0 > "/tmp/cmsd-already-there"
         }' $FILE >/dev/null

#
# if it is not there, add it
#  (note openwin cmsd is one rev back, its at level 2-4)
#

    if [ ! -f /tmp/cmsd-already-there ]
    then
        echo "100068/2-4 dgram rpc/udp wait root $CMSD rpc.cmsd" >>$FILE
    else
        rm /tmp/cmsd-already-there
    fi
}



case "$mode" in
'-d')
	/usr/bin/rm -f $LEVEL_2_START

	if [ -f $LEVEL_2_START ]; then
	   Message 12 "disable failed"
	   Message 13 "could not remove $LEVEL_2_START" $LEVEL_2_START
	   exit 1
	fi

	Message 14 "done"
	Message 15 "desktop auto-start disabled."
	;;

'-e')
	if [ -f $RC_MASTER ]; then
	    /usr/bin/rm -f $RC_COPY
	fi

	if [ ! -x $RC_COPY ]; then
	   if [ ! -f $RC_MASTER ]; then
	      Message 16 "enable failed"
	      Message 17 "could not find $RC_MASTER" $RC_MASTER
	      exit 1
	   fi

	   /usr/bin/cp $RC_MASTER $RC_COPY	
	   /usr/bin/chmod 0555 $RC_COPY
	
	   if [ ! -x $RC_COPY ]; then
	      Message 16 "enable failed"
	      Message 18 "could not create $RC_COPY" $RC_COPY
	      exit 1
	   fi
	fi

	/usr/bin/rm -f $LEVEL_0_KILL
	/usr/bin/ln $RC_COPY $LEVEL_0_KILL 

	/usr/bin/rm -f $LEVEL_1_KILL
	/usr/bin/ln $RC_COPY $LEVEL_1_KILL 

	/usr/bin/rm -f $LEVEL_2_START
	/usr/bin/ln $RC_COPY $LEVEL_2_START

	if [ ! -f $LEVEL_2_START ]; then
	   Message 16 "enable failed"
	   Message 19 "could not create $LEVEL_2_START" $LEVEL_2_START
	   exit 1
	fi

	Message 14 "done"
	Message 20 "desktop auto-start enabled."
	;;

'-inetd')
	nawk_check
	inetdSP
	inetdCM
	inetdTT
	Message 14 "done"
	Message 27 "Next system boot, following will be run from /usr/dt/bin"
	Message 28 "    rpc.ttdbserverd         (ToolTalk)" 
	Message 29 "    rpc.cmsd                (Calendar Manager)"
	Message 30 "    dtspcd                  (Subprocess Control)"
	;;

'-inetd.ow')
    nawk_check
    owCM
    owTT
    Message 14 "done"
    Message 31 "Next system boot, following will be run from /usr/openwin/bin"
    Message 32 "    rpc.ttdbserverd         (ToolTalk)" 
    Message 33 "    rpc.cmsd                (Calendar Manager)"
    ;;

'-kill')
	rc_master_check

	$RC_MASTER stop

	Message 14 "done"
	Message 21 "dtlogin kill complete."
	;;

'-reset')
	rc_master_check

	$RC_MASTER reset

	Message 14 "done"
	Message 22 "dtlogin config resources reloaded."
	;;

'-p')
	rc_master_check

	$RC_MASTER update_printers 

	Message 14 "done"
	Message 23 "printer action update is complete"
	;;

*) 	
	echo " "
	Message 24 "Usage:"
	usage_error
	exit 1
	;;

esac

exit 0
