#!/bin/sh
#
# Copyright (c) AT&T 1999. All rights reserved.
# @(#)setservername.sh	1.16	8/3/99
# 

textdomaindir=/opt/lanman/lib/locale

textdomain='setservername'
NBBIND=/opt/lanman/lib/nbbind
NBUTIL=/opt/SUNWlznb/sbin/nbutil
#
# Function to run gettext with TEXTDOMAINDIR set.
# TEXTDOMAINDIR is not set in the exported environment so subprocesses 
# do not inadvertently use it.
#

pgettext () {
	gttext=gettext #Trick to fool shgettext
	TEXTDOMAINDIR=${textdomaindir} ${gttext} ${textdomain} "$1"
}

INSTANCE_NOT_FOUND="\nThe instance is unavailable."
INSTANCE_NOT_SPECIFIED="\nThe instance must be specified."

TMP_FILE=/tmp/regload.$$

#
# These variables may be used immediately
#
FATAL_EXIT_CODE=1

# Used in messages
Progname=`basename $0`

#----------------------------------------------------------------------------

# 
# usage - display more info for bdc than pdc
#
usage ()
{
	if [ "$Role" = "primary" -o "$Role" = "" ]
	then
eval echo "\"`pgettext 'Usage: ${Progname} [-I instance] [ -n newname -a admin_account -p password ][ -s ]\n'`\""
pgettext " where:\n"\
"-I <instance number | instance name> - Specify the PCNL instance\n"\
"newname       - the new name of this server\n"\
"admin_account - the name of the administrative account\n"\
"password      - the password for the administrative account\n"\
"-s	       - the server is not started when this argument is set\n"

	else
eval echo "\"`pgettext 'Usage: ${Progname} [ -n newname [ -a admin_account -p password ] -d pdc_name ][ -s ] '`\""
pgettext "where: \n"\
"	newname       - the new name of this server\n"\
"	admin_account - the name of the administrative account on the primary\n"\
"	password      - the password for the administrative account\n"\
"	pdc_name      - the name of the primary domain controller\n"\
"	-s	      - the server is not started when this argument is set\n"

	fi
	exit 1
}

#----------------------------------------------------------------------------

convert_instance() {

if [ $1 -lt 1 -o $1 -gt 99 ]
then
	ilist=`/opt/lanman/lib/instlist |awk -e ' { print $1 }'`	
	for i in $ilist
        do
		name=`/opt/lanman/sbin/srvconfig -I $i -g "server,listenname"`
		if [ "$name" = "$1" ]
		then
			PCNL_INSTANCE=$i
			return
		fi
        done
	echo $INSTANCE_NOT_FOUND
	exit 1
fi
}		

read_miconfig() {

if [ $# -gt 0 ]
then
	if [ $1 = "-I" ]
	then
		PCNL_INSTANCE=$2
		convert_instance $PCNL_INSTANCE
		IDIR=`/opt/lanman/lib/instlist ${PCNL_INSTANCE} |awk -e ' { print $2 }'`
		if [ ${IDIR:-""} = "" ]
		then
			echo $INSTANCE_NOT_FOUND
			exit 1
		fi
	else
		echo $INSTANCE_NOT_SPECIFIED
		usage
		exit 1
	fi
elif [ ${PCNL_INSTANCE:-""} != "" ]
then
	convert_instance $PCNL_INSTANCE
	IDIR=`/opt/lanman/lib/instlist ${PCNL_INSTANCE} |awk -e ' { print $2 }'`
	if [ ${IDIR:-""} = "" ]
	then
		echo $INSTANCE_NOT_FOUND
		usage
		exit 1
	fi
else
	count=0
	ilist=`/opt/lanman/lib/instlist | while read inum path lhost
	do
		echo $inum
	done`
	for INS in ${ilist}
	do
		count=`expr $count + 1`
	done
	if [ $count -eq 1 ]
	then
		PCNL_INSTANCE=${ilist}
		IDIR=`/opt/lanman/lib/instlist ${PCNL_INSTANCE} |awk -e ' { print $2 }'`
	else
		echo $INSTANCE_NOT_SPECIFIED
		usage
		exit 1
	fi
fi

if [ "$IDIR" = "/" ]
then
	IDIR=""
fi

export PCNL_INSTANCE
export IDIR

if [ ! -d ${IDIR}/var/opt/lanman/${PCNL_INSTANCE} ]
then
	echo $INSTANCE_NOT_FOUND
	exit 1
fi

}

instance=""
while getopts 'n:p:d:a:sI:?' c
do
    case $c in
	I) 	instance=$OPTARG
		;;
	d)	;;
	a)	;;
	p)	;;
	s)	;;
	n)	;;
	\?)	;;
    esac		
