#!/bin/bash
# ALTIBASE_HOME, ORACLE_HOME and ORA_ADAPTER_HOME environment variables are required.

START_SLEEP_TIME=2
CHECK_SLEEP_TIME=1
ORA_ADAPTER_NAME=$ORA_ADAPTER_HOME/bin/oraAdapter

# check environment variable
if [ -z $ORA_ADAPTER_HOME ]
then
    echo "Please set ORA_ADAPTER_HOME environment variable."
    exit 255
fi
if [ -z "$ALTIBASE_HOME" ]
then
    echo "Please set ALTIBASE_HOME environment variable."
    exit 255
fi
if [ -z "$ORACLE_HOME" ]
then
    echo "Please set ORACLE_HOME environment variable."
    exit 255
fi

# usage print function
printUsage()
{
    echo "Usage: oaUtility [start|stop|check [alive|constraints]|status]"
}

# oraAdapter start function
startFunc()
{
    ALIVE=`ps -ef | grep $ORA_ADAPTER_NAME | grep "\-\-daemon" | grep -v $0 | grep -v grep | wc -l`
    if [ $ALIVE -eq 0 ]
    then
        echo "Start $ORA_ADAPTER_NAME..."
        valgrind --tool=memcheck --leak-check=full --trace-children=yes --show-reachable=yes --error-limit=no $ORA_ADAPTER_HOME/bin/oraAdapter 2>> $ORA_ADAPTER_HOME/trc/valgrind.log
        sleep $START_SLEEP_TIME

        ALIVE=`ps -ef | grep $ORA_ADAPTER_NAME | grep "\-\-daemon" | grep -v $0 | grep -v grep | wc -l`
        if [ $ALIVE -ne 1 ]
        then
            echo "Failed to start $ORA_ADAPTER_NAME, please check trace log."
        fi
    elif [ $ALIVE -eq 1 ]
    then
        echo "$ORA_ADAPTER_NAME was already started."
    elif [ $ALIVE -gt 1 ]
    then
        echo "Multiple $ORA_ADAPTER_NAME are detected, please check $ORA_ADAPTER_NAME process."
    fi
}

checkAlive()
{
    RETURN_VALUE=0

    ALIVE=`ps -ef | grep $ORA_ADAPTER_NAME | grep "\-\-daemon" | grep -v $0 | grep -v grep | wc -l`
    if [ $ALIVE -eq 0 ]
    then
        echo "$ORA_ADAPTER_NAME is terminated."
        RETURN_VALUE=1
    elif [ $ALIVE -gt 1 ]
    then
        echo "Multiple $ORA_ADAPTER_NAME are detected, please check $ORA_ADAPTER_NAME process."
        RETURN_VALUE=2
    else
        echo "$ORA_ADAPTER_NAME is alive."
    fi

    return $RETURN_VALUE
}

# main processing routine
case "$1" in
'start')
    #oaCheckConstraints
    if [ $? -eq 0 ]
    then
        startFunc
    else
        echo "Failed to start $ORA_ADAPTER_NAME, please check constraints."
    fi
;;
'stop')
    ORA_ADAPTER_PID=`ps -ef | grep $ORA_ADAPTER_NAME | grep "\-\-daemon" | grep -v grep | awk '{print $2}' | head -1`
    if [ -z $ORA_ADAPTER_PID ]
    then
        echo "$ORA_ADAPTER_NAME is not started."
    else
        echo "$ORA_ADAPTER_NAME's PID is $ORA_ADAPTER_PID."
        echo "Shutdown $ORA_ADAPTER_NAME..."
        kill -9 $ORA_ADAPTER_PID
    fi
;;
'check')
    case "$2" in
    'alive')
        checkAlive
    ;;
    'constraints')
        oaCheckConstraints
    ;;
    *)
        while [ 1 ]
        do
            checkAlive
            if [ $? -eq 1 ]
            then
                echo "Try auto startup..."
                oaCheckConstraints
                if [ $? -eq 0 ]
                then
                    startFunc
                else
                    echo "Failed to start $ORA_ADAPTER_NAME, please check constraints."
                fi
            fi

            sleep $CHECK_SLEEP_TIME
        done
    ;;
    esac
;;
'status')
    checkAlive
;;
*)
    printUsage
;;
esac

exit 0
