#! /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.
#
# @(#)arch.sh 6.1 95/12/15 Copyright (c) 1995, Legato Systems, Inc.
#
# Platform specific install remove shell functions.
#
# Rules:
# 1) There should not be any user input required by any functions
#    in this file.
#

# Do SCO specific SLS checking.
# Exits on error.
#
verifyscosls()
{

	if [ ${sco} = n -o X${server} != Xy -o X${op} != Xinstall ]; then
		return 0		# for SCO server installs only
	fi
	errquit=n
	exec="swconfig | grep \"Enhanced SCSI Tape Drivers\" 2>&1 >/dev/null"
	doexec
	errquit=y
	if [ ${execstatus} -eq 0 ]; then
		return 0		# we found what we are looking for
	fi

	ver=`uname -X | grep Release | cut -c15-17`
	if [ `expr "X${ver}" : 'X4.[012]'` -ne 4 ]; then
		return 0		# not an interesting version for us
	fi

	# Print an error message and exit
	cat << EOF

*** Warning: You must install SLS UDO376 "Enhanced SCSI Tape Drivers"
before installing NetWorker.  You can obtain SLS UDO376 from SCO
Technical Support.  See "Release Notes Legato NetWorker for UNIX
SCO Version" for details. ***

EOF
	exit 1
} # end verifyscosls

# Run arch dependant script to do things like build kernel on SCO
# install apps_default, set up icon on desktop...
myarchize()
{
if [ ${op} = install ]; then
	if [ -f ${myarch}/${myarch}_ize ]; then
		exec=". ${myarch}/${myarch}_ize"
# This is needed to tell myarch_ize if we are a server install
		export server
		doexec
# Added to support pkg installs
	elif [ -f /usr/sbin/${myarch}_ize ]; then
		exec=". /usr/sbin/${myarch}_ize"
		export server
		doexec
	elif [ -f /usr/bin/${myarch}_ize ]; then
		exec=". /usr/bin/${myarch}_ize"
		export server
		doexec
	fi
fi
} # end myarchize
# @(#)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
}
# @(#)prpoinrm.sh 6.8 97/01/28 Copyright (c) 1997, Legato Systems, Inc.
#
# All NetWorker install and remove shell functions that are
# independent of NetWorker binaries should go here.
# (e.g. configuring the /nsr directory, editing the rc scripts,
#  editing /etc/rpc, ...)
#
# Rules:
# 1) No user interaction can be requested, it should be driven by
#    sourcing the ${NSRREQUEST} file.
# 2) There should be no dependence on NetWorker binaries.
#    (e.g. uasm, nsr_shutdown) 
#

#
# add_device function
#
#       Append a resource descriptor for a device to a file
#
# inputs:
#
#	op = "install" or "remove"
#       DEVLIST = array of device name device type pairs
#	source ${NSRREQUEST} to define DEVLIST 
#
add_device()
{
	if [ ${op} = install ]; then
		file=${nsrres}
		if [ ! -f "${file}" ]; then
			# Avoid a null set command. Prepend X and shift it off.
			# A null set command prints the current environment.
			set X `echo ${DEVLIST}` 
			shift
			while [ ! -z "$1" ]; do
				device="$1"
				type="$2"
				# fix up device type strings with spaces
				if [ "$3" = "5GB" ]; then
					type="${type} $3"
					shift
				fi
				shift
				shift
				echo "type: NSR device;" >> ${file}
				echo "media type: ${type};" >> ${file}
				echo "name: ${device};" >> ${file}
				echo "" >> ${file}
			done
		fi
	fi
} # end add_device

#
# Like rmdir_if_empty but don't ask first.
#
# Inputs:
#	$1 	-- the directory to remove (if empty)
#
rmdir_if_empty()
{
	rda_dir=$1
	if [ "X`ls ${rda_dir}`" = X ]; then
		# Directory is empty, remove it
		# check if directory to be removed is a symlink
		rie_ls_output="`ls -ld ${rda_dir}`"
		case "$rie_ls_output" in
		l* )
			# remove what it points to
			exec="rmdir `cd ${rda_dir}; /bin/pwd`"
			errquit=n
			doexec
			;;
		esac

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

