#!/bin/csh -f
#
# Copyright (c) 1992, 2009, Oracle and/or its affiliates. All rights reserved. 
#
#   NAME
#     uninstallob
#
#   DESCRIPTION
#     uninstall Oracle Secure Backup
#
#   MODIFIED   (MM/DD/YY) 
#   asathyam   10/12/09 - Bug 8472913:- Allow for preservation of local config
#                         [DB] directory as per the user's request.
#   jwadleig   10/02/07 - Put request for system name under QUERYPKG env. variable
#   jwadleig   08/16/07 - fix bug 6152160 disable multiple system code
#   ghickey    05/09/07 - make remove prompt clearer
#   rvullika   01/26/06 - Allow users to save the OSB home if they want to
#   rvullika   01/24/06 - Remove NFS reference
#   tdziedzi   05/18/05 - Product name change 
#   rvullika   04/28/05 - (Bug 4334532) Get admin directory location from 
#                         /etc/obconfig
#   rvullika   01/05/05 - Fix language
#   rvullika   10/29/04 - Some messages should show only if host is admin
#   rvullika   06/17/04 - Fixed a script error
#   tdziedzi   05/19/04 - Executables name change 
#   tdziedzi   03/05/04 - Update vendor/product name, copyright info 
#
# ******************************************************************************
#  Old changes (pre ade migration):
#    07/22/02 jrw  fix re-use of target.
#    07/18/02 pfg  unset target after uninstall of target system.
#                  limited the number of times a failed ping of
#                  the host to uninstall to 3.
#    06/15/02 jrw  update copyright/IP notice.
#    12/04/01 gms  v3 mods.
#    09/14/00 gms  added -np arg (to suppress confirmation).
#    09/13/00 gms  recognize -U, -UU (via invoke from instnet.sh).
#    11/24/99 jrw  fix trouble-talking-with line length.
#    01/03/99 jrw  update phraseology.
#    02/13/98 jrw  be more clever when trying to find rb dir.
#    08/14/97 gms  pass tgt_os_ver to uninstallhere.
#    08/12/97 gms  better arg checking.
#    03/21/95 gms  fixed -y recognition.
#    11/28/94 gms  added trace.
#    02/03/94 kcl  /tmp => ${QR_TMP}
#    11/25/92 jrw  refer to pathnames explicitly when rsh'ing.
#    09/05/92 gms/ers fixed to run with sys5.
#    06/18/92 jrw  original coding.
#
#   Usage:
#       $idir/uninstallob [-m <host>] [-q <obparmsfile>] [-U | -UU]
#
#       if -m is specified, the prompt for the hostname is suppressed.
#       if -q is specified, the obparameters prompt is suppressed.
#       if -U is specified, all prompts are suppressed (we're being
#           invoked from installnet -U -> instnet.sh).
#       -UU is equivalent to -U with deletion of the admin dir.
#
#
#   following is temporary, until we call initinstall.sh... but don't override if
#   user has already set it.

    onintr interrupted
    if (! $?OB_TMP) setenv OB_TMP "/tmp"

################################################################
#
#   If OBUNCMD is defined, we're removing Oracle Secure Backup from the local
#   machine and running in an exec'ed copy of uninstallob
#   Just do the deed and report on how we did...

    if ($?OBUNCMD) then
        if ($OBTRACE) echo $OBUNCMD
        $OBUNCMD
        set st = $status
        setenv OBTRACE 0
        goto done
        endif

################################################################
#
#   Look for usage request...

    echo "$argv" | grep "\-h" >&/dev/null
    if ( $status == 0 ) goto usage
    echo "$argv" | grep "\?" >&/dev/null
    if ( $status == 0 ) then
usage:
        unset echo
        echo "Usage:   uninstallob [option ... ]"
        echo ""
        echo "uninstallob removes Oracle Secure Backup from one or more hosts that"
        echo "you specify. You will be prompted for various options, such as"
        echo "whether or not you want to retain a host's administrative directory."
        echo "Hosts are processed one at a time, and if you plan to remove Oracle"
        echo "Secure Backup from this (the local) host, you should do this last, since"
        echo "this script will be removed along with everything else."
        echo ""
        echo "Command line options are:"
        echo "    -so   show-only. display (but do not execute) the commands"
        echo "          that will be executed on the target host."
        echo "    -x    turn on full echoing."
        echo "    -y    trace progress through subscripts."
        echo ""
        exit 0
        endif
