#!/bin/sh
#
# Copyright (c) AT&T 1999. All rights reserved.
# @(#)setdomainname.sh	1.17	10/19/99
# 


#
# These variables may be used immediately
#
NBUTIL_PATH=/opt/SUNWlznb/sbin/nbutil
FATAL_EXIT_CODE=1

textdomaindir=/opt/lanman/lib/locale

textdomain='setdomainname'

#
# 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"
}

# Used in messages
Progname=`basename $0`

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

# error messages

FIXMEMSRV_DEL_ERROR="ERROR:\tFailed to delete built-in domain groups from\n\
member server's account database."
 
FIXMEMSRV_ADD_ERROR="ERROR:\tOne or more built-in global groups are not added to\n\
built-in local groups at member server." 

MAKEMACH_STAND_ALONE_ERROR="ERROR:\tAn account for this machine cannot be created\n\
on the primary domain controller.\n\
The primary domain controller for this domain must be started and active\n\
on the network before the installation of a member server is attempted."

MAKEMACH_PASSWORD_ERROR="ERROR: The password for this account could not be set in local database.\n\
Make sure the existing account at primary '\$primary' is a '\$Role'\n\
account type."

FIXDB_ERROR="ERROR:\tDatabase could not be updated for the\n\
new domain."

MEMBER_LOOKING_FOR_ACCT="Looking for an existing account at primary '\$primary'..."

MEMBER_ACCOUNT_FOUND="\nFound a valid existing account at primary '\$primary'"

MEMBER_ERROR_ACCOUNT_EXIST="\nERROR:\tA domain controller account with the same server name you specified\n\
exists on primary domain controller. You can re-new this account or choose\n\
different server name by first running '\$SETSERVERNAME_NAME'"

MEMBER_ACCOUNT_NOT_FOUND="\nExisting account was not found."

PDC_NAME_ERROR="\nERROR:The new domain's PDC name doesn't match with the original PDC for this domain\n\
Use "joindomain" if you want to move this server to different domain."

DOMAINNAME_ERROR="\nERROR:The new domain name is already in use,\n\
Use a different domain name.\n"

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

# initialze the common shell variables
TMP_FILE=/tmp/regload.$$

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

# 
# Usage - display more info for bdc than pdc
# Globals: references Progname
#
usage_dc ()
{
	eval echo "\"`pgettext 'Usage: ${Progname}  [-I <instance number | instance name>] [-n NewDomainName] [-s] \nwhen -s is specified the server is not automatically started'`\""
	exit 1
}

#
# We have a different command line non-interactive usage for member
# servers 
#
usage_mem ()
{
	echo "Usage: ${Progname}  [-I instance] [-d PDC Name] [-a Administrator Acct] [-p Administrator Password] [-s]"
	echo "when -s is specified the server is not automatically started\n"
	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_dc
		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_dc
		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_dc
		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 ':d:a:p:sI:n:?' 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

# 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 '\nYou 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
}

#
# Check for a legal name. 
# Return 0 if ok, 1 otherwise.
# This routine takes one argument, the name to validate.
#
check_name ()
{
	name=$1
	if [ "$name" = "" ]
	then
		pgettext "You must enter a domain name.\n"
		return 1
	fi

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

	# Domain name can't be server name
	# test must be case insensitive.  use sort since it honors locale
	locale="`$SRVCONFIG_PATH -g lmxserver,lang`"
	count=`echo "$name\n$Servername" | LC_CTYPE="$locale" sort -fu | wc -l`
	if [ "$count" -eq 1 ]
	then
		pgettext "The domain name cannot be the Server 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 "Domain 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 domain name contains invalid characters.\n"
		eval echo "\"`pgettext 'The invalid characters are: ${INVALIDCH}'`\""
		return 1
	fi

	# is the name the current name?
	if [ "$name" = "$Current_domainname" ]
	then
		pgettext "The new name is the same as the current 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( ).
#
# This routine takes the command line arguments specified on the invocation of
# the shell script.  All args have values and are of the form arg:val
# Usage: check_commandline arg1 arg2 arg3 [arg4]
#
# Globals: modifies NewDomain
#
check_commandline ()
{
	# are there any commandline arguments?
	if [ "$1" = "" ]
	then
		return 0
	else
		if [ "$Role" = "member" ]
		then
		
			while getopts ':d:a:p:sI:?' c
			do
				case $c in
				d)	primary=$OPTARG;;
				a)      admin=$OPTARG;;
				p) 	domain_pw=$OPTARG;;
				s)	Autostart_flag=no;;
				I)	;;
				\?)	usage_mem;;
				esac		
			done
	
			if [ -z "$admin" -a -z "$domain_pw" -a -n "$primary" ]
			then
				# user is claiming that there is
				# an existing machine account at primary
				machine_account_exist=yes
				admin="invalid"
				domain_pw="invalid"
			elif [ -z "$primary" -o -z "$admin" -o -z "$domain_pw" ]
			then
				return 0
			fi

		else
 
			while getopts 'n:sI:?' c
			do
				case $c in
				n)	NewDomain=$OPTARG;;
				s)	Autostart_flag=no;;
				I)	;;
				\?) 	usage_dc;;
				esac
			done

			if [ "${NewDomain}" = "" ]
			then
				return 0
			fi

			check_name $NewDomain
			if [ $? -eq 1 ]
			then
				usage_dc
			fi
		fi
		
		return 1
	fi
}
#----------------------------------------------------------------------------

