:
# @(#)x_dbinst 6.1.1  1994-12-19
#
# Initialise a blank database for the conversational components of DBS.
# CREATED BY    : L. Jungnickel                           AT: 1992-11-12
# CHANGE HISTORY:
# 1994-01-**: -change $DBROOT/env/load.prot to RUNDIR/x_dbinst.prot
#             -syntax -d<node>:<serverdb> allowed
#             -delete of QUERY.ins,INFO.ins,WBAPP.ins in update part
#              because double calling in DBSUPD.ins
#             -change exit to exit 1 (rc=1)
#             -rc handling for upd DMAPP, SPROC, SFUNC
#             -xload call with option -S DBS
# 1994-02-14: -New batch option '-b'; stio to ./x_dbinst.log
# 1994-02-16: -<TAB> replaced by '    '
# 1994-03-21: -Default installation is -i upd
# 1994-03-30: -Default installation is -i min
# 1994-05-18: -New order for DOMAIN installation
# 1994-05-24: -Correction of $mig in if statement
# 1994-06-17: -Min installation to hidden
# 1994-06-21: -Current directory is default for distribution
# 1994-07-07: -Migration for DOMAIN separated from Migration for tools. (B.J.)
# 1994-07-07: -$migdom and $migtools instead of $mig
# 1994-10-21: -Message for installationkind changed
# 1994-10-27: -MIGRATION.ino changed in MIGRAT.ino
# 1994-12-19: -Processid for /tmp/x
# 1994-12-21: -TRIGGER ON in any case after migration (B.J.)
# 1996-01-16: -QP.ino (L.J)
# 1999-03-09: -$migdom=0 at the beginning, because bash reports
#              "$migdom" -eq 0  [: : integer expression expected
#              if migdom is not set  (M.L.)
# 1999-11-23: -use of $DBROOT/pgm/plrun instead of $DBROOT/pgm/sqlpl
#             -batlog disabled
#              (M.L.)

ECHO="echo"

PROT=prot

INSTKEY="\
\n  all\t - complete installation for a new database\
\n  upd\t - update installation"
#\n  dom\t - domain installation (inclusive min if DB is new)\
#\n  pl \t - sqlpl installation (inclusive min if DB is new)\

USAGE="correct use of $0 is:\
\n
\n connect user \t \t :: -u <userid>,<password>\
\n domain password \t :: -w <domain password>\
\n database     \t \t :: -d <serverdb>\
\n nodename     \t \t :: -n <servernode>\
\n install      \t \t :: -i all|upd\
\n batch        \t \t :: -b\
\n help information\t :: -h\
\n $INSTKEY"

mod="BATCH"
prog="DBS.ins"
xwb=0
upd=0
dom=0
migdom=0
bat=0
#batlog="$DBROOT/env/x_dbinst.log"

OPTIONS=
OPTSTR="u:w:d:n:i:bh?"

if [ "$OPTIND" = 1 ]
then
    while getopts $OPTSTR O
    do
    case $O in
    u )
        sysdba=`$ECHO "$OPTARG" | cut -d"," -f1`
        case "$OPTARG" in
        *,* )
            syspwd=`$ECHO "$OPTARG" | cut -d"," -f2`;;
        * )
            syspwd="";;
        esac ;;
    w )
        dompwd="$OPTARG" ;;
    d )
        case "$OPTARG" in
        *:* )
            nodename=`$ECHO "$OPTARG" | cut -d":" -f1`
            basename=`$ECHO "$OPTARG" | cut -d":" -f2` ;;
        * )
            basename="$OPTARG" ;;
        esac ;;
    n )
        nodename="$OPTARG" ;;
    b )
        bat=1 ;;
    i )
        case $OPTARG in
        min )
           komp="MINIMAL $komp"
           prog="DBSMIN.ins"
           chk=0 ;;
        pl )
           komp="$komp SQLPL"
           prog="DBSMIN.ins"
           chk=1
           xwb=1 ;;
        dom )
           komp="$komp DOMAIN"
           prog="DBSMIN.ins"
           chk=1
           dom=1 ;;
        all )
           komp="COMPLETE"
           prog="DBS.ins"
           chk=0
           xwb=1 ;;
        upd )
           komp="UPDATE"
           prog="DBS.ins"
           chk=0
           upd=1
           xwb=1 ;;
        * )
           $ECHO "unknown installation: $OPTARG"
           exit 1 ;;
        esac ;;
    \? | h )
        $ECHO $USAGE
        exit 1 ;;
        esac
    done

    shift `expr $OPTIND - 1`

