#!/bin/csh -f
#
# Copyright (c) 1992, 2005, Oracle. All rights reserved.  
#
#   NAME
#     loadlicense
#
#   DESCRIPTION
#     Oracle Secure Backup easy license loader.
#
#   MODIFIED   (MM/DD/YY) 
#   tdziedzi   05/19/05 - Product name change 
#   tdziedzi   05/18/04 - Executables name change 
#   tdziedzi   03/05/04 - Update vendor/product name, copyright info 
#
# ******************************************************************************
#  Old changes (pre ade migration):
#     09/03/02 jrw  change name to loadlicense for consistency.
#     08/26/02 pfg  added checking for path arg. Add code to test for password and
#                   pass password to rbtool.
#     08/21/02 pfg  origional coding.
#

# Make sure we have two arguments.
if ($1 == "") then
    echo""
    echo "Usage: loadlicense <full path to license file>"
    echo""
    goto done
    endif
  
# Print header stuff.
echo ""
echo "Welcome to the Oracle Secure Backup license install script."
echo ""

# Check for obtool
set idir = `pwd`
set obtool = "$idir/bin/obtool"
set tmpfile = "/tmp/.loadlicob"
if (! -f $obtool ) then
    echo "Error: unable to find $obtool."
    goto done
    endif
    endif

# Check for license file.
printf "Checking for license file[$1]..."
if (! -f $1 ) then
    echo " missing."
    echo "Error: unable to find [$1]."
    goto done
    endif
echo " found."
    
# Check for an existing license.
printf "Checking for existing license..."
set password = ""
$obtool --gui -u admin lslic <<EOF >& $tmpfile

EOF
set msg  = `grep Customer $tmpfile`
if ("$msg" == "") then
    set msg = `cat $tmpfile | grep "Info: Oracle Secure Backup license data have not yet been entered."`
    if ("$msg" != "") then
        echo " no license found."
        set op = "Make"
        set command = "mklic"
    else
        printf "\nPlease enter the admin password:"
        stty -echo
        set password = $<
        stty echo
        $obtool --gui -u admin lslic <<EOF >& $tmpfile
$password
EOF
        set msg  = `grep Customer $tmpfile`
        echo ""
        if ("$msg" == "") then
            set msg = `cat $tmpfile | grep "Info: Oracle Secure Backup license data have not yet been entered."`
            if ("$msg" != "") then
                echo "No license found."
                set op = "Make"
                set command = "mklic"
            else
                echo ""
                cat $tmpfile
                echo ""
                goto done
                endif
        else
            echo "License found."
            set op = "Change"
            set command = "chlic"
            endif
        endif
else
    echo " found."
    set op = "Change"
    set command = "chlic"
    endif

# Load the license file.
#  Load common vars.
set ccode  = `grep 'Customer code:' $1 | awk '{print $NF}'`
set snum   = `grep 'Serial number:' $1 | awk '{print $NF}'`
set xdate  = `grep 'Expiration date:' $1 | awk '{print $NF}'`
set key    = `grep 'Authorization key:' $1| awk '{print $NF}'`

#  Load options
set admin  = `grep 'Administrative server:' $1| awk '{print $NF}'`
set wgu    = `grep 'Workgroup server (UNIX):' $1| awk '{print $NF}'`
set wglw   = `grep 'Workgroup server (Linux, Windows):' $1| awk '{print $NF}'`
set mslw   = `grep 'Media server (Linux, Windows):' $1| awk '{print $NF}'`
set msu    = `grep 'Media server (UNIX):' $1| awk '{print $NF}'`
set msn    = `grep 'Media server (NAS):' $1| awk '{print $NF}'`
set sanu   = `grep 'SAN server (UNIX):' $1| awk '{print $NF}'`
set sanlw  = `grep 'SAN server (Linux, Windows):' $1| awk '{print $NF}'`
set sann   = `grep 'SAN server (NAS):' $1| awk '{print $NF}'`
set culw   = `grep 'Client (UNIX, Linux, Windows):' $1| awk '{print $NF}'`
set cn     = `grep 'Client (NAS):' $1| awk '{print $NF}'`
set lscsi  = `grep 'Library drive (SCSI):' $1| awk '{print $NF}'`
set lfcsan = `grep 'Library drive (FC SAN):' $1| awk '{print $NF}'`
set lgbe   = `grep 'Library drive (GbE):' $1| awk '{print $NF}'`
set db     = `grep 'Database:' $1| awk '{print $NF}'`
set msex   = `grep 'MS Exchange:' $1| awk '{print $NF}'`
set mssql  = `grep 'MS SQL:' $1| awk '{print $NF}'`

# Process the common license information.
if ($ccode == "") then
    echo "Error: unable to find 'Customer code'."
    goto done
    endif
if ($key == "") then
    echo "Error: unable to find 'Authorization key'."
    goto done
    endif
if ($snum == "") then
    echo "Error: unable to find 'Serial number'."
    goto done
    endif

# Set the command vars on the command line.
set obcommand = "$obtool --gui -u admin $command --key $key -C $ccode -S $snum"

# Process the optional license information.
if ($xdate != "") then
    set obcommand = "$obcommand -X $xdate"
    endif
if ($admin != "") then
    set obcommand = "$obcommand -a $admin"
    endif
if ($wgu != "") then
    set obcommand = "$obcommand -w $wgu"
    endif
if ($wglw != "") then
    set obcommand = "$obcommand -W $wglw"
    endif
if ($msu != "") then
    set obcommand = "$obcommand -m $msu"
    endif
if ($mslw != "") then
    set obcommand = "$obcommand -M $mslw"
    endif
if ($msn != "") then
    set obcommand = "$obcommand -n $msn"
    endif
if ($sanu != "") then
    set obcommand = "$obcommand -u $sanu"
    endif
if ($sanlw != "") then
    set obcommand = "$obcommand -l $sanlw"
    endif
if ($sann != "") then
    set obcommand = "$obcommand -v $sann"
    endif
if ($culw != "") then
    set obcommand = "$obcommand -c $culw"
    endif
if ($cn != "") then
    set obcommand = "$obcommand -N $cn"
    endif
if ($lscsi != "") then
    set obcommand = "$obcommand -d $lscsi"
    endif
if ($lfcsan != "") then
    set obcommand = "$obcommand -f $lfcsan"
    endif
if ($lgbe != "") then
    set obcommand = "$obcommand -g $lgbe"
    endif
if ($db != "") then
    set obcommand = "$obcommand -D $db"
    endif
if ($msex != "") then
    set obcommand = "$obcommand -x $msex"
    endif
if ($mssql != "") then
    set obcommand = "$obcommand -q $mssql"
    endif

# Print command.
echo ""
echo "$op the license; [$obcommand]"
  
# Run the command
if ($password == "") then
    $obcommand <<EOF >& $tmpfile

EOF
else
    $obcommand <<EOF >& $tmpfile
$password
EOF
    endif 

# Process the output
if (! -f $tmpfile) then
    echo ""
    echo "Error: can't find $tmpfile!"
    goto done
    endif
set msg = `grep "Error:" $tmpfile`
if ("$msg" != "") then
    echo ""
    echo "$msg"
    goto done
    endif
 
# Remove the temp file
rm -rf $tmpfile

# Print success msg.
echo ""
echo "Oracle Secure Backup license loaded."

# Print footer stuff.
echo ""
echo "Oracle Secure Backup license install script complete."
echo ""

# Do done stuff.
done:
set st = 0
