#!/usr/bin/ksh
# Written By    : Kynan Dent
# Written On    : 08/07/2002
# Function      : This script provides a psuedo front end to pdadmin to
#                 facilitate using shell tools on pdadmin output
VERSION=2.0

#set -x

# init
PD_BAIL="quit"
PD_USAGE="Usage:\nEnter the PD admin command and press enter.  If you wish to pipe the output through a shell command"

echo "Please enter the pdadmin user you wish to use [sec_master]: \c"
read PD_USER
if [ "$PD_USER" -eq "" ];then
        PD_USER=sec_master
fi
echo "Please enter the password for $PD_USER: \c"
stty -echo
read PD_PASSWD
stty echo

echo "\n\nWelcome to $0.  This program will remember your last command and "
echo "display it in square brackets.  To use it just press enter.  If "
echo "you want to enter nothing the you have to enter NOTHING as the"
echo "command string.\n"

PD_JOB=""
while [ "$PD_JOB" != "$PD_BAIL" ]
do
        # initialise
        RUN_SHELL="TRUE"
        RUN_PDADMIN="TRUE"

        # get pdadmin input
        PD_PROMPT="_mypdadmin [$PD_JOB]> "
        echo "$PD_PROMPT\c"
        read ANSWER
        case "$ANSWER" in
                $PD_BAIL)       PD_JOB=$PD_BAIL
                                        ;;
                "NOTHING")      RUN_PDADMIN=""
                                        PD_JOB=""
                                        ;;
                "")                     if [ "$PD_JOB" = "" ];then
                                                RUN_PDADMIN=""
                                        fi
                                        ;;
                *)                      PD_JOB=$ANSWER
                                        echo "$PD_JOB" > /tmp/PD_JOB.$$
                                        ;;
        esac
        # are we about to quit?
        if [ "$PD_JOB" != "$PD_BAIL" ];then
                # now get the shell input
                SH_PROMPT="_shell [$SH_JOB]> "
                echo "$SH_PROMPT\c"
                read ANSWER
                case "$ANSWER" in
                        $PD_BAIL)       PD_JOB=$PD_BAIL
                                                ;;
                        "NOTHING")      RUN_SHELL=""
                                                SH_JOB=""
                                                ;;
                        "")                     if [ "$SH_JOB" = "" ];then
                                                        RUN_SHELL=""
                                                fi
                                                ;;
                        *)                      SH_JOB=$ANSWER
                                                ;;
                esac

                if [ "$RUN_PDADMIN" ];then
                        eval "pdadmin -a $PD_USER -p $PD_PASSWD /tmp/PD_JOB.$$ | grep -v 'cmd>' $SH_JOB"
                else
                        if [ "$RUN_SHELL" ];then
                                eval "$SH_JOB"
                        else
                                echo "Nothing to do!"
                        fi
                fi
        fi
done
if [ -r /tmp/PD_JOB.$$ ]; then
        rm /tmp/PD_JOB.$$
fi