#!/bin/csh -f
#
# Copyright (c) 1992, 2012, Oracle and/or its affiliates. All rights reserved. 
#
#   NAME
#     installhere
#
#   DESCRIPTION
#     complete the installation of Oracle Secure Backup on this host
#
#   MODIFIED   (MM/DD/YY) 
#   jaswan     04/11/12 - Bug 13899880 Fix handling of special characters in
#                         passwords
#   tdziedzi   09/03/08 - Add minimum password length checks on user passwords
#   tdziedzi   05/01/08 - Fix some excess vertical whitespace
#   pmaseedu   06/30/07 - OSB admin user emailid should be prompted only for
#                         admin installation.
#   tdziedzi   07/30/07 - Move key wallet under encryption tree
#   jwadleig   06/04/07 - correct the construction of the "make_opt" arg for makelinks
#   ghickey    05/08/07 - prompt for the admin's password on domain upgrades
#                         too. Also fix a problem with keystore passwords on
#                         upgrade.
#   ghickey    02/12/07 - add step to request an email address for the admin
#                         user
#   tdziedzi   12/18/06 - Prompt for key wallet password if necessary on an
#                         upgrade
#   tdziedzi   09/13/06 - Add prompt for key wallet password
#   rvullika   03/28/06 - Support OSB upgrade with new input parameter "-n"
#   rvullika   12/28/05 - Fix solaris64 install
#   tdziedzi   11/23/05 - Restore tty echo on interrupt 
#   tdziedzi   11/03/05 - Prompt for admin user password on admin/gadmin installations
#   tdziedzi   05/18/05 - Product name change 
#   pmaseedu   01/11/05 - change "administrative host" to "administrative server"
#                         in user-facing text
#   pmaseedu   01/11/05 - Name change from admin/administrative 'host' to 
#                         'server'. 
#   jrward     09/23/04 - quote $argv[1] ref to avoid csh complaints.
#   tdziedzi   06/08/04 - Hooks for SBT library installation 
#   tdziedzi   05/18/04 - Executables name change 
#   tdziedzi   03/05/04 - Update vendor/product name, copyright info 
#
# ******************************************************************************
#  Old changes (pre ade migration):
#     07/11/03 jrw  remove obsi references.
#     09/13/02 pfg  rename rbtar_version_file => VTAR.
#     08/13/02 jrw  support Mirapoint eval use, testing mode.  Remove Ultrix.
#     06/15/02 jrw  update copyright/IP notice.
#     06/10/02 gms  undid next change.
#     07/07/02 gms  .drv.<arch> for all.
#     03/20/02 pfg  removed xtra endif.
#     03/08/02 gms  removed 'authorize' references.
#     12/03/01 gms  v3 mods.
#     09/12/00 jrw  make QRC_ADMIN_HOST uppercase.
#     03/17/00 jrw  change do_obsi to QR_DO_OBSI.
#     01/03/99 jrw  update phraseology.
#     01/15/98 jrw  handle OBSI installation.
#     03/21/95 gms  fixed -y recognition.
#     11/28/94 gms  added trace.
#     10/27/94 jrw  correct shared filesystem detection.
#     10/18/94 jrw  use informal machine name for admin host.  Also, correct nfs install.
#     09/28/94 jrw  support decult as ultrix for back compatibility.
#     07/26/94 jrw  remove unsetenv dependencies for SCO.
#     05/27/94 jrw  change 'home' to 'qr dir'.
#     03/02/94 gms  incorporate installheru.sh inline.
#     02/25/94 jrw  s/fml_mach_name/f_mach_name/.
#     02/29/94 jrw  write the FQDN machine name to the last_admin_host file.
#     02/03/94 kcl  /tmp => ${QR_TMP}
#     11/12/92 jrw  if the local qr directory has a .shared file, don't do deletions
#                   also, generate .shared if needed
#     09/08/92 gms  removed root check (done in installnet).
#     05/27/92 jrw  confirm that invoker is root.
#     04/28/92 jrw  remove qtar version file on interrupt.
#     04/17/92 jrw  support -f, -a options.
#
#  Usage:
#      cd <ob-directory>
#      $idir/installhere [client | gclient | server | gserver | admin | gadmin ] \
#                          [-f] [-a admin-server] [-o] [-n]
#
#  Exit Codes:
#      0 -- installation successful
#      1 -- installation failed
#      2 -- installation suppressed - version number matched

