#! /bin/sh
#
# @(#)shhdr.sh 6.1 95/12/15 Copyright (c) 1990-1995, Legato Systems, Inc.
#
# All rights reserved.
#
# Include this first in any generated installation removal shell script.
#
# @(#)include.sh 6.3 96/04/23 Copyright (c) 1996, Legato Systems, Inc.
#
# General purpose installation removal shell functions
# Rules:
# 1) No user interaction can be requested, it should be driven by
#    sourcing the ${NSRREQUEST} file.
# 2) This file should be include in every install remove shell script.
# 3) This file should be included near the top of every install remove
#    shell script.

#
# Print a Legato Copyright notice.
#
copyright()
{
COPYRIGHT="Copyright (c) 1990-1997, Legato Systems, Inc."
echo "		${productname}(TM) - Release ${RELEASE}"
echo "${COPYRIGHT}  All rights reserved."
echo "This product includes software developed by the University of"
echo "California, Berkeley and its contributors."
echo ""
} # end copyright

#
# Call nsr_shutdown in the dir where we are executing from
#
do_shutdown()
{
	if [ -z "${mypath}" -o \! -f ${mypath}/nsr_shutdown ]; then
		# run in a subshell to hide error messages, such as
		# "nsr_shutdown: not found" if the command is not installed.
		(nsr_shutdown $*) 2> /dev/null
	else
		${mypath}/nsr_shutdown $*
	fi
}

#
# See if the any of the known ${productname} daemons are currently running.
#
# Outputs:
# 	Puts a list of running daemons into ${daemonsrunning}
#
find_daemons()
{
	# Call the nsr_shutdown script to return a list of daemons
	# that are running
	daemonsrunning="`do_shutdown -D -a -n | sort -u`"
}

# Try to set mypath from the PATH, looking for dbmi_ize
setmypath()
{
	_p="$PATH"
	mypath=""
	while [ "X${_p}" != "X" ]
	do
		if [ `expr "X${_p}" : ".*:"` -gt 0 ]; then
			_i=`expr "${_p}" : '\([^:]*\):'`
			_q=`echo "X${_p}" | sed -e 's/[^:]*://'`
		else
			_i="${_p}"
			_q=""
		fi
		case "X${_i}" in
		X)	_i="."
			;;
		esac
		_p=${_q}
		if [ -f "${_i}/dbmi_ize" ]; then
			mypath="${_i}"
			break
		fi
	done

	if [ "X${mypath}" = "X" -a -f ./dbmi_ize ]; then
		mypath="."
	fi
}

installinit()
{
RELEASE=dbmi-1.1

mypath="`expr X\"${0}\" : X'\(.*\)/.*' \| X\"${0}\" : X'\(/\)[^/]*$' \| 'X'`"
myname=`basename $0`

# if mypath isn't set, try to find the origin of this dbmi_ize via PATH
case "X${mypath}" in
XX)	setmypath
	;;
esac

# NOTE: some systems put "Berkeley" utilities in /usr/ucb, others (e.g. SGI)
# put them in /usr/bsd.  Also, some systems use /usr/etc and other use
# /usr/sbin.  We include all variants in addition to the path to this
# program to be safe.
#
PATH=/usr/ucb:/usr/bsd:/bin:/usr/bin:/etc:/usr/etc:/usr/sbin:$mypath:$PATH
export PATH

# default values
productname=NetWorker
productized="nsr-ized"
deproductized="de nsr-ized"
productizing="nsr-izing"
deproductizing="de nsr-izing"

nsrhome=/nsr		# normally set up to be a symlink to a large partition
raphome=/var/rap
rapmom=/var
raplares=/etc/la.res
index_dir="${nsrhome}/index"
mm_dir="${nsrhome}/mm"
log_dir="${nsrhome}/logs"
cores_dir="${nsrhome}/cores"
default_bin=/usr/bin
exdir=${default_bin}
old_nsrres=/etc/nsr.res
nsrreshome=/nsr/res
jboxhome=${nsrreshome}
nsrres=${nsrreshome}/nsr.res
jboxres=${jboxhome}/nsrjb.res
nsrlares=${nsrreshome}/nsrla.res

server=UNKNOWN
qflag=n
nflag=n
xflag=n
kflag=n
mflag=y
Fflag=n
pflag=n
demo=n
errquitdefault=y
errquit=${errquitdefault}
op=
proc=
upgrade=false
local_only=
link_test_opt=-h
domanpage=n

daemons="ansrd nsrd nsrindexd nsrmmd nsrmmdbd nsrexecd"

start_message=\
"# Start of lines added by ${productname} - DO NOT DELETE OR CHANGE THIS LINE"
end_message=\
"# End of lines added by ${productname} - DO NOT DELETE OR CHANGE THIS LINE"

if [ -f /bin/hostname ]; then
	host=`/bin/hostname`
elif [ -f /usr/ucb/hostname ]; then
	host=`/usr/ucb/hostname`
elif [ -f /usr/bsd/hostname ]; then
	host=`/usr/bsd/hostname`
elif [ -f /usr/local/bin/hostname ]; then
	host=`/usr/local/bin/hostname`
else
	host=
fi
if [ -r /bin/uname -o -r /usr/bin/uname ]; then
	uname=`uname`
	proc=`uname -p 2>&1`
	if [ "X${host}" = X ]; then
		host=`uname -n`
	fi
fi
if [ X${host} = X ]; then
	echo "no host name set - assuming loopback"
	hostname loopback
fi

sun=n
dec=n
hpux=n
hpux_9=n
hpux_10=n
aix=n
sgi=n
sco=n
linux=n
# True if we have System V style /etc/rc*.d files
svrc=y
bsd_echo=true

df=df
id=id