done
OPTIND=1

if [ "$instance" != "" ]
then
	read_miconfig -I $instance
else
	read_miconfig
fi

export PCNL_INSTANCE
export IDIR

# initialze the common shell variables

LMPATHS="`/bin/dirname $0`/../lib/lmpaths"

if [ -f ${LMPATHS} ]
then
    . ${LMPATHS}
else
    . lmpaths
fi

#
# if not root then exit
#
ID="`id | cut -d\( -f1`"
if [ "${ID}" != "uid=0" ]
then
	eval echo "\"`pgettext 'You must be root to run ${Progname}.\n'`\""
	exit ${FATAL_EXIT_CODE}
fi


#----------------------------------------------------------------------------
# Routines
#----------------------------------------------------------------------------

#
# Ask the user a question, return true(YES) if yes, false(NO) if no
#
NO=255
YES=0
NO_DEFAULT=2
yorn ()
{
	def=$1
	shift
	while [ 1 ]
	do
		if [ $def = $NO ]
		then
			echo "$* [y/n]? n\b\c"
		elif [ $def = $YES ]
		then
			echo "$* [y/n]? y\b\c"
		else
			echo "$* [y/n]? \c"
		fi

		case `line` in
		y|Y|[yY][eE][sS])
			return $YES
			;;
		n|N|[nN][oO])
			return $NO
			;;
		'')
			if [ $def != $NO_DEFAULT ]
			then
				return $def
			fi
			pgettext "\tPlease respond with y(es) or n(o)\n"
			;;
		*)
			pgettext "\tPlease respond with y(es) or n(o)\n"
			;;
		esac
	done
}

#----------------------------------------------------------------------------

#
# If the password is ok, return 0. 
#
checkpw ()
{
	n=`echo "$1" | wc -c`
	if [ $n -gt 15  -o  $n -lt 0 ]
	then
		pgettext "Password must be shorter than 15 characters.\n"
		return 1
	else
		return 0
	fi
}

#----------------------------------------------------------------------------

#
# Get the password.
# Globals: modifies AdminPW
#
getpw()
{
	while [ 1 ]
	do
		echo "\n$1"
		stty -echo
		read pw
		checkpw $pw
		if [ $? -ne 0 ]
		then
			continue
		fi
		pgettext "\nRe-enter password: "
		read ANS2
		stty echo
		if [ "$pw" != "$ANS2" ]
		then
			pgettext "\nPassword mismatch!\n"
			continue
		fi
		echo
		break
	done
	AdminPW=$pw
}
#String Compare By ignoring case
strcmpi () {
	if [ `echo $1 | tr "[:lower:]" "[:upper:]"`  =  `echo $2 | tr "[:lower:]" "[:upper:]"` ]
	then
		return 0
	else
		return 1
	fi
}
#Validate for primary domain controller
check_domain_controller_name () 
{
	check_name $1
	if [ $? -eq 1 ]
	then
		return 1
	fi
	$NBUTIL -f $1 > $DEV_NULL_PATH 2>&1
	if [ $? -ne 0 ]
	then
		eval echo "\"`pgettext 'Unable to contact the PDC $1'`\""
		return 1
	fi
	return 0
}

#Validate for servername
check_server_name ()
{
	check_name $1
	if [ $? -eq 1 ]
	then
		return 1
	fi

	#Check for the local name .
	ilist=`/opt/lanman/lib/instlist |awk -e ' { print $1 }'`
	for i in $ilist
	do
		name=`$SRVCONFIG_PATH -I $i -g "server,listenname"`
		strcmpi $name $1
		if [ $? -eq 0 ]
		then
			eval echo "\"`pgettext 'The server name is already in use.'`\""
			return 1
		fi
	done

	#check for remote name.
	$NBBIND -s $1 > $DEV_NULL_PATH 2>&1
	if [ $? -eq 1 ]
	then
		eval echo "\"`pgettext 'The server name is already in use.'`\""
		return 1
	fi
	return 0
}