#
# backoutfile function
#
# Backout nsr changes to a given file
#
# inputs:
#	file	= file to egrep thru
#	tfile	= tmp file created by stripping out nsr-izations of ${file}
#
# outputs:
#	filechange=	y if file changed
#			n if file is not changed
#
backoutfile()
{
	filechange=n
	if [ -f ${file}.prensr ]; then
		cmp -s ${tfile} ${file}.prensr
		cmpstatus=$?
		if [ ${cmpstatus} -eq 2 ]; then
			qecho "${myexecname}: error cmp'ing ${file} and ${tfile}"
			kill -2 $$
		elif [ ${cmpstatus} -eq 0 ]; then
			# the same, just use ${file}.prensr
			exec="mv -f ${file}.prensr ${file}"
			doexec
			filechange=y
			return		# all done
		else
			# files are different, warn the user
			qecho \
"${file} has changed since ${productname} was originally installed."
		fi
		qecho \
"	Removing ${productname} modifications from ${file}"
		exec="mv -f ${tfile} ${file}"
		doexec
		if [ -f ${file}.prensr ]; then
			exec="rm -f ${file}.prensr"
			doexec
		fi
		filechange=y
	else
		exec="mv -f ${tfile} ${file}"
		doexec
		filechange=y
	fi
}

# modfile function
#
# Scan $file for $searchstr.
#	If installing, and no match is found this function's behavior
#	depends upon the setting of ${insertmode}:
#		If ${insertmode} = "y", then ${insertcmd} is used to change
#		${file}.
#		If insertmode != y then contents of ${appendstr} are appended
#		to ${file}.
#
#	If removing, and a match is found,
#	use $sedcmd to strip out lines of a given type or range.
#
# inputs
#	op=		"install" or "remove"
#	file=		file to egrep thru and modify
#	searchstr=	string to egrep for to determine if already nsr-ized
#
# inputs when in install mode:
#	appendstr=	string to append to ${file} if ${searchstr} not found
#	insertcmd=	sed command to stick lines in file if in insert mode
#	insertmode=	y if lines are to be inserted in ${file}.
#
# inputs when in remove mode:
#	sedcmd=		sed command to use to remove changes if searchstr found
#
# outputs when in install mode:
#	filechange=	y if file changed
#			n if file is not changed
#
modfile()
{
	egrep "${searchstr}" ${file}	>/dev/null 2>&1
	grepstatus=$?
	filechange=n
	if [ ${grepstatus} -eq 2 ]; then
		echo "${myname}: could not read ${file}"
		kill -2 $$
	elif [ ${grepstatus} -eq 0 ]; then
		# ${searchstr} was found
		if [ ${op} = install ]; then
			qecho "	${file} already ${productized}"
			return
		fi
		# remove the lines in ${file} using ${sedcmd}
		tfile=/tmp/file.$$
		# copy so the new file has to correct permissions
		if [ "X${file}" != "X${_lastmod}" ]; then
			qecho "	Modifying ${file}"
			_lastmod="${file}"
		fi
		cp ${file} ${tfile}
		sed -e "${sedcmd}" ${file} > ${tfile}
		backoutfile
	else
		# ${searchstr} not found
		if [ ${op} = remove ]; then
			qecho "	${file} already ${deproductized}"
			return
		fi
		if [ "X${file}" != "X${_lastmod}" ]; then
			qecho "	Modifying ${file}"
			_lastmod="${file}"
		fi
		# For now, only keep a .prensr copy if debugging...
		if [ ${xflag} = y -a ! -f ${file}.prensr ]; then
			exec="cp ${file} ${file}.prensr"
			doexec
		fi
		if [ "${insertmode}" = "y" ]; then
			# apply ${insertcmd} to ${file}
			tfile=/tmp/file.$$
			# copy so the new file has the correct permissions
			cp ${file} ${tfile}
			sed -e "${insertcmd}" ${file} > ${tfile}
			backoutfile
			filechange=y
		else
			# Append the new data
			exec="cat >> ${file} << EOF
${appendstr}
EOF"
			doexec
			filechange=y
		fi
	fi
}