elif set -- `getopt $OPTSTR $*`
then
    for O do
    shift

    case $O in
    --)
        break;;
    -u)
        sysdba=`$ECHO "$1" | cut -d"," -f1`
        case "$1" in
        *,* )
            syspwd=`$ECHO "$1" | cut -d"," -f2`;;
        * )
            syspwd="";;
        esac ;;
    -w)
        dompwd="$1" ;;
    -d)
        case "$1" in
        *:* )
            nodename=`$ECHO "$1" | cut -d":" -f1`
            basename=`$ECHO "$1" | cut -d":" -f2` ;;
        * )
            basename="$1" ;;
        esac ;;
    -n)
        nodename="$1" ;;
    -b)
        bat=1 ;;
    -i)
        case $1 in
        min )
           komp="MINIMAL $komp"
           prog="DBSMIN.ins"
           chk=0 ;;
        pl )
           komp="$komp SQLPL"
           prog="DBSMIN.ins"
           chk=1
           xwb=1 ;;
        dom )
           komp="$komp DOMAIN"
           prog="DBSMIN.ins"
           chk=1
           dom=1 ;;
        all )
           komp="COMPLETE"
           prog="DBS.ins"
           chk=0
           xwb=1 ;;
        upd )
           komp="UPDATE"
           prog="DBS.ins"
           chk=0
           upd=1
           xwb=1 ;;
        * )
           $ECHO "unknown installation: $1"
           exit 1 ;;
        esac ;;
    -? | -h)
        $ECHO $USAGE
        exit 6 ;;
        esac
    done
fi

if [ $# -gt 0 ]
then
    $ECHO $USAGE
    exit 6
fi

if [ "$bat" -ne 0 ]
then
    $ECHO "Start of x_dbinst                           at "`date`
    #$ECHO "Start of x_dbinst                           at "`date` >> $batlog
    #chmod 666 $batlog
    if [ -z "$komp" ]
    then
        komp="UPDATE"
    fi

    if [ -z "$sysdba" ]
    then
        miss="\n\tSYSDBA NAME"
    fi

    if [ -z "$syspwd" ]
    then
        miss="$miss \n\tSYSDBA PASSWORD"
    fi

    if [ -z "$dompwd" -a "$komp" != " SQLPL" ]
    then
        miss="$miss \n\tDOMAIN PASSWORD"
    fi

    if [ -z "$basename" -a -z "$DBNAME" -a -z "$SERVERDB" ]
    then
        miss="$miss \n\tSERVERDB NAME"
    fi
fi

if [ -n "$miss" ]
then
    $ECHO "Missing :$miss"
    #$ECHO "Missing :$miss"  | tee -a $batlog
    exit 6
fi

if [ -z "$komp" ]
then
    $ECHO "UPDATE INSTALLATION ? <y/n>: \c"
    read req
    while [ "$ok" != "OK" ]
    do
       ok="OK"
       if [ "$req" != "y" -a "$req" != "Y" ]
       then
           $ECHO "INSTALLATION KIND <all|upd/quit>: \c"
           read instk
       else
           instk="upd"
       fi

       case $instk in
        min )
           komp="MINIMAL $komp"
           prog="DBSMIN.ins"
           chk=0 ;;
        pl )
           komp="$komp SQLPL"
           prog="DBSMIN.ins"
           chk=1
           xwb=1 ;;
        dom )
           komp="$komp DOMAIN"
           prog="DBSMIN.ins"
           chk=1
           dom=1 ;;
        all )
           komp="COMPLETE"
           prog="DBS.ins"
           chk=0
           xwb=1 ;;
        upd )
           komp="UPDATE"
           prog="DBS.ins"
           chk=0
           upd=1
           xwb=1 ;;
        quit )
           exit 1 ;;
        * )
           $ECHO "$INSTKEY"
           $ECHO "\nPLEASE TYPE VALID KEYWORD OR quit FOR exit"
           ok="notok" ;;
        esac
    done
fi

if [ -z "$sysdba" ]
then
    $ECHO "SYSDBA NAME    : \c"
    read sysdba
fi
trap 'stty echo;exit 1' 2
if [ -z "$syspwd" ]
then
    $ECHO "SYSDBA PASSWORD: \c"
    stty -echo
    read syspwd
    stty echo
    $ECHO