#
# Initialize variables, perform startup checks.
# Sets ulimit, Servername, Listenname, Current_domainname, Role. 
# Checks if server is running and sets Serverup. 
#
initialize ()
{
	# set global variables

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

	# Owner and Group for datafiles 
	OWNER=lanman
	GROUP=DOS----

	# The variables needed to set the name
	NewDomain=""
	Listenname=`$SRVCONFIG_PATH -g "server,listenname"`
	if [ "$Listenname" = "" ]
	then
		# no entry was entered, name must be uname.serve
		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`
		Servername="`uname -n | cut -d. -f1 | cut -c1-${len}`${ListenExtension}"
	else
		Servername=$Listenname
	fi

	# The current domain name
	Current_domainname=`$SRVCONFIG_PATH -g "workstation,domain"`

	# Role is used to issue role-specific messages, 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?
	rm -f ${TMP_FILE}
	ps -ef -o pid,args > ${TMP_FILE}
	count=`egrep "${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?
	# only applicable to member servers
	machine_account_exist=no

	return
}

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

savedatabase ()
{
	# save a copy of the SAM database
	rm -rf $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1
	mkdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1
	cp $SAMDIR_PATH/$Current_domainname $SAMDIR_PATH/.tmp> $DEV_NULL_PATH 2>&1
	cp $LSA_PATH $LSA_PATH.t > $DEV_NULL_PATH 2>&1
	cp $BUILTIN_PATH $BUILTIN_PATH.t > $DEV_NULL_PATH 2>&1
}

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

