#!/bin/csh -f

# $Header: /tmp_mnt/am/cm9/cm-stub/cparis/cmds/nqs/RCS/install-nqs,v 1.7 91/10/01 04:18:12 sean Rel $
# This script installs nqs from the manufacturing area.
# It takes an optional argument: the manufacturing directory in which to look
# for the NQS subdirectories.  If no argument is supplied, the current
# directory is assumed.
# It asks whether the user wants to build the nqs database.  This can be
# customized not to ask, if the release requires a rebuild of the database.

# First we set up the NQS variables in the cm file finder, if they're not
# there already: NQS-LIB-DIR (where daemons etc. live), NQS-USR-DIR (where
# q* user programs live), and NQS-QMGR-HELPFILE (where qmgr gets its help
# text from).

set whoiam = `whoami`
if ($whoiam != "root") then
	echo 'Sorry, you must run as root to install the NQS software; su root and try again.'
	exit 1
endif

# read optional args
set mfgdir = $1

# use cwd if no arg to tell us where to look for utilities.
if ("$mfgdir" == "") set mfgdir = `pwd`

set configfile = `cm-base-system-config`
# these directory names are relative to $mfgdir
if (-f /vmb.exe) then
	set mfgnqsdir_user = nqsbin.vax
	set mfgnqsdir_admin = nqslib.vax
else
	set mfgnqsdir_user = nqsbin.sun4
	set mfgnqsdir_admin = nqslib.sun4
endif	
set mfgnqsdir_man=nqsman

# be sure to put the trailing "/" on directory names
#set default_lib_dir = /usr/lib/nqs/
set default_lib_dir = /usr/local/lib/nqs/
set default_usr_dir = /usr/local/bin/
set default_qmgr_hlp = /usr/local/lib/nqs/qmgr.hlp

# The nqsmkdirs and nqsmktrans programs set NQS_GROUP to 0 ("wheel" on sun,
# now "system" on vaxen as of ultrix 4.x), and NQS_OWNER to 0 ("root").  So
# don't change these definitions without changing these programs.  The
# programs could easily be modified to take owner and/or group as args, if
# need be.  (Note: we would like to use the numeric versions to protect
# ourselves against future name changes, but "install" can't deal with that.)
if (-f /vmb.exe) then
	set NQS_GROUP = system
else					
	set NQS_GROUP = wheel
endif
set NQS_OWNER = root
#set NQS_GROUP = 0
#set NQS_OWNER = 0

# Read or set up NQS-LIB-DIR
set nqs_lib_dir = `cm-base-system-config NQS-LIB-DIR`
if ("$nqs_lib_dir" == "") then
	echo "# Where NQS looks for daemon, sysadmin executables" >> $configfile
	echo "NQS-LIB-DIR = ${default_lib_dir}" >> $configfile
	echo "" >> $configfile
	echo "Setting NQS-LIB-DIR to ${default_lib_dir} in $configfile"
	set nqs_lib_dir = ${default_lib_dir}
endif
if (! -e $nqs_lib_dir) then
	mkdir -p $nqs_lib_dir
	chmod 755 $nqs_lib_dir
	/etc/chown $NQS_OWNER.$NQS_GROUP $nqs_lib_dir
endif

# Read or set up NQS-USR-DIR
set nqs_usr_dir = `cm-base-system-config NQS-USR-DIR`
if ("$nqs_usr_dir" == "") then
	echo "# Where NQS looks for user executables" >> $configfile
	echo "NQS-USR-DIR = ${default_usr_dir}/" >> $configfile
	echo "" >> $configfile
	echo "Setting NQS-USR-DIR to ${default_usr_dir} in $configfile"
	set nqs_usr_dir = ${default_usr_dir}
endif
# note: the user dir is likely to be an already-existing standard unix directory
if (! -e $nqs_usr_dir) then
	mkdir -p $nqs_usr_dir
	chmod 755 $nqs_usr_dir
	/etc/chown $NQS_OWNER.$NQS_GROUP $nqs_usr_dir
endif

# Read or set up NQS-QMGR-HELPFILE
set nqs_qmgr_helpfile = `cm-base-system-config NQS-QMGR-HELPFILE`
if ("$nqs_qmgr_helpfile" == "") then
	echo "# Where NQS looks for qmgr helpfile" >> $configfile
	echo "NQS-QMGR-HELPFILE = $default_qmgr_hlp" >> $configfile
	echo "" >> $configfile
	echo "Setting NQS-QMGR-HELPFILE to $default_qmgr_hlp in $configfile"
	set nqs_qmgr_helpfile = $default_qmgr_hlp
endif