fi
if [ -z "$dompwd"  -a "$komp" != " SQLPL" ]
then
    chkpwd="\#"
    while [ "$chkpwd" != "$dompwd" ]
    do
    $ECHO "DOMAIN PASSWORD: \c"
    stty -echo
    read chkpwd
    stty echo
    $ECHO "\nREPEAT DOMAIN PASSWORD: \c"
    stty -echo
    read dompwd
    stty echo
    echo
    if [ "$chkpwd" != "$dompwd" ]
    then
        $ECHO "mismatch - try again (y/n) ? : \c"
        read req
        if [ "$req" != "y" -a "$req" != "Y" ]
        then
            exit 4
        fi
    fi
    done
fi
trap 2

if [ -z "$basename" ]
then
    if [ -z "$DBNAME" -a -z "$SERVERDB" ]
    then
        $ECHO "DB NAME        : \c"
        read basename
    else
        if [ -z "$DBNAME" ]
        then
             DBNAME="$SERVERDB"
        fi

        case "$DBNAME" in
        *:* )
            if [ -z "$nodename" ]
            then
             nodename=`$ECHO "$DBNAME" | cut -d":" -f1`
            fi
            basename=`$ECHO "$DBNAME" | cut -d":" -f2`;;
        * )
            basename="$DBNAME" ;;
        esac
    fi
fi

locnode=`uname -a | cut -d' ' -f2`

if [ -z "$nodename" ]
then
    nodename="$locnode"
fi

if [ "$nodename" = "$locnode" ]
then
    WRKDIR=`$DBROOT/pgm/getparam $basename RUNDIRECTORY 2>/dev/null`
else
    WRKDIR=`pwd`
fi

ENVDIR="$DBROOT/env"

if [ ! -d "$WRKDIR" ]
then
    $ECHO "Rundirectory $WRKDIR missing"
    #$ECHO "Rundirectory $WRKDIR missing" | tee -a $batlog
    exit 6
fi

if [ ! -f "$ENVDIR/$prog" ]
then
    $ECHO "Installation not possible - missing $prog"
    #$ECHO "Installation not possible - missing $prog" | tee -a $batlog
    exit 6
fi

if [ "$bat" -eq 0 ]
then
    clear
fi

#{
$ECHO "========== Load systemtables for $komp installation =========="
$ECHO " SYSDBA  : $sysdba"
$ECHO " DATABASE: $nodename:$basename"

if [ -n "$miss" ]
then
    $ECHO "Missing :$miss"
    exit 6
fi

if [ "$bat" -eq 0 ]
then
    $ECHO "\nIs this correct (y/n): \c"
    read req
    if [ "$req" != "y" -a "$req" != "Y" ]
    then
        exit 6
    fi
    $ECHO
fi

shwusr=" -u $sysdba,...    -d $basename -n $nodename"
sysusr=" -u $sysdba,$syspwd -d $basename -n $nodename"
domusr=" -u DOMAIN,$dompwd -d $basename -n $nodename"
migpar=" $sysdba $syspwd $basename $nodename $dompwd "
qpipar=" $sysdba $syspwd $basename $nodename "

xload="$DBROOT/pgm/load -S ADABAS"
xplrun="$DBROOT/pgm/plrun"
xutil="$DBROOT/pgm/utility"
tmpfil="/tmp/x$$"

cd $WRKDIR

if [ "$prog" = "DBS.ins" ]
then
    $xutil $sysusr -b "$ENVDIR/TERMCHAR.ind"
    if [ "$?" -eq 0 ]
    then
        $ECHO "DIAGNOSE TRIGGER OFF" >$tmpfil
        $xutil $sysusr -b $tmpfil
        rc="$?"
    else
        rc=1
    fi

#   -------------------------------------------------------------
#   1. Migration for DOMAIN:
#   "Drop Domain Views" before istalling new DOMAIN system tables
#   -------------------------------------------------------------
    if [ "$upd" -eq 1 -a "$rc" -eq 0 ]
    then
        $ECHO "EXISTS TABLE DOMAIN.SYSOBJECT" >$tmpfil
        $xload $sysusr -b $tmpfil >/dev/null 2>/dev/null
        migdom="$?"
        rm $tmpfil
        if [ "$migdom" -eq 0 ]
        then
            $ECHO "--- Migration for DOMAIN is active"
            $xplrun $domusr -r "$ENVDIR/MIGRAT.ino" $migpar "DDV"
            rc="$?"
            if [ "$rc" -ne 0 ]
            then
                $ECHO "Errors occurred during migration of DOMAIN TABLES - returncode: $rc"
            fi
        fi
    fi