################################################################
#
#   Process command line arguments...

    set sep = '-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
    set idir = "install"
    set showit = ""
    set progname = "$0"

    unset quiet target_set target obpa batch nuke_osb nuke_admin nuke_admin_set nuke_osb_set np
    set myargs = ( $argv )
    setenv OBTRACE 0
    set xarg = ""
    set yarg = ""

    set i = 1
    while ($i <= $#argv)
        switch ($argv[$i])
            case -quiet :
                set quiet
                breaksw

            case -x :
                set xarg = "-x"
                set echo
                breaksw

            case -so :
                set showit = "-so"
                breaksw

            case -y :
                setenv OBTRACE 1
                set yarg = "-y"
                if (-f trenter) then
                    trenter uninstallob $myargs
                else if (-f install/trenter) then
                    install/trenter install/uninstallob $myargs
                    endif
                breaksw

            case -m :
                @ i = $i + 1
                set target = "$argv[$i]"
                set target_set
                breaksw

            case -np :
                set np
                breaksw

            case -q :
                @ i = $i + 1
                set obpa = "$argv[$i]"
                breaksw

            case -UU :
                set nuke_admin
                goto ucom

            case -U :
ucom:
                set nuke_admin_set
                set quiet
                set batch
                breaksw

            default :
                echo "Unknown command line option, $argv[$i]."
                goto failed

            endsw

        @ i = $i + 1
        end

#  Say hi!

    if (! $?quiet) then
        echo " "
        echo "Welcome to Oracle's Un-Install program for Oracle Secure Backup."
        echo "This program will remove Oracle Secure Backup from one or more machines"
        echo "on your network. If you are going to remove Oracle Secure Backup from"
        echo "this host, make sure you do it last (lest the un-install program disappear"
        echo "before you're done with it)."
        echo " "
        echo "For most questions, a default answer appears enclosed in square brackets."
        echo "Press Enter to select this answer."
        echo " "
        echo -n "Please wait a few seconds while I learn about this machine..."
        if ($OBTRACE) echo ""
        endif

#  Common initialization:

    unset st
    if (! $?installobinit) then
        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
            set trydir = $progname:h
            if ("$trydir" != "" && "$trydir" != "$0") then
                cd $trydir/.. >&/dev/null
                if ($status == 0 && -f install/initinstall.sh) then
                    source install/initinstall.sh
                    set st = $status
                    endif
                endif
            endif

        if (! $?st) then
            if ($?trydir) then
                set trytext = "'$trydir', "
            else
                set trytext = ""
                endif
            echo "ERROR: No initinstall.sh program exists in ${trytext}'.' or './install'."
            echo "    Can't continue."
            set st = 1 
            endif
        if ($st) goto done
        endif

    source $idir/mymachinfo.sh

    if (! $?quiet) then
        echo " done."
        echo $sep
        endif

#  User must be 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 remove Oracle Secure Backup.  Please"
            echo "change your user identity to 'root' and re-run this program."
            goto failed
            endif
        endif

################################################################
#
#   First job: get the name of the host from which to remove Oracle Secure Backup
#
    set errorcount = 0
    while (1)
        if ($?target_set) then
            unset target_set
        else
	    if $?QUERYPKG then
		set target = ""
		echo " "
		echo "Enter the name of a host from which you'd like to remove Oracle Secure Backup"
		echo -n ": "
		set target = $<
		if ("$target" == "") continue
	    else
		set target = "`hostname`"
		endif
            endif

        echo -n "Just a moment while I learn about ${target}..."
        if ($OBTRACE) echo ""

        unset error
        set host = $target
        source $idir/tgtmachinfo.sh
		echo "done."
        if ($?error) then
            @ errorcount = $errorcount + 1
            echo "ERROR: Trouble talking with ${target}:"
            echo "    $error"
            if ($errorcount == "3") then
                break
                endif
            continue
            endif

        unset reboot_if_drives
        if ($?ob_kernel_path) set reboot_if_drives
        unset removing_me
        set hosttext = "host $target"
        if ("$my_f_mach_name" == "$target_f_mach_name") then
            #  See if the host is Oracle Secure Backup admin
            if (-d $idir/../bin && -d $idir/../etc) set ob_installed
            if (-f /etc/obconfig) then
                set obadmindir = `grep -i < /etc/obconfig "^[       ]*admin[        ][      ]*dir:[         ]"`
                if ($status || $#obadmindir < 3) then
                    if (-e $idir/../admin/state/general/vol-sequence && $?ob_installed) set ob_admin_installed
                else
                    if (-e $obadmindir[3]/state/general/vol-sequence && $?ob_installed) set ob_admin_installed
                    endif
                endif
            echo " "
            echo "You selected this machine, $my_mach_name, from which to remove Oracle Secure Backup."
            if ($?ob_admin_installed) then
                echo "(Note: once Oracle Secure Backup is removed from this host, you will not"
                echo "       be able to remove Oracle Secure Backup from other hosts.)"
                endif
            set removing_me
            set hosttext = "this host"
            endif

        if ("$target" != "$target_mach_name" && "$target" != "$target_f_mach_name") then
            echo " "
            echo "Note: $target's formal name is $target_mach_name; do you still want to"
            echo -n "remove Oracle Secure Backup from this host [yes]? "
            set ayenay_dft = yes
            source $idir/ayenay.sh
            set st = $status
            unset ayenay_dft
            if (! $st) goto rmvloop
            endif

################################################################
#
#   Next determine what obparameters file to use...
#
        if (! $?obpa) then
            while (1)
                echo " "
                echo "Enter the name of an obparameters file that was used to install"
                echo -n "Oracle Secure Backup on $target [$obparameters]: "
                set obpa = $<
                if ("$obpa" == "") set obpa = $obparameters
                if (-r $obpa) break
                echo "$obpa could not be found"
                end
            endif

################################################################
#
#   See if he wants to delete the Oracle Secure Backup dir...
#
        echo " "
        echo -n "Do you want to remove the Oracle Secure Backup directory [no]?"
        set ayenay_dft = no
        source $idir/ayenay.sh
        set st = $status
        unset ayenay_dft

        if ($st) then
            set nuke_osb
            set osbopt = "-ds"
            set nuketext = "removed"
        else
            unset nuke_osb
            set osbopt = ""
            set nuketext = "retained"
            endif

################################################################
#
#   See if he wants to save the admin dir...
#
        set st = 1
        if ($?nuke_admin_set) then
            if ($?nuke_admin) then
                set st = 0
            else
                set st = 1
                endif
        else
            echo " "
            if ($?ob_admin_installed) then
                echo -n "Do you want to save the admin directory on $target [yes]? "
                set ayenay_dft = yes
                source $idir/ayenay.sh
                set st = $status
                unset ayenay_dft
                endif
            endif

        set admtext2 = ""
        if ($st) then
            unset nuke_admin
            set admopt = ""
            set admtext = "retained"
        else
            set nuke_admin
            set admopt = "-da"
            set admtext = "removed"
            endif

################################################################
#
#   See if user wants to preserve the "db" dir, effectively thereby
#   preserving the identity of the system in the admin domain
#
        if (! $?ob_admin_installed) then
            echo "Do you want to remove $target's identity as a member of the"
            echo -n "administrative domain [no]? "
            set ayenay_dft = no
            source $idir/ayenay.sh
            set st = $status
            unset ayenay_dft

            if ($st) then
                set dbopt = "-db"
                set dbtext = "removed"
            else
                set dbopt = ""
                set dbtext = "retained"
            endif
        else
            if ($?nuke_admin) then
                set dbtext = "removed"
                set dbopt = "-db"
            else
                set dbtext = ""
                set dbopt = ""
            endif
        endif

################################################################
#
#   Seek assurance that this is really what he wants...
#
        if (! $?np) then
            echo ""
            echo "Oracle Secure Backup will be removed from ${hosttext}."
            echo "The backup directory will be ${nuketext}."
            if ($?ob_admin_installed) then
                echo "The admin directory will be ${admtext}."
            else
                echo "The system's Oracle Secure Backup identity will be ${dbtext}."
                endif
 
            echo ""
            echo -n "Do you wish to continue [no]? "
            set ayenay_dft = no
            source $idir/ayenay.sh
            set st = $status
            unset ayenay_dft
            if (! $st) then
                if ($?batch) goto done
                goto rmvloop
                endif
            endif

################################################################
#
#   Proceed with the destruction...
#
        echo ""
        echo "Un-installing Oracle Secure Backup from ${target}..."

        set tmpbase = ${OB_TMP}"/ob"
        set tmpsh   = "${tmpbase}sh$$"
        set tmppa   = "${tmpbase}pa$$"
        set tmpup   = "${tmpbase}upd$$"

        #   setting of "kernel_leaf" tells us that we've diddled with the host's
        #   kernel and it need replacing...
        
        if ($?kernel_path) then
            set kp = "-kp $kernel_path"
        else
            set kp = ""
            endif

        #   note: we pass uninstallhere what tgtmachinfo.sh told us, since there
        #   may not be an install dir on the rmt machine and we don't want to
        #   duplicate machinfo.sh in uninstallhere.

        set cmdline = ($tmpsh -a $target_mach_arch -o $target_os_name -osv $target_os_ver \
                       -p $tmppa $admopt -u $tmpup $osbopt $dbopt $kp $showit $xarg $yarg)

        if ($?removing_me) then

            cp $idir/uninstallhere $tmpsh;chmod +x $tmpsh
            cp $idir/updaterc.sh   $tmpup;chmod +x $tmpup
            cp $obpa $tmppa

            #  To prevent us from deleting ourselves, we move this script to a temp location,
            #  and exec from there.

            set iob = ${OB_TMP}"/`basename $0`"
            rm -f $iob
            cp $idir/uninstallob $iob
            chmod 755 ${OB_TMP}/uninstallob
            setenv OBUNCMD "$cmdline"
            exec $iob
            exit (0)
        else
            #   copy uninstallhere to remote host...
            $rsh $target <$idir/uninstallhere "/bin/cat >$tmpsh;chmod +x $tmpsh"
            if ($status) then
                echo "    ERROR: remote copy of shell program failed"
                echo ""
                set st = 1
                goto rmvloop
                endif

            #   copy obparameters file remote host...
            $rsh $target <$obpa "/bin/cat >$tmppa"
            if ($status) then
                echo "    ERROR: remote copy of parameters file failed"
                $rsh $target /bin/rm -f $tmpsh
                got rmvloop
                endif

            #   copy updaterc.sh to remote host...
            $rsh $target <$idir/updaterc.sh "/bin/cat >$tmpup;chmod +x $tmpup"
            if ($status) then
                echo "    ERROR: remote copy of updaterc.sh file failed"
                $rsh $target /bin/rm -f $tmpup
                got rmvloop
                endif

            #   start him up...
            $rsh $target "$cmdline -rmt"
            set st = $status
            $rsh $target /bin/rm -f $tmpsh >&/dev/null
            $rsh $target /bin/rm -f $tmppa >&/dev/null

            if ($st != 0) then
                echo "    ERROR: remote un-installation script invocation failed on ${target}"
                echo ""
                endif
            endif

rmvloop:
        echo " "; echo "$sep"; echo " "
###########################################################
#
#       disable multiple system functionality for now
#       leaving the code for now and just skipping it
#       in case the need for this returns. --- joew 8/15/07
#
        break    

        if ($?batch) then
            break
        else
            echo -n "Would you like to remove Oracle Secure Backup from any other machines [yes]? "
            set ayenay_dft = yes
            source $idir/ayenay.sh
            set st = $status
            unset target
            unset ayenay_dft
            if (! $st) break
            endif
        end

    echo ""
    set st = 0
done:
    if ($st == 9) then
        echo ""
        echo "uninstall interrupted by user"
        endif
    if ($OBTRACE) $idir/trexit uninstallob $st
    exit ($st)

interrupted:
    set st = 9
    goto done