#
# nsr_done
#	Finish the install or removal process, updating or removing ${flagfile}
#
# inputs:
#	exdir=		directory where executables for this machine go
#	flagfile=	single file to be installed/removed
#	banner=		first part of a generic "everything ok" message
#	skip_execs=	don't install/remove anything if this is true
#
nsr_done()
{
if [ "${myarch}" != "solaris" ]; then
	file="${exdir}/${flagfile}"
	qecho ""
	if [ -f "${file}" -a X${skip_execs} = Xfalse ]; then
		if [ ${op} = install ]; then
			if [ ${nflag} = y ]; then
				qecho "	${file} already exists, overwriting (not really)..."
			else
				qecho "	${file} already exists, overwriting..."
			fi
			exec="rm -f ${file}"
			doexec
			exec="cp ${flagfile} ${file}"
			doexec
		else
			exec="rm -f ${file}"
			doexec
			rmdir_if_empty ${exdir}
			rmdir_if_empty "${nsrhome}"
		fi
	elif [ X${skip_execs} = Xfalse ]; then
		if [ ${op} = install ]; then
			exec="cp ${flagfile} ${file}"
			doexec
		else
			qecho "${file} does not exist"
		fi
	elif [ X${skip_execs} = Xtrue -a "${op}" = "remove" ]; then
		if [ ${myarch} = sco -o ${myarch} = solaris -o \
		     ${myarch} = sol86 -o ${myarch} = solppc ]; then
			if [ X${server} = Xy ]; then
				rmdir_if_empty "${nsrhome}"
			fi
		fi
	fi
fi
	if [ ${op} = install ]; then
		qecho \
"${banner} successfully installed on \`${host}'!"
	elif [ ${upgrade} = true ]; then
		qecho \
"${banner} successfully removed for upgrade from \`${host}'."
	else
		qecho \
"${banner} successfully removed from \`${host}'."
	fi
} # nsr_done