#   --------------------------------------------------------------
#   2. Migration for Tools:
#   "Drop Tool Views" before istalling new system tables for tools
#   --------------------------------------------------------------
    if [ "$upd" -eq 1 -a "$rc" -eq 0 ]
    then
        $ECHO "EXISTS TABLE DOMAIN.SYSOBJECT" >$tmpfil
        $xload $sysusr -b $tmpfil >/dev/null 2>/dev/null
        migtools="$?"
        rm $tmpfil
        if [ "$migtools" -eq 0 ]
        then
            $ECHO "--- Migration for tools is active"
            $xplrun $domusr -r "$ENVDIR/MIGRAT.ino" $migpar "DTV"
            rc="$?"
            if [ "$rc" -ne 0 ]
            then
                $ECHO "Errors occurred during migration of TOOLS TABLES - returncode: $rc"
            fi
        fi
    fi

    if [ "$rc" -eq 0 ]
    then
        $xload $sysusr -b "$ENVDIR/$prog" "$dompwd"
        rc="$?"
    fi

    if [ "$rc" -eq 0 ]
    then
        $xload $domusr -b "$ENVDIR/DOMAIN.ins"
        rc="$?"
        $xload $sysusr -b "$ENVDIR/QP.ins"
        rc="$?"
    fi

    if [ "$prog" = "DBS.ins" ]
    then
#       if [ "$rc" -eq 0 ]
#       then
#           $xload $sysusr -b "$ENVDIR/QP.ins"
#           rc="$?"
#       fi

       if [ "$rc" -eq 0 ]
       then
           $xplrun $sysusr -b "$ENVDIR/QP.ino " $qpipar
           rc="$?"
       fi
    fi

#   --------------------------------------------------------------
#   3. Migration for DOMAIN: "Insert Select - Drop Domain Systemtables"
#   - Save the 3.1.2 DOMAIN data into 6.1.1 System tables.
#   - Drop 3.1.2 DOMAIN system table: DOMAIN.SYSRELSHIP
#   --------------------------------------------------------------
    if [ "$upd" -eq 1 -a "$migdom" -eq 0 -a "$rc" -eq 0 ]
    then
        $ECHO "--- Migration of DOMAIN data is active"
        $xplrun $domusr -r "$ENVDIR/MIGRAT.ino" $migpar "ISDD"
        rc="$?"
        if [ "$rc" -ne 0 ]
        then
            $ECHO "Errors occurred during migration DOMAIN DATA - returncode: $rc"
        fi
    fi

#   ------------------------------------------------------------------------
#   4. Migration for tools: "Insert Select - Drop Tool Systemtables"
#   - Save the 3.1.2 tools data into 6.1.1 System tables.
#   - Drop 3.1.2 tools tables: SYSDBA.SYSDBA_OBJECT and SYSDBA.SYSDBA_RELSHIP
#   ------------------------------------------------------------------------
    if [ "$upd" -eq 1 -a "$rc" -eq 0 ]
    then
        $ECHO "EXISTS TABLE SYSDBA.SYSDBA_OBJECT" >$tmpfil
        $xload $sysusr -b $tmpfil >/dev/null 2>/dev/null
        migtools="$?"
        rm $tmpfil
        if [ "$migtools" -eq 0 ]
        then
            $ECHO "--- Migration of tools data is active"
            $xplrun $domusr -r "$ENVDIR/MIGRAT.ino" $migpar "ISDT"
            rc="$?"
            if [ "$rc" -ne 0 ]
            then
                $ECHO "Errors occurred during migration of TOOLS DATA - returncode: $rc"
            fi
        fi
    fi

    $ECHO "DIAGNOSE TRIGGER ON" >$tmpfil
    $xutil $sysusr -b $tmpfil
    rm $tmpfil

#   ------------------------------------------------------------------------
#   5. Migration for DOMAIN:
#   - Save the 3.1.2 comments on objects
#   - Drop 3.1.2 DOMAIN system table: DOMAIN.SYSOBJECT
#   ------------------------------------------------------------------------
    if [ "$upd" -eq 1 -a "$migdom" -eq 0 -a "$rc" -eq 0 ]
    then
        $ECHO "SET NOLOG OFF" >$tmpfil
        $xutil $sysusr -b $tmpfil
        rc="$?"
        if [ "$rc" -ne 0 ]
        then
            $ECHO "Errors occurred during migration (SET NOLOG OFF) - returncode: $rc"
        fi

        $ECHO "--- Migration of DOMAIN comments is active"
        $xplrun $domusr -B "DOMAIN.SYS_PROC.DD_COMMENT"
        rc="$?"
        if [ "$rc" -ne 0 ]
        then
            $ECHO "Errors occurred during migration of DOMAIN COMMENTS - returncode: $rc"
        fi
    fi