# Make sure /usr/spool/nqs exists also.  It's hardwired into nqsdirs.h
if (! -e /usr/spool/nqs) then
	mkdir -p /usr/spool/nqs
	chmod 755 /usr/spool/nqs
	/etc/chown $NQS_OWNER.$NQS_GROUP /usr/spool/nqs
endif

# Make sure man directory exists too
set nqs_man_dir = /usr/man/manl/
if (! -e $nqs_man_dir) then
	mkdir -p $nqs_man_dir
	chmod 755 $nqs_man_dir
	/etc/chown $NQS_OWNER.$NQS_GROUP $nqs_man_dir
endif

# put daemon, sysadmin executables into NQS-LIB-DIR.
# modes etc. are taken from original nqs makefile.
# We have to cd here because Ultrix install copies the path along with
# the filename (so $mfgdir/lib/foo ends up in
# $nqs_lib_dir/$mfgdir/lib/foo -- blech) 
set orig_dir = `pwd`
cd $mfgdir
cd $mfgnqsdir_admin
echo "Installing daemon executables from $mfgdir/$mfgnqsdir_admin into $nqs_lib_dir"
install -c -o $NQS_OWNER -g $NQS_GROUP -m 555 lpserver $nqs_lib_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 100 netclient $nqs_lib_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 100 pipeclient $nqs_lib_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 6111 shlexefai $nqs_lib_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 744 sitename $nqs_lib_dir
# backup old daemon if it's there
if (-e $nqs_lib_dir/nqsdaemon) mv -f $nqs_lib_dir/nqsdaemon $nqs_lib_dir/OLDnqsdaemon
install -c -o $NQS_OWNER -g $NQS_GROUP -m 2100 nqsdaemon $nqs_lib_dir
if (-e $nqs_lib_dir/logdaemon) mv -f $nqs_lib_dir/logdaemon $nqs_lib_dir/OLDlogdaemon
install -c -o $NQS_OWNER -g $NQS_GROUP -m 100 logdaemon $nqs_lib_dir
if (-e $nqs_lib_dir/netdaemon) mv -f $nqs_lib_dir/netdaemon $nqs_lib_dir/OLDnetdaemon
install -c -o $NQS_OWNER -g $NQS_GROUP -m 100 netdaemon $nqs_lib_dir
if (-e $nqs_lib_dir/netserver) mv -f $nqs_lib_dir/netserver $nqs_lib_dir/OLDnetserver
install -c -o $NQS_OWNER -g $NQS_GROUP -m 111 netserver $nqs_lib_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 755 nqsmkdirs $nqs_lib_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 755 nqsmktrans $nqs_lib_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 755 qmkdir $nqs_lib_dir

# put user executables in NQS-USR-DIR
cd ../$mfgnqsdir_user
echo "Installing user executables from $mfgdir/$mfgnqsdir_user into $nqs_usr_dir"
install -c -o $NQS_OWNER -g $NQS_GROUP -m 6111 qdel $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 6111 qdev $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 6111 qlimit $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 6111 qmgr $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 6111 qpr $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 6111 qstat $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 6111 qsub $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 755 nmapmgr $nqs_usr_dir

# these are not part of the nqs package, but they are handy to have around
install -c -o $NQS_OWNER -g $NQS_GROUP -m 755 shownqs $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 744 startnqs $nqs_usr_dir
install -c -o $NQS_OWNER -g $NQS_GROUP -m 744 stopnqs $nqs_usr_dir

# install qmgr helpfile
cd ../$mfgnqsdir_admin
echo "Installing qmgr helpfile from $mfgdir/$mfgnqsdir_admin/qmgr.hlp into $nqs_qmgr_helpfile"
install -c -o $NQS_OWNER -g $NQS_GROUP -m 0444 qmgr.hlp $nqs_qmgr_helpfile

# install man pages
cd ../$mfgnqsdir_man
echo "Installing nqs man pages from $mfgdir/$mfgnqsdir_man into $nqs_man_dir"
foreach manfile (q*)
	install -c -o $NQS_OWNER -g $NQS_GROUP -m 644 $manfile $nqs_man_dir/${manfile}.l
end
cd $orig_dir

# (re)build database if requested
ask:
echo -n "Do you want to (re)build the nqs database [y/n]? "
set a = $<
if (("$a" == "Y") || ("$a" == "YES") || ("$a" == "Yes") || ("$a" == "y") || ("$a" == "yes")) set build_dbase = 1
if (("$a" == "N") || ("$a" == "NO") || ("$a" == "No") || ("$a" == "n") || ("$a" == "no")) set build_dbase = 0
if (! $?build_dbase) then
	echo "Sorry, I didn't understand your answer.  Try again please."
	goto ask
endif
if ($build_dbase == 1) then
	$mfgdir/$mfgnqsdir_admin/build-nqs-dbase $nqs_lib_dir
endif

echo "Done installing NQS, ready to run startnqs."

