#!/bin/csh -f
#
# Copyright (c) 1992, 2006, Oracle. All rights reserved.  
#
#   NAME
#     installnet
#
#   DESCRIPTION
#     network-wide installation for Oracle Secure Backup
#
#   MODIFIED   (MM/DD/YY) 
#   rvullika   08/03/06 - Fix bug 5394101 - basically when we choose to process
#                         only a subset of hosts from the ndf file, the subsequent
#                         processing was adding some blanks before and after the drive
#                         list as a result of which only the first entry in the drive 
#                         list was getting processed, rest were droppe on the floor
#   tdziedzi   05/18/05 - Product name change 
#   pmaseedu   01/13/05 - change "administrative host" to "administrative server"
#                         in user-facing text
#   tdziedzi   05/19/04 - Executables name change 
#   tdziedzi   03/05/04 - Update vendor/product name, copyright info 
#
# ******************************************************************************
#  Old changes (pre ade migration):
#    06/15/02 jrw  update copyright/IP notice.
#    12/03/01 gms  v3 mods.
#    09/14/00 gms  added -np arg (turns off confirmation in uninstallrb).
#    09/11/00 jrw  correct semicolon handling.
#    04/20/00 gms  update infile, uninstallrb, today.
#    01/03/99 jrw  update phraseology.
#    03/21/95 gms  fixed -y recognition.
#    01/09/94 gms  accept -y (trace).
#    11/28/94 gms  added trace.
#    07/26/94 jrw  remove unsetenv dependencies for SCO.
#    03/02/94 gms  incorporate installnetu.sh inline.
#    02/03/94 kcl  /tmp => ${QR_TMP}
#    11/05/92 jrw  use hotdate.sh to get today's date.
#    09/14/92 jrw  clean up temp files on exit.
#    09/08/92 gms  fixed root check for sys5.
#    07/08/92 jrw  detect duplicate names; do only one mymachinfo for the
#                  entire installation.
#    06/17/92 jrw  add uninstall (-U | -UU)
#    06/14/92 jrw  allow selective installation using -h <hostlist>
#    05/27/92 jrw  ensure invoker is root.
#    04/28/92 jrw  remove tmp on interrupt.
#
#  Usage:
#    install/installnet <desc-file> [-f] [-U | -UU] [-h hostlist...]
#
#  <desc-file> is the name of a network installation description file.
#  include -f on the command line to forcibly update all machines in the file,
#  even those that are already up to date.
#  -U causes this program to OBUNINSTALL ob on the given hosts; any admin
#  directories are left alone.  -UU causes us to remove Oracle Secure Backup,
#  including the admin directories.

if (! $?OBTRACE) then
    echo "$argv" | grep "\-y" >&/dev/null
    if ( $status == 0 ) then
        setenv OBTRACE 1
    else
        setenv OBTRACE 0
        endif
    endif
if ($OBTRACE) then
    if (-f trenter) then
        trenter installnet $argv
    else if (-f install/trenter) then
        install/trenter installnet $argv
        endif
    endif

#  Common initialization:

if (! $?installobinit) then
    if (-f initinstall.sh) then
        source initinstall.sh
        set rval = $status
    else if (-f install/initinstall.sh) then
        source install/initinstall.sh
        set rval = $status
    else
        echo "ERROR: No initinstall.sh program exists in either '.' or './install'.  Can't continue."
        set rval = 1
        endif
    if ($rval) goto done
    endif


#  Parse the command line arguments, check the validity of the command line:

set rval = 0            # this program's exit value
set nomode
set hostlist = ()
source $idir/uiparse.sh
if ($?fail || ! $?other_args) then
usage:
    echo "usage:  install/installnet <desc-file> [-f | [[-U | -UU] [-np]]] [-h <host>...]"
    echo "    <desc-file>   is the name of an Oracle Secure Backup installation description file"
    echo "    [-f]          forces an update of each host, regardless of whether the"
    echo "                  version of Oracle Secure Backup software on that host is already"
    echo "                  up to date; also forces re-link of the kernel with the"
    echo "                  Oracle Secure Backup high performance device driver."
    echo "    [-U]          uninstalls Oracle Secure Backup from each host."
    echo "    [-UU]         uninstalls Oracle Secure Backup from each host and removes all admin"
    echo "                  directories."
    echo "    [-np]         suppresses confirmation message for each host to be uninstalled."
    echo "    [-h <host>]   limit this installation to the host(s) named."
    set rval = 1
    goto done
    endif
unset nomode
setenv OBINFILE ""

