#!/usr/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/svcmd/setuname/setuname.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2001,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 

# @(#)70        1.3  src/bos/usr/bin/svcmd/setuname/setuname.sh, uw7cmds, bos720 3/22/07 10:15:33

# FILE DESCRIPTION: High-level command to set node name.
#
#
# RETURN VALUE DESCRIPTION:
#       0 successful
#       1 command syntax incorrect or internal error
#
# EXTERNAL PROCEDURES CALLED: getopt, id, hostname, uname, chdev. 
#
#
# GLOBAL VARIABLES: NFLG, TFLG, NARG, DSP, RCODE
#

NFLG=0
TFLG=0
NARG=""
RCODE=1

###################### cleanup() ##########################
#
# NAME: cleanup()
#
# DESCRIPTION: Called from trap command to clean up
#               environment and exit.
#
# OUTPUT: Exit with exit code
#

cleanup() {
if [ $RCODE ]
then
	if [ $TFLG -eq 1 ]
	then
		/usr/bin/hostname $OLDHNAME > /dev/null
		exit 1
	else
		/usr/sbin/chdev -l inet0 -a hostname=$OLDHNAME  > /dev/null		
		exit 1
	fi
fi
}

######## Execution starts here ######################
#Setup trap for signals
trap 'cleanup' 1 2 15 

#Check if dspmsg exists
if [ -e /usr/bin/dspmsg ]
then
	DSP=0
else
	DSP=1
fi

#Check for occurance of "--" in the argument list
for i in $*
do
	if [ $i = "--" ]
	then
		if [ $DSP ]
		then
			/usr/bin/dspmsg -s 1 sysvsetuname.cat 1 \
					"Usage: setuname [-t] [-n node]\n" 1>&2
		else
			echo "Usage: setuname [-t] [-n node]" 1>&2		
		fi
	exit 1
	fi
done

#Set up the argument list
set -- `/usr/bin/getopt n:t $*`
#Verify the arguments 
if [ $? != 0 ]
then
       	if [ $DSP ]
	then
		/usr/bin/dspmsg -s 1 sysvsetuname.cat 1 "Usage: setuname [-t] [-n node]\n" 1>&2
	else
		echo "Usage: setuname [-t] [-n node]" 1>&2
	fi
	exit 1
fi

#Set the flags
while [ $1 != -- ]
do
        case $1 in
        -n)     # set up the -n flag
                NFLG=1
                NARG=$2
                shift;;
        -t)     # set up the -t flag
                TFLG=1;;
        esac
        shift   # next flag
done
shift   

#Verify whether the -n flag is specified
if [ $NFLG -eq 0 ]
then
	if [ $DSP ]
	then
		/usr/bin/dspmsg -s 1 sysvsetuname.cat 2 "The -n flag must be specified\n" 1>&2
	else
		echo "The -n flag must be specified" 1>&2
	fi
	exit 1
fi

#Check for redundant arguments
if [[ $1 != "" ]]
then
	if [ $DSP ]
        then
		/usr/bin/dspmsg -s 1 sysvsetuname.cat 1 "Usage: setuname [-t] [-n node]\n" 1>&2
        else
                echo "Usage: setuname [-t] [-n node]" 1>&2
        fi
        exit 1
fi  

#Check for permissions
if [ `/usr/bin/id -un` != "root" ]
then
	if [ $DSP ]
	then
		/usr/bin/dspmsg -s 1 sysvsetuname.cat 3 "Only root can change node name\n" 1>&2
	else
		echo "Only root can change node name" 1>&2
	fi
	exit 1
fi

#Extract the domain from the hostname
OLDHNAME=`/usr/bin/hostname`
HOST=`/usr/bin/hostname -s`
DOMAIN=`/usr/bin/hostname | /usr/bin/sed 's/'$HOST'//'`
HNAME=$NARG$DOMAIN

#Now change node name temporarily or permanently 
if [ $TFLG -eq 1 ]
then
	/usr/bin/hostname $HNAME > /dev/null
	if [ $? -ne 0 ]
	then
		exit 1
	fi
	RCODE=0
	/usr/bin/uname -S $NARG > /dev/null
	if [ $? -ne 0 ]
        then
		/usr/bin/hostname $OLDHNAME > /dev/null
                exit 1
        fi
else
	/usr/sbin/chdev -l inet0 -a hostname=$HNAME  > /dev/null
	if [ $? -ne 0 ] 
        then    
                exit 1
        fi
	RCODE=0
	/usr/bin/uname -S $NARG > /dev/null
	if [ $? -ne 0 ]
        then
		/usr/sbin/chdev -l inet0 -a hostname=$OLDHNAME  > /dev/null
                exit 1
        fi
fi