#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2link.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)39	1.3 src/43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2link.sh, hacmp.assist, 61haes_r714 11/28/11 15:26:29
###############################################################################
#
# Name: cl_db2link
#
# Adds or removes the Custom SQL Application Monitor script created by cl_db2import.
#
# Syntax:
# cl_db2link [-D | -v] {-a | -r} INSTANCE_OWNER
# 
# Arguments:
#	-D		Turn on debug mode.
#	-v		Turn on verbose mode.
#	-a		Adds the hard link for INSTANCE_OWNER's SQL AppMon.
#	-r		Removes the hard link for INSTANCE_OWNER's SQL AppMon.
#	INSTANCE_OWNER	DB2 instance owner
# 
# Returns:
#	0 on SUCCESS
#	1 on FAILURE
# 
###############################################################################

###############################################################################
#
# Name: usage
#
# Prints usage message and exits the program.
#
# Arguments:  none
#
# Returns:    does not return
#
################################################################################
usage() {
    set +u
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x
    set -u

    dspmsg -s 6 $DB2SACAT 1 "Usage:\ncl_db2link [-D | -v] {-a | -r} INSTANCE_OWNER\n\

    -D  Turn on debug mode.\n\
    -v  Turn on verbose mode.\n\
    -a  Adds the hard link for INSTANCE_OWNER's SQL AppMon.\n\
    -r  Removes the hard link for INSTANCE_OWNER's SQL AppMon.\n\
    INSTANCE_OWNER must be a valid system user.\n"

    exit 1
}


###############################################################################
# Main program procedure.
###############################################################################
PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../../../utilities/cl_get_path all)"
PATH=$PATH:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin
FPATH_BASE=/usr/es/lib/ksh93
FPATH=$FPATH_BASE/hacmp:$FPATH_BASE/sa:$FPATH_BASE/db2:$FPATH_BASE/aix:$FPATH_BASE/aix/odm
export PATH FPATH

[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.3 $Source: 61haes_r711 43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2link.sh 1$'

export DB2SA_HOME="/usr/es/sbin/cluster/sa/db2"
export DB2SA_ETC_PATH="$DB2SA_HOME/etc"
export DB2SA_SBIN_DIR="$DB2SA_HOME/sbin"

export DEBUG=0
export VERBOSE=0
export ADD_LINK=0
export REMOVE_LINK=0
export DB2SALOG="/var/hacmp/log/db2sa.log"
export DB2SACAT="db2sa.cat"
export INSTANCE_OWNER=""
export ACTION=""                # add or remove

NUM_ARGS=$#

[[ $NUM_ARGS -eq 0 ]] && usage

set -- $(getopt Dvar $* 2> /dev/null)

if [[ $? != 0 ]]; then
    usage
fi

while [[ $1 != -- ]]
do
    case $1 in
        -D) # Verbose on + Debug on
            VERBOSE=1
            DEBUG=1
            shift
            ;;
        -v) # Verbose on
            VERBOSE=1
            shift
            ;;
        -a) # Add the hard link
            if [[ "$REMOVE_LINK" = "1" ]]; then
               usage
            fi
            ACTION="add"
            ADD_LINK=1
            shift
            ;;
        -r) # Remove the hard link
            if [[ "$ADD_LINK" = "1" ]]; then
               usage
            fi
            ACTION="remove"
            REMOVE_LINK=1
            shift
            ;;
        *)
            usage
            ;;
    esac
done

shift    # get rid of the trailing --

INSTANCE_OWNER=$1

if [[ "$INSTANCE_OWNER" = "" ]]; then
    KLIB_DB2_print_message 1 2 "ERROR: DB2 instance owner required!\n"
    usage
fi

if [[ "$ACTION" = "add" ]]; then
    KLIB_DB2_print_message 6 2 "INFO: Adding Custom SQL AppMon hard link for instance %s.\n" $INSTANCE_OWNER
    [[ -f $DB2SA_SBIN_DIR/cl_db2cmon_$INSTANCE_OWNER ]] && rm -f $DB2SA_SBIN_DIR/cl_db2cmon_$INSTANCE_OWNER
    ln -f $DB2SA_SBIN_DIR/cl_db2cmon $DB2SA_SBIN_DIR/cl_db2cmon_$INSTANCE_OWNER
    if [[ $? -ne 0 ]]; then
        KLIB_DB2_print_message 6 3 "ERROR: Failed creating Custom SQL AppMon hard link for instance %s!\n" $INSTANCE_OWNER
        exit 1
    fi
elif [[ "$ACTION" = "remove" ]]; then
    KLIB_DB2_print_message 6 4 "INFO: Removing Custom SQL AppMon hard link for instance %s.\n" $INSTANCE_OWNER
    if [[ -f $DB2SA_SBIN_DIR/cl_db2cmon_$INSTANCE_OWNER ]]; then
        rm -f $DB2SA_SBIN_DIR/cl_db2cmon_$INSTANCE_OWNER
        if [[ $? -ne 0 ]]; then
            KLIB_DB2_print_message 6 5 "ERROR: Failed removing Custom SQL AppMon hard link for instance %s!\n" $INSTANCE_OWNER
            exit 1
        fi
    else
        KLIB_DB2_print_message 6 7 "WARNING: Custom SQL AppMon hard link does not exist, removal not needed!\n"
        exit 0
    fi
else
    # Should not get here at all!
    KLIB_DB2_print_message 6 6 "ERROR: Unexpected DB2 hard link action encountered!\n"
    usage
fi

exit 0