@ i = 1
unset in_hostlist have_hostlist
setenv OBUNINSTALL 0
setenv OBNP ""
while ($i <= $#argv)
    echo "$argv[$i]" | grep "^-" >&/dev/null
    if ($status == 0) then
        set is_flag
    else
        unset is_flag
        endif

    if ("$argv[$i]" == "") then
        unset in_hostlist
    else if ("$argv[$i]" == "-U") then
        setenv OBUNINSTALL "-U"
    else if ("$argv[$i]" == "-UU") then
        setenv OBUNINSTALL "-UU"
    else if ("$argv[$i]" == "-np") then
        setenv OBNP "-np"
    else if ("$argv[$i]" == "-h") then
        set in_hostlist
        set have_hostlist
    else if ("$argv[$i]" == "-y") then
        goto nxt
    else if ($?is_flag) then
        goto usage
    else if ($?in_hostlist) then
        set hostlist = ($hostlist "$argv[$i]")
    else if ("$OBINFILE" != "") then
        goto usage
    else
        setenv OBINFILE "$argv[$i]"
        endif
nxt:
    @ i = $i + 1
    end

if ("$OBINFILE" == "") then
    goto usage
    endif


#  Be sure invoker is root:

whoami |& grep root >&/dev/null
if ($status) then
    id |& grep "uid=0" >&/dev/null
    if ($status) then
        echo "You must be logged in as 'root' to install Oracle Secure Backup.  Please"
        echo "change your user identity to 'root' and re-run this program."
        set rval = 1
        goto done
        endif
    endif


if (! -r $OBINFILE) then
    if (-r $idir/$OBINFILE) then
        setenv OBINFILE $idir/$OBINFILE
    else
        echo "${OBINFILE}: non-existant or unreadable"
        set rval = 1
        goto done
        endif
    endif


#  Local initialization.  If the -f flag was included on the command line, we
#  must forcibly update each machine.  Translate that option into the
#  force_net_install flag.

if (! $?obc_force) setenv obc_force 0
if ($obc_force) then
    setenv force_net_install true
    endif

source $idir/hotdate.sh
setenv TODAY   "$todaysdate"
unset todaysdate
set    tmp1    = ${OB_TMP}"/obnia$$"
set    tmp2    = ${OB_TMP}"/obnib$$"
set    tmp3    = ${OB_TMP}"/obnic$$"

if ("$OBUNINSTALL" == "0") then
    set operation = "installation"
else
    set operation = "deletion"
    endif

set hosts_done = ()


#  Get information about the machine on which we're running:

source $idir/mymachinfo.sh


#  We remove .last_admin_host to be sure that the user has included an administrative
#  server specification first in the network installation description file.

if (-e $last_admin_host) rm $last_admin_host


#  Now pick up the meat of the network description file -- the installation
#  type, host name, number of OB tape devices, and current status.  If the
#  user has specified a hostlist (to limit installation to the named hosts),
#  peruse that list:

onintr int_remove_tmp
@ n = 0

if ($?have_hostlist) then
    set outfile = "$tmp2"
    if (-e $outfile) rm -f $outfile
    sed -f $idir/justtokens.sed $OBINFILE | \
    sed 's,^\(.*\)$,set line = (\1); source $idir/selhosts.sh,' >$tmp1
    source $tmp1

    if ($n == 0) then
        echo "ERROR: none of the hosts named on the command line were found in $OBINFILE"
        set rval = 1
        goto remove_tmp
        endif

    @ missing = $#hostlist - $n
    if ($missing > 0) then
        echo "ERROR: one or more hosts named on command line were not found in ${OBINFILE}:"
        @ i = 1
        while ($i <= $#hostlist)
            if ("$hostlist[$i]" != "") echo "    $hostlist[$i]"
            @ i = $i + 1
            end
        set rval = 1
        goto remove_tmp
        endif

    unset outfile hostlist i missing
    set temp_OBINFILE = "$tmp2"

else
    set temp_OBINFILE = "$OBINFILE"
    endif


#  Make sure that we delete the current host last:

sed -f $idir/justtokens.sed $temp_OBINFILE >$tmp3
if ("$OBUNINSTALL" != "0") then
    set search = "^[ 	]*[^ 	][^ 	]*[ 	]`hostname`[ 	]*"
    grep <$tmp3 $search:q >$tmp2
    if ($status == 0) then
        grep -v <$tmp3 $search:q >$tmp1
        cat $tmp1 $tmp2 >$tmp3
        endif
    endif


#  Create a file we can source:

grep <$tmp3 ';' >&/dev/null
if ($status == 0) then
    # translate all semi's to commas, replace each comma in each bus name with the letters, COMMA
    sed <$tmp3 's,;,SEMI,g; s/,/COMMA/g; s/SEMI/,/g' >$tmp2
else
    # no semi's, therefore commas are device separators
    cp $tmp3 $tmp2
    endif

#  Remove any blank spaces before or after the commas
sed <$tmp2 's/ , /,/g' >$tmp3
cp $tmp3 $tmp2
sed <$tmp2 's,^\(.*\)$,set dup_check_name = (\1); source $idir/dupcheck.sh; if ($status == 0) then; $idir/instnet.sh \1; set s = $status; if ($s == 9) exit (9); if ($s == 0) @ n_ok = $n_ok + 1; endif,'  |\
tr ';' '\012' >$tmp1
unset temp_OBINFILE


#  Tell the user how many we have:

if (! $?have_hostlist) then
    set tmp = `wc $tmp1`
    set n = $tmp[1]
    @ n = $n / 8
    unset tmp
    endif

if ($n == 0) then
    echo "no ${operation}s to perform"
    goto remove_tmp
    endif

if ($n == 1) then
    echo "1 $operation to perform..."
else
    echo "$n ${operation}s to perform..."
    endif


#  Do the real work:

@ n_ok = 0
source $tmp1
if ($status == 9) then
    set rval = 9
    onintr
    goto remove_tmp
    endif
onintr


#  Report how we did:

echo ""
if ($n == 1) then
    echo -n "1 ${operation} attempted; "
    if ($n_ok > 0) then
        echo "it succeeded"
    else
        echo "it failed"
        set rval = 1
        endif
else
    echo -n "$n ${operation}s attempted; "
    if ($n_ok == $n) then
        echo "all succeeded"
    else if ($n_ok == 0) then
        echo "all failed"
        set rval = 1
    else
        @ n_failed = $n - $n_ok
        echo "$n_ok succeeded, $n_failed failed"
        set rval = 1
        endif
    endif

goto remove_tmp


int_remove_tmp:
set rval = 9

remove_tmp:
if (-e $tmp1) rm -f $tmp1
if (-e $tmp2) rm -f $tmp2
if (-e $tmp3) rm -f $tmp3

done:
if ($OBTRACE) $idir/trexit installnet $rval
exit ($rval)