#
# Check for a legal name. Return 0 if ok, 1 otherwise
# usage: check_name name_to_check
#
check_name ()
{
	name=$1
	if [ "$name" = "" ]
	then
		pgettext "You must select a server name.\n"
		return 1
	fi

	# if no allowing uname as server name, can't be uname
	if [ "$ALLOW_UNAME_SERVER" != yes -a "$name" = "`uname -n | cut -d. -f1`" ]
	then
		pgettext "The server name cannot be the system name.\n"
		pgettext "You must select a unique name.\n"
		return 1
	fi

	# check name length
	namelen=`echo $name | wc -c`
	namelen=`expr $namelen - 1`
	if [ $namelen -gt 15 ]
	then
		pgettext "Server names must be 15 characters or less.\n"
		return 1
	fi

	# check for valid character set
	len=`expr "$name" : "[^${INVALIDCH}]*"`
	if [ "$len" -ne "$namelen" ]
	then
		pgettext "The server name contains invalid characters.\n"
		eval echo "\"`pgettext 'The invalid characters are: ${INVALIDCH}'`\""
		return 1
	fi
	#Verify whether current server name is equal to new name.
	if [ "$name" = "$Original_servername" ]
	then
		eval echo "\"`pgettext 'The server name is already in use.'`\""
		return 1
	fi

	# server name may not be the same as the domain name
	# test must be case insensitive.  use sort since it honors locale
	locale="`$SRVCONFIG_PATH -g lmxserver,lang`"
	domain="`$SRVCONFIG_PATH -g workstation,domain`"
	count=`echo "$name\n$domain" | LC_CTYPE="$locale" sort -fu | wc -l`
	if [ "$count" -eq 1 ]
	then
		pgettext "The server name cannot be the same as the domain name.\n"
		return 1
	fi

	return 0
}

#----------------------------------------------------------------------------

#
# Check the command line options. 
# Return 0 if no options (interactive mode), or 1 for non-interactive. 
# If invalid options or arguments, call usage(a).
#
# usage: check_commandline arg1 arg2 arg3 [arg4]
# These args are those specified on the commandline. All args have values and
# are of the form arg:val.
#
check_commandline ()
{
		while getopts 'n:p:d:a:I:s?' c
		do
			case $c in
			n)	Servername=$OPTARG;;
			p)	AdminPW=$OPTARG;;
			d)	Primary=$OPTARG;;
			a)	Administrator=$OPTARG;;
			I)	;;
			s)	Autostart_flag=no;;
			\?) 	usage;;
			esac
		done
	if [ "$Servername" != "" -o "$Primary" != "" -o "$AdminPW" != "" -o "$Administrator" != "" ]
	then
		check_server_name $Servername
		if [ $? -eq 1 ]
		then
			usage
		fi
		
		if [ -n "$Primary" ]
		then
			check_domain_controller_name $Primary
			if [ $? -eq 1 ]
			then
				usage
			fi
		fi
	
		if [ -n "$Servername" -a -z "$AdminPW" \
		     -a -z "$Administrator" -a "$Role" != "primary" ]
		then
			# user is claiming that there is an existing
			# account at pdc. set admin account and password
			# to bogus values
			machine_account_exist=yes
			Administrator="invalid"
			AdminPW="invalid"
		else
			checkpw $AdminPW
			if [ $? -eq 1 ]
			then
				usage
			fi
		
			# were all args specified?
			if [ "$Servername" = ""  -o  "$AdminPW" = "" \
				-o  "$Administrator" = "" ] 
			then 
				usage
			fi
		fi

		# PDC name is a required option when changing the BDC name
		# or member server name
		if [ $Role != primary  -a  "$Primary" = "" ]
		then
			usage
		fi
			
			
		return 1
	fi
	return 0
}

#----------------------------------------------------------------------------