set nonomatch
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 installhere $argv
    else if (-f install/trenter) then
        install/trenter installhere $argv
        endif
    endif


#  Common initialization:

if (! $?installobinit) then
    if ($?R_TESTING && -f /ccs/rd/qr/install/initinstall.sh) then
        set r_testing
        source /ccs/rd/qr/install/initinstall.sh
        set st = $status
    else if (-f initinstall.sh) then
        source initinstall.sh
        set st = $status
    else if (-f install/initinstall.sh) then
        source install/initinstall.sh
        set st = $status
    else
        echo "No initinstall.sh program exists in either '.' or './install'.  Be sure"
        echo "to run the Oracle Secure Backup 'installhere' program with your current"
        echo "working directory set to the Oracle Secure Backup directory for this host."
        set st = 1
        endif
    if ($st) goto done
    endif


#  Offer help:

if ($#argv > 0) then
    set noglob
    if ("$argv[1]" == '?' || "$argv[1]" == '-?') then
usage:
        echo "usage:  install/installhere <installtype> [-a <admin-server>] [-f]"
        echo "    <installtype>   is one of 'admin', 'gadmin', 'server', 'gserver, 'client' or 'gclient'"
        echo "    <admin-server>  is the name of an administrative server that this host"
        echo "                    is to use"
        echo "    [-f]            forces an update of the /etc/obconfig file"
        set st = 0
        goto done
        endif
    unset noglob
    endif


#  Be sure we have the proper number of arguments; if not, complain.

if ($#argv < 1) then
    goto usage
    endif


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

source $idir/uiparse.sh
if ( ( ! $?mode) || $?fail || $?other_args) then
    goto usage
    endif


#  Get information about the current machine:

source $idir/mymachinfo.sh
if ($status == 9) then
    set st = 9
    goto done
    endif


#  Show the user what we're up to:

echo ""
if ("$my_os_ver" == "unknown") then
    set tmp_ver_string = ""
else
    set tmp_ver_string = " version $my_os_ver"
    endif

if ("$my_os_name" == "solaris64") then
    set tmp_os_name = "solaris"
else
    set tmp_os_name = $my_os_name
    endif
echo "Installing Oracle Secure Backup on $my_mach_name (${tmp_os_name}$tmp_ver_string)"
unset tmp_ver_string
unset tmp_os_name


#  Determine what directory we're supposed to be in.  If the OS on this
#  machine isn't supported, say so:

set dir_info = `grep -i <$obparameters "^[ 	]*${my_os_name}[ 	][ 	]*ob[ 	][ 	]*dir:[ 	]"`

if ($#dir_info < 4) then
    echo "    ERROR: no entry in $obparameters for operating system ${my_os_name}"
    set st = 1
    goto done
    endif

set dir_name = $dir_info[4]
source $idir/iscwd.sh

if ($?fail) then
    if (! $?cwd) set cwd = "`pwd`"
    echo "    ERROR: The $obparameters file indicates that the home for Oracle Secure Backup"
    echo "        on $my_os_name systems is ${dir_info[4]}.  However, you're running the"
    echo "        Oracle Secure Backup installation tool from $cwd."
    echo "        Please move the Oracle Secure Backup tree to ${dir_info[4]}, or update the"
    echo "        $obparameters file.  (If you do the latter, all of your $my_os_name"
    echo "        systems must also have this software installed in ${cwd}.)"
    set st = 1
    goto done
    endif

#  Determine the db dir so that we can move back any saved DB dir to that location

set db_dir = `grep -i <$obparameters "^[ 	]*${my_os_name}[ 	][ 	]*db[ 	][ 	]*dir:[ 	]"`
if ($#db_dir < 4) then
    echo "    ERROR: no db dir entry in $obparameters for operating system ${my_os_name}"
    set st = 1
    goto done
    endif

set dbdir_name = $db_dir[4]
if (-e $dir_name/admin && -e $dir_name/admin/db) then
    if (! -e $dbdir_name) mkdir $dbdir_name
    cp -rf $dir_name/admin/db/* $dbdir_name
    cp -rf $dir_name/admin/db/.hostid $dbdir_name
    rm -rf $dir_name/admin/db
    set obc_noinit = 1
    endif 
unset dir_name
unset dbdir_name

#  Make sure we have a binaries directory here for the current machine type.
#  Ditto for drivers directory.

if (! { $idir/checkdirs.sh $mode $my_mach_arch } ) then
    set st = 1
    goto done
    endif


#  If this directory is not already so marked, see if it lives on a remote machine
#  and is accessed through NFS.  Leave a marker (the .shared file) if we can, but
#  don't have a cow if we can't.

if (-e .shared) then
    setenv nfs_shared "RO"
else
    $idir/isnfs.sh `pwd`
    if ($status == 1) setenv nfs_shared "RO"
    endif

if ($?nfs_shared) then
    touch .shared >&/dev/null
    if ($status == 0) setenv nfs_shared "RW"
    endif


#  If we were asked to show the version of obtar that we're about to install, do so
#  now:

onintr remove_vf
if ($?show_version || $?current_ob_version) then
    set obtar_file = ${bin_pfx}${my_mach_arch}/obtar
    set obtar_ver = `strings - $obtar_file | grep "^obtar version"`

    if ($#obtar_ver >= 3) then
        if ($?show_version) then
            echo $obtar_ver[3] >$VTAR
            endif

        if ($?current_ob_version) then
            if ($current_ob_version == $obtar_ver[3]) then
                echo "    Oracle Secure Backup is up to date on this machine"
                set st = 2
                goto done
                endif
            endif

        endif

    unset obtar_file obtar_ver
    endif

if ($?cwd) then                     # cwd may not be defined on SCO
    set local_cwd = "$cwd"
else
    set local_cwd = "`pwd`"
    endif


#  Do the proper completion of installation using make_links:
#  Skip the admin password reset if we are upgrading an existing admin,
#  but do prompt for the key wallet password if the key wallet does not
#  exist (this is probably an upgrade).
set adminpass1 = ""
set adminpass2 = ""
set adminemail = ""
set keypass1 = ""
set keypass2 = ""
set obc_init_keywallet = 0

set pwd_info = `grep -i <$obparameters "^[ 	]*minimum[ 	][ 	]*user[ 	][ 	]*password[ 	][ 	]*length:[ 	]"`

if ($#pwd_info < 5) then
    echo "    ERROR: entry in $obparameters for minimum user password length is invalid"
    set st = 1
    goto done
    endif
set minpwdlen = $pwd_info[5]
 
if ($mode == admin || $mode == gadmin) then

    if (-e $local_cwd/admin/encryption/wallet/ewallet.p12) then
        goto havekeywallet
        endif

# Double quote every time adminpass and keypass are read from input and subsequently
# used to prevent the shell from trying to interpret any special chars which can be
# used as part of a password. Also if these variables are used in other variables,
# they also need to be quoted.

getakeypwd:
    echo " "
    echo "You must now enter a password for the Oracle Secure Backup encryption"
    echo "key store.  Oracle suggests you choose a password of at least 8"
    echo "characters in length, containing a mixture of alphabetic and numeric"
    echo "characters."
regetkeypwd:
    printf "\nPlease enter the key store password: "
    stty -echo
    set keypass1 = "$<"
    stty echo
    printf "\nRe-type password for verification: "
    stty -echo
    set keypass2 = "$<"
    stty echo
    if ("$keypass1" != "$keypass2") then
        echo " "
        echo "Passwords did not match; please try again."
        goto regetkeypwd
        endif
    if ("$keypass1" == "") then
        echo " "
        echo "The encryption key store password can not be null; please try again."
        goto regetkeypwd
        endif
    echo " "
    set obc_init_keywallet = 1

havekeywallet:

getadminpwd:
    echo " "
    echo "You must now enter a password for the Oracle Secure Backup 'admin' user."
    echo "Oracle suggests you choose a password of at least 8 characters in length,"
    echo "containing a mixture of alphabetic and numeric characters."
regetadminpwd:
    printf "\nPlease enter the admin password: "
    stty -echo
    set adminpass1 = "$<"
    stty echo
    printf "\nRe-type password for verification: "
    stty -echo
    set adminpass2 = "$<"
    stty echo
    if ("$adminpass1" != "$adminpass2") then
        echo " "
        echo "Passwords did not match; please try again."
        goto regetadminpwd
        endif
    set pwdlen = `echo "$adminpass1" | awk '{print length ($0)}'`
    if ($minpwdlen > 0 && $pwdlen < $minpwdlen) then
        echo " "
        echo "The admin user password must be at least $minpwdlen characters"
        echo "in length; please try again."
        goto regetadminpwd
        endif
    echo " "
    endif

nopasswd_reset:

if ($mode == admin || $mode == gadmin) then
    echo " "
    echo "You should now enter an email address for the Oracle Secure Backup 'admin'"
    echo "user. Oracle Secure Backup uses this email address to send job summary"
    echo "reports and to notify the user when a job requires input. If you leave this"
    echo "blank, you can set it later using the obtool's 'chuser' command."
    printf "\nPlease enter the admin email address: "
    set adminemail = $<
    echo " "
    echo " "
    endif

noadmin_email:
if ($?nfs_shared) then
    echo "    NOTE: the Oracle Secure Backup directory on this machine is shared through NFS"
    if ($nfs_shared == "RO") then
        echo "        and is read-only; certain installation steps will be skipped. For"
        echo "        proper operation, ensure that you've installed Oracle Secure Backup"
        echo "        on a machine on which the Oracle Secure Backup directory is writable"
        endif
    endif

# Use an extra set of quotes on the password variables.  They could contain characters
# special to the shell and we don't want them interpreted. When they are passed to
# makelinks the quotes need to be stripped off so they don't become part of the password.

if ($obc_noinit == 1) then
    if ($obc_init_keywallet == 1) then
       set make_opt = (-upgrade -adminpwd \""$adminpass1"\" -adminemail \"$adminemail\" -keypwd \""$keypass1"\")
    else 
       set make_opt = (-upgrade -adminpwd \""$adminpass1"\" -adminemail \"$adminemail\")
       endif
else
    set make_opt = (-adminpwd \""$adminpass1"\" -adminemail \"$adminemail\" -keypwd \""$keypass1"\")
    endif

if (! { $idir/makelinks.sh $mode $local_cwd "$make_opt" } ) then
    echo "    correct these errors and rerun $idir/installhere"
    set st = 1
    goto done
    endif

set make_opt = ""
set adminpass1 = ""
set adminpass2 = ""
set adminemail = ""
set keypass1 = ""
set keypass2 = ""


#  If nfs_shared is set, it indicates we're dealing with an NFS-mounted
#  directory that may be shared amongst many machines; don't do deletions in that
#  case.

else if (! $?nfs_shared) then


#  Delete the directories we won't need:

    echo "    cleaning up unused files"

    set bin_dir = "${bin_pfx}${my_mach_arch}"
    set drv_dir = "${drv_pfx}${my_mach_arch}"
    set etc_dir = "${etc_pfx}${my_mach_arch}"
    set lib_dir = "${lib_pfx}${my_mach_arch}"

    set zaplist = `ls -d .bin.* .drv.* .etc.* .lib.*`
    while ($#zaplist > 0)
        if ($zaplist[1] == $bin_dir) then
            :
        else if ($zaplist[1] == $drv_dir && ($mode == server || $mode == gserver)) then
            :
        else if ($zaplist[1] == $etc_dir) then
            :
        else if ($zaplist[1] == $lib_dir) then
            :
        else
            rm -r $zaplist[1]
            endif

        shift zaplist
        end

    endif


#  If there's a ${OB_TMP}/obdir.tar file, nuke it:

if (-e ${OB_TMP}/obdir.tar) rm ${OB_TMP}/obdir.tar


#  If we installed this as an administrative server, write our machine name
#  to the install/last_admin_host file.

if (! $?OBC_ADMIN_HOST) setenv OBC_ADMIN_HOST ""
if ($mode == admin || $mode == gadmin) then
    echo $my_mach_name >$last_admin_host
else if ("$OBC_ADMIN_HOST" != "" && -d $idir) then
    echo $OBC_ADMIN_HOST >$last_admin_host
    endif


#  If a drive list was specified on the command line, build the device special
#  files and install our device driver.

if ($?drives) then
    $idir/setupserver.sh $drives
    set st = $status
    goto done
    endif

set st = 0

done:
set adminpass1 = ""
set adminpass2 = ""
set keypass1 = ""
set keypass2 = ""
set make_opt = ""
if ($OBTRACE) $idir/trexit installhere $st
exit ($st)

remove_vf:
if (-e $VTAR) rm -f $VTAR
stty echo
set st = 9
goto done