restoredatabase ()
{
	cp -r  $SAMDIR_PATH/.tmp/* $SAMDIR_PATH > $DEV_NULL_PATH 2>&1
	rm -f $SAMDIR_PATH/.tmp/* > $DEV_NULL_PATH 2>&1
	rmdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1
	rm -f $SAMDIR_PATH/$NewDomain > $DEV_NULL_PATH 2>&1

	mv $LSA_PATH.t $LSA_PATH > $DEV_NULL_PATH 2>&1
	mv $BUILTIN_PATH.t $BUILTIN_PATH > $DEV_NULL_PATH 2>&1

	# restore group/owner
	chgrp $GROUP $SAMDIR_PATH/* $LSA_PATH/* $BUILTIN_PATH/* > $DEV_NULL_PATH 2>&1
	chown $OWNER $SAMDIR_PATH/* $LSA_PATH/* $BUILTIN_PATH/* > $DEV_NULL_PATH 2>&1
}

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

#
# Get the new domain name.
# Globals: referenced: Servername, Role, Current_domainname
#	   modified: NewDomain
#
getdomain_name ()
{
	if [ "$Role" = "member" ]
	then
		echo "\nThis server, ${Servername}, is a member server in the"
	else
		echo "\nThis server, ${Servername}, is a ${Role} domain controller in the"
	fi

	echo "domain ${Current_domainname}.\n"
	yorn $YES Would you like to change the domain name now
	if [ $? = $NO ]
	then
		eval echo "\"`pgettext 'The server domain ${Current_domainname} will not be changed.'`\""
		restorestate
	fi

	newdomain=""

	if [ "$Role" = "member" ]
	then
	 	while [ 1 ]
		do
			machine_account_exist=no

			get_primary_info

			newdomain=`${MAKEMACH_PATH} -q -a $admin -p "$domain_pw" -s $primary`
			if [ $? -ne 0 ]
			then
				continue
			fi
			break
		done
	else
		while [ 1 ]
		do
			newdomain=""
			echo "\nEnter the new domain name (15 characters or less): \c"
			read newdomain

			# Make sure domain name is OK
			check_name "$newdomain"
			if [ $? -eq 1 ]
			then
				continue
			fi
			break
		done
	fi

	NewDomain=$newdomain

	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 "Restoring server to its previous state ... \n"
	stty echo
	if [ "$Current_domainname" != "" ]
	then
		$SRVCONFIG_PATH -s "workstation,domain=$Current_domainname" \
			> $DEV_NULL_PATH 2>&1
	fi
	if [ -d $SAMDIR_PATH/.tmp ]
	then
		restoredatabase
	fi

	pgettext "Success\n"

	if [ $Serverup = yes ]
	then
		eval echo "\"`pgettext 'Restarting the ${COMMENT} ... '`\""
		$NET_PATH start server > $DEV_NULL_PATH 2>&1
		if [ $? -ne 0 ]
		then
			pgettext "Failed to start\n"
		else
			pgettext "Started successfully\n"
		fi
	fi
	exit ${FATAL_EXIT_CODE}
}

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

#
# Setname: this is the worker routine. It is called in both interactive and
# non-interactive modes.
# All the required information has been specified on the command line and
# checked.
#
setname ()
{
	# set the lanman.ini keyword
	$SRVCONFIG_PATH -s "workstation,domain=$NewDomain"


	#set the new domain name in the LSA database
	if [ "$Role" = "member" ]
	then

    		# change the primary domain controller name to all upper case
    		# before we store it into the database.

    		upper_primary=`echo $primary | tr '[a-z]' '[A-Z]'`

		if [ "$machine_account_exist" = "no" ]
		then
                	$MAKEMACH_PATH -M -a $admin -p "$domain_pw" -s $upper_primary > $DEV_NULL_PATH 2>&1
                	if [ $? -ne 0 ]
                	then
				echo
                        	echo "${MAKEMACH_STAND_ALONE_ERROR}"
                        	restorestate
                	fi
		else
			$MAKEMACH_PATH -M -s $upper_primary -e > $DEV_NULL_PATH 2>&1

                	if [ $? -ne 0 ]
                	then
				echo
                        	eval echo \"${MAKEMACH_PASSWORD_ERROR}\"
                        	restorestate
                	fi
		fi

                ${FIXMEMSRV_PATH} -x > $DEV_NULL_PATH  2>&1

		if [ $? -ne 0 ]
		then
			echo "${FIXMEMSRV_DEL_ERROR}"
                        restorestate
		fi

                $FIXDB_PATH -d $NewDomain -a $admin -p $domain_pw -s $upper_primary > $DEV_NULL_PATH 2>&1

                if [ $? -ne 0 ]
                then
			echo "${FIXDB_ERROR}"
                        restorestate
                fi

                ${FIXMEMSRV_PATH} > $DEV_NULL_PATH 2>&1

                if [ $? -ne 0 ]
                then
                        echo "${FIXMEMSRV_ADD_ERROR}"
                        restorestate
		fi

		echo "Domain name changed successfully\n"	
	else
		if [ "$Role" = "primary" ]
		then
			r=`${NBUTIL_PATH} -T 0x1c -f ${NewDomain}` 
		  	if [ $? -ne 255 ]
                   	then
                       		echo "${DOMAINNAME_ERROR}"
                       		restorestate
                   	fi
		elif [ "$Role" = "backup" ]
		then
			PDCName=""
			plist=`${NBUTIL_PATH} -T 0x1b -n ${NewDomain} | grep -v Deregistered | grep "\[20\]" | cut -c10-24 | sed -e "s' *$''" | grep -v "\*"`
			newdomainU=`echo $NewDomain |nawk ' { s=toupper($1); print s }'`
			for srv in $plist
		 	do
		  		tempdom=`${MAKEMACH_PATH} -q -s "$srv"`
				tempdomU=`echo $tempdom |nawk ' { s=toupper($1); print s }'`
				if [ "$tempdomU" = "$newdomainU" ]
				then
			  		temprole=`${MAKEMACH_PATH} -q -r -s "$srv"`
			  		if [ $temprole = "3" ]
			  		then
			    			PDCName="$srv"
			    			break
			  		fi
				fi
		  	done
		  	if [ "$PDCName" = "" ]
		  	then
		        	echo "\nThe primary domain controller for domain $NewDomain cannot be reached."
		    		restorestate
		  	fi
			GetRole_PDC=`${GETROLE_PATH} -p | tr -d "\012"`
			if [ "$PDCName" != "$GetRole_PDC" ]
			then
				echo "${PDC_NAME_ERROR}"
				restorestate
			fi
		fi

		$SETDOMAIN_PATH  -n $NewDomain
		if [ $? != 0 ]
		then
			restorestate
		else
			echo "Domain name changed successfully\n"
		fi

		# rename the domains blob file
		# first, make sure it exists
	    	if [ ! -f $SAMDIR_PATH/$Current_domainname ]
	    	then
			restorestate
	    	fi

	    	cp $SAMDIR_PATH/$Current_domainname $SAMDIR_PATH/$NewDomain
	    	rm -f $SAMDIR_PATH/$Current_domainname 

	    	# set the owner/group on the new filename
	    	chgrp $GROUP $SAMDIR_PATH/*
	    	chown $OWNER $SAMDIR_PATH/*

                $SEPRIMARY_PATH $NewDomain > $DEV_NULL_PATH 2>&1

                if [ $? -ne 0 ]
                then
			echo "${FIXDB_ERROR}"
		fi
		
	    	# check the database
		# use the repair "-rv" option to update the path
		# the account database path in the database
	    	$SAMCHECK_PATH -rv > $DEV_NULL_PATH 2>&1

                if [ $? -ne 0 ]
                then
                     echo "\nSAM seems to be corrupted!\n"
                     restorestate
                fi
	fi

	return
}

#
# get PDC name, admin and the password
#
get_primary_info ()
{
    if [ "$primary" != "" ]
    then
            echo "Enter the name of the primary domain controller"
            echo "or press Enter to select '$primary': \c"
            read x
            if [ "$x" != "" ]
            then
                  primary=$x
            fi
    else
            echo "Enter the name of the primary domain controller: \c"
            read primary
            if [ "$primary" = "" ]
            then
                  primary="UNKNOWN"
            fi
    fi

    eval echo \"${MEMBER_LOOKING_FOR_ACCT}\"
    sv_type=`${MAKEMACH_PATH} -C -s $primary -m $Servername`

    if [ $? -eq 0 ]
    then
	 if [ "$sv_type" = "WORKSTATION" ]
	 then
		eval echo \"${MEMBER_ACCOUNT_FOUND}\"
		machine_account_exist=yes
	 else
		eval echo \"${MEMBER_ERROR_ACCOUNT_EXIST}\"
		echo
		yorn $YES Do you want to re-new the account
		if [ $? = $NO ]
		then
			restorestate
		fi
	 fi	
    else
	 echo "${MEMBER_ACCOUNT_NOT_FOUND}"
    fi

    if [ "$machine_account_exist" = "no" ]
    then
    	admin="Administrator"
    	echo "\nEnter the name of an administrator account on '$primary'"
    	echo "or press Enter to select '$admin': \c"
    	read x
    	if [ "$x" != "" ]
    	then
          	admin=$x
    	fi
    	echo ""
    	getpw 0 "Enter the password for $admin on '$primary': \c"
    	domain_pw="$pw"
    else
	# there's already an existing account at primary
	# set these values to bogus values
	admin="invalid"
	domain_pw="invalid"
    fi

    return
}

#
# get the password
#
getpw ()
{
        while [ 1 ]
        do
                echo $2
                stty -echo
                read pw
                n=`echo "$pw" | wc -c`
                if [ $n -gt 15 -o $n -lt $1 ]
                then
                        echo "\nPassword must be shorter than 15 characters."
                        continue
                fi
                echo "\nRe-enter password: \c"
                read ANS2
                stty echo
                if [ "$pw" != "$ANS2" ]
                then
                        echo "\nPassword mismatch!"
                        continue
                fi
                echo
                break
        done
	return 
}




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

#
# 
#
changename ()
{
	#
	# Interactive = no, command line has been used, just do it!
	#
	if [ $Interactive = yes ]
	then 
		getdomain_name
	else
		if [ "$Role" = "member" ]
		then
			NewDomain=`$MAKEMACH_PATH -q -a $admin -p "$domain_pw" -s $primary`
			if [ $? -ne 0 ]
			then
				restorestate
			fi
		fi
	fi
 	
	# do the actual name update
	setname

	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 "The server is running.\n"
			pgettext "The domain 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 "Stopping the server ... \n"
		$NET_PATH stop server /y > $DEV_NULL_PATH 2>&1
		if [ $? -eq 0 ]
		then
			pgettext "Success\n"
		else
			pgettext "Error: 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} ... '`\""
		$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\nNote: If the replicator service is running on this server, it may have \nto be reconfigured. Use the Properties function of Server Manager to check \nthe replication configuration.\n"
		fi
	fi
}

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

#
# Beginning of main function of setdomainname
#

#
# 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})\nallows you to change the domain name on a server.  If the domain\nname of this server is changed, the domain name must be changed on every\nserver in this domain. This utility must be run on each of these servers.\n(${Progname}) also enables member servers to join an existing domain\nwithout re-building its local accounts databases.\nThe 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


savedatabase

stop_the_server

trap 'restorestate' 2 3 15


#
# main routine to change the name
#
changename


start_the_server


trap 2 3 15

#remove saved files
rm -f	$SAMDIR_PATH/.tmp/* > $DEV_NULL_PATH 2>&1
rmdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1
rm -f	$LSA_PATH.t  > $DEV_NULL_PATH 2>&1
rm -f   $BUILTIN_PATH.t > $DEV_NULL_PATH 2>&1