#
# Initialize variables, perform startup checks.
# Sets Role, Original_listenname, Original_servername, Administrator.
#
initialize ()
{
	
	# set global variables

	# The product name
	COMMENT="Solaris (TM) PC NetLink"

	# Invalid characters for server name
	INVALIDCH="][/\*:+;<>,?'=@ 	"	


	# The variables needed to set the name
	Primary=""
	Servername=""
	AdminPW=""
	Administrator=""

	ListenNameCheck=`$SRVCONFIG_PATH -g "lmxserver,listennamechk"`
	ListenExtension=`$SRVCONFIG_PATH -g "lmxserver,listenextension" | \
						tr '[A-Z]' '[a-z]'`
	if [ -z "$ListenExtension" ]
	then
		ListenExtension=".serve"
	fi
	len=`echo $ListenExtension | wc -c`
	len=`expr 16 - $len`
	UnameDotServe="`uname -n | cut -d. -f1 | cut -c1-${len}`${ListenExtension}"

	# The current server name
	Original_listenname=`$SRVCONFIG_PATH -g "server,listenname"`
	if [ "$Original_listenname" = "" ]
	then
		# no entry was entered, name must be uname.serve
		Original_servername=$UnameDotServe
	else
		Original_servername=$Original_listenname
	fi

	# Role is used to issue role-specific messages, command-line args, etc.
	Role=`${GETROLE_PATH} -r | tr -d "\012"`


	# The accounts database may be VERY large; 
	# Never run out of ulimit space
	ulimit 1000000	  


	# is server running?
#	ps -e | fgrep $LMXCTRL_NAME > $DEV_NULL_PATH 2>&1

	ps -ef -o pid,args > ${TMP_FILE} 
	count=`fgrep "${LMXCTRL_NAME} -I ${PCNL_INSTANCE}" ${TMP_FILE}`
	if [ "${count:-""}" != "" ]
	then
		Serverup=yes
	else
		Serverup=no
	fi
	rm -f ${TMP_FILE}

	# This flag set to no if command line arg (-s) is specified.
	# The server will be started if the flag is yes in non-interactive mode.
	Autostart_flag=yes

	# is there an existing machine account at primary?
	machine_account_exist=no

	# save the database
	rm -rf $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1
	mkdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1
	cp -r $SAMDIR_PATH/* $SAMDIR_PATH/.tmp> $DEV_NULL_PATH 2>&1
	cp $LSA_PATH $SAMDIR_PATH/.lsatmp> $DEV_NULL_PATH 2>&1
	cp $REGISTRY_PATH $SAMDIR_PATH/.regtmp> $DEV_NULL_PATH 2>&1

	return
}

#----------------------------------------------------------------------------

#
# Get the pdc name. 
# Globals: references and modifies Primary
# 
getpdcname ()
{
	
	while [ 1 ]
	do
		if [ "$Primary" != "" ]
		then
			eval echo "\"`pgettext 'Enter the name of the primary domain controller\n or press Enter to select ${Primary}: '`\c\""
			read x
			if [ "$x" != "" ]
			then
				Primary=$x
			fi
			break
		else
			pgettext "\nEnter the name of the primary domain controller: "
			read Primary
			if [ "$Primary" != "" ]
			then
				break
			else
				pgettext "\nYou must enter the name of the primary domain controller\n"
			fi
		fi
	done
}

#----------------------------------------------------------------------------

#
# Get the name of the administrator account on the pdc
# Globals: modifies  Administrator
# usage: getadminacct <on_server>
#
getadminacct () 
{
	name=$1
	Administrator="administrator"
	while [ 1 ]
	do
		eval echo "\"`pgettext 'Enter the name of an administrator account on ${name}\n or press Enter to select ${Administrator}: '`\c\""
		read x
		if [ "$x" != "" ]
		then
			Administrator=$x
		fi
		break
	done
}

#----------------------------------------------------------------------------

#
# Get the server name
# Globals: referenced: Original_servername
#	   modified: Servername
#
getserver_name ()
{
	Servername=$Original_servername
	eval echo "\"`pgettext 'The current name of this server is (${Servername}).'`\""
	eval yorn $YES "\"`pgettext 'Would you like to change this name now'`\""
	if [ $? = $NO ]
	then
		eval echo "\"`pgettext '\nThe server name (${Servername}) will not be changed.'`\""
	        restorestate	
	fi

	while [ 1 ]
	do
		newservername=""
		pgettext "\nEnter the new servername (15 characters or less):"
		read newservername

		# Make sure server name is OK
		check_server_name "$newservername"
		if [ $? -eq 1 ]
		then
			continue
		fi
		break
	done
	Servername=$newservername

	return
}

#----------------------------------------------------------------------------

# 
# Restore Original_servername, replace accounts database file, start the server
# if it had been running.
# Exits with FATAL_EXIT_CODE
#
restorestate()
{
	trap 2 3 15
	pgettext "\nRestoring server to its previous state ... \n"
	stty echo
	if [ "$Original_listenname" != "" ]
	then
		$SRVCONFIG_PATH -s "server,listenname=$Original_listenname" \
			> $DEV_NULL_PATH 2>&1
	else
		# remove listenname if it exists
		sed '/^listenname=/d' < $LANMAN_INI_PATH > $LANMAN_INI_PATH.t
		mv $LANMAN_INI_PATH.t $LANMAN_INI_PATH
	fi
	cp -r  $SAMDIR_PATH/.tmp/* $SAMDIR_PATH > $DEV_NULL_PATH 2>&1
	rm -f  $SAMDIR_PATH/.tmp/* > $DEV_NULL_PATH 2>&1
	rm -f  $SAMDIR_PATH/$upper_new > $DEV_NULL_PATH 2>&1
	cp $SAMDIR_PATH/.lsatmp $LSA_PATH > $DEV_NULL_PATH 2>&1
	rm -f  $SAMDIR_PATH/.lsatmp > $DEV_NULL_PATH 2>&1
	cp $SAMDIR_PATH/.regtmp $REGISTRY_PATH > $DEV_NULL_PATH 2>&1
	rm -f  $SAMDIR_PATH/.regtmp > $DEV_NULL_PATH 2>&1
	rmdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1
	pgettext "Success\n"
	if [ $Serverup = yes -a $Autostart_flag = yes ]
	then
		eval echo "\"`pgettext 'Restarting the ${COMMENT} ... '`\c\""
		$NET_PATH start server > $DEV_NULL_PATH 2>&1
		if [ $? -ne 0 ]
		then
			pgettext "\n\tFailed to start\n"
		else
			pgettext "\n\tStarted successfully\n"
		fi
	fi
	exit ${FATAL_EXIT_CODE}
}

#----------------------------------------------------------------------------

#
# This routine does the work. It is called after all the required information
# has been specified and checked.
#
setname ()
{
	# update the listenname only if a servername is entered, it is
	# not uname.serve, and the user has changed it
	# makemach reads the name from lanman.ini (using getlmxname() )

	if [ "$Servername" != ""  -a  "$Servername" != "$Original_servername" ]
	then
		# update listenname.  
		# set if no unix listener or new name is not uname.serve.
		# remove if unix listener and new name is uname.serve.
		if [ "$ListenNameCheck" = no  -o \
				"$Servername" != $UnameDotServe ]
		then
			$SRVCONFIG_PATH -s "server,listenname=$Servername"
		else
			sed '/^listenname=/d' < $LANMAN_INI_PATH > $LANMAN_INI_PATH.t
			mv $LANMAN_INI_PATH.t $LANMAN_INI_PATH
		fi
	fi

	if [ $Role != primary ]
	then
		eval echo "\"`pgettext 'Contacting the server ${Primary} ... '`\c\""
		serverarg=${Primary}
		if [ $Role = backup ]
		then
			rolearg='-B'
		else
			rolearg='-M'
		fi
	else
		pgettext "\nUpdating the domain account database ... \n"
		serverarg=${Servername}
		rolearg='-P'
	fi

	
	if [ "$machine_account_exist" = "no" -o "$Role" = "primary" ]
	then
		$MAKEMACH_PATH $rolearg -d $Original_servername \
			-a $Administrator -p "$AdminPW" \
			-s $serverarg > $DEV_NULL_PATH 2>&1 
	else
		$MAKEMACH_PATH $rolearg -s $Primary -e > $DEV_NULL_PATH 2>&1
	fi

	if [ $? != 0 ]
	then
		if [ $Role != primary -a "$machine_account_exist" = "no" ]
		then
			eval echo "\"`pgettext 'Unable to contact the PDC ${Primary}.'`\""
		else
			pgettext "\nUnable to update the domain account database.\n"
		fi

		if [ $Interactive = yes ]
		then
			eval yorn $YES "\"`pgettext 'Do you want to retry with new information'`\""
			if [ $? = $YES ]
			then
				# have the previously created SAM stuff trashed
				return 1
			fi
		fi

		restorestate
	fi

	pgettext "\nDomain account database updated successfully\n"

        if [ $Role = member ]
        then
		pgettext "\nUpdating the local account database ... \n"

    		OldLocalDomain=`echo $Original_servername | tr '[a-z]' '[A-Z]'`
                NewLocalDomain=`echo $Servername | tr '[a-z]' '[A-Z]'`

	    	mv $SAMDIR_PATH/$OldLocalDomain $SAMDIR_PATH/$NewLocalDomain
		if [ $? -ne 0 ]
		then
			restorestate
		fi

	    	# Use samcheck's repair operation to update both the local
	    	# domain name and the database path in the local account
                # database.
	    	$SAMCHECK_PATH -rv > $DEV_NULL_PATH 2>&1
                if [ $? -ne 0 ]
                then
                        pgettext "\nLocal account database is corrupted!\n"
                        restorestate
                fi

                pgettext "\nLocal account database updated successfully\n"
        fi

	# If the servername is 'uname'.serve, update UNIX listener database
	# Tell nlsadmin to install the Client Vc handoff program
	# Do it for every network protocol loaded.

	if [ "$ListenNameCheck" = yes -a "$Servername" = $UnameDotServe ]
	then
		for x in `$NLSADMIN_PATH -x | cut -f1`
		do
			$NLSADMIN_PATH -q -z 2 $x > $DEV_NULL_PATH 2>&1
			if [ $? -eq 0 ]
			then
				eval echo "\"`pgettext 'Removing old entry for the server on protocol stack: ${x}'`\""
				$NLSADMIN_PATH -r 2 $x > $DEV_NULL_PATH 2>&1
			fi
			eval echo "\"`pgettext 'Adding the server to protocol stack:${x}'`\""
			eval $NLSADMIN_PATH -m -a 2 -c "$PASSCLI_PATH" -y \
			"\"`pgettext 'Used by ${COMMENT}'`\"" -w root $x
		done

	fi

	return 0
}

#----------------------------------------------------------------------------

#
# This routine does most of the work
#
changename ()
{
	#
	# Interactive = no, command line has been used, just do it!
	#
	if [ $Interactive = no ]
	then 
		setname
		return
	fi


	# 
	# Interactive mode
	#
	while [ 1 ]
	do
		machine_account_exist=no

		getserver_name

		if [ "$Role" != primary ]
		then
			#
			# Update the machine account at the PDC
			#
			
			pgettext "\n"\
"This command will notify the primary domain controller of the change\n in server name.  If there is no existing account at primary, you will be prompted to enter the name of the PDC,\n and an adminstrative account name on the primary along with its password.\n In order for the server name change to complete successfully, the PDC\n must be running and connected to the network.\n"

			getpdcname

			echo
			sv_type=`$MAKEMACH_PATH -C -s $Primary -m $Servername`

			if [ $? -eq 0 ]
			then
				echo "$sv_type account $Servername found on $Primary\n"
				if [ "$sv_type" = "WORKSTATION" -a \
				     "$Role" = "member" -o \
				     "$sv_type" = "DOMAIN_CONTROLLER" -a \
				     "$Role" = "backup" ]
				then
					cat <<!

You need an administrative account and password at primary domain controller
if you intend to remove the old account. 

!
					yorn $YES Do you want to remove the old account
					if [ $? = $NO ]
					then
						machine_account_exist=yes
					fi
				fi  
			fi
			
			if [ "$machine_account_exist" = "no" ]
			then	
				getadminacct $Primary
			fi
		else
			getadminacct $Servername
		fi

		if [ "$machine_account_exist" = "no" ]
		then
			eval getpw "\"`pgettext 'Enter the password for $Administrator:'`\c\""
		fi

		# We have the answers.  Give one last chance to change them
		#
		eval echo "\"`pgettext 'Confirm choices:   New servername            : ${Servername}'`\""
		if [ "$machine_account_exist" = "no" ]
		then
			eval echo "\"`pgettext '		   Administrator account     : ${Administrator}'`\""
		fi

		if [ $Role != primary ]
		then
			eval echo "\"`pgettext '		   Primary domain controller : ${Primary}\n'`\""
		fi

		eval yorn $YES "\"`pgettext 'Is this correct'`\""
		if [ $? = $NO ]
		then
			# get the info again
			continue
		fi

		setname

		if [ $? -ne 0 ]
		then
			continue
		fi

		break
	done
	return
}

#----------------------------------------------------------------------------

#
# Prompt to stop the server
# Globals: references Serverup, Interactive
#
stop_the_server ()
{
	# See if the server is running and then stop it.

	if [  $Serverup = yes ]
	then
		if [ $Interactive = yes ]
		then
			pgettext "\nThe server is running.\n"
			pgettext "The server name cannot be changed while the server is running.\n"
			eval yorn $YES "\"`pgettext 'Do you want to stop the server and continue'`\"" 
			if [ $? != $YES ]
			then
				exit ${FATAL_EXIT_CODE}
			fi
		fi
		pgettext "\nStopping the server ... \n"
		$NET_PATH stop server /y > $DEV_NULL_PATH 2>&1
		if [ $? = 0 ]
		then
			pgettext "Success\n"
		else
			pgettext "\nError: Unable to stop the server.\n"
			exit ${FATAL_EXIT_CODE}
		fi
	fi
}

#----------------------------------------------------------------------------

#
# Start_the_server - determine whether to automatically start, prompt to start,
# or not start the server.
# Globals: references Serverup, Interactive
#
start_the_server ()
{
	#
	# If the server was not up AND we are running interactively, 
	# then prompt to restart; 
	# else do not start if the flag is set to not start;
	# all other times automatically restart
	#
	if [ $Serverup = no  -a  $Interactive = yes ]
	then
		eval yorn $YES "\"`pgettext 'Do you want to start the server now'`\""
		startserver=$?
	elif [ $Autostart_flag = no ]
	then
		startserver=$NO
	else
		startserver=$YES
	fi


	if [ $startserver = $YES ]
	then
		eval echo "\"`pgettext 'Starting the ${COMMENT} ... '`\c\""
		$NET_PATH start server > $DEV_NULL_PATH 2>&1
		if [ $? -ne 0 ]
		then
			pgettext "Unable to start the server.\n"
	#		restorestate
		else
	pgettext "Server started successfully\n\n"\
"Note: If the replicator service is running on this server, it may have to be\n"\
"      reconfigured. Use the Properties function of Server Manager to check\n"\
"      the replication configuration.\n"
		fi

	else

		pgettext "The name change will not take effect until the server has been started.\n\n"

	fi
}

#----------------------------------------------------------------------------
#----------------------------------------------------------------------------

#
# Beginning of main function of setservername
#

#
# perform initialization, startup checks
#
initialize


# is there are command line? should we run interactively?
check_commandline $*
if [ $? -eq 0 ]
then
	Interactive=yes
else
	Interactive=no
fi


if [ $Interactive = yes ]
then

eval echo "\"`pgettext 'This ${COMMENT} utility (${Progname})\n allows you to change the server name on either the primary domain controller (PDC) or backup domain controller (BDC).  If no valid existing account is found at primary domain controller, (${Progname}) will prompt for an administrative account and password of primary, and create an account for the new server name\n and delete the account with the old server name.\n When this utility is run on a BDC or member server with no existing account at primary, the PDC is contacted and \n the new account is added to the domain, then the old account is removed from the domain.\nIf a valid existing account is found at primary, you will not be prompted for an administrative account and password at primary unless you choose to remove the old account.\n The server will be stopped each time you run ${Progname}.'`\"" 

	eval yorn $NO "\"`pgettext 'Do you want to continue'`\""
	if [ $? = $NO ]
	then
		exit ${FATAL_EXIT_CODE}
	fi
fi


stop_the_server

trap 'restorestate' 2 3 15


#
# perform the name change
#
changename


start_the_server


trap 2 3 15

rm -f	$SAMDIR_PATH/.tmp/* > $DEV_NULL_PATH 2>&1
rm -f	$SAMDIR_PATH/.lsatmp > $DEV_NULL_PATH 2>&1
rm -f	$SAMDIR_PATH/.regtmp > $DEV_NULL_PATH 2>&1
rmdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1