man=/usr/man

# prefix to section directory where man pages go
sectiondir=man
# unformatted man pages sourcemandir=man, formated man pages sourcemandir=catman
sourcemandir=man
fmt="fmt"

# save the args
ARGS="$*"

# find the binary type of /bin/sh
set X `file /bin/sh`
shift
# for some shells, the above 'set' results in 'X' being $0 and '/bin/sh:'
# in $1 so check for that and shift if it is true
if [ X"$1" = X/bin/sh: ]; then
	shift
fi
if [ X"$1" = Xsymbolic ]; then
	set X `file -L /bin/sh`
	shift
fi
bin_type="$1"
set X $ARGS
shift
} # end installinit

setsolarisbins()
{
	case x$PKG in
		xSUNW*)
			default_bin=${BASEDIR}/sbin/nsr
			exdir=${BASEDIR}/sbin/nsr
			;;
		*)
			default_bin=${BASEDIR}/sbin
			exdir=${BASEDIR}/sbin
			;;
	esac
}

findarch()
{
#
# DEC's standard version of test doesn't know
# about -x option, so just use the -r option.
# note that we cannot use "elif", since this may be a BSD sh (news)!
#
if [ "X${proc}" = Xsparc ]; then
	# System V release 4 on SPARC, a.k.a. SunOS 5.0.x
	myarch=solaris
	sun=n
	setsolarisbins
	df="df -l"
	no_rewind_example=/dev/rmt/0mbn
	rewind_example=/dev/rmt/0mb
	if [ -f /usr/ucb/echo ]; then
		bsd_echo=true
	else
		bsd_echo=false
	fi
elif [ "X${uname}" = XSunOS -a "X${proc}" = Xppc ]; then
	# System V release 4 on PPC, a.k.a. SunOS 5.x
	myarch=solppc
	sun=n
	setsolarisbins
	df="df -l"
	no_rewind_example=/dev/rmt/0mbn
	rewind_example=/dev/rmt/0mb
	if [ -f /usr/ucb/echo ]; then
		bsd_echo=true
	else
		bsd_echo=false
	fi
elif [ "X${uname}" = XSunOS -a "X${proc}" = Xi386 ]; then
	# System V release 4 on i386, a.k.a. SunOS 5.0.x
	myarch=sol86
	sun=n
	setsolarisbins
	df="df -l"
	no_rewind_example=/dev/rmt/0mbn
	rewind_example=/dev/rmt/0mb
	if [ -f /usr/ucb/echo ]; then
		bsd_echo=true
	else
		bsd_echo=false
	fi
elif [ "X${uname}" = XLinux ]; then
	myarch=linux
	linux=y
	svrc=n
	fmt=cat
	default_bin=/usr/sbin
elif [ X${uname} = XOSF1 ]; then
	#
	# DEC OSF/1 is a little different from Ultrix
	#
	dec=y
	svrc=y
	myarch=decaxp
	bsd_echo=false
	no_rewind_example=/dev/nrmt0h
	rewind_example=/dev/rmt0h
	link_test_opt=-L
elif [ -r /usr/bin/sh5 -o X"${bin_type}" = Xmipsel ]; then
	dec=y
	svrc=n
	if [ -r /bin/machine ]; then
		# Should be a DEC box running Ultrix
		myarch=dec`machine`
		if [ $myarch = "decmips" ]; then
			myarch=decsystem
		fi
	else
		if [ X"$bin_type" = Xmipsel ]; then
			myarch=decsystem
		else
			myarch=decvax
		fi
	fi
	bsd_echo=false
	no_rewind_example=/dev/nrmt0h
	rewind_example=/dev/rmt0h
	default_bin=/usr/etc
elif [ X${uname} = XHP-UX ]; then
	if [ "X`uname -r | grep 10\.`" != "X" ]; then
		myarch=hp-pa
		default_bin=/opt/networker/bin
		man=/usr/share/man
		svrc=y
		hpux_10=y
	elif [ "X`uname -r | grep 9\.`" != "X" ]; then
		if [ X"$bin_type" = Xs200 ]; then
			myarch=hp-mc68k
		else
			myarch=hp-pa
		fi
		default_bin=/usr/networker/bin
		svrc=n
		hpux_9=y
	else
		echo "ERROR: Cannot determine base machine type for \`${host}'."
		exit 1
	fi
	hpux=y
	df="df -l"
	fmt="cat"
	no_rewind_example=/dev/rmt/0mn
	rewind_example=/dev/rmt/0m
	bsd_echo=false
elif [ -f /bin/hinv ]; then
	myarch=sgi
	sgi=y
	df="df -l -k"
	fmt=/usr/sbin/fmt
	no_rewind_example=/dev/nrtape
	rewind_example=/dev/tape
	bsd_echo=false
	man=/usr/share/man/a_man
	sourcemandir=catman
elif [ "X${uname}" = XAIX ]; then
	myarch=aix6000
	aix=y
	svrc=n
	df="df -I"
	link_test_opt=-L
	no_rewind_example=/dev/rmt0.1
	rewind_example=/dev/rmt0
	bsd_echo=false
elif [ "X${uname}" = XUNIX_SV ]; then
	myarch=unixware
	df="/bin/df -l -k"
	no_rewind_example=/dev/rmt/ntape1
	rewind_example=/dev/rmt/ctape1
	if [ -f /usr/ucb/echo ]; then
		bsd_echo=true
	else
		bsd_echo=false
	fi
	man=/usr/flib/books/man
	sectiondir=cat
	sourcemandir=catman
elif [ -x /usr/bin/scosh ]; then
	sco=y
	myarch=sco
	df="df -l -k"
	no_rewind_example=/dev/nrStp0
	rewind_example=/dev/rStp0
	deffpy=/dev/rdsk/f03ht
	bsd_echo=false
	fmt=cat
	sectiondir=cat
	sourcemandir=catman
elif [ X${bin_type} = XiAPX ]; then
	# must go AFTER the other iAPX architectures like SCO
	myarch=sequent
	no_rewind_example=/dev/tx0n
	rewind_example=/dev/tx0
	bsd_echo=false
	fmt="/bin/cat"
elif [ -r /usr/bin/arch ]; then
	sun=y
	svrc=n
	myarch=`arch`
	default_bin=/usr/etc
	#
	# Since we explicitly set the PATH ourselves w/o the sys5 stuff,
	# we shouldn't get the sys5 echo behaviour from the shell.  We
	# don't use /bin/echo here because the sun386 /bin/echo will drop
	# lines after seeing a return within double quotes!
	#
	no_rewind_example=/dev/nrst8
	rewind_example=/dev/rst8
	df="df -t 4.2"
	id="whoami"
	sourcemandir=man
else
	echo "ERROR: Cannot determine base machine type for \`${host}'."
	exit 1
fi

# This is to allow installing with a default file generated by a form
# before dbmi_ize is run so that Compaq can use their front end to dbmi_ize.
if [ -x /tmp/default.cfg ]; then
. /tmp/default.cfg
fi
} # end findarch

