#!/bin/sh
#******************************************************************************
#                       ISTP LZPR SOFTWARE
#               Property of the U.S. Government
#                       NASA/GSFC/Code 560
#******************************************************************************
#
# UNIT NAME: lzp_remove
#
# PURPOSE: To remove specified sessions from the AD database
#
# INVOCATION METHOD: lzp_remove [-h] sessions
#
# CHANGE HISTORY:
# Author        Change ID  Build/Release   Date      Description of Change
# ------        ---------  ------------- ---------   ---------------------
# R. Wiechert   ISTP        R1.1A         04/08/98   Original code
# R. Wiechert   ISTP        R1.3          06/16/98   Updates for changes
#                                                     to Oracle procedures
#
# NOTES:
# This script is typically called by RM, but may also be called by human.
# The session arguments should match the DPS sessions to be removed.
#
#******************************************************************************
tmpfile="/tmp/lzp_remove.$$.tmp"
cleanup="eval rm -f $tmpfile 2>/dev/null"
trap "$cleanup; echo ERROR:  procedure interrupted; exit 1" 1 2 3 15
sqlprompt="IGNORESQLPROMPT "

# determine runtime options
while getopts h option
do
   help=true
done

shift `expr $OPTIND - 1`
if [ "$help" -o $# -eq 0 ]
then
cat << ENDOFHELPTEXT

USAGE:     lzp_remove [-h] sessions

PURPOSE:   Removes the specified sessions from the AD database.

SYNOPSIS:  The AD login is obtained from the DPS database and
           used to login to the AD databse and execute the
           cleanup_session() procedure, passing it each session
           identifier which was specified on the command line.

NOTES:     This script is typically invoked by the DPS RM task
           when deleting sessions manually or automatically.
           Thus, the AD database is kept synchronized with the
           DPS database.  When invoked directly, this script
           will not remove sessions from the DPS database.
           Use the PDP data maintenance window in order to
           remove sessions from both the AD and DPS databases.

ENDOFHELPTEXT
$cleanup
exit 2
fi

# create temporary file
rm -f $tmpfile 2>/dev/null
touch $tmpfile 2>/dev/null
chmod 600 $tmpfile 2>/dev/null
if [ $? -ne 0 ]
then
   echo "ERROR: unable to create $tmpfile"
   $cleanup
   exit 3
fi

# obtain AD login from DPS database
userpass=`decrypt.sql $PICSHOME`
sqlplus $userpass 1>/dev/null 2>/dev/null << ENDORACLEARGS
set pagesize 100
set linesize 100
set sqlprompt "$sqlprompt"
set sqlcontinue "$sqlprompt"
set sqlnumber OFF
set heading OFF
set feedback OFF
spool $tmpfile
select ORACLELOGINANDPASS from db_oraclelogin where RESOURCETYPE like 'AD%';
ENDORACLEARGS

if [ $? -ne 0 ]
then
   echo "ERROR: unable to access DPS database"
   $cleanup
   exit 4
fi

userpass=`cat $tmpfile 2>/dev/null | grep -v "$sqlprompt"`
if [ "$userpass" = "" ]
then
   echo "ERROR: unable to obtain AD login"
   $cleanup
   exit 5
fi

#
# Remove each session from AD database one by one.
# Most missions have only one AD login, however the
# POLAR mission has two:  one for generic TDM and
# one for the special PWIW datastream.  Process each
# AD login to remove the sessions from each login.
#

session_list="$*"
for sessionid in $session_list
do

shift $#
set -- $userpass
while [ $# -gt 0 ]
do

# truncate temporary file
rm -f $tmpfile 2>/dev/null
touch $tmpfile 2>/dev/null
chmod 600 $tmpfile 2>/dev/null
if [ $? -ne 0 ]
then
   echo "ERROR: unable to truncate $tmpfile"
   $cleanup
   exit 6
fi

# use AD login to attempt remove of session
sqlplus $1 1>/dev/null 2>/dev/null << ENDORACLEARGS
set pagesize 100
set linesize 100
set sqlprompt "$sqlprompt"
set sqlcontinue "$sqlprompt"
set sqlnumber OFF
set heading OFF
set feedback OFF
spool $tmpfile
exec cleanup_session($sessionid);
ENDORACLEARGS

if [ $? -ne 0 ]
then
   echo "ERROR: unable to access AD database"
   $cleanup
   exit 7
fi

# extract success/failure messages from temporary file
messages=`cat $tmpfile 2>/dev/null | grep -v "$sqlprompt"`
rm -f $tmpfile 2>/dev/null
echo "$messages" >> $tmpfile
success=`tail -1 $tmpfile 2>/dev/null | grep "PROCEDURE CLEANUP SESSION WAS SUCCESSFULLY EXECUTED"`

# exit on failure
if [ "$success" ]
then
   echo "SUCCESS: session $sessionid removed"
else
   echo "ERROR: session $sessionid not removed"
   $cleanup
   exit 8
fi

# repeat for each AD login
shift 1
done

# repeat for each session
done

# exit success
$cleanup
exit 0
