#!/bin/bsh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# onc720 src/oncplus/usr/sbin/sysmgt/mkclient.sh 1.11 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1989,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
# @(#)37      1.11  src/oncplus/usr/sbin/sysmgt/mkclient.sh, nis, onc720 11/12/07 06:41:25
#
#   COMPONENT_NAME: onccmds
#
#   FUNCTIONS: place
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1989,1997
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

set -f			# this is so that the case statement will work

PG=`/usr/bin/basename $0`
TMPDIR=${TMPDIR:-$HOME/tmp}
[ ! -d $TMPDIR ] && TMPDIR=/tmp
TMPDIR=$TMPDIR/$PG.$$

/usr/bin/mkdir $TMPDIR || {
	echo "$PG: Could not create temporary files"
	exit 1
}

COMMAND_NAME=$PG
SED_CMD="/bin/sed"
RC_NFS="/etc/rc.nfs"
TMP_RC_NFS="$TMPDIR/tmp.rc.nfs"
RC_TCPIP="/etc/rc.tcpip"
TMP_RC_TCPIP="$TMPDIR/tmp.rc.tcpip"
MKITAB="/usr/sbin/mkitab"
YPSERVER=""

USAGE="
usage:  ${COMMAND_NAME} [-I|-B|-N] [-S server]\n"

if [ -z "`/usr/bin/domainname`" ] ;then
	/usr/bin/dspmsg cmdnfs.cat -s 37 43 "^G%s: The local host's domain name has not been set.  Please set it.\n" ${COMMAND_NAME}
	trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
	exit 1
fi 

set -- `/usr/bin/getopt S:IBN $*  2>/dev/null` 
if [ $? != 0 ] ; then         # Test for syntax error
    /usr/bin/dspmsg cmdnfs.cat -s 37 44 "${USAGE}" $COMMAND_NAME
    trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
    exit 1
fi

while [ $1 != -- ]
do
    case "$1" in
	"-I")			#WHEN should this  take place (IPL)
	    if [ -n "${WHEN}" ] ; then
		/usr/bin/dspmsg cmdnfs.cat -s 37 44 "$USAGE" $COMMAND_NAME ;
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 1 ;
	    fi
	    WHEN="I"
	    shift
	    ;;
	"-N")			#WHEN should this  take place (NOW)
	    if [ -n "${WHEN}" ] ; then
		/usr/bin/dspmsg cmdnfs.cat -s 37 44 "$USAGE" $COMMAND_NAME ;
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 1 ;
	    fi
	    WHEN="N"
	    shift
	    ;;
	"-B")			#WHEN should this  take place (BOTH)
	    if [ -n "${WHEN}" ] ; then
		/usr/bin/dspmsg cmdnfs.cat -s 37 44 "$USAGE" $COMMAND_NAME ;
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 1 ;
	    fi
	    WHEN="B"
	    shift
	    ;;
	"-S")
	    shift
	    if [ -n "$1" ]
	    then
		YPSERVER=$1
	        shift
	    else
		/usr/bin/dspmsg cmdnfs.cat -s 37 44 "${USAGE}" $COMMAND_NAME
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 1
	    fi
	    ;;

	*)
	    /usr/bin/dspmsg cmdnfs.cat -s 37 44 "$USAGE" $COMMAND_NAME 
	    trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
	    exit 1
	    ;;
    esac
done

# check to make sure that there are not any extra parameters
shift		# get rid of the -- parameter
if [ -n "$1" ] ; then	# something extra here.
    /usr/bin/dspmsg cmdnfs.cat -s 37 44 "${USAGE}" $COMMAND_NAME
    trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
    exit 1
fi

if [ "$YPSERVER" != "" ]
then
    /usr/bin/ypservers -r -s -d `/usr/bin/domainname` $YPSERVER
    if [ $? != 0 ]
    then
	/usr/bin/dspmsg cmdnfs.cat -s 37 45 "NIS server %s is not responding\n" $YPSERVER
	trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
	exit 1
    fi
    SERVERIP=`LANG=C /usr/bin/host $YPSERVER`
    if [ $? != 0 ]
    then
	/usr/bin/dspmsg cmdnfs.cat -s 37 55 "Host %s unknown\n" $YPSERVER
	trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
	exit 1
    fi
    YPSERVER=`IFS=" ,"  set -- $SERVERIP; echo $3`
else
    /usr/bin/ypservers -r -s -d `/usr/bin/domainname`
    if [ $? != 0 ]
    then
	/usr/bin/dspmsg cmdnfs.cat -s 37 47 "%s: no NIS servers are responding.  If no NIS server exists on this\nsubnet, then a server will need to be specified\n" $COMMAND_NAME
	trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
	exit 1
    fi
fi


# Set the defaults for this command
WHEN=${WHEN:-"B"}


