#! /bin/ksh
# @(#)92	1.4.1.2  src/cde/admin/IntegTools/post_install/ibm/configTT.src, desktop, cde72V, v2020_34B7 8/20/20 20:04:19
#   (c) Copyright 1993, 1994 International Business Machines Corp.

INETD=/etc/inetd.conf
USRBIN=/usr/bin

CheckForttinInetd()
{
    $USRBIN/grep "ttdbserverd" $INETD > /dev/null 2>&1 && $USRBIN/sed "/ttdbserverd/d" $INETD > /tmp/configTT.$$ && $USRBIN/mv /tmp/configTT.$$ $INETD
    rc=`$USRBIN/grep "ttdbserver" $INETD | $USRBIN/cut -c1 | $USRBIN/grep -xv "#" \
           1>/dev/null 2>/dev/null;echo $?`
    return $rc

}


#
# Checks if portmap daemon is already running; if not
# starts the portmap daemon.
#
CheckPortmap()
{
    $USRBIN/ps -ef | $USRBIN/fgrep portmap | $USRBIN/grep -v grep 2>/dev/null 1>/dev/null
    if [ $? -ne 0 ]
    then
       $USRBIN/startsrc -s portmap
    fi
}
StopDaemon()
{
    $USRBIN/ps -ef | $USRBIN/fgrep $1 | $USRBIN/grep -v grep >/tmp/tmppsout
    if [ -s /tmp/tmppsout ]
    then
        $USRBIN/awk '{print "kill " $2}' /tmp/tmppsout | /bin/ksh 1>/dev/null
        $USRBIN/sleep 2
        $USRBIN/ps -ef | $USRBIN/fgrep $1 | $USRBIN/grep -v grep >/tmp/tmppsout
        if [ -s /tmp/tmppsout ]
        then
            $USRBIN/awk '{print "kill -TERM " $2}' /tmp/tmppsout | /bin/ksh 1>/dev/null
            $USRBIN/sleep 2
            $USRBIN/ps -ef | $USRBIN/fgrep $1 | $USRBIN/grep -v grep >/tmp/tmppsout
            if [ -s /tmp/tmppsout ]
            then
                $USRBIN/awk '{print "kill -9 " $2}' /tmp/tmppsout | /bin/ksh 1>/dev/null
                $USRBIN/sleep 2
            fi
        fi
    fi
    $USRBIN/rm -f /tmp/tmppsout
}

FixEtcRpc()
{
#
# now check to see if the proper entry is in /etc/rpc
#

    RPCFILE=/etc/rpc
    TMPFILE=/tmp/etc-rpc

    if [ ! -f $RPCFILE ]
    then
	# if the file doesn't exist (highly unlikely), make one
	# with the proper entry
	#

	echo "ttdbserver     100083  tooltalk" >$RPCFILE

    else
	#
	# check to see if the entry is already there
	#
    	$USRBIN/grep "ttdbserverd" $RPCFILE > /dev/null 2>&1 && $USRBIN/sed "/ttdbserverd/d" $RPCFILE > /tmp/configTTrpc.$$ && $USRBIN/mv /tmp/configTTrpc.$$ $RPCFILE

	$USRBIN/awk '{if ($1 == "ttdbserver" && $2 == "100083")
		print $0 > "/tmp/etc-rpc-already-there"
	     }' $RPCFILE >/dev/null

	if [ ! -f /tmp/etc-rpc-already-there ]
	then
	#
	# if it isn't, check to see if either term already
	# exists. If either one does they need to be commented
	# out.

	    $USRBIN/awk '{if (($1 == "ttdbserver" && $2 != "100083") ||
		      ($2 == "100083" && $1 != "ttdbserver"))
			print "#cde " $0;
		  else
			print $0
		 }' $RPCFILE >$TMPFILE

	    echo "ttdbserver     100083  tooltalk" >>$TMPFILE
	    $USRBIN/mv $TMPFILE $RPCFILE

	else
	    $USRBIN/rm /tmp/etc-rpc-already-there
	fi
    fi
}

UnfixEtcRpc()
{
FILE="/etc/rpc"
TMPFILE="/tmp/etc-rpc"

    $USRBIN/awk '{if ($1 == "ttdbserver" && $2 == "100083")
		;
    	  else if ($1 == "ttdbserverd" && $2 == "100083")
		;
	  else
		print $0
	 }' $FILE >$TMPFILE

    $USRBIN/mv $TMPFILE $FILE

    $USRBIN/awk '{if ($1 == "#cde") {
		$1 = $2
		$2 = ""
	     }
	  print $0
	 }' $FILE >$TMPFILE

    $USRBIN/mv $TMPFILE $FILE
}

RemovettFromInetserv()
{
TTDB=/usr/dt/bin/rpc.ttdbserver

    /usr/sbin/chsubserver -d -v ttdbserver -t sunrpc_tcp -p tcp -w wait -u root \
		-g $TTDB -r inetd rpc.ttdbserver 100083 1  \
						   1>/dev/null 2>/dev/null
    $USRBIN/grep -v "#ttdbserver" $INETD >/tmp/configTT.$$ && $USRBIN/cp /tmp/configTT.$$ $INETD


    if [ "$?" -ne "0" ]
    then
        exit 1
    fi

}

AddttToInetserv()
{
INETD=/etc/inetd.conf
TTDB=/usr/dt/bin/rpc.ttdbserver

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

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



    /usr/sbin/chsubserver -a -v ttdbserver -t sunrpc_tcp -p tcp -w wait -u root \
                -g $TTDB -r inetd rpc.ttdbserver 100083 1   \
					    1>/dev/null  2>/dev/null

    if [ "$?" -ne "0" ]
    then
        exit 1
    fi

}

HandleOption()
{
  while [ $# -ne 0 ]; do
    case $1 in
        -e) DO_CONFIGURATION="yes"
            shift;
            ;;
	-d) DO_CONFIGURATION="no"
	    shift;
	    ;;
    esac
  done
}

###################################################################
#
# Main Body
#
###################################################################
DO_CONFIGURATION=""
PRODUCT=CDE
FILESET=CDE-TT
retval=0


  HandleOption $*

  if [ "$DO_CONFIGURATION" = "yes" ]
  then

    FixEtcRpc


    CheckForttinInetd
    rc=$?
    if [ "$rc" = "0" ]
    then
	RemovettFromInetserv
    fi

    AddttToInetserv

  elif [ "$DO_CONFIGURATION" = "no" ]
  then

    UnfixEtcRpc


    CheckForttinInetd
    rc=$?
    if [ "$rc" = "0" ]
    then
        RemovettFromInetserv
    else
        echo "\nToolTalk is already disabled..\n"
        exit 0
    fi

  fi


