#! /bin/ksh
# @(#)91        1.10.1.2  src/cde/admin/IntegTools/post_install/ibm/configRun.src, desktop, cde72V, v2020_34B7 8/20/20 20:04:30
#   (c) Copyright 1993, 1994 International Business Machines Corp.


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

#
# The "sm" object definitions for 410 are in /usr/lib/objrepos so
# ODMDIR need to get set to the correct path name .
#

ODMDIR=/usr/lib/objrepos

CreateAppConfigDirectory()
{
#
# Create the APPCONFIG directory inside DT_CONFIG_TOP and create
# all of its subdirectories
#

    $USRBIN/cd $DT_CONFIG_TOP
    if [ ! -d $APPCONFIG ]
    then
	$USRBIN/mkdir $APPCONFIG
    fi

    $USRBIN/cd $APPCONFIG

    for i in $APPCONFIG_DIRS
    do
	if [ ! -d $i ]
	then
	    $USRBIN/mkdir $i
	fi
	$USRBIN/cd $i
	#
	# for each locale
	#
	for j in $DT_TOP/$APPCONFIG/$i/*
	do
	    if [ ! -d `$USRBIN/basename $j` ]
	    then
		$USRBIN/mkdir `$USRBIN/basename $j`
	    fi
	done
	$USRBIN/cd ..
    done

}

CheckFor24cmsdinInetd()
{

    rc=`$USRBIN/grep "cmsd" $INETD | $USRBIN/grep "2-4" | $USRBIN/cut -c1 | $USRBIN/grep -xv "#" \
		1>/dev/null 2>/dev/null; echo $?`
    return $rc
}

CheckForcmsdinInetd()
{

    rc=`$USRBIN/grep "cmsd" $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 "cmsd     100068  dtcalendar" >$RPCFILE

    else
	#
	# check to see if the entry is already there
	#

	$USRBIN/awk '{if ($1 == "cmsd" && $2 == "100068")
		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
	#

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

	#
	# if either one does they need to be commented out
	#

	    if [ ! -f /tmp/etc-rpc-already-there ]
	    then
	        echo "cmsd     100068  dtcalendar" >>$RPCFILE
	    else
		$USRBIN/awk '{if ($1 == "cmsd" || $2 == "100068")
			print "#cde " $0; 
		      else 
			print $0
		     }' $RPCFILE >$TMPFILE

	        echo "cmsd     100068  dtcalendar" >>/tmp/etc-rpc
		$USRBIN/mv $TMPFILE $RPCFILE
		$USRBIN/rm /tmp/etc-rpc-already-there
	    fi
	else
	    $USRBIN/rm /tmp/etc-rpc-already-there
	fi
    fi
}

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

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

    $USRBIN/mv $TMPFILE $FILE

    $USRBIN/awk '{if ($1 == "#cde" && ($2 == "cmsd" || $3 == "100068"))
		;
	  else
		print $0
	 }' $FILE >$TMPFILE

    $USRBIN/mv $TMPFILE $FILE
}

Remove24cmsdFromInetd()
{

CMSD=/usr/dt/bin/rpc.cmsd


   /usr/sbin/chsubserver -d -v cmsd -t sunrpc_udp -p udp -w wait -u root \
		-g $CMSD -r inetd cmsd 100068 2-4          1>/dev/null  2>/dev/null
   $USRBIN/grep -v "#cmsd" $INETD >/tmp/configRun.$$ && $USRBIN/mv /tmp/configRun.$$ $INETD
 

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

}

RemovecmsdFromInetserv()
{

CMSD=/usr/dt/bin/rpc.cmsd


   /usr/sbin/chsubserver -d -v cmsd -t sunrpc_udp -p udp -w wait -u root \
		-g $CMSD -r inetd cmsd 100068 2-5          1>/dev/null  2>/dev/null
   $USRBIN/grep -v "#cmsd" $INETD >/tmp/configRun.$$ && $USRBIN/mv /tmp/configRun.$$ $INETD
 

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

}

AddcmsdToInetserv()
{
CMSD=/usr/dt/bin/rpc.cmsd

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

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



   /usr/sbin/chsubserver -a -v cmsd -t sunrpc_udp -p udp -w wait -u root \
                -g $CMSD -r inetd cmsd 100068 2-5        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
}

LinkOtherIcons()
{
    while read SRC
    do
	$USRBIN/ln -sf $DT_TOP/$APPCONFIG/icons/C/$SRC $DT_TOP/$APPCONFIG/icons/C/F$SRC 2>/dev/null
    done <<-EOF
fphelp.t.pm
help.t.pm
paint.l.pm
paint.m.pm
paint.t.pm
penpad.t.pm
term.t.pm
	EOF
}

ConfigurePrintersDir()
{
    $USRBIN/cd $DT_TOP/$APPCONFIG/appmanager
#
# for each locale
#
    for i in *
    do
        if [ -d $i ]
	then
	    $USRBIN/cd $i
	#
	# for each directory in the locale, check for a 
	# localized version of the Printers directory
	# and for the file lp_Print within that directory
	#
	    for j in *
	    do
	      if [ -f $j/$DEFAULT_PRINTER ]
	      then

		PRINTERS=$j

		# make the equivalent directory in the configuration
		# directory

	        if [ ! -d $DT_CONFIG_TOP/$APPCONFIG/appmanager/$i ]
	        then
		    $USRBIN/mkdir $DT_CONFIG_TOP/$APPCONFIG/appmanager/$i
	        fi
	        $USRBIN/cd $DT_CONFIG_TOP/$APPCONFIG/appmanager/$i
	        if [ ! -d $DT_CONFIG_TOP/$APPCONFIG/appmanager/$i/$PRINTERS ]
	        then
		    $USRBIN/mkdir $DT_CONFIG_TOP/$APPCONFIG/appmanager/$i/$PRINTERS
                fi
	        $USRBIN/cd $DT_CONFIG_TOP/$APPCONFIG/appmanager/$i/$PRINTERS

		# now copy the file over from the installation dirctory
		# if it is not already present.

	        if [ ! -f $DEFAULT_PRINTER ]
	        then
		    $USRBIN/cp $DT_TOP/$APPCONFIG/appmanager/$i/$PRINTERS/$DEFAULT_PRINTER .
	        fi
	        $USRBIN/cd $DT_TOP/$APPCONFIG/appmanager/$i
	      fi
	    done
	    $USRBIN/cd $DT_TOP/$APPCONFIG/appmanager
	fi
    done
}

	   
#############################################################################
#
#				Main Body
#
##############################################################################

PRODUCT=CDE
FILESET=CDE-RUN

retval=0

DT_TOP=/usr/dt
DT_CONFIG_TOP=/etc/dt
DT_TEMP_TOP=/var/dt
DO_CONFIGURATION=""
ROOT=/

retval=0

APPCONFIG=appconfig
APPCONFIG_DIRS="appmanager help icons types"
PRINTERS=""
DEFAULT_PRINTER="DtPrint"


  HandleOption $*

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


#
# create the /var/dt directory
#
    if [ ! -d $DT_TEMP_TOP/$APPCONFIG/appmanager ]
    then
	$USRBIN/mkdir -p $DT_TEMP_TOP/$APPCONFIG/appmanager
    fi
$USRBIN/cd $DT_TEMP_TOP
    $USRBIN/chmod -R 755 * 2> /dev/null
    $USRBIN/chown -R bin * 2> /dev/null
    $USRBIN/chgrp -R bin * 2> /dev/null
#  Change ownership of user specific directories back to the correct owner
    for DIR_TO_BE_FIXED in `find /var/dt/appconfig/appmanager -type d  -print`
    do
        if [ "$DIR_TO_BE_FIXED" != "/var/dt/appconfig/appmanager" ]
        then
            DIRNAME=${DIR_TO_BE_FIXED##/var/dt/appconfig/appmanager/}
            USERNAME=${DIRNAME%%-*}
            $USRBIN/chown $USERNAME $DIR_TO_BE_FIXED 2> /dev/null
        fi
    done

# Set the sticky bit for /var/dt/tmp so that only the 
# owner can delete a file.  This will protect the 
# Xauthority files that may be written to this directory
# if unable to write to $HOME/.Xauthority.
    $USRBIN/chmod 1777 /var/dt/tmp

#
# create the /etc/dt and its config directory
#
    if [ ! -d $DT_CONFIG_TOP ]
    then
	$USRBIN/mkdir -p $DT_CONFIG_TOP
    fi
    if [ ! -d $DT_CONFIG_TOP/config ]
    then
	$USRBIN/mkdir -p $DT_CONFIG_TOP/config
    fi

    CreateAppConfigDirectory

    ConfigurePrintersDir

    $USRBIN/env LANG=C /usr/dt/bin/dtprintinfo -populate

#
# Configure Xsession.d
#

    $USRBIN/cd $DT_CONFIG_TOP/config
    if [ ! -d Xsession.d ]
    then
	$USRBIN/mkdir Xsession.d
    fi

    $USRBIN/cd $DT_CONFIG_TOP
    $USRBIN/chmod -R 755 * 2> /dev/null

    LinkOtherIcons


    FixEtcRpc

#After adding the Calendar Manager to the rpc service list the 
#portmapper needs to be restarted.


    CheckFor24cmsdinInetd
    rc=$?
    if [ "$rc" = "0" ]
    then
	Remove24cmsdFromInetd
    	$USRBIN/refresh -s inetd

    fi

    CheckForcmsdinInetd
    rc=$?
    if [ "$rc" = "0" ]
    then
	RemovecmsdFromInetserv
    fi
    AddcmsdToInetserv





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


    UnfixEtcRpc

#After deleting the Calendar Manager to the rpc service list the 
#portmapper needs to be restarted.


    CheckForcmsdinInetd
    rc=$?
    if [ "$rc" = "0" ]
    then
        RemovecmsdFromInetserv
    else
        echo "\ncmsd is already disabled..\n"
        exit 0
    fi



  fi