else
    rc=1
    if [ "$chk" -eq 1 ]
    then
        $ECHO "Check installation"
        $ECHO "EXISTS TABLE SYSDBA.SYSDIALOGLIBRARY" >$tmpfil
        $xload $sysusr -b $tmpfil >/dev/null 2>/dev/null
        rc="$?"
        rm $tmpfil
    fi

    if [ "$rc" -ne 0 ]
    then
        trap 'stty echo;cat load.$PROT>>x_dbinst.$PROT;rm -f load.$PROT;exit 1' 2
        if [ -z "$dompwd"  ]
        then
            chkpwd="\#"
            while [ "$chkpwd" != "$dompwd" ]
            do
            $ECHO "DOMAIN PASSWORD: \c"
            stty -echo
            read chkpwd
            stty echo
            $ECHO "\nREPEAT DOMAIN PASSWORD: \c"
            stty -echo
            read dompwd
            stty echo
            echo
            if [ "$chkpwd" != "$dompwd" ]
            then
                $ECHO "mismatch - try again (y/n) ? : \c"
                read req
                if [ "$req" != "y" -a "$req" != "Y" ]
                then
                    exit 4
                fi
            fi
            done
        fi
        trap 2
        $xutil $sysusr -b "$ENVDIR/TERMCHAR.ind"
        $xload $sysusr -b "$ENVDIR/$prog" "$dompwd"
        rc="$?"
        if [ "$rc" -ne 0 ]
        then
            $ECHO "Errors occurred during installation - returncode: $rc"
        fi
    fi

    if [ "$dom" -eq 1 -a "$rc" -eq 0 ]
    then
        $ECHO "DIAGNOSE TRIGGER OFF" >$tmpfil
        $xutil $sysusr -b $tmpfil
        $xload $domusr -b "$ENVDIR/DOMAIN.ins"
        rc="$?"
        $xload $sysusr -b "$ENVDIR/QP.ins"
        rc="$?"
        $ECHO "DIAGNOSE TRIGGER ON" >$tmpfil
        $xutil $sysusr -b $tmpfil
        rm $tmpfil

        if [ "$rc" -ne 0 ]
        then
            $ECHO "Errors occurred during domain installation - returncode: $rc"
        fi
    fi

    if [ "$xwb" -eq 1 -a "$rc" -eq 0 ]
    then
        $xload $sysusr -b "$ENVDIR/WBAPP.ins"
        rc="$?"
        if [ "$rc" -ne 0 ]
        then
            $ECHO "Errors occurred during sqlpl installation - returncode: $rc"
        fi
    fi
fi

if [ "$rc" -ne 0 ]
then
    case $rc in
        1)  MSGTXT="START OF DATABASE MACHINE REQUIRED" ;;
        2)  MSGTXT="RESTART OF DATABASE SYSTEM REQUIRED" ;;
        3)  MSGTXT="TOO MANY DATABASE USERS ACTIVE" ;;
        4)  MSGTXT="USERNAME/PASSWORD ILLEGAL" ;;
        5)  MSGTXT="PROTOCOL FILE CANNOT BE CREATED" ;;
        6)  MSGTXT="BATCH RUN MUST BE ABBORTED" ;;
        7)  MSGTXT="SQL ERROR" ;;
        8)  MSGTXT="LOAD ERROR" ;;
        9)  MSGTXT="LINES REJECTED BY DATALOAD OR DATAUPDATE" ;;
        *)  MSGTXT="UNDEFINED ERROR" ;;
    esac
    $ECHO $MSGTXT
    $ECHO "$komp Installation aborted with return code: $rc"
else
    $ECHO "$komp Installation successfully finished "
fi
cat load.$PROT>>x_dbinst.$PROT
rm -f load.$PROT
$ECHO "Protocols written to $WRKDIR/x_dbinst.$PROT"
xutil -b nocmd
UTILPROT=`ls -tr util*$PROT | tail -1`
rm $UTILPROT
exit "$rc"
#} | tee -au $batlog