if [ "${WHEN}" = "I" -o "${WHEN}" = "B" ] ; then
        # Add  rc.nfs in /etc/inittab
	$MKITAB -i rctcpip "rcnfs:2:wait:/etc/rc.nfs > /dev/console 2>&1 # Start NFS Daemons"
        # Uncomment the starting of portmap in /etc/rc.tcpip
        ${SED_CMD} "\?.*/usr/sbin/portmap?s/^#//" ${RC_TCPIP} > ${TMP_RC_TCPIP} 2>/dev/null
        if [ $? != 0 ] ; then
                /usr/bin/dspmsg cmdnfs.cat -s 37 14 "${COMMAND_NAME}: error in updating ${RC_TCPIP}\n" ${COMMAND_NAME} ${RC_TCPIP}
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
                exit 1
        fi
        #copy the file back to /etc
        /bin/cp ${TMP_RC_TCPIP} ${RC_TCPIP} > /dev/null 2>&1
        if [ $? != 0 ] ; then
                /usr/bin/dspmsg cmdnfs.cat -s 37 14 "${COMMAND_NAME}: error in updating ${RC_TCPIP}\n" ${COMMAND_NAME} ${RC_TCPIP}
                trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
                exit 1
        fi

        # Remove the temporary file
        trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15

	# Uncomment the line that says yp is being started
	# Uncomment the starting of the ypbind daemon
    	${SED_CMD} "/starting NIS services:/s/#//
		\?-x.*/usr/lib/netsvc/yp/ypbind?,\?fi?s/#//" ${RC_NFS} > ${TMP_RC_NFS} 2>/dev/null
	if [ $? != 0 ] ; then
		/usr/bin/dspmsg cmdnfs.cat -s 37 14 "${COMMAND_NAME}: error in updating ${RC_NFS}\n" ${COMMAND_NAME} ${RC_NFS}
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 1
	fi
	
	# stop this daemon on the off chance that user already has it started.
	/usr/bin/stopsrc -s ypbind
	if [ "$YPSERVER" != "" ]
	then
	    /usr/bin/chssys -s ypbind -a -ypsetme
	    YPSETME=" -ypsetme"
	else
	    /usr/bin/chssys -s ypbind -a ""
	    YPSETME=""
	fi

	#make sure ypbind has the -ypset flag if a server was specified
	$SED_CMD "/.*start ypbind.*/s?/usr/lib/netsvc/yp/ypbind.*\$?/usr/lib/netsvc/yp/ypbind${YPSETME}?" ${TMP_RC_NFS} > ${TMP_RC_NFS}.x

	/usr/bin/cp ${TMP_RC_NFS}.x ${TMP_RC_NFS}
	if [ "$YPSERVER" != "" ]
	then
	    /usr/bin/grep -q /usr/sbin/ypset ${TMP_RC_NFS}.x
	    if [ $? != 0 ]
	    then
		$SED_CMD '/.*start ypbind.*/a\
\	/usr/sbin/ypset $YPSERVER
' ${TMP_RC_NFS} > ${TMP_RC_NFS}.x
	    fi
	    $SED_CMD "/.*usr.sbin.ypset.*/s?/usr/sbin/ypset.*?/usr/sbin/ypset $YPSERVER?" ${TMP_RC_NFS}.x > ${TMP_RC_NFS}
	else
	    /usr/bin/grep -v /usr/sbin/ypset ${TMP_RC_NFS}.x > ${TMP_RC_NFS}
	fi
	/usr/bin/rm ${TMP_RC_NFS}.x


	# copy the file back to /etc
	/bin/cp ${TMP_RC_NFS} ${RC_NFS} > /dev/null 2>&1
	if [ $? != 0 ] ; then
		/usr/bin/dspmsg cmdnfs.cat -s 37 14 "${COMMAND_NAME}: error in updating ${RC_NFS}\n" ${COMMAND_NAME} ${RC_NFS}
	fi

	# Add the plus entries to the /etc/passwd and /etc/group file
	# We check both files to see if they have plus entries.  If the 
	# file has a plus entry we do not add the plus entry to that file. 
	# Adding extra plus entries wastes system resources when looking up
	# passwords and groups.  It also can cause a user to be in too
	# many groups causing the "user in too many groups" message to be 
	# displayed when logging in.
	# The format of the next few lines should not be changed.

	/bin/grep "+::0:0:::" /etc/passwd > /dev/null 2>&1
	if [ $? != 0 ] ; then
	/usr/bin/cat >> /etc/passwd << END
+::0:0:::
END
	fi

	/bin/grep "+:" /etc/group > /dev/null 2>&1
	if [ $? != 0 ] ; then
	/usr/bin/cat >> /etc/group << END
+:
END
	
	fi
fi

if [ "${WHEN}" = "N" -o "${WHEN}" = "B" ] ; then

# The format of the following lines that edit /etc/passwd and /etc/group
# should NOT be altered

	/bin/grep "+::0:0:::" /etc/passwd > /dev/null 2>&1
	if [ $? != 0 ] ; then
	/usr/bin/cat >> /etc/passwd << END
+::0:0:::
END
	fi

	/bin/grep "+:" /etc/group > /dev/null 2>&1
	if [ $? != 0 ] ; then
	/usr/bin/cat >> /etc/group << END
+:
END
	
	fi
    # Start the portmapper and refresh inetd
    /usr/bin/startsrc -s portmap
    /bin/refresh -s inetd

    if [ "$COMMAND_NAME" = "chclient" ]
    then
	/usr/bin/stopsrc -s ypbind
    fi

    if [ "$YPSERVER" != "" ]
    then
	/usr/bin/startsrc -s ypbind -a -ypsetme
	/usr/sbin/ypset $YPSERVER
    else
	/usr/bin/startsrc -s ypbind -a ""
    fi
fi
    
# remove temporary dir
trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15