# For pkgadd systems, check if the prerequisite is installed.
# exits with a failure message if the prerequisite is not installed.
#
# INPUT:
#	prereq=	the name of the predicate package
checkpred()
{
	_ans=`pkginfo | grep ${prereq}`

	if [ -z "${_ans}" ]; then
		echo "
FATAL ERROR: This package cannot be installed until after the ${prereq}
package has been installed." | ${fmt}
		exit 1
	fi
}

setecho()
{
umask 22


if [ $bsd_echo = true ]; then
	echo_n()
	{

		echo -n "$*"
	}
else
	echo_n()
	{

		echo "$*\c"
	}
fi
} # end setecho

#
# explain function
#
# Print out a message explaining this program
#
explain()
{
	echo "
The ${myname} program will install or remove ${productname}
software.  The \`-i' flag to ${myname} will install ${productname},
the \`-r' flag to ${myname} will remove ${productname} from the system,
and the \`-u' flag will do an \"upgrade\" removal leaving the
${productname} configuration files and databases files.
If none of the \`-i', \`-r' or \`-u' flags are specified,
${myname} will automatically select the mode of operation.
By default, all commands that modify the file system will be shown.
The \`-q' flag to ${myname} will suppress this information.
The \`-n' flag will make ${myname} skip operations that modify the file
system and can be used to determine what the ${productname}
installation or removal process would entail.
The ${myname} program will provide interactive prompts with
normal defaults.  Unless you have any special installation requirements,
you can simply hit [Return] at each prompt.  The ${myname} program
is reentrant and can be interrupted and restarted.  Consult the
${productname} documentation for further details." | ${fmt}

}

#
# usage function - prints out a usage message and exits
#
usage()
{
	explain
	echo ""
	echo "usage: ${myname} [ -i | -r | -u ] [ -kmnqxv ]"
	exit 1
}

#
# dirname function
#
# prints a non-NULL directory name for the given file
# Normally this is a system command, but it is optional on many systems.
#
dirname()
{
	expr X"${1}" : X'\(.*\)/.*' \| X"${1}" : X'\(/\)[^/]*$' \| '.'
}

#
# qecho
#	quite echo: if the qflag is no, then echo all arguments,
#	otherwise don't.
#
qecho()
{
	if [ ${qflag} = n ]; then
		echo "$*" | ${fmt}
	fi
}

#
# doexec function
#
# inputs:
#	exec=		string to execute
#	errquit=	if !"n", then take error exit on bad return value
#	nflag=		if "y", then don't really do it
#
# outputs:
#	execstatus=	return value from the eval'ing ${exec}
#	errquit=	${errquitdefault}
#
# Execute the "exec" string, echo'ing the string if qflag
# is not set and not really doing it if nflag is set.
#
doexec()
{
	# protect ourselves from keyboard interrupts if requested
	if [ ${nflag} != y ]; then
		eval "${exec}"
		execstatus=$?
		if [ ${execstatus} -ne 0 -a X${errquit} != Xn ]; then
			echo_n \
"${myname}: unexpected return code ${execstatus}"
			if [ ${qflag} = y ]; then
				echo " trying to execute:"
				echo "${exec}"
			else
				echo ""
			fi
			kill -2 $$
		fi
	else
		execstatus=0		# assume it would have worked...
	fi
	errquit=${errquitdefault}
}

#
# mmkdir function
#
# Function to recursively make directories, all the way back to /,
# if necessary.  Some systems provide a mkdir that does this, but
# it is not a standard feature.
#
# inputs:
#	$1	directory to create
#
mmkdir()
{
	case X"${1}" in
	X | X/ )
		;;
	* )
		if [ ! -d "${1}" ]; then
			mmkdir "`dirname ${1}`"
			exec="mkdir ${1}"
			doexec
		fi
		;;
	esac
}

#
# local_dir function:
#
# Generic function to test if (potential) directory is local
# inputs:
#	$1 directory to test for localhood
# outputs:
# 	result=		true or false; true if directory appears to be local
#
local_dir()
{
	_d="$1"
	while [ ! -d "${_d}" ]; do
		_d=`dirname "${_d}"`
	done
	if [ ${sco} = y ]; then
		mntfsname="`df "${_d}" 2>/dev/null \
			| sed -e 's/(/( /' \
			| awk '{ print $3 }'`"
	else
		mntfsname="`df "${_d}" 2>/dev/null \
			| grep '^[^ 	]' \
			| grep -v '^Filesystem' \
			| awk '{ print $1 }'`"
	fi
	if expr "$mntfsname" : '^[^/].*:/'> /dev/null 2>&1; then
		result=false
	else
		result=true
	fi
}
#! /bin/sh
#
# @(#)askiodbmi.sh 1.2 96/04/23 Copyright (c) 1996, Legato Systems, Inc.
#
# All rights reserved.
#
# Rules:
# 1) Store user responses in ${NSRREQUEST} file for use by other scripts.
# 2) Other scripts must source the ${NSRREQUEST} for their input.
# 3) The flow of the questions must be as deterministic as possible so
#    that automated installs can be done with response files generated
#    by pkgask.

#
# Return the name of a local directory with the most space
# that is writable by root
#
# outputs:
#	bigdir= directory path
#	bigdirs= the 4 mount points with the most free space...
#
find_bigdir()
{
	x4=
	bigdirs=
	bigdir=
	OIFS="$IFS"
	if [ ${sco} = y -o ${hpux} = y ]; then
		output="`${df} 2>/dev/null | sort -nr -t: +1 \
				| awk '/\(\/dev\// { if ( $1 != "/tmp" ) print sprintf("%-30s:%8.8s", $1, $4) }'`"
	elif [ ${sgi} = y ]; then
		output="`${df} 2>/dev/null | sort -nr +4 -6 \
				| awk '/^\/dev/ { if ( $7 != "/tmp" ) print sprintf("%-30s:%8.8s", $7, $5) }'`"
	else
		output="`${df} 2>/dev/null | sort -nr +3 -5 \
				| awk '/^\/dev/ { if ( $6 != "/tmp" && $6 != "/dev/fd" ) print sprintf("%-30s:%8.8s", $6, $4) }'`"
	fi
	# don't loose the white space -- remove space from the internal field
	# separator list (only tab and ret)
	IFS='
	'
	# use tabs, not spaces, in the following line...
	for	i	in	$output
	do
		# trim the spaces and :
		IFS=":${OIFS}"
		set -- $i
		# go back to the original
		IFS="${OIFS}"
		if [ -w "${1}" ]; then
			x4="${x4}x"	# cheap way of counting to 4.
			if [ -z "${bigdir}" ]; then
				bigdir="${1}"
			fi
			bigdirs="${bigdirs}
${i}";
			if [ ${x4} = "xxxx" ]; then
				return	# we've got four now...
			fi
		fi
	done
}

#
# verify that we can write in ${1}
#
testdir()
{
	_dir="${1}"
	while [ ! -w "${_dir}" ];
	do

		echo "
${myname}: ERROR, the permissions on ${_dir} do not allow root to write in it"
		prompt="Should I change the permissions on ${_dir} to allow root write access?"
		default="yes"
		yesno
		if [ $result = y ]; then
			chmod u+w "${_dir}" > /dev/null 2>&1
		else
			echo "
${myname}: ERROR, this process cannot complete.  Please rerun ${myname} when you
have either fixed the problem, or decided to use another directory for
the ${productname} ${test_objects}." | ${fmt}
			echo "
${myname}: aborting..."
			exit 2
		fi

	done
	touch "${_dir}/nsr$$" > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "
${myname}: ERROR, ${_dir} is not writable by root!

This may be caused by ${_dir} being a remote filesystem where this
machine doesn't have write permission privledges for root. See the
${productname} 'Read This First' document for further instructions on how
to use a remote filesystem for the ${productname} ${test_objects}.

After correcting this problem please run ${myname} again.

${myname}: aborting..."
		rm -f "${_dir}/nsr$$"
		exit 1
	fi
	set X `ls -l ${_dir}/nsr$$`
	if [ "$4" != "root" ]; then
		echo "
${myname}: FATAL ERROR, files created in ${_dir} by root do not end up being owned by root!

This can be caused by ${_dir} being a remote filesystem where the
root user id from this machine is being mapped to user \`$4' on the
remote machine.

After correcting this problem please run ${myname} again.

${myname}: aborting..."
		rm -f "${_dir}/nsr$$"
		exit 1
	fi
	rm -f "${_dir}/nsr$$"
}

#
# rmdir_if_empty
#
# remove the specified directory, if it is empty.  If it really is a symlink
# to a empty directory, remove both.
#
#	$1 	-- the directory to remove (if empty)
#
rmdir_if_empty_ask()
{
	rie_dir="$1"
	rie_ls_output="`ls ${rie_dir} 2>&1`"

	if [ -z "${rie_ls_output}" ]; then
		# directory is now empty, nuke it
		prompt="${rie_dir} is empty, remove it"
		default="yes"
		yesno
		if [ ${result} = n ]; then
			qecho "	Skipping removal of ${rie_dir} directory"
		else
			# check if directory to be removed is a symlink
			rie_ls_output="`ls -ld ${rie_dir}`"
			case "$rie_ls_output" in
			l* )
				# remove what it points to
				exec="rmdir `cd ${rie_dir}; /bin/pwd`"
				errquit=n
				doexec
				;;
			esac

			exec="rm -rf ${rie_dir}"
			doexec
		fi
	fi
}

#
# isupgrade function - determine whether this will be an upgrade
# removal or a complete removal.
#
isupgrade()
{
qecho ""
qecho "WARNING: if you answer [yes] to the following question the entire"
qecho "contents of the ${nsrhome} directory will be removed. This"
qecho "includes the online client index files and server media index"
qecho "file. You should only answer [yes] if you want to completely"
qecho "remove ${productname} from the system."

prompt="Do you want to remove ${productname} configuration and database files"
	default="no"
	yesno
	if [ X${result} = Xn ]; then
		upgrade=true
	fi
}

#
# Process arguments
#
processargs()
{
while [ $# -gt 0 ]; do
	case "$1" in
	-i)
		if [ ! -z "${op}" ]; then
			usage
		fi
		op=install
		shift
		;;
	-r)
		if [ ! -z "${op}" ]; then
			usage
		fi
		op=remove
		shift
		;;
	-u)
		if [ ! -z "${op}" -a "${op}" != remove ]; then
			usage
		fi
		op=remove
		upgrade=true
		shift
		;;
	-c)
		server=n
		shift
		;;
	-s)
		server=n
		shift
		;;
	-k)
		kflag=y
		shift
		;;
	-m)
		mflag=n
		shift
		;;
	-x)
		xflag=y
		shift
		;;
	-v)
		set -x
		shift
		;;
	-n)
		nflag=y
		shift
		;;
	-q)
		qflag=y
		shift
		;;
	-F)
		# allow *unsupported* install of server software
		Fflag=y
		shift
		;;
	-p)
		pflag=y
		shift
		;;
	-??*)
		# rip apart the concatenated argument, reset 'em, and reloop.
		first_opt="`expr substr "$1" 2 1`"
		rest_opts="`expr substr "$1" 3 255`"
		shift
		set -- "-$first_opt" "-$rest_opts" $*
		;;
	*)
		usage
		;;
	esac
done

#
# If not running in "do nothing mode" (or not debugging),
# insist on being run by the Super user.
#
whoiam=`${id} | grep root`
if [ ${nflag} != y -a ${xflag} != y -a -z "$whoiam" ]; then
	echo ""
	echo "FATAL ERROR: ${myname} must be run by the Super user!"
	exit 1
fi
} # end processargs

# yesno function
#
# Generic function to get yes/no answer
#
# inputs:
#	prompt=		string to prompt with (excluding default value)
#	default=	name of default value if no response
#
# outputs:
#	result=		'y' or 'n'
#
yesno()
{
	qecho ""
	if [ $nflag = y ]; then
		notreally="(not really) "
	else
		notreally=
	fi
	while true
	do
		if [ -z "${default}" ]; then
			echo_n "${prompt}${notreally}? "
			read ans
		elif [ ${qflag} = n ]; then
			echo_n "${prompt} ${notreally}[${default}]? "
			read ans
		else
			ans=y
		fi
		if [ "X${ans}" = X ]; then
			result="${default}"
		else
			result="${ans}"
		fi

		if [ `expr "X${result}" : 'X[yY]'` -ne 0 ]; then
			result=y
			break
		elif [ `expr "X${result}" : 'X[nN]'` -ne 0 ]; then
			result=n
			break
		else
			echo "Please respond \`yes' or \`no'"
		fi
	done
}

#
# verifycontinue
#
# Verify whether or not this program should continue
#
# inputs:
#	continuestr=	string to prompt with
#	default=	name of default value if no response
#
verifycontinue()
{
	echo "${continuestr}"
	prompt="Do you wish to continue with ${myname}"
	yesno
	if [ ${result} = n ]; then
		kill -2 $$
	fi
}

#
# verifydir function
#
# Generic function to verify or create a directory
#
# inputs:
#	prompt=		string to prompt with (excluding default value)
#	default=	name of default value
#	op=		"install" to create dir if it doesn't exist
#	local_only=	if "yes" filesystem must be local
# outputs:
#	result=		resultant confirmed value
#
verifydir()
{
	qecho ""
	while true
	do
		if [ -z "${default}" ]; then
			echo_n "${prompt}? "
			read ans
		elif [ ${qflag} = n ]; then
			echo_n "${prompt} [${default}]? "
			read ans
		else
			ans=
		fi
		if [ ! -z "${ans}" ]; then
			_res="${ans}"
		else
			if [ -z "${default}" ]; then
				continue
			fi
			_res="${default}"
		fi

		# verify that the file exists of the given type
		while true; do
			# it might be SingleServer; force local
			if [ "X${local_only}" = "Xyes" ]; then
				local_dir ${_res}
				if [ ${result} = false ]; then
					qecho "
Directory \`${_res}' is within a remote filesystem.
The directory must be within a local filesystem!"
					break
				fi
			fi
			if [ -d "${_res}" ]; then
				if [ -w "${_res}" ]; then
					result=${_res}
					return
				fi
				echo "\`${_res}' is not writable"
				break
			fi
			if [ ${op} = install ]; then
				result=${_res}
				return
			else
				echo "\`${_res}' does not exist"
				break
			fi
		done
	done
}

#
# testfiles function
#	Check if any files on ${list} exist in ${1}, if so
#	ask if they can be overwritten, exit if user says "no".
#
# input:
#	$1:	the directory to test
#	$list:	the files to check for in that directory
#
testfiles()
{
	_dir="${1}"
	_list=""

	for file in ${list}
	do
		if [ -f ${_dir}/${file} ]; then
			_list="${_list} ${file}"
		fi
	done

	case "X${_list}" in
	X)	;;
	*)	echo ""
		echo "The following files in ${_dir} will be overwritten:"
		echo ${_list} | ${fmt}
		continuestr=
		default="no"
		verifycontinue
		;;
	esac
}

#
# select_one_of function
#
# Let the user select from a set of values.
#
# inputs:
#	prompt=		string to prompt with (excluding default value)
#	default=	name of default value
#	selections=	space separated set of values
#	gb=		separate string for "8mm 5GB" hack
#
# outputs:
#	result=		selected value
#
select_one_of()
{
	result=""
	while true
	do
		# in quiet mode we just take the default if there is one
		if [ ${qflag} = n -o "X${default}" = X ]; then
			if [ "X${default}" = X ]; then
				qecho ${prompt}
				echo_n \
`echo "(one of \"${selections} ${gb}\")?" | ${fmt}`" "
			else
				qecho ${prompt}
				echo_n \
`echo "(one of \"${selections} ${gb}\") [${default}]?" | ${fmt}`" "
			fi
			read _ans
		else
			_ans=
		fi
		if [ -z "$_ans" ]; then
			_ans="${default}"
		fi
		for value in ${selections}
		do
			case "$_ans" in
				"$value" )
					result="${value}"
					break
				;;
			esac
		done
		if [ ! -z "${result}" ]; then
			break
		fi
		if [ "X${gb}" = "X${_ans}" ]; then
			result="${gb}"
			break
		fi
		echo "\`${_ans}' is not one of \"${selections} ${gb}\"; please try again"
	done
}

#
# getdevices function
#
# outputs:
#	none
#
getdevices()
{
	qecho ""
	if [ ${aix} = y ]; then
		echo "
The following is a list of the tape devices available on your system:" | ${fmt}
		device_count=`lsdev -C -c tape | wc -l`
		lsdev -C -c tape
		if [ ${device_count} = 1 ]; then
			dev=/dev/`lsdev -C -c tape -r name`.1
			type=`lsdev -C -c tape -r type`
			case ${type} in
			    9trk )
				type=himt
				;;
			    150mb )
				type=qic
				;;
			    ost )
				type=4mm
				;;
			    8mm5gb )
				type="8mm 5GB"
				;;
			    * )
				# hope it is correct
				;;
			esac
			echo "
The only tape device on your system is ${dev} of type ${type}."
			prompt=\
"Do you wish to use this as your ${productname} device"
			default="yes"
			yesno
			if [ ${result} = y ]; then
				if [ "${type}" = "8mm" ]; then
					chdev -l `lsdev -C -c tape -r name` -a extfm='yes'
				fi
				DEVLIST="$DEVLIST ${dev} ${type}"
				return
			fi
		fi
		qecho "
The name needed by ${productname} is usually \`/dev/' plus the name
of the device as given the first column of the above table, followed by
\`.1' to indicate a no-rewind device name."
	fi
	if [ ${sgi} = y ]; then
		echo "
The following is a list of the tape devices available on your system:" | ${fmt}
		hinv -c tape
	fi
	if [ ${demo} = y ]; then
		echo "
Enter the tape or disk device that is going to be used by the
${productname} server.  Use the no-rewind name for the tape device
(i.e., use ${no_rewind_example} instead of ${rewind_example})." | ${fmt}

		_prompt="
Enter device name:"

	else
		echo "
Enter the tape or disk device(s) that are going to be used by the
${productname} server. Use the no-rewind name for each tape device
If you do not choose a device a default device will be created for you.
(i.e., use ${no_rewind_example} instead of ${rewind_example}).
" | ${fmt}

		_prompt="
Enter device name ([Return] if no more):"
	fi

	gotadev=true
	while true
	do
		echo_n "${_prompt} "
		read dev
		if [ "X${dev}" = "X" -o "X${dev}" = "X." ]; then
			type="8mm 5GB"
			if [ ${dec} = y ]; then
				type="tk50"
			fi
			dev=${no_rewind_example}
			if [ ${gotadev} = false ]; then
				echo "
No backup devices have been specified.
${myname} needs to know which tape device(s) to set up for use
by the ${productname} server when files are being saved.
If no devices are specified here,
${productname} will be set up to use the default
tape device \`${dev}' with type \`${type}'.
" | ${fmt}
				prompt=\
"Use the default tape device configuration"
				default="yes"
				yesno
				if [ ${result} = n ]; then
					continue
				fi
				if [ ${aix} = y ]; then
					chdev -l rmt0 -a extfm='yes'
				fi
				DEVLIST="$DEVLIST ${dev} ${type}"
			fi
			# all done
			echo "DEVLIST=\"${DEVLIST}\"" >> ${NSRREQUEST}
			return
		fi
		_verify=n

		if [ ${aix} = y ]; then
			device_prefix=/dev/
			device_preflength=6
		else
			device_prefix=/dev/n
			device_preflength=7
			if [ ${myarch} = "solaris" -o ${myarch} = "sol86" -o \
				${myarch} = "solppc" ]
			then
				device_prefix=/dev/rmt
				device_preflength=9
			fi
		fi
		echo ${dev} | egrep '[:;]' > /dev/null
		if [ $? -ne 1 -o `echo "X${dev}" | wc -w` -ne 1 -o \
		    `expr "X${dev}" : X${device_prefix}` \
			-lt ${device_preflength} ]; then
			echo \
"\`${dev}' does not look like a typical tape device name
(it does not start with \`${device_prefix}' or is more than one word)" | ${fmt}
			_verify=y
		elif [ ! -c "${dev}" -a ! ${link_test_opt} "${dev}" ]; then
			echo \
"\`${dev}' is not a character special device on \`${host}'"
			_verify=y
		fi

		if [ $_verify = y ]; then
			prompt="Is this the device name you really wanted"
			default="no"
			yesno
			if [ $result = n ]; then
				continue
			fi
		fi

		prompt="Select device type for ${dev}:"
		if [ ${aix} = y ]; then
			default="8mm"
			basedev=`echo "${dev}" | awk '($2 ~ "dev") {print $3}' FS="[\.\/]" -`
			type=`lsdev -C -l ${basedev} -r type`
			case ${type} in
			    9trk )
				type=himt
				;;
			    150mb )
				type=qic
				;;
			    ost )
				type=4mm
				;;
			    8mm5gb )
				type="8mm 5GB"
				;;
			    8mm )
				chdev -l ${basedev} -a extfm='yes'
				;;
			    * )
				# hope it is correct already?
				;;
			esac
			echo "Device ${basedev} is of ${productname} type: ${type}"
		else
			if [ ${demo} = y ]; then
				default=""
			else
				default="4mm"
			fi
			selections="8mm 4mm himt 3480 qic optical dlt"
			gb="8mm 5GB"
			echo "Select the device type for \"${dev}\"."
			echo "The possible types are:"
			echo "	8mm 5GB	- 8mm double density tape."
			echo "	8mm 	- 8mm tape."
			echo "	4mm	- 4mm DAT tape."
			echo "	himt	- 1/2 inch magnetic tape."
			echo "	3480	- 1/2 inch cartridge tape."
			echo "	qic	- 1/4 inch data cartridge."
			echo "	optical	- Optical Disk."
			echo "	dlt	- Digital Linear Tape."
			echo "	vhs	- VHS Format Tape."
			echo ""
			select_one_of
			type=${result}
		fi
		echo ""

		# add device to the ${file} file
		DEVLIST="$DEVLIST ${dev} ${type}"
		if [ ${demo} = y ]; then
			break
		fi
	done
	echo "DEVLIST=\"${DEVLIST}\"" >> ${NSRREQUEST}
}

#
# See if a mode of operation was specified.
# If not, try to figure it out ourselves.
# If argv[0] has install or remove it in, we
# do that operation, otherwise we use the existence
# of ${flagfile} to determine mode of operation.
#
findoperation()
{
verifymode=n
if [ X${op} = X ]; then
	if [ `expr X${myname} : '.*install'` -ne 0 ]; then
		op=install
	elif [ `expr X${myname} : '.*remove'` -ne 0 ]; then
		op=remove
	elif [ ! -f ${default_bin}/${flagfile} ]; then
		op=install
		verifymode=y
	else
		op=remove
		verifymode=y
	fi
fi
} # end findoperation

#
# Verify the mode of operation
#
verifyoperationmode()
{
while true; do
	if [ ${op} = install ]; then
		preposition="on"
	elif [ ${upgrade} = true ]; then
		preposition="for upgrade from"
	else
		preposition="from"
	fi
	prompt="${op} ${productname}"
	if [ ${server} = UNKNOWN ]; then
		qecho "
${myname} is about to ${prompt} ${preposition} machine \`${host}'."
	else
		if [ ${server} = y ]; then
			qecho "
${myname} is about to ${prompt} server software
${preposition} machine \`${host}'."
		else
			qecho "
${myname} is about to ${prompt} client software
${preposition} client machine \`${host}'."
		fi
	fi
	if [ ${verifymode} = n ]; then
		break
	fi

	default="yes"
	prompt="Ready to ${prompt}"
	yesno
	if [ ${result} = y ]; then
		break
	fi

	if [ ${op} = install ]; then
		op=remove
		preposition="for"
	else
		op=install
		preposition="on"
	fi
	prompt=\
"Do you wish to ${op} ${productname} ${preposition} machine \`${host}' instead"
	default="yes"
	yesno
	if [ ${result} = n ]; then
		echo "${myname}: ERROR, cannot continue with this operation"
		echo "${myname}: aborting..."
		exit 1
	fi
done
} # end verifyoperationmode

installprolog()
{
if [ ${op} = install ]; then
	#
	# Installing NSR - make sure that we are
	# in the directory that this executable is in.
	#
	dir="`dirname "$0"`"
	cd ${dir}

	#
	# Set our path to include the binaries for this architecture
	# so that we can use uasm to install the binaries for various
	# versions of Unix and maintain all modes, owners, and times.
	#
	PATH="`pwd`/${myarch}:${PATH}"
	export PATH

	if [ ${sun} = y ]; then
		#
		# Verify that the machine is running 4.1.x.                #
		if [ "X${uname}" != X ]; then
			osrel=`uname -r`
		fi
		if [ "X${osrel}" = X ]; then
			osrel=`dmesg - | egrep 'SunOS.*Release' | tail -1 | \
			sed -e 's/.*Release \([0-9\.]*\).*/\1'/`
		fi
		if [ "X${osrel}" = X -a -f /etc/motd ]; then
			osrel=`egrep 'SunOS.*Release' /etc/motd | \
			tail -1 | sed -e 's/.*Release \([0-9\.]*\).*/\1'/`
			if [ "X${osrel}" = X -a -f /etc/motd ]; then
				osrel=`egrep 'SunOS 5' /etc/motd | \
				tail -1 | sed -e 's/.*SunOS \([0-9\.]*\).*/\1'/`
			fi
		fi
		if [ "X${osrel}" = X ]; then
			osrel=`egrep 'SunOS.*Release' /usr/adm/messages | \
			tail -1 | sed -e 's/.*Release \([0-9\.]*\).*/\1'/`
		fi

		if [ "X${osrel}" = X ]; then
			echo "WARNING: unable to determine OS release level."
		elif [ `expr "X${osrel}" : "X5."` -eq 3 ]; then
			echo "Running SunOS binaries on Solaris 2.x"
		elif [ `expr "X${osrel}" : "X4.1"` -lt 4 ]; then
			echo \
"ERROR: Machine \`${host}' is running SunOS Release ${osrel},
${productname} ${RELEASE} requires a 4.1.x SunOS Release."
			exit 1
		fi
	else
		#
		# XXX - should add tests for other OS installs here.
		# For now, assume all is ok...
		#
		true
	fi

else
	# remove - assume "server mode" if ${nsrhome} exists
	# and nsrd exists in the same directory as nsr_ize
	server="n"
fi
} # end installprolog

#
# Determine which servers can talk to nsrexecd
#
# outputs:
#	result=		list of servers for nsrexecd, with appropriate "-s"s
#
ask_servernames()
{
	if [ $server = y ]; then
		servermsg=\
"  By default, only this server will be allowed access.  Enter the
value 'all' to allow all servers to back up this computer."
		result=" -s localhost"
	else
		servermsg=""
		result=""
	fi

	qecho "
The nsrexecd program restricts access to a select set of
${productname} servers.  Please enter the names of each computer running
a ${productname} server that will back up this computer, one name at a time.
If a computer has more than one network interface, please enter each
interface's name (one at a time).${servermsg}
"
	_next="first"
	while true; do
		echo_n "Enter the ${_next} ${productname} server's name [no more]: "
		read _ans
		if [ X"${_ans}" = X ]; then
			break
		fi
		if [ `expr X"${_ans}" : ".* "` -ne 0 ]; then
			echo "Please enter one name at a time."
		elif [ X"${_ans}" = Xall ]; then
			result=""
			break
		else
			result="${result} -s ${_ans}"
			_next="next"
		fi
	done

	if [ X"${result}" = X ]; then
		if [ $server = n -o X"${_ans}" = Xall ]; then
			qecho "Allowing access to all ${productname} servers."
		fi
	fi

	echo "NSREXECD=\"${result}\"" >> ${NSRREQUEST}
}

#
# Stop the currently running ${productname} daemons (if really needed).
# If we got a -k flag or are running in demo mode, don't bother asking
# if the daemons are running, just shut 'em down without asking.
#
stopdaemons()
{
	find_daemons

	#
	# If there are no running daemons, just return
	#
	if [ -z "${daemonsrunning}" ]; then
		return 0
	fi

	#
	# Prompt if we are not running in demo mode.
	#
	if [ ${kflag} = n -a ${demo} = n ]; then
                qecho "
The following ${productname} programs are currently
running and must be shutdown before continuing:"
		if [ ${qflag} = n ]; then
			echo "${daemonsrunning}"
		fi
		prompt=\
"Shutdown currently running ${productname} programs"
		default="yes"
		yesno
		if [ ${result} = n ]; then
			echo "ERROR: cannot continue while daemons are running..."
			exit 1
		fi
	fi

	if [ ${nflag} = y ]; then
		# assume it would have worked
		execstatus=0
	else
		do_shutdown -a -q
		execstatus=$?
	fi
	if [ ${execstatus} -ne 0 ]; then
		echo \
"ERROR: Can't kill ${productname} daemons, giving up"
		exit 1
	fi
} # end stopdaemons

#
# askbuildindexdirs - ask where the indexdirs should be
#
askbuildindexdirs()
{
test_objects="indexes"
if [ ${op} = install ]; then
	qecho "
To set up a ${productname} server, you need to supply a
directory with enough free space to maintain all the on-line
save file indexing and media management information."
	qecho "
To set up a ${productname} client, you need to supply a
directory for the nsrexecd state file."
	result=
	while true; do
		find_bigdir
		if [ -d ${nsrhome} ]; then
			qecho \
"
Directory ${nsrhome} exists, it will be used as the default."
		fi
		if [ ! -z "${bigdirs}" ]; then
			prompt="
Below is a list of some of the filesystems, with their free space, which you
might consider:

$bigdirs

Directory to use for client and server information"
		else
			prompt="
Directory to use for client and server information"
		fi
		if [ "X${bigdir}" = "X/" ]; then
			bigdir=""
		fi
		if [ -d ${nsrhome} ]; then
			default=${nsrhome}
		else
			default="${bigdir}/nsr"
		fi
		# Ensure default is local.  Only test if default
		# already exists, cuz local_dir is slow on sco.
		if [ -d ${default} ]; then
			local_dir ${default}
			if [ ${result} = false ]; then
				default="${bigdir}/nsrdir"
			fi
		fi
		local_only=yes
		verifydir
		local_only=

		dir=${result}
		if [ `expr X${dir} : X/` -lt 2 ]; then
			echo \
"ERROR: \"${dir}\" needs to be specified as an absolute path"
			exit 2
		fi

		if [ ! -d ${dir} -a -r ${dir} ]; then
			echo "ERROR: \"${dir}\" is not a directory"
			exit 2
		fi
		echo "NSRHOME=${dir}" >> ${NSRREQUEST}
		break;
	done
fi
} # askbuildindexdirs

#
# requestmisc - Ask the misc install questions and cache the answers
# in shell variables.
# 
askmisc()
{
if [ ! -f ${nsrres} -a "${op}" = "install" -a "${server}" = y ]; then
	getdevices
fi

if [ "${op}" = install ]; then
	if [ "${server}" = n -o "${pflag}" != y ]; then
		ask_servernames
	fi
fi

if [ "${op}" = install ]; then
	prompt="Start ${productname} daemons at end of install"
	default="yes"
	yesno
	echo "DODAEMONS=${result}" >> ${NSRREQUEST}
fi
}

#
# requestmain - Driver function to ask all questions, and set environment.
#
requestmain()
{
if [ "${server}" != y -o "${pflag}" != y ]; then
	askbuildindexdirs
fi
askmisc
}
# @(#)rqdbmi.sh 1.1 96/04/17 Copyright (c) 1996, Legato Systems, Inc.
#
# This file will drive the request script for a pkgadd NetWorker DBMI.
#
# Rules:
# 1) There should be mostly calls to shell functions that exist in
#    other install remove shell function files.
# 2) The shell functions called from this file can store shell
#    variables in ${NSRREQUEST} for use during other install phases.
#
NSRREQUEST=${NSRREQUEST-/tmp/nsrrequest}
rm -f ${NSRREQUEST}

installinit $*

server=n
op=install
pflag=y
skip_execs=true

copyright
findarch
setecho