#
# check_setuid
#	Verify that the setuid files in the given directory look correct.
#	If not, issue a warning and give the operator a chance to bail out.
#
# inputs:
#	dir		directory with the binaries to check over
#	server		y or n
#	arch		the arch we're checking
#	myarch		the arch of the system we're running.
#
check_setuid()
{
	if [ $server = y -a ${arch} = ${myarch} ]; then
		_list="${setuid_client} ${setuid_server}"
	else
		_list="${setuid_client}"
	fi
	_tfile=/tmp/ls.$$
	for _file in ${_list}
	do
		ls -l ${dir}/${_file} > ${_tfile}
		egrep "${setuid_mode}" ${_tfile} > /dev/null
		if [ $? -ne 0 ]; then
			qecho \
"ERROR: ${dir}/${_file} does not have the right permissions.
Expected mode bits of \`${setuid_mode}', but found this:" | ${fmt}
			cat ${_tfile}
			exit 1
		fi

		egrep " ${setuid_owner} " ${_tfile} > /dev/null
		if [ $? -eq 0 ]; then
			continue
		fi

		qecho \
"ERROR: ${dir}/${_file} does not have the right owner.
Expected the owner to be \`${setuid_owner}', but found this:" | ${fmt}
		cat ${_tfile}
		egrep "nobody" ${_tfile} > /dev/null
		if [ $? -eq 0 ]; then
			echo "
This looks like a potential problem with \`${setuid_owner}'
being mapped to \`nobody' via an NFS mounted file system.
You should rerun ${myname} using a different filesystem
other than the one ${dir} is contained within." | ${fmt}
		fi
		exit 1
	done
	exec="rm -f ${_tfile}"
	doexec
}

#
# If remove server files that  might be present.
#
removeserverfiles()
{
if [ ${op} = remove -a ${upgrade} = false ]; then
	isupgrade
fi
if [ ${op} = remove -a ${upgrade} = false ]; then
	if [ -f ${old_nsrres} ]; then
		qecho \
"	Removing ${old_nsrres} (${productname} server configuration) file"
		exec="rm -f ${old_nsrres}"
		doexec
	fi

	if [ -f ${nsrres} ]; then
		qecho \
"	Removing ${nsrres} (${productname} server configuration) file"
		exec="rm -f ${nsrres}"
		doexec
	fi

	if [ -f ${jboxres} ]; then
		qecho \
"	Removing ${jboxres} (${productname} jukebox configuration) file"
		exec="rm -f ${jboxres}"
		doexec
	fi

	if [ -f ${nsrlares} ]; then
		qecho \
"	Removing ${nsrlares} (${productname} nsrexecd configuration) file"
		exec="rm -f ${nsrlares}"
		doexec
	fi

	if [ -d "${nsrreshome}" ]; then
		rmdir_if_empty "${nsrreshome}"
	fi

	file=/.nsr
	if [ -f ${file} ]; then
		qecho "	Removing ${file} file"
		exec="rm -f ${file}"
		doexec
	fi
fi
} # end removecommonfiles

#
# Start up the master nsrd daemon at reboot time.
# The daemon handles index file checking
# and starting all the other daemons.
#
# exdir should be initialized above
# when installing local server binaries.
#
restart_mod()
{
	if [ $hpux_10 = y ]; then
		file=/sbin/init.d/networker
	elif [ $hpux = y ]; then
		# we really should modify the localrc() subroutine in /etc/rc,
		# but that requires a set of support functions.
		file=/etc/rc
	elif [ $aix = y ]; then
		file=/etc/rc.nsr
	elif [ $linux = y ]; then
		file=/etc/rc.d/rc.local
	elif [ $svrc = y ]; then
		file=/etc/init.d/networker
	else
		file=/etc/rc.local
	fi
	searchstr="${start_message}"
	sedcmd="/${start_message}/,/${end_message}/d"

	# construct the kind of echo which should be used in the ${file} shell.
	if [ ${bsd_echo} = false -a ${dec} = n ]; then
		nf=
		nt="\c"
	else
		nf=-n
		nt=
	fi

	if [ $aix = y -o $svrc = y ]; then
		# We create a new startup file for these architectures.
		if [ $op = remove ]; then
			qecho "	Removing ${file}"
			rm -f "${file}"
		else
			# remove in case it is something other than a file...
			rm -f ${file}
			qecho "	Creating ${file}"
			cat > "${file}" << EOF
${appendstr}
EOF
			chmod 744 "${file}"
		fi
	else
		modfile
	fi
	if [ $aix = y ]; then
		# Deal with inittab.  The idea is to add a line in
		# the appropriate place, after rctcpip, or rcnfs,
		# or rcpnd, whichever occurs last in inittab
		file=/etc/inittab
		rctcpip="`grep -n '^rctcpip' ${file} | awk -F: '{print $1}'`"
		rcnfs="`grep -n '^rcnfs' ${file} | awk -F: '{print $1}'`"
		rcpnd="`grep -n '^rcpnd' ${file} | awk -F: '{print $1}'`"
		if [ "${rcnfs}" -gt "${rctcpip}" ]; then
			linebefore="rcnfs"
		else
			linebefore="rctcpip"
		fi
		if [ -n "${rcpnd}" ]; then
			linebefore="rcpnd"
		fi
		# The AIX inittab uses colons to quote lines.
		# not specifying the quote character in $sedcmd allows for
		# backward compatiblity
		aix_start_message=\
"Start of lines added by ${productname} - DO NOT DELETE OR CHANGE THIS LINE"
		aix_end_message=\
"End of lines added by ${productname} - DO NOT DELETE OR CHANGE THIS LINE"
		searchstr="${aix_start_message}"
		sedcmd="/${aix_start_message}/,/${aix_end_message}/d"
		if [ -n "${rcnfs}" -o -n "${rctcpip}" ]; then
			# we had better just append to the end in this case
			insertmode=y
			insertcmd="/${linebefore}/a\\
: ${aix_start_message}\\
: installed by ${myname} on `date`\\
rcnsr:2:wait:sh /etc/rc.nsr\\
: ${aix_end_message}"
		fi
		modfile
		insertmode=""
	fi
}

#
# Test if rpc is running. If not, don't edit rc file to startup nsrexecd
# Will exit on a client side install.
#
modifyclientfiles()
{

if [ ${op} = "install" ]; then
#
# install daemons in the rc file, and start them
#
	if [ -f /tmp/nsrrequest ]; then
		. /tmp/nsrrequest
	fi
	nsrexeccmd="${exdir}/nsrexecd${NSREXECD}"

	if [ ${dec} = y ]; then
		appendstr=\
"${start_message}
# installed by ${myname} on `date`
	(echo ${nf} 'Starting ${productname} daemons...${nt}') > /dev/console
[ -f ${exdir}/nsrexecd ] && {
	(${nsrexeccmd}) > /dev/console 2>&1
	(echo '') > /dev/console
}
[ -f ${exdir}/nsrd ] && {
	(nsrd) > /dev/console 2>&1
	(echo '') > /dev/console
}
${end_message}"

	else
	if [ ${svrc} = y ]; then
appendstr=\
"#! /bin/sh
# installed by ${myname} on `date`
case \$1 in
'start')
(echo ${nf} 'starting ${productname} daemons:${nt}') > /dev/console
if [ -f ${exdir}/nsrexecd ]; then
	(${nsrexeccmd})	> /dev/console 2>&1
	(echo ${nf} ' nsrexecd${nt}') > /dev/console
	(echo '') > /dev/console
fi
if [ -f ${exdir}/nsrd ]; then
	(${exdir}/nsrd) > /dev/console 2>&1
	(echo ${nf} ' nsrd${nt}') > /dev/console
        (echo '') > /dev/console
fi
	;;
'stop')
(echo ${nf} 'stopping ${productname} daemons:${nt}') > /dev/console
if [ -f ${exdir}/nsr_shutdown ]; then
	(${exdir}/nsr_shutdown -a -q&) > /dev/console 2>&1
	(echo ${nf} ' nsr_shutdown -a -q${nt}')	> /dev/console
	(echo '') > /dev/console
fi
	;;
*)
echo \"usage: \`basename \$0\` {start|stop}\"
	;;
esac"

	else
appendstr=\
"${start_message}
# installed by ${myname} on `date`
(echo ${nf} 'starting ${productname} daemons:${nt}') > /dev/console
if [ -f ${exdir}/nsrexecd ]; then
	(${nsrexeccmd})	> /dev/console 2>&1
	(echo ${nf} ' nsrexecd${nt}') > /dev/console
	(echo '') > /dev/console
fi
if [ -f ${exdir}/nsrd ]; then
	(${exdir}/nsrd)	> /dev/console 2>&1
	(echo ${nf} ' nsrd${nt}') > /dev/console
	(echo '') > /dev/console
fi
${end_message}"

	fi
	fi
fi
	#restart_mod
	# nothing more to do
	banner="${productname}"
if [ X${myarch} = Xsolaris -o X${myarch} = Xsol86 -o \
     X${myarch} = Xsolppc ]; then
	#
	# "man.cf" is a man page configuration file. When the user does NOT
	# request a specific section on the "man" command line, this file
	# is used to determine the order manual sections in this directory
	# tree are searched. If the file exists only those sections listed
	# in the file are searched. If the file does NOT exist all
	# sub-directories are searched.
	#
	# If the file does not exist it should not be created, because only
	# the sections added by this script would be searched.
	#
	# So if the file exists make sure section 8 is on the list of
	# sections to be serached, else don't do anything.
	#
	if [ -f /usr/man/man.cf -a -w /usr/man/man.cf -a "${domanpage}" = "y" ]
	then
		file=/usr/man/man.cf
		insertmode=y
		searchstr=",8"
		insertcmd="s/MAN.*/&\,8/"
		sedcmd="s/\,8//"
		modfile
		insertmode=""
	fi
fi
} # end modifyclientfiles

#
# Server installation: make sure that we have ${nsrhome} set up.
#

#
# XXX - later add code to check for available disk space using
# df and give some useful information about what can be supported
# based on the current amount of free space in ${nsrhome}.
#
buildindexdirs()
{
test_objects="indexes"
if [ ${op} = install ]; then
	if [ ! -d "${NSRHOME}" ]; then
		qecho "	Directory ${NSRHOME}, does not exist."
		qecho "	Creating directory ${NSRHOME}."
		mmkdir "${NSRHOME}"
	fi	
	#
	# If the user supplied nsr home directory NSRHOME is not the default
	# nsr home directory nsrhome then create a symbolic link.
	#
	if [ ! "${NSRHOME}" = "${nsrhome}" ]; then
		if [ -d ${nsrhome} -a "${pflag}" = y ]; then
			# probably pkgadd after LGTOclnt created ${nsrhome}
			# if there's something useful in it, rename the
			# existing /nsr, else blow it away and start again
			if [ -f ${nsrres} -o -d ${index_dir}/${host} ]; then
			    exec="mv -f ${nsrhome} ${nsrhome}.pre-${RELEASE}"
			    doexec
			else
			    exec="rm -rf ${nsrhome}"
			    doexec
			fi
			if [ -d ${nsrhome} ]; then
				echo "
FATAL ERROR: cannot create symbolic link ${nsrhome} to ${NSRHOME}
because ${nsrhome} already exists. " | ${fmt}
				exit 1
			fi
		fi
		qecho "	Installing ${productname} home directory in ${NSRHOME}"
		exec="ln -s "${NSRHOME}" ${nsrhome}"
		doexec
	fi
	if [ ! -d "${nsrreshome}" ]; then
		exec="mkdir ${nsrreshome}"
		doexec
	fi
	if [ ! -d "${rapmom}" ]; then
		# probably an RS/6000 or SCO, or SGI
		exec="mkdir ${rapmom}"
		doexec
	fi
	if [ ! -d ${index_dir} ]; then
		exec="mkdir ${index_dir}"
		doexec
	fi
	if [ ! -d ${mm_dir} ]; then
		exec="mkdir ${mm_dir}"
		doexec
	fi
	if [ ! -d ${nsrhome}/tmp ]; then
		exec="mkdir ${nsrhome}/tmp"
		doexec
	fi
	if [ ! -d ${nsrhome}/applogs ]; then
		exec="mkdir ${nsrhome}/applogs"
		doexec
	fi
	exec="chmod 777 ${nsrhome}/applogs"
	doexec
	
	chmod 777 ${nsrhome}/tmp
	if [ ! -d ${log_dir} ]; then
		exec="rm -f ${log_dir}"
		doexec
		exec="mkdir ${log_dir}"
		doexec
		exec="touch ${log_dir}/messages ${log_dir}/summary"
		doexec
		exec="chown daemon ${log_dir}/messages ${log_dir}/summary"
		doexec
	fi
	if [ ! -d "${raphome}" ]; then
		exec="rm -f ${raphome}"
		doexec
		if [ ! -d "${nsrhome}/rap" ]; then
			exec="mkdir ${nsrhome}/rap"
			doexec
		fi
		exec="ln -s ${nsrhome}/rap ${raphome}"
		doexec
	fi
fi
} # end buildindexdirs

#
# Handle each of the system files that we need to modify.
#
# /etc/rpc
# /etc/rc.local
# /etc/syslog.conf
#

#
# Add name to number mapping to /etc/rpc
#
modifyserverfiles()
{
file=/etc/rpc
searchstr='^nsr'
sedcmd="/${searchstr}/d"
appendstr=\
"nsrd		390103	nsr
nsrmmd		390104	nsrmm
nsrindexd	390105	nsrindex
nsrmmdbd	390107	nsrmmdb
nsrstat		390109
nsrjb		390110	nsrjbd
nsrexec		390113	nsrexecd"

if [ $server = y ]; then
	modfile
fi

#
# Add RAP name to number mapping to /etc/rpc too
#
file=/etc/rpc
searchstr='39010[1-2]'
sedcmd="/${searchstr}/d"
appendstr=\
"rap		390101	rapla
rapserv		390102	raprd"

if [ $server = y ]; then
	modfile
fi

#
# Make the default syslog changes
#
file=/etc/syslog.conf
#
# Different syslogs have different configuration formats
#
if [ ${sun} = y -o ${hpux} = y -o ${aix} = y -o ${sco} = y -o \
     ${myarch} = solaris -o ${myarch} = sol86 -o ${myarch} = solppc ]; then
	searchstr="${start_message}"
	sedcmd="/${start_message}/,/${end_message}/d"
	appendstr=\
"${start_message}
# installed by ${myname} on `date`
daemon.notice			/dev/console
daemon.notice			${log_dir}/messages
daemon.notice			operator
local0.notice			${log_dir}/summary
local0.alert			root, operator
${end_message}"

	if [ $server = y ]; then
		modfile
	fi
else
	# We don't bother using the DEC syslog facility by default.
	filechange=n
fi

if [ X${filechange} = Xy ]; then
	file=/etc/syslog.pid
	if [ -f ${file} ]; then
		echo \
"	Restarting syslog daemon"
		exec="kill -1 `cat ${file}`"
		errquit=n
		doexec
		errquit=y
	fi
	if [ ! -f ${file} -o ${execstatus} -ne 0 ]; then
		echo "
I'm having trouble figuring out how to (re)start syslog daemon
on \`${host}'.  Restart the syslog daemon by hand, if necessary.
" | ${fmt}
	fi
fi
} # end modifyserverfiles

installepilog()
{
if [ ${op} = install ]; then
	#
	# Now do the real work...
	#

	qecho \
"	Completing Installation"
	exec="(mkdir /nsr/res) > /dev/null 2>&1"
	errquit=n
	doexec
	# Copy NSR resource file to new location.
	if [ $server = y -a -f ${old_nsrres} ]; then
		errquit=y
		exec="cp ${old_nsrres} ${nsrres}"
		doexec
		# Before removing old placement of nsr.res, make sure new one
		# is actually there.
		if [ -f ${nsrres} ]; then
			exec="rm ${old_nsrres}"
			doexec
		fi
	fi

	if [ $server = y -a "${pflag}" != "y" ]; then
		add_rmv_drivers	# install any drivers for this architecture
	fi

	if [ "${DODAEMONS}" = y ]; then
		qecho \
"	Starting ${productname} daemons"
		exec="(cd /; ${exdir}/nsrexecd${NSREXECD})"
		doexec
		if [ $server = y ]; then
			exec="(cd /; ${exdir}/nsrd)"
			doexec
		fi
	fi
else
	if [ ${upgrade} = false -a -d "${nsrhome}" -a "${pflag}" != y ]; then
		qecho \
"	Removing ${productname} directories"
		if [ $server = y ]; then
			add_rmv_drivers	# possibly deinstall any added drivers
		fi
		exec="rm -rf ${cores_dir}"
		doexec

		# XXX
		# Need question in request
		#
		if [ -d "${raphome}" ]; then
			qecho \
"	Removing RAP resource directory ${raphome}"
			exec="rm -rf ${raphome} ${nsrhome}/rap"
			doexec
		fi
		# XXX
		# Need question in request
		#
		if [ -d ${log_dir} ]; then
			qecho \
"	Removing ${productname} logging directory ${log_dir}"
			exec="rm -rf ${log_dir}"
			doexec
		fi
		if [ -d ${nsrhome}/applogs ]; then
			qecho \
"	Removing ${productname} logging directory ${nsrhome}/applogs"
			exec="rm -rf ${nsrhome}/applogs"
			doexec
		fi

		# XXX
		# Need a way to delay this action for 5 or 10 min.
		# Warn customer that this will occur, and how to stop
		# it.
		if [ -d ${mm_dir} ]; then
			qecho \
"	Removing all on-line index and volume information"
			exec="rm -rf ${mm_dir} ${index_dir} ${nsrhome}/tmp"
			doexec
		fi

	elif [ $server = y -a "${pflag}" != y ]; then
		add_rmv_drivers # possibly deinstall any added drivers
		exec="rm -f ${index_dir}/*/.nsr"
		doexec
	else
		qecho ""
		qecho "To completely remove ${productname} from the system"
		qecho "you must remove the ${nsrhome} directory. ${nsrhome}"
		qecho "may be a symbolic link, if so you will also need to"
		qecho "remove the directory that it points to."
		qecho ""
		qecho "Warning: the ${nsrhome} directory contains the online"
		qecho "client file index files and the server media index file."
		qecho "You should only remove it if you want to completely"
		qecho "remove ${productname} from the system."
	fi
fi
} # end installepilog
# @(#)prrmdbmi.sh 1.1 96/04/17 Copyright (c) 1996, Legato Systems, Inc.
#
# Driver file for the preremove 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) No user interaction is allowed from within this file.
#
NSRREQUEST=${NSRREQUEST-/tmp/nsrrequest}
if [ -f ${NSRREQUEST} ]; then
	. ${NSRREQUEST}
	rm -f ${NSRREQUEST}
fi
installinit $*

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

findarch
setecho
do_shutdown -q
modifyclientfiles
banner=${productname}
nsr_done
exit 0

