#!/bin/bash
#
#########################################################################################
# Script: abs.sh
# Description: startup multiple versions/instances of the App/Rep/Web server 
# use option -h for all options (-h = helpscreen)
# Author: ABS Laundry Business Solutions/ROP
# Version: 		5.7
#### Changelog ##########################################################################
# 1.0, 20080209 - initial version
# 1.2, 20080422 - changed PID-output, Tomcat per slot instead of per server,
#                 small(ish) fixes, added -l option
#      20080424 - bug fixed in pidlist and changed kill method
#      20080428 - added %CPU, sysinfo header and STARTED-column in PIDlist
# 1.3, 20080602 - Added viewing logfiles and changed option PID to STATUS
#                 Added -v option for verbose startup
#                 Added logfile view option 
#                 Added threaddump
#      20080610 - Changed status list behaviour
#                 Added listoption ("-l s" or "-l n")
#                 Implemented better daemonizing of spawned processes
#                 Added usage logging
# 1.4, 20080707 - Added displaynumber offset for Xvfb
#                 Added multislot selection. 
#                 example: ./abs.sh -t app -c restart -s235 will restart APP server in slots 2, 3 and 5
#      20080708 - Added Tomcat memory variable
#                 Added check for Xvfb exists yes/no
#      20080717 - Bug fixed in multi-slot select
#      20080811 - Added HeapDumpOnOutOfMemoryError option to server start functions (app, rep and web)
#      20080819 - Search for %CPU column in TOP-list (no longer hard coded)
#               - Added switch to enable/disable java out of heap heapdump (MEMERRHEAPDUMP variable)
#      20080901 - Cleaned up CPU-column search routine
#      20080902 - Added portlist per process (option -p)
# 1.5, 20080919 - Added creating heapdumps using SDK-tool jmap (new command dump)
#      20080929 - Bugfix in memory allocation for Tomcat (wrong setting was made)
# 1.6, 20081117 - Added local and remote monitoring using jconsole. Requires SDK
#      20081124 - Added options MEMERRTHREADDUMP, MEMERRACTION,
#                 FATALERRTHREADDUMP and FATALERRACTION
#      20090107 - Added -Xms (minimum reserved hep space) on all java lines
# 2.0, 20090109 - Added emailing in case of abssolute crash or usage of script
#      20090203 - Added jvisualvm.
#                 If jvisualvm is not found then jconsole is used to monitor
# 2.1, 20090209 - Moved configurations to a seperate shell script
#                 Added e-mail settings for maintenance mails (on/off per command)
#      20090216 - Added collect command
# 2.2, 20090302 - Script looks for a config file in the current directory. If it does
#                 not find one, it looks in /abs
#               - Added a config-option and a commandliine option to enable JDBC logging
#               - Added settings to the config file for sending console output to a
#                 file in stead of to /dev/null when running as daemon.
#      20090327 - fixed a bug when using ps to get PID's. the width of the standard
#                 ps output could be too small to hold the complete command line
#               - Build in checks to see if directories for oracle_home and java_home
#                 really exist, and if the configured ojdbc_jar is an existing file
#      20090623 - Changed layout of status list
#               - made attachement of scriptlog in e-mails switchable
# 2.3, 20090720 - Implemented glassfish as a seperate service type
#      20090819 - Added ABSSolute version in processlist header
#      20090820 - Added 3-letter licence code in processlist header
# 2.4, 20091124 - Added filter on CC.log
#      20091130 - Check if slots realy exist when doing a -ls or -ln
# 2.5, 20100618 - Added export ORACLE_HOME
#               - Changed "grep -i DBSID=" to "grep -i DBSID=" for exclude DBSID_BATCH property
#               - Removed getpids function from function show_absheader to see down slot contents
# 2.6, 20100707 - Removed sendmail command line option -n -s 
#               - Changed Console log name from report server to ReportServerConsoleLog
# 2.7, 20101008 - Added logrotate funtionality for ABSSolute logfiles (app, rep, CC)
# 3.0, 20110214 - Added resourcefile definition to config file
#                 Show build date and time for server.jar and reportserver.jar files
#                 Increased possible slots from 0-9 to 00-99. This means that slots MUST be pointed
#                 out using 2 digits (01 - 99) !!!!!  Use commandline option -k1 to still be able to
#                 use slot 0 - 9 (only 0-9 and for backwards compatibility reasons only). The
#                 standard setting is -k2
# 3.1, 20120406 - Added commands createdomain and deletedomain for type glass
# 3.2, 20130930 - Remove Tomcat support, add glassfish to -t all processing
#      20131001 - Add enable/disable different components per slot
#                 New commands -c enable and -c disable added for this
#      20131001 - If patchversion <> serverjarversion (excluding the pxx part) then
#                   give a warning and do not start server
#                 Version 8.03.08 and up: use java 7.  For this, the java_home entry in
#                   abs_config has been changed to java6_home and java7_home
#                 Version 8.03.09 and up: use different classpath (jars + upper/lower 
#                   case) for reportserver
#      20131031 - Added webapplication deploy (-c deploy)
#      20131213 - Added command isrunning to check if a component is running
# 3.3, 20140707 - FIXED: Version 8.03.06 (was 8.03.09... fixed) and up: use different classpath
#                   (upper/lower case) for reportserver.  Jars still 8.03.09 and up
# 4.0, 20150223 - Added starting/stopping of ABSSolute client, tsmats, tsoutscansort, tsweigh
#                 tspresortscan, tssoilreceiving and tsspecialscans
# 4.1, 20150928 - Added support for v9 (jdk 1.8, gf 4)
# 4.2, 20160118 - Added support for version 9.00.00.b and higher
#                 - changed jar structure
#                 - changed version determination
# 4.3, 20161227 - Correction glassfish version retrieval
#		  		- Correct notation for ABSMENU on RHET 7
#		  		- Correct value for Glassfish 4.1.1 (411)
# 4.4, 20170131 - Added the automatic unattended scriprun option (-A deploy)
# 4.5, 20170220 - ORACLE_HOME and LD_LIBRARY_PATH will be generated based on the OracleHome value
#				  that is defined in the absserver.properties file in the corresponding slot
# 4.6, 20170131 - Added enabled secure-admin after deploy
# 4.7, 20170223 - Changed report server startup command so the cc.log entries can be forwarded to the reportserverlog
# 4.8, 20170223 - Added enabled secure-admin after deploy
# 4.9, 20170704 - Remote restart reports service does not disconnect the ABSmenu ssh session, pressing CTL+C necessary for disconnect.
# 4.10 20170807 - Add Java 1.8_131 for JAVA8_131_HOME
# 4.11 20180928 - Fixed startup without Java 1.8.131 JRE
#               - Added automatic patchrun command
# 5.0  20181227 - Query status of Glassfish domain
#				- Change startup of Xvfb/Glassfish to change group process id
# 5.1  20190107 - Added Xfvb stop if not required
# 5.2  20190108 - Added retry feature on patch scriptrun for possible execution order related issues
# 5.3  20190306 - Added SNMP check on application servers before glassfish startup on complete slot startup
# 5.4  20190324 - Added support to deploy new webapplications: wua=webclient my uniform assistant wrd=webclient residential delivery wrr=webclient route returns
# 5.5  20190404 - Added memory configuration option for app/rep in startable.abs
# 				- Added actions for all slots at once
# 				- Added action to run conversion scripts on database
# 5.6  20190417 - Fix header DB and SID values when report only slot
#				- Config file existence check before startup of application/report server
#
#
# 5.7 CHECK BITBUCKET LOG FOR HISTORY CHANGES
# 
#    DO NOT ADD HISTORY CHANGES ANYMORE
#########################################################################################

#### settings / parameters / variables / etc ############################################
SCRIPTRELEASE="5.7"             # script version
SCRIPTDATE="040417"             # date of last change to script (ddmmyy)
SCRIPTUSER=$(whoami)            # user that started the script. 
SCRIPTUSERID=$(id -u)           # userID that started the script. 
ABS_ROOTDIR=/abs
DELAYAFTERSTART=5               # wait this amount of seconds after starting java as daemon
                                # or starting/stopping glassfish

#### Internal variables /parameters -- DO NOT CHANGE ####################################
LOGDATE=`date +%Y_%V*`
SCRIPTVERSION="$SCRIPTRELEASE"_"$SCRIPTDATE"
APPSRVMAINCLASS="absgroup.bl.Main"
APPSRVGUICLASS="absgroup.forms.MainUI"
REPSRVMAINCLASS="absgroup.reportserver.Main"
REPSRVGUICLASS="absgroup.reportserver.MainUI"
CLIENTTSMATSCLASS="absgroup.ts.mat.TSAppMatStorage"
CLIENTTSOUTSCANSORTCLASS="absgroup.ts.pwb.TSAppOutscanSort"
CLIENTTSPRESORTSCANCLASS="absgroup.ts.pwb.TSAppPresortScan"
CLIENTTSSOILRECEIVINGCLASS="absgroup.ts.pwb.TSAppSoilReceiving"
CLIENTTSSPECIALSCANSCLASS="absgroup.ts.pwb.TSAppSpecialScans"
CLIENTTSWEIGHCLASS="absgroup.ts.weighing.TSAppWeighingTicket"
CLIENTMAINCLASS="absgroup.forms.Main"
TOTSYSMEM=$(free -m | grep -i mem: | awk '{print $2}')
ORG_DISPLAY=$DISPLAY

### Set environment using the abs_config script #########################################
if [ -x abs_config.sh ]
then
    . ./abs_config.sh
else
    if [ -x $ABS_ROOTDIR/abs_config.sh ]
    then
        . $ABS_ROOTDIR/abs_config.sh
    else
        echo "ABS management script - settings not found !  Please check..."
        echo "Aborting action"
        exit 1
    fi
fi

#########################################################################################
make_wideslot()
{
# take $1 and make it $SLOTPOS positions wide
# if $SLOTPOS < length($1) then do not change the slotnumber
    WIDESLOT=$1
    if [ $SLOTPOS -ge ${#WIDESLOT} ]
    then
        WIDESLOT="00000000000000"$1
        WIDESLOT=${WIDESLOT:(-$SLOTPOS)}
    fi
    export WIDESLOT
}

#########################################################################################

snmpcheck_app_server()
{
	# Check SNMP status code. Return SNMP status

	# $1 = SNMP_SERVERPORT
	SNMP_SERVERPORT=$1
	
	SNMPVALUE=$(snmpwalk -c public -r 1 -v 1 udp:${SNMP_SERVERPORT} 1.3.6.1.4.1.227.1.1 | rev | head -c2 | rev | xargs)
	
	[ "$SNMPVALUE" != "1" ] && return 1
	return 0
}

#########################################################################################
show_version()
{
    echo "Version $SCRIPTVERSION"
}

show_usage()
{   
    show_version
	echo "Usage: `basename $0` [-h] [-u] [-v] [-x] [-r <jmxconstr>] [-l <listoption>] [-p] [-t <type>] [-c <command>] [-s <slot>] [-a <abs home>] [-o <oracle home>] [-j <java home>] [-d <logdate>] [-f] [-w webtype:webname:webcontext:weblib]"
	echo "-A          - Start server for updates unattended"
	echo "-P          - Start patchrun on database. Starting scripts after given patchnumber."
	echo "-h          - Show this helpscreen"
	echo "-u          - Start server for updates"
	echo "-g          - Start server with GUI"
    echo "-v          - verbose output while starting server"
    echo "              Only use -v for debugging purposes !"
    echo "-x          - enable JDBC logging"
    echo "-l          - option n: show status tables for all processes/slots"
    echo "              option s: Only shows running processes/slots."
    echo "-p          - Show used portnumbers in the -ls or -ln list"
    echo "-b          - Used with -c deletedomain, -c createdomain , -c statusdomain and -c listdomain"
    echo "              Name of the domain that will be deleted or created"
    echo "              If -b is not used then the setting from abs_config.sh will"
    echo "              be used (standard production)"
    echo "-w          - Used for deploying web application in glassfish"
    echo "              Format: webtype:webname:webcontext:weblib"
    echo "                        webtype    - wpo,gws,ws2,w4m,wp2,wsr,wum, wcl, wua, wrd, wrr, wsb, wmr, wfb" 
    echo "                        webname    - name as it appears in gfish"
    echo "                        webcontext - webpath of the webapp"
	echo "                                     i.e. / or webportal"
	echo "                                    (root / is the only one with /-sign)"
    echo "                        weblib     - extra needed libraries"
    echo "              The warfile must be located in $ABS_ROOTDIR/<slot>/deploy/<webtype>"
    echo "              The name of the warfile must be <webtype>.war"
	echo "type        - app             = ABSSolute application server"
	echo "              rep             = ABSSolute report server"
	echo "              glass           = Glassfish application server"
	echo "              all             = app + rep + glass"
	echo "              wpo             = Web Portal"
	echo "              gws             = Generic Webservice"
	echo "              ws2             = WebService2"
	echo "              w4m             = WebService4Mobile"
	echo "              wp2             = New Web Portal"
	echo "              wsr             = REST WebService"
	echo "              wum             = Web UHF Monitor"
	echo "              wcl             = Web Client"
	echo "              wua             = webclient my uniform assistant"
	echo "              wrd             = webclient residential delivery"
	echo "              wrr             = webclient route returns"
	echo "              wfb             = absmobilefirebaseservices"
    echo "              wsb             = webclient replace smart barcode"
    echo "              wmr             = Mobile RestService"
	echo "              ara             = ABS Route Assistant"
	echo "              client          = main ABSSolute client"
	echo "              tsmats          = mat scanning"
	echo "              tsoutscansort   = residential outscan"
	echo "              tspresortscan   = residential presort"
	echo "              tssoilreceiving = soil receive scan"
	echo "              tsspecialscans  = special scan"
	echo "              tsweigh         = weiging client"
	echo "command     - start        = start ABSSolute app/rep/web server/client"
	echo "              stop         = stop ABSSolute app/rep/web server/client"
	echo "              restart      = restart ABSSolute app/rep/web server/client"
	echo "              status       = show status list of ABSSolute app/rep/web/Xvfb server"
	echo "              gui          = launch ABSSolute app/rep server GUI"
	echo "              log          = show logfile"
	echo "              thread       = create thread dump"
	echo "              dump         = create heap dump (needs JDK)"
	echo "              monitor      = monitor java process with jVisualVM"
	echo "              collect      = collect logfiles, heapdumps and threaddumps and create a TAR-file"
	echo "              deletedomain = remove glassfish domain. Use -b to tell which domain"
	echo "              createdomain = create glassfish domain. Use -b to tell which domain"
	echo "              statusdomain = status glassfish domain. Use -b to tell which domain. If the domain already exists then nothing happens"
	echo "              listdomain   = Returns the webname of all web application in the domain"
	echo "              deploy       = deploy a war in glassfish. This needs option -w"
	echo "              enable       = enable starting of the selected component in the selected slot"
	echo "              disable      = disable starting of the selected component in the selected slot"
	echo "              isrunning    = Check if selected component in the selected slot is running."
	echo "                             If it is, show the PID of the process"
	echo "              maxheap      = Set the maximal heapspace set with -M for the app/rep service set with -t."
	echo "              resetheap    = Removes the custome maximal heapspace set for the app/rep service set with -t."
    echo "jmxconstr   - remote VisualVM monitor connection string (host:port)."
	echo "              does not require a -s, -c or -t option"
    echo "slot        - ABSSolute setup slot number(s)"
	echo "abs home    - installation root directory"
	echo "oracle home - Oracle home directory"
	echo "java home   - Java home directory"
	echo "logdate     - use with -c log --> show log from week logdate"
	echo "              Format used: yyyy_ww, i.e. 2008_12 for year 2008 week 12"
    echo "              the 12th of april 2008"
    echo "-f          - use with -c log --> follow logfile (real-time)"
    echo "-e          - show script version number"
    echo
	echo "Defaults: -t all" 
	echo "          -c status"
	echo "          -s 01"
	echo "          -a /abs"
	echo "          -o /abs/instantclient_11_1"
	echo "          -j /abs/jdk1.6.0_06"
    echo
}

#### Handle commandline options #########################################################
while getopts a:b:c:d:efghij:k:l:mn:o:pqr:s:t:uvw:xy:z:AP:M: OPT
do
	case "$OPT" in
        a)      export ABS_ROOTDIR=$OPTARG;;
		A)		export AUTOUPDATE="yes";;
        b)      export GFDOMAIN=$OPTARG;;
        c)      export ABS_CMD=$OPTARG;;
        d)      export LOGDATE=$OPTARG;;
        e)      export SHOWVERSION="yes";;
        f)      export FOLLOWLOG="yes";;
        g)      export STARTWITHGUI="yes";;
        i)      export CAUSEDBYFATALERR="yes";;
        j)      export JAVAPAR_HOME=$OPTARG;;
        k)      export SLOTPOS=$OPTARG;;
        l)      export LISTALLPIDS="yes"; LISTOPTION=$OPTARG;;
        m)      export CAUSEDBYMEMERR="yes";;
		M)		export MEMORYVALUE=$OPTARG;;
        n)      export SCRIPTUSER=$OPTARG;;
        o)      export ORACLE_HOME=$OPTARG;;
        p)      export SHOWPORTLIST="yes";;
		P)		export PATCHRUN_PREVIOUSPATCH=$OPTARG;;
        q)      export DOEM="abs";;
        r)      export REMOTEMON="yes"; MONHOST=$OPTARG;;
        s)      export ABS_SLOT=$OPTARG;;
        t)      export ABS_TYPE=$OPTARG; [ "$ABS_TYPE" == "glass" ] && export GFONLY=Y;;
        u)      export STARTFORUPDATE="yes"; export STARTWITHGUI="yes";;
        v)      export VERBOSE="-verbose";;
		w)      export WEBLIST=$OPTARG;;
        x)      export JDBCLOGFLAG=true;;
		y)      export DELAYAFTERSTART=$OPTARG;;
        z)      export CRASHFLAG=$OPTARG;;
        ?|h)    show_usage; exit 1;;
    esac
done

if [ "$ABS_TYPE" == web ] 
then
	export ABS_TYPE=glass
	export GFONLY=Y
fi

make_wideslot $ABS_SLOT
export ABS_SLOT=$WIDESLOT

#### Locate needed binaries #############################################################

# try to locate mua
MAILSENDER_FOUND=no
[ -x "$MAILBINPATH" ] && MAILSENDER_FOUND=yes
if [ "$MAILSENDER_FOUND" == "no" ] # if mua not found then do not try to send any e-mail
then
    [ "EMAIL_ON_OOM_FATAL" == on ] && SMNF_MESSAGE=yes
    [ "EMAIL_ON_MAINT" == on ] && SMNF_MESSAGE=yes
    EMAIL_ON_OOM_FATAL=off
    EMAIL_ON_MAINT=off
    [ "SMNF_MESSAGE" == yes ] && echo "* SENDMAIL NOT FOUND *"
fi

# try to locate the unzip executable
[ -x "/bin/unzip" ] && UNZIPPATH="/bin/unzip"
[ -x "/usr/bin/unzip" ] && UNZIPPATH="/usr/bin/unzip"

# try to locate the Xvfb executable
XVFBPATH="-"
[ -x "/usr/X11R6/bin/Xvfb" ] && XVFBPATH="/usr/X11R6/bin/Xvfb"
[ -x "/usr/bin/Xvfb" ] && XVFBPATH="/usr/bin/Xvfb"
if [ "$XVFBPATH" == "-" ] # Xvfb not found in the given locations
then
    echo
    echo "WARNING: Xvfb program not found !  Please install the Xvfb package."
    echo "If it is already installed, then please add the location of the Xvfb executable"
    echo "in the abs.sh script."
    echo
    [ "$NOXVFB_CRITICAL" == yes ] && [ "$GFONLY" == Y ] && exit 1
# if Xvfb not found, and this is not a crtitical situation --> start on current desktop.
    export STARTWITHGUI=yes
fi

if [ ! -z "$DOEM" ] 
then
    EMAIL_ON_OOM_FATAL=off
    EMAIL_ON_MAINT=off
fi

# try to locate the strace executable. Used to create an on-the-fly threaddump
if [ -x "$STRACEPATH/strace" ] 
then
    STRACE_FOUND=yes
else
    if [ -x "/usr/bin/strace" ] 
    then
        STRACE_FOUND=yes
        STRACEPATH="/usr/bin"
    else
        if [ -x "/bin/strace" ] 
        then
            STRACE_FOUND=yes
            STRACEPATH="/bin"
        fi
    fi
fi

# q&d search for what column in top represents %cpu
TOPHEADER=`top -b -n 1 -s | grep -i cpu | grep -i mem | grep -i pid | grep -i command`
export CPUCOLNR=`echo $TOPHEADER | awk '{ for (k=1; k<NF; k++) if ($k == "%CPU") print k }'`

# build the log filename
ABS_LOGDIR="$ABS_ROOTDIR"/log
[ ! -d "$ABS_LOGDIR" ] && mkdir "$ABS_LOGDIR"
[ ! -d "$ABS_LOGDIR" ] && ABS_LOGDIR="$ABS_ROOTDIR"
SCRIPTLOGNAME=`date +"$ABS_LOGDIR"/abs_%Y_%V.log`

#### Logging ############################################################################
echo "include $ABS_ROOTDIR/$LOGROTCONFDIR" > "$ABS_ROOTDIR/abslogrotate.conf"
[ ! -d "$ABS_ROOTDIR/$LOGROTCONFDIR" ] && mkdir "$ABS_ROOTDIR/$LOGROTCONFDIR"

[ "$REPDEBUGLOGFILTER" == "yes" ] && REPLOGCONSOLE=yes 

setup_logrotate()
{
    # Write logrotate configuration file
    if [ "$1" == "applog" ]
    then
        ABSLOGS_PATH="$ABS_HOME/Server/Log/*.txt"
        ROTATECONF="$ABS_ROOTDIR/$LOGROTCONFDIR/appsrv_$ABS_SLOT"
    fi
    if [ "$1" == "replog" ]
    then
        ABSLOGS_PATH="$ABS_HOME/Reportserver/Log/*.txt"
        ROTATECONF="$ABS_ROOTDIR/$LOGROTCONFDIR/repsrv_$ABS_SLOT"
    fi
    if [ "$1" == "cclog" ]
    then
        ABSLOGS_PATH=`grep -i log.file $ABS_HOME/Reportserver/Config/crystalclear.properties | grep -v ^# | sed 's/\(.*\)\(log.file=\)\(.*\)/\3/'`
        ROTATECONF="$ABS_ROOTDIR/$LOGROTCONFDIR/repcc_$ABS_SLOT"
    fi
        
    if [ "$USELOGROTATE" == "yes" ]
    then
        echo "$ABSLOGS_PATH {" > "$ROTATECONF"
        echo "    compress" >> "$ROTATECONF"
        echo "    copytruncate" >> "$ROTATECONF"
        [ $LOGROTMAXAGE != 0 ] && echo "    maxage $LOGROTMAXAGE" >> "$ROTATECONF"
        echo "    missingok" >> "$ROTATECONF"
        echo "    noolddir" >> "$ROTATECONF"
        echo "    notifempty" >> "$ROTATECONF"
        echo "    rotate $LOGROTKEEP" >> "$ROTATECONF"
        echo "    size $LOGROTMAXSIZE" >> "$ROTATECONF"
        echo "}" >> "$ROTATECONF"
    else
        [ -e "$ROTATECONF" ] && rm -f "$ROTATECONF"
    fi
}

log()   # add an entry (line heading) to the logfile
{
    echo -n `date +%T` >> $SCRIPTLOGNAME
    echo -n "  " >> $SCRIPTLOGNAME
}

logline()
{
	log
	echo $1 >> $SCRIPTLOGNAME
}
touch $SCRIPTLOGNAME
echo "-------------------------------------------------------------------------------" >> $SCRIPTLOGNAME
log; echo "User $SCRIPTUSER (ID=$SCRIPTUSERID) started $0 on `date +%d-%b-%Y`" >> $SCRIPTLOGNAME
if [ "$CAUSEDBYMEMERR" ]
then
    log; echo '***** ACTIONS CAUSED BY AN OUT-OF-MEMORY SITUATION *****' >> $SCRIPTLOGNAME
fi
if [ "$CAUSEDBYFATALERR" ]
then
    log; echo '***** ACTIONS CAUSED BY A FATAL ERROR SITUATION *****' >> $SCRIPTLOGNAME
fi
log; echo -n "Used parameters: " >> $SCRIPTLOGNAME
for a in $*
do
    echo -n "$a " >> $SCRIPTLOGNAME
done
echo >> $SCRIPTLOGNAME
log; echo -n "All actions done in slot" >> $SCRIPTLOGNAME
SLEN=${#ABS_SLOT}
if [ $SLEN -eq $SLOTPOS ]
then
    echo " $ABS_SLOT" >> $SCRIPTLOGNAME
else
    echo -n "s " >> $SCRIPTLOGNAME
    for ((a=0; a<$SLEN; a+=$SLOTPOS))
    do
        echo -n " ${ABS_SLOT:$a:$SLOTPOS} " >> $SCRIPTLOGNAME
    done
    echo >> $SCRIPTLOGNAME
fi

#### Check important paths from the config file #########################################
if [ "$GFONLY" = N ]
then
    if [ ! -d "$ORACLE_HOME" ]
    then
        echo "The ORACLE_HOME setting ($ORACLE_HOME) points to a non-existing"
        echo "directory. Please check the configuration script."
        log; echo "ORACLE_HOME ($ORACLE_HOME) not found" >> $SCRIPTLOGNAME
        exit 1
    fi

    export ORACLE_HOME

    if [ ! -f "$ORACLE_HOME"/"$OJDBC_JAR" ]
    then
        echo "The OJDBC_JAR setting ($OJDBC_JAR) points to a non-existing"
        echo "file. Please check the configuration script."
        log; echo "OJDBC_JAR ($OJDBC_JAR) not found" >> $SCRIPTLOGNAME
        exit 1
    fi
fi

##### Initial settings ##################################################################
getcpu()
{
    TMP_CPU=`echo "$TOPLIST" | grep $1`
    export SHR_CPU=`echo $CPUCOLNR $TMP_CPU | awk '{n=$1+1; print $n}'`
}

checkclientpid()
{
    export CLIENTPID=`echo "$LSTPIDCMD" | grep $1 | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export CLIENTMP=`echo "$LSTPMEMCMD" | grep $1 | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export CLIENTMB=`echo $APPSRVMP $TOTSYSMEM | awk '{printf "%7.2f",($1/100*$2)}'`
    export CLIENTSTIME=`echo "$LSTSTARTTIMECMD" | grep $1 | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
}

getpids()
{
# $1 = slotnr
    LSTPIDCMD=`ps --cols 2000 -eo pid,cmd`
    LSTPIDSTARTTIME=`ps --cols 2000 -eo pid,start_time`
    LSTPMEMCMD=`ps --cols 2000 -eo pmem,cmd`
    LSTSTARTTIMECMD=`ps --cols 2000 -eo start_time,cmd`
    [ $ABSSOLUTENUMVERSION -ge 90504 ] && APPSRVMAINCLASS="server-impl.jar"
    export XVIRTPID=`echo "$LSTPIDCMD" | grep Xvfb | grep ":$XVFBOFFSET$1" | awk '{print $1}'`
    export XVIRTMP=`echo "$LSTPMEMCMD" | grep Xvfb | grep ":$XVFBOFFSET$1" | awk '{print $1}'`
    export XVIRTMB=`echo $XVIRTMP $TOTSYSMEM | awk '{printf "%7.2f",($1/100*$2)}'`
    [ "$XVIRTPID" ] && export XVIRTSTIME=`echo "$LSTPIDSTARTTIME" | grep $XVIRTPID | awk '{print $2}'`
    
    export APPSRVPID=`echo "$LSTPIDCMD" | grep $APPSRVMAINCLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export APPSRVMP=`echo "$LSTPMEMCMD" | grep $APPSRVMAINCLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export APPSRVMB=`echo $APPSRVMP $TOTSYSMEM | awk '{printf "%7.2f",($1/100*$2)}'`
    export APPSRVSTIME=`echo "$LSTSTARTTIMECMD" | grep $APPSRVMAINCLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    
    export APPGUIPID=`echo "$LSTPIDCMD" | grep $APPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export APPGUIMP=`echo "$LSTPMEMCMD" | grep $APPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export APPGUIMB=`echo $APPGUIMP $TOTSYSMEM | awk '{printf "%7.2f",($1/100*$2)}'`
    export APPGUISTIME=`echo "$LSTSTARTTIMECMD" | grep $APPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    
    export REPSRVPID=`echo "$LSTPIDCMD" | grep $REPSRVMAINCLASS | grep -v $REPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export REPSRVMP=`echo "$LSTPMEMCMD" | grep $REPSRVMAINCLASS | grep -v $REPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export REPSRVMB=`echo $REPSRVMP $TOTSYSMEM | awk '{printf "%7.2f",($1/100*$2)}'`
    export REPSRVSTIME=`echo "$LSTSTARTTIMECMD" | grep $REPSRVMAINCLASS | grep -v $REPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    
    export REPGUIPID=`echo "$LSTPIDCMD" | grep $REPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export REPGUIMP=`echo "$LSTPMEMCMD" | grep $REPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export REPGUIMB=`echo $REPGUIMP $TOTSYSMEM | awk '{printf "%7.2f",($1/100*$2)}'`
    export REPGUISTIME=`echo "$LSTSTARTTIMECMD" | grep $REPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`

    export GLASSPID=`echo "$LSTPIDCMD" | grep -i glassfish | grep -iv grep | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export GLASSMP=`echo "$LSTPMEMCMD" | grep -i glassfish | grep -iv grep | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    export GLASSMB=`echo $GLASSMP $TOTSYSMEM | awk '{printf "%7.2f",($1/100*$2)}'`
    export GLASSSTIME=`echo "$LSTSTARTTIMECMD" | grep -i glassfish | grep -iv grep | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    
    export CLIENTPID=`echo "$LSTPIDCMD" | grep $CLIENTMAINCLASS | grep -v $APPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    if [ "$CLIENTPID" ]
    then
        export CLIENTMP=`echo "$LSTPMEMCMD" | grep $CLIENTMAINCLASS | grep -v $APPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
        export CLIENTMB=`echo $CLIENTMP $TOTSYSMEM | awk '{printf "%7.2f",($1/100*$2)}'`
        export CLIENTSTIME=`echo "$LSTSTARTTIMECMD" | grep $CLIENTMAINCLASS | grep -v $APPSRVGUICLASS | grep $ABS_ROOTDIR/$1 | awk '{print $1}'`
    else
        checkclientpid $CLIENTTSMATSCLASS
        [ ! "$CLIENTPID" ] && checkclientpid $CLIENTTSOUTSCANSORTCLASS
        [ ! "$CLIENTPID" ] && checkclientpid $CLIENTTSPRESORTSCANCLASS
        [ ! "$CLIENTPID" ] && checkclientpid $CLIENTTSSOILRECEIVINGCLASS
        [ ! "$CLIENTPID" ] && checkclientpid $CLIENTTSSPECIALSCANSCLASS
        [ ! "$CLIENTPID" ] && checkclientpid $CLIENTTSWEIGHCLASS
    fi
    
    if [ -z $TOPLIST_DONE ]
    then
        export TOPLIST_DONE=yes
        [ -f ~/.toprc ] && mv ~/.toprc ~/toprc_tmp.abs
        export TOPLIST=`top -b -n 1 -s`
        [ -f ~/toprc_tmp.abs ] && mv ~/toprc_tmp.abs ~/.toprc
    fi
    
    export XVIRTCPU=
    export APPSRVCPU=
    export APPGUICPU=
    export REPSRVCPU=
    export REPGUICPU=
    export GLASSCPU=
    export CLIENTCPU=
    [ "$XVIRTPID" ] &&  getcpu "$XVIRTPID";  export XVIRTCPU=$SHR_CPU
    [ "$APPSRVPID" ] && getcpu "$APPSRVPID"; export APPSRVCPU=$SHR_CPU
    [ "$APPGUIPID" ] && getcpu "$APPGUIPID"; export APPGUICPU=$SHR_CPU
    [ "$REPSRVPID" ] && getcpu "$REPSRVPID"; export REPSRVCPU=$SHR_CPU    
    [ "$REPGUIPID" ] && getcpu "$REPGUIPID"; export REPGUICPU=$SHR_CPU    
    [ "$GLASSPID" ] &&  getcpu "$GLASSPID";  export GLASSCPU=$SHR_CPU    
    [ "$CLIENTPID" ] && getcpu "$CLIENTPID"; export CLIENTCPU=$SHR_CPU

    STRIPPEDSLOT=`echo $1 | sed 's/^0*//'`
    export JMONAPPPORT=$(($JMONAPPBASEPORT + $STRIPPEDSLOT*$SLOT_MULT))
    export JMONREPPORT=$(($JMONREPBASEPORT + $STRIPPEDSLOT*$SLOT_MULT))
    export JMONWEBPORT=$(($JMONWEBBASEPORT + $STRIPPEDSLOT*$SLOT_MULT))
}

##### Functions / procedures ############################################################
checkifrunning()
{
	unset ABSRUNNINGPID
	case "$ABS_TYPE" in
		app)				        	   ABSRUNNINGPID=$APPSRVPID;;
		rep)                        	   ABSRUNNINGPID=$REPSRVPID;;
		glass|wpo|gws|ws2|w4m|wp2|wsr|wum|wcl|wua|wrd|wrr|wsb|wmr|wfb) ABSRUNNINGPID=$GLASSPID;;
        tsmats|tsoutscansort|tspresortscan|tssoilreceiving|tsspecialscans|tsweigh|client)
                                    ABSRUNNINGPID=$CLIENTPID;;
    esac
	[ $ABSRUNNINGPID ] && echo $ABSRUNNINGPID
}

startxvirt()
{	
    if [ -z "$STARTFORUPDATE" ]
    then
        export DISPLAY=":$XVFBOFFSET$ABS_SLOT$XVFBTAIL"
        if [ -z "$XVIRTPID" ]
        then
            echo "$MSGINDENT Starting and daemonizing Xvfb"
            cd /
			setsid nohup $XVFBPATH $DISPLAY < /dev/null > /dev/null 2>&1 &
			cd $ABS_HOME
            log; echo "Xvfb started" >> $SCRIPTLOGNAME
        fi
    fi
}

kill_abs_process()
{   
	# $1 = PID to be killed
    kill $1
    for ((a=1; a<=25; a++)) # wait 25 seconds max. for kill to complete
    do
        sleep 1
        STILLACTIVE=`ps -eo pid | grep "$1"`
        [ -z "$STILLACTIVE" ] && break  # PID no longer exists --> processed killed
        echo -n "."
    done
    if [ "$STILLACTIVE" ]   # process is still active...  use a bigger gun
    then
        kill -9 $1
        echo "    Done (using -9 option)"
    else
        echo "    Done"
    fi
}

stopxvfb()
{	
	if [ "$XVIRTPID" ]
	then
		echo -n "$MSGINDENT Stopping Xvfb (pid $XVIRTPID)"
		kill_abs_process $XVIRTPID
		log; echo "Stopped Xvfb" >> $SCRIPTLOGNAME
	fi
}

stopappserver()
{	
    if [ "$APPGUIPID" ] 
    then
		echo -n "$MSGINDENT Stopping ABSSolute application server GUI (pid $APPGUIPID)"
		kill_abs_process $APPGUIPID
		log; echo "Stopped ABSSolute application server GUI" >> $SCRIPTLOGNAME
		unset APPGUIPID
	fi
	if [ "$APPSRVPID" ] 
    then
		echo -n "$MSGINDENT Stopping ABSSolute application server (pid $APPSRVPID)"
		kill_abs_process $APPSRVPID
		log; echo "Stopped ABSSolute application server" >> $SCRIPTLOGNAME
		unset APPSRVPID
	fi
}

stoprepserver()
{	
	if [ "$REPGUIPID" ] 
	then
		echo -n "$MSGINDENT Stopping ABSSolute reportserver GUI (pid $REPGUIPID)"
		kill_abs_process $REPGUIPID
		log; echo "Stopped ABSSolute reportserver GUI" >> $SCRIPTLOGNAME
		unset REPGUIPID
	fi
	if [ "$REPSRVPID" ] 
	then
		echo -n "$MSGINDENT Stopping ABSSolute reportserver (pid $REPSRVPID)"
		kill_abs_process $REPSRVPID
		log; echo "Stopped ABSSolute reportserver" >> $SCRIPTLOGNAME
		unset REPSRVPID
	fi
}

stopglassfish()
{	
    if [ "$GLASSPID" ] 
    then
        echo -n "$MSGINDENT Stopping Glassfish (pid $GLASSPID) "
        $ABS_HOME/$GLASSFISHPATH/bin/asadmin stop-domain $GFDOMAIN
        STILLACTIVE=`ps -eo pid | grep "$GLASSPID"`
		echo "Still active? $STILLACTIVE"
        [ "$STILLACTIVE" ] && kill -9 $GLASSPID
        log; echo "Stopped Glassfish" >> $SCRIPTLOGNAME
		unset GLASSPID
    fi
}

stopglassfishapp()
{
    if [ "$GLASSPID" ] 
    then
        case $1 in
            wpo)    WEBNAME="$WEBPORTALNAME";;
            gws)    WEBNAME="$GENERICWEBSERVICENAME";;
            ws2)    WEBNAME="$WEBSERVICE2NAME";;
            w4m)    WEBNAME="$MOBILEWEBSERVICENAME";;
            wp2)    WEBNAME="$WEBPORTAL2NAME";;
            wsr)    WEBNAME="$WEBRESTSERVICENAME";;
            wum)    WEBNAME="$WEBUHFMONNAME";;
            wcl)    WEBNAME="$WEBCLIENTNAME";;
            wua)    WEBNAME="$WEBCLIENTMYUNIFORMASSISTANT";;
            wrd)    WEBNAME="$WEBCLIENTRESIDENTIALDELIVERY";;
            wrr)    WEBNAME="$WEBCLIENTROUTERETURNS";;
            wsb)    WEBNAME="$WEBCLIENTREPLACESMARTBARCODE";;
            wmr)    WEBNAME="$MOBILERESTSERVICENAME";;
            wfb)    WEBNAME="$MOBILEFIREBASESERVICESNAME";;
            ara)    WEBNAME="$ABSROUTEASSISTANDNAME";;
        esac
        GFADMINPORT="5"$ABS_SLOT"34"
        [ ${#GFADMINPORT} == 4 ] && GFADMINPORT="50"$ABS_SLOT"34"
        echo "$MSGINDENT Stopping Glassfish application $WEBNAME"
        PWFILE="$ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN/config/absgfpwd.txt"
        echo "AS_ADMIN_PASSWORD=$GLASSADMINPWD" > $PWFILE
        $ABS_HOME/$GLASSFISHPATH/bin/asadmin --passwordfile=$PWFILE --port $GFADMINPORT --user $GLASSADMINUSR disable $WEBNAME
        log; echo "Stopped Glassfish application $WEBNAME" >> $SCRIPTLOGNAME
        rm -f $PWFILE
    fi
}

checkXvfb()
{
    if [ -z "$APPSRVPID" ] && [ -z "$REPSRVPID" ] && [ -z "$GLASSPID" ]
	then
		stopxvfb
	fi
}

#### Build java commandline for starting servers ########################################
# DO _NOT_ TOUCH THESE !
build_errcmd_line()
{
    ERR_MEMFATAL="$1"   # memory error or fatal error?
    ERR_HEAPDMP="$2"    # start heapdump on/off
    ERR_THREADDMP="$3"  # start threaddump on/off
    ERR_ACTION="$4"     # kill process, restart process or do nothing?

    if [ "$ERR_MEMFATAL" == "mem" ]
    then
        JAVAXXOPT="OnOutOfMemoryError"  # set the OnOutOfMemoryError command
        TELLMEMFATAL="-m"               # tell the script the action was caused by a memory error
    else
        JAVAXXOPT="OnError"             # set the OnError command
        TELLMEMFATAL="-i"               # tell the script the action was caused by a fatal error
    fi

    BUILDFLAG=""
    [ "$ERR_THREADDMP" == "on" ] && BUILDFLAG="T$BUILDFLAG"
    [ "$ERR_HEAPDMP" == "on" ] && BUILDFLAG="H$BUILDFLAG"
    case "$ERR_ACTION" in
        kill)       BUILDFLAG="K$BUILDFLAG";;
        restart)    BUILDFLAG="R$BUILDFLAG";;
    esac

    [ "$BUILDFLAG" == "" ] && BUILDFLAG="X"

    ABSTMPCMD=""
    if [ "$ABS_TMP_TYPE" == web ]
    then
        ABSTMPCMD="-XX:$JAVAXXOPT=\\\"$ABS_ROOTDIR/abs.sh"
    else
        ABSTMPCMD="-XX:$JAVAXXOPT=\"$ABS_ROOTDIR/abs.sh"
    fi
    ABSTMPCMD="$ABSTMPCMD $TELLMEMFATAL -s $ABS_SLOT -t"
    case "$ABS_TMP_TYPE" in
        app)    ABSTMPCMD="$ABSTMPCMD app";;
        rep)    ABSTMPCMD="$ABSTMPCMD rep";;
        web)    ABSTMPCMD="$ABSTMPCMD web";;
    esac
    if [ "$ABS_TMP_TYPE" == "web" ]
    then
        ABSTMPCMD="$ABSTMPCMD -c crash -z $BUILDFLAG\\\""
    else
        ABSTMPCMD="$ABSTMPCMD -c crash -z $BUILDFLAG\""
    fi
    export ABSERRORCMD="$ABSTMPCMD"
}

build_java_line()
{
    if [ -e $ABS_HOME/abshost ]
    then
        ABSRMIHOST=$(cat $ABS_HOME/abshost)
    else
        ABSRMIHOST=$ABSHOST
    fi

    case "$ABS_TMP_TYPE" in
        app)
            export APPJAVALINE=""
            [ "$VERBOSE" ] && export APPJAVALINE="$VERBOSE"
			
            CUSTOMHEAP=$(grep APP $STARTFLAGFILE | awk '{ print $3 }')
			if [ -z "$CUSTOMHEAP" ] || [[ $CUSTOMHEAP =~ [^[:digit:]] ]]
			then 
				export APPJAVALINE="$APPJAVALINE -Dfile.encoding=Cp1252 -Xmx$APPSRVMAXMEM -XX:HeapDumpPath=$ABS_HOME/Server/Log"
			else
				export APPJAVALINE="$APPJAVALINE -Dfile.encoding=Cp1252 -Xmx${CUSTOMHEAP}m -XX:HeapDumpPath=$ABS_HOME/Server/Log"
			fi
			
			[ $ABSSOLUTENUMVERSION -lt 90400 ] && export APPJAVALINE="$APPJAVALINE -XX:MaxPermSize=$APPMAXPERM"
            export APPJAVALINE="$APPJAVALINE -Djava.util.prefs.userRoot=$JAVA_HOME/userprefs.app"
            export APPJAVALINE="$APPJAVALINE -Djava.util.prefs.systemRoot=$JAVA_HOME/systemprefs.app"
            export APPJAVALINE="$APPJAVALINE -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMONAPPPORT -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
            export APPJAVALINE="$APPJAVALINE -Dsun.java2d.noddraw=true -Doracle.jdbc.Trace=$JDBCLOGFLAG"
            [ "$JDBCLOGFLAG" == "true" ] && export APPJAVALINE="$APPJAVALINE -Djava.util.logging.config.file=$ABS_HOME/Server/Config/jdbclogging.properties"
            
			if [ $ABSSOLUTENUMVERSION -lt 90504 ]
			then
				export APPJAVALINE="$APPJAVALINE -Djava.rmi.server.hostname=$ABSRMIHOST $APPSRVMAINCLASS -I $ABS_HOME/Server/Lib"
			else
				export APPJAVALINE="$APPJAVALINE -Djava.rmi.server.hostname=$ABSRMIHOST "
				export APPJAVALINE="$APPJAVALINE -cp $ABS_HOME/Server/Lib/server-impl.jar -Dloader.path=\"$ABS_HOME/Server/Lib/*\" org.springframework.boot.loader.PropertiesLauncher -I $ABS_HOME/Server/Lib --spring.config.location=$ABS_HOME/Server/Config/application.properties"
			fi
			
			[ "$STARTFORUPDATE" ] && export APPJAVALINE="$APPJAVALINE -U"
			[ "$AUTOUPDATE" ] && export APPJAVALINE="$APPJAVALINE -AU";;
        rep)
            export REPJAVALINE=""
            [ "$VERBOSE" ] && export REPJAVALINE="$VERBOSE"
			
			CUSTOMHEAP=$(grep REP $STARTFLAGFILE | awk '{ print $3 }')
			if [ -z "$CUSTOMHEAP" ] || [[ $CUSTOMHEAP =~ [^[:digit:]] ]]
			then 
				export REPJAVALINE="$REPJAVALINE -Xmx$REPSRVMAXMEM -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log"
			else
				export REPJAVALINE="$REPJAVALINE -Xmx${CUSTOMHEAP}m -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log"
			fi
			
            [ $ABSSOLUTENUMVERSION -lt 90000 ] && export REPJAVALINE="$REPJAVALINE -XX:MaxPermSize=$REPMAXPERM"
            export REPJAVALINE="$REPJAVALINE -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMONREPPORT -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
            export REPJAVALINE="$REPJAVALINE -Dsun.java2d.noddraw=true -Doracle.jdbc.Trace=$JDBCLOGFLAG" 
            [ "$JDBCLOGFLAG" == "true" ] && export REPJAVALINE="$REPJAVALINE -Djava.util.logging.config.file=$ABS_HOME/Server/Config/jdbclogging.properties"
            export REPJAVALINE="$REPJAVALINE -Djava.rmi.server.hostname=$ABSRMIHOST $REPSRVMAINCLASS -I $ABS_HOME/Reportserver/Lib";;
    esac
    build_errcmd_line mem $MEMERRHEAPDUMP $MEMERRTHREADDUMP $MEMERRACTION
    export MEMERRCMD="$ABSERRORCMD"
    build_errcmd_line fatal $FATALERRHEAPDUMP $FATALERRTHREADDUMP $FATALERRACTION
    export FATALERRCMD="$ABSERRORCMD"
}

#### Setup classpath for app en rep server ##############################################
setclasspath()
{
    case "$1" in
        app) 
			if [ $ABSSOLUTENUMVERSION -ge 90400 -a -e "$ABS_HOME/Server/Lib/server-impl.jar" ]
			then
				export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/*
			else
                export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/commons-beanutils-1.8.3.jar
                export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/commons-logging-1.2.jar
                if [ $ABSSOLUTENUMVERSION -ge 90000 -a -e "$ABS_HOME/Server/Lib/server-impl.jar" ]
                then
                    export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/patchserver-api.jar
					export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/patchserver-common.jar
                    export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/patchserver-impl.jar
                    export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/server-api.jar
					export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/server-common.jar
                    export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/server-impl.jar
                else
                    export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/patchserver.jar
                    export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/server.jar
                    if [ $ABSSOLUTENUMVERSION -ge 80306 ]
                    then
                        export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/reportserver.jar
                    else
                        export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/ReportServer.jar
                    fi
                    export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/jbcl.jar
                fi
                export CLASSPATH=$CLASSPATH:$ORACLE_HOME/$OJDBC_JAR
                export CLASSPATH=$CLASSPATH:${CP_RESOURCES//__homedir__/$ABS_HOME}
                export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/ons.jar
			fi
			;;
        *)  
			if [ $ABSSOLUTENUMVERSION -ge 90504 -a -e "$ABS_HOME/Reportserver/Lib/server-api.jar" ]
			then
				export CLASSPATH=$CLASSPATH:$ABS_HOME/Reportserver/Lib/*
			else
				if [ $ABSSOLUTENUMVERSION -ge 90400 -a -e "$ABS_HOME/Server/Lib/server-impl.jar" ]
				then
					export CLASSPATH=$CLASSPATH:$ABS_HOME/Reportserver/Lib/*
					export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/*
				else
					if [ $ABSSOLUTENUMVERSION -ge 90000 -a -e "$ABS_HOME/Server/Lib/server-impl.jar" ]
					then
						export CLASSPATH=$CLASSPATH:$ABS_HOME/Reportserver/Lib/ClearReports.jar
						export CLASSPATH=$CLASSPATH:$ABS_HOME/Reportserver/Lib/reportserver.jar
						export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/patchserver-api.jar
						export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/patchserver-common.jar
						export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/server-api.jar
						export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/server-common.jar
					else
						if [ $ABSSOLUTENUMVERSION -ge 80309 ]
						then
							export CLASSPATH=$ABS_HOME/Reportserver/Lib/CCservlet-api.jar
							export CLASSPATH=$CLASSPATH:$ABS_HOME/Reportserver/Lib/ClearReports.jar
						else
							export CLASSPATH=$ABS_HOME/Reportserver/Lib/CrystalClear.jar
						fi
						if [ $ABSSOLUTENUMVERSION -ge 80306 ]
						then
							export CLASSPATH=$CLASSPATH:$ABS_HOME/Reportserver/Lib/reportserver.jar
						else
							export CLASSPATH=$CLASSPATH:$ABS_HOME/Reportserver/Lib/ReportServer.jar
						fi
						export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/patchserver.jar
						export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/server.jar
					fi
					export CLASSPATH=$CLASSPATH:$ABS_HOME/Reportserver/Lib/JBarcodeBean.jar
					export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib/jbcl.jar
					export CLASSPATH=$CLASSPATH:${CP_RESOURCES//__homedir__/$ABS_HOME}
					export CLASSPATH=$CLASSPATH:$ABS_HOME/Server/Lib
					export CLASSPATH=$CLASSPATH:$ORACLE_HOME/$OJDBC_JAR
				fi
			fi
			;;
    esac
}

abs_sleep()
{   
# $1 = seconds to delay
    if [ "$1" -ge 1 ]
    then
        echo -n "$MSGINDENT Waiting $1 second"
        [ "$1" -gt 1 ] && echo -n s
        echo -n " to allow process startup to finish"
        for ((a=1; a<=$1; a++))
        do
            sleep 1
            echo -n "."
        done
        echo "  (done)"
    fi
}


#### Start / stop server processes ######################################################
checkstartflagfile()
{
    [ ! -d "$ABS_HOME" ] && mkdir -p "$ABS_HOME"
    export STARTFLAGFILE="$ABS_HOME/startable.abs"
    if [ ! -e "$STARTFLAGFILE" ]
    then
        echo "APP 1" > "$STARTFLAGFILE"
        echo "REP 1" >> "$STARTFLAGFILE"
        echo "GLASS 1" >> "$STARTFLAGFILE"
        echo "CLIENT 1" >> "$STARTFLAGFILE"
    fi
}

startappserver()
{
    checkstartflagfile
	PROPERTIESFILE="$ABS_HOME/Server/Config/absserver.properties"
	[ -e $ABS_HOME/Server/Config/application.properties ] && PROPERTIESFILE="$ABS_HOME/Server/Config/application.properties"

		if [ "$SERVERJARVERSION" == "$PATCHJARMAJORVERSION" ]
		then
			CHKAPP=`grep APP $STARTFLAGFILE | awk '{ print $1,$2 }' `
			if [ "$CHKAPP" == "APP 1" ]
			then
				if [ -f "$PROPERTIESFILE" ] 
				then
					setclasspath app
					startxvirt
					if [ -z "$APPSRVPID" ]
					then
		#
		#           startwith
		#           gui update   action
		#       -----------------------------------------------------------------------
		#        1)  0    0      start as daemon, no gui            daemon=yes gui=no
		#        2)  0/1  1      start with gui and update          daemon=no  gui=update
		#        3)  1    0      start as daemon, then start gui    daemon=yes gui=gui

						if [ -z "$STARTFORUPDATE" ]
						then
							STARTASDAEMON=yes
							STARTGUI=gui
							[ -z "$STARTWITHGUI" ] && STARTGUI=no
						else
							STARTASDAEMON=no    # ... and always start with gui
						fi
			
						echo -n "$MSGINDENT Starting ABSSolute application server..."
						cd $ABS_HOME/Server/Lib
						build_java_line
						log; echo "Starting ABSSolute application server as daemon" >> $SCRIPTLOGNAME
						if [ "$STARTASDAEMON" == "yes" ]
						then
							# build console output file name and start and daemonize the applicationserver process
							if [ "$APPLOGCONSOLE" == "no" ] 
							then
								if [ "$JAVA_TMP_DIR" ]
								then
									nohup java -Djava.io.tmpdir="$JAVA_TMP_DIR" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Server/Log "$MEMERRCMD" "$FATALERRCMD" $APPJAVALINE < /dev/null > /dev/null 2>&1 &
								else
									nohup java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Server/Log "$MEMERRCMD" "$FATALERRCMD" $APPJAVALINE < /dev/null > /dev/null 2>&1 &
								fi
							else
								CONSOLEFILE=`date +"$ABS_HOME"/Server/Log/ServerConsoleLog_%Y_%V.txt`
								touch "$CONSOLEFILE"
								if [ "$JAVA_TMP_DIR" ]
								then
									nohup java -Djava.io.tmpdir="$JAVA_TMP_DIR" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Server/Log "$MEMERRCMD" "$FATALERRCMD" $APPJAVALINE < /dev/null >> "$CONSOLEFILE" 2>&1 &
								else
									nohup java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Server/Log "$MEMERRCMD" "$FATALERRCMD" $APPJAVALINE < /dev/null >> "$CONSOLEFILE" 2>&1 &
								fi
							fi
							echo "     Started as daemon "
						else
							# start and detach the applicationserver process
							if [ "$JAVA_TMP_DIR" ]
							then
								java -Djava.io.tmpdir="$JAVA_TMP_DIR" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Server/Log "$MEMERRCMD" "$FATALERRCMD" $APPJAVALINE &
							else
								java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Server/Log "$MEMERRCMD" "$FATALERRCMD" $APPJAVALINE &
							fi
							[ -z "$VERBOSE" ] && echo "     Started (NOT daemonised)"
							[ "$VERBOSE" ] && echo "     Started with console I/O (NOT daemonised)"
							log; 
							echo -n "Started ABSSolute application server " >> $SCRIPTLOGNAME
							[ -z "$VERBOSE" ] && echo "(update mode, NOT daemonised)" >> $SCRIPTLOGNAME
							[ "$VERBOSE" ] && echo "with console I/O (update mode, NOT daemonised)" >> $SCRIPTLOGNAME
						fi
						abs_sleep $DELAYAFTERSTART
						cd $ABS_HOME
						[ "$LOGROTAPPSRV" == "yes" ] && setup_logrotate applog
						[ "$STARTGUI" == "gui" ] && startappgui
					else
						echo "$MSGINDENT ABSSolute application server already running (PID = $APPSRVPID)"
						log; echo "Attemped to start ABSSolute application server, but server is already running" >> $SCRIPTLOGNAME
					fi
				else
					echo "$MSGINDENT Not starting application server: Config file $PROPERTIESFILE not found !"
					log; echo "$MSGINDENT Not starting application server: Config file $PROPERTIESFILE not found !" >> $SCRIPTLOGNAME
				fi
			else
				echo "$MSGINDENT ABSSolute application server is disabled !"
				log; echo "Attemped to start ABSSolute Application server, but server is disabled" >> $SCRIPTLOGNAME
			fi
		else
			echo "$MSGINDENT Difference in server.jar ($SERVERJARVERSION) and patchserver.jar ($PATCHJARMAJORVERSION) major version !"
			log; echo "Difference in server.jar ($SERVERJARVERSION) and patchserver.jar ($PATCHJARMAJORVERSION) major version !" >> $SCRIPTLOGNAME
		fi
}

check_logflags()
{
    if [ "$REPDEBUGLOGFILTER" == "yes" ] 
    then
        if [ "$ABS_CC_ENABLED" == "no" ]
        then
            echo ""
            echo "*** WARNING : Report debug logging is set to YES while the reportengine CC.log setting is set to NO"
            echo ""
            log; echo "*** WARNING : Report debug logging is set to YES while the reportengine CC.log setting is set to NO" >> $SCRIPTLOGNAME
        fi
    else
        if [ "$ABS_CC_ENABLED" == "yes" ]
        then
            echo ""
            echo "*** WARNING : Report debug logging is set to NO while the reportengine CC.log setting is set to YES"
            echo ""
            log; echo "*** WARNING : Report debug logging is set to NO while the reportengine CC.log setting is set to YES" >> $SCRIPTLOGNAME
        fi
    fi
}

startrepserver()
{	
    checkstartflagfile
	CHKREP=`grep REP $STARTFLAGFILE | awk '{ print $1,$2 }' `
    if [ "$CHKREP" == "REP 1" ]
	then
		if [ -f "$ABS_HOME/Reportserver/Config/crystalclear.properties" ]
		then
			setclasspath rep
			startxvirt
			if [ -z "$REPSRVPID" ]
			then
				echo -n "$MSGINDENT Starting ABSSolute reportserver..."
				cd $ABS_HOME/Reportserver/Lib
				build_java_line

				if [ "`egrep -i --line-buffered ^log.engine=true $ABS_HOME/Reportserver/Config/crystalclear.properties`" == "" ]
				then
					export ABS_CC_ENABLED=no
				else
					export ABS_CC_ENABLED=yes
				fi
				check_logflags
			
				if [ -z "$VERBOSE" ] 
				then
					log; echo "Started ABSSolute reportserver as daemon" >> $SCRIPTLOGNAME
					# build console output file name and start and daemonize the reportserver process
					if [ "$REPLOGCONSOLE" == "no" ] 
					then
							if [ "$JAVA_TMP_DIR" ]
							then
								nohup java -Djava.io.tmpdir="$JAVA_TMP_DIR" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log "$MEMERRCMD" "$FATALERRCMD" $REPJAVALINE  < /dev/null > /dev/null 2>&1 &
							else
								nohup java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log "$MEMERRCMD" "$FATALERRCMD" $REPJAVALINE  < /dev/null > /dev/null 2>&1 &
							fi
					else
						CONSOLEFILE=`date +"$ABS_HOME"/Reportserver/Log/ReportServerConsoleLog_%Y_%V.txt`
						touch "$CONSOLEFILE"
						if [ "$REPDEBUGLOGFILTER" == "yes" ] 
						then
							if [ "$JAVA_TMP_DIR" ]
							then
								(nohup java -Djava.io.tmpdir="$JAVA_TMP_DIR" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log "$MEMERRCMD" "$FATALERRCMD" $REPJAVALINE | egrep --line-buffered "$REPDEBUGLOGFILTERVALUE") < /dev/null >> "$CONSOLEFILE" 2>&1 &
							else
								(nohup java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log "$MEMERRCMD" "$FATALERRCMD" $REPJAVALINE | egrep --line-buffered "$REPDEBUGLOGFILTERVALUE") < /dev/null >> "$CONSOLEFILE" 2>&1 &
							fi
						else
							if [ "$JAVA_TMP_DIR" ]
							then
								nohup java -Djava.io.tmpdir="$JAVA_TMP_DIR" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log "$MEMERRCMD" "$FATALERRCMD" $REPJAVALINE >> "$CONSOLEFILE" 2>&1 &
							else
								nohup java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log "$MEMERRCMD" "$FATALERRCMD" $REPJAVALINE  >> "$CONSOLEFILE" 2>&1 &
							fi
						fi
					fi
					echo "     Started as daemon"
					[ "$STARTWITHGUI" ] && startrepgui
				else
					# start and detach the reportserver process
					if [ "$JAVA_TMP_DIR" ]
					then
						java -Djava.io.tmpdir="$JAVA_TMP_DIR" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log "$MEMERRCMD" "$FATALERRCMD" $REPJAVALINE  &
					else
						java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$ABS_HOME/Reportserver/Log "$MEMERRCMD" "$FATALERRCMD" $REPJAVALINE  &
					fi
					echo "     Started with console I/O (NOT daemonised)"
					log; echo "Started ABSSolute reportserver with console I/O (NOT daemonised)" >> $SCRIPTLOGNAME
				fi

				abs_sleep $DELAYAFTERSTART

				[ "$LOGROTREPSRV" == "yes" ] && setup_logrotate replog
				[ "$LOGROTREPCC" == "yes" ] && setup_logrotate cclog

				cd $ABS_HOME
			else
				echo "$MSGINDENT ABSSolute report server already running (PID = $REPSRVPID)"
				log; echo "Attemped to start ABSSolute report server, but server is already running" >> $SCRIPTLOGNAME
			fi
		else
			echo "$MSGINDENT Not starting report server: Config file $ABS_HOME/Reportserver/Config/crystalclear.properties not found !"
			log; echo "$MSGINDENT Not starting report server: Config file $ABS_HOME/Reportserver/Config/crystalclear.properties not found !" >> $SCRIPTLOGNAME
		fi
	else
		echo "$MSGINDENT ABSSolute report server disabled"
		log; echo "Attemped to start ABSSolute report server, but server is disabled" >> $SCRIPTLOGNAME
	fi
}

snmp_check_startglassfish()
{

  [ ! -d $ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN/applications ] && return 0 

	export GFSTARTUPTIMEOUT=$(date --date="$GFSNMPMAXAPPSTARTTIME min" +"%s");
	
	echo "$MSGINDENT Checking SNMP status of required application services for web applications"

	for directory in `find $ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN/applications/ -mindepth 1 -maxdepth 1 -type d ! -name '_*'`
	do
		# Check if directory containts absgenericwebservice.properties
		if [ $(find $directory -name "absgenericwebservice.properties" | wc -l) -gt 0 ]
		then
			# ---> Process GWS properties
			SNMPSERVER=$(grep "^nsServer=" $directory/WEB-INF/classes/absgenericwebservice.properties |  tr -d '[:space:]' | sed 's/nsServer=//')
			SNMPPORT=$(grep "^nsPort=" $directory/WEB-INF/classes/absgenericwebservice.properties |  tr -d '[:space:]' | sed 's/nsPort=//' | sed 's/.$/4/')
		else
			# ---> Process web.xml
			SNMPSERVER=$(egrep "<param-name>nsServer</param-name>|<param-name>nsHost</param-name>" $directory/WEB-INF/web.xml -A 1 | sed -n 2p | sed 's/<[^>]*>//g' | tr -d '[:space:]')
			SNMPPORT=$(egrep "<param-name>nsPort</param-name>" $directory/WEB-INF/web.xml -A 1 | sed -n 2p | sed 's/<[^>]*>//g' | tr -d '[:space:]' | sed 's/.$/4/')
		fi
		SNMPVALUES="$SNMPVALUES $SNMPSERVER:$SNMPPORT"
		SNMPVALUES=$(echo $SNMPVALUES | tr ' ' '\n' | sort -u | tr '\n' ' ')
	done
	
	while true
	do
		# Check all application server SNMP ports
		REMAININGSNMPVALUES=$SNMPVALUES
		for ((i=1;i<=$(echo $SNMPVALUES | tr ' ' '\n' | wc -l);i++)); 
		do 
			SNMPSERVERPORT=$(echo $SNMPVALUES | tr ' ' '\n' | sed -n $(echo $i)p)
			# Check snmp and possibly remove serverport from REMAININGSNMPVALUES
			snmpcheck_app_server $SNMPSERVERPORT &> /dev/null
			[ $? -eq 0 ] && REMAININGSNMPVALUES=$(echo $REMAININGSNMPVALUES | sed "s/$SNMPSERVERPORT//")
		done
		SNMPVALUES=$REMAININGSNMPVALUES

		# Succesfull SNMP check
		[ ${#SNMPVALUES} -eq 0 ]  && break;

		# Check timeout based on GFSTARTUPTIMEOUT
		if [ $(date +"%s") -gt $GFSTARTUPTIMEOUT ] 
		then
			log; echo "INFO: Unsuccessfull SNMP check on application servers used by the web applications on SNMP ports: $(echo $SNMPVALUES | tr ' ' ',')" >> $SCRIPTLOGNAME
			echo "$MSGINDENT INFO: Unsuccessfull SNMP check on application servers used by the web applications on SNMP ports: $(echo $SNMPVALUES | tr ' ' ',')"
			break;
		fi
	done
}

startglassfish()
{
    checkstartflagfile
	CHKGLASS=`grep GLASS $STARTFLAGFILE | awk '{ print $1,$2 }' `
    if [ "$CHKGLASS" == "GLASS 1" ]
	then
		startxvirt
		if [ -x "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ]
		then
			if [ -z "$GLASSPID" ] 
			then
				[ "$GFSTARTUPSNMPCHECK" == "Y" ] && snmp_check_startglassfish
				echo -n "$MSGINDENT Starting Glassfish..."
				setsid $ABS_HOME/$GLASSFISHPATH/bin/asadmin start-domain $GFDOMAIN
                abs_sleep $DELAYAFTERSTART
                getpids $ABS_SLOT
                if [ -z "$GLASSPID" ] 
                then
                    echo "$MSGINDENT Failed to start glassfish !"
                    log; echo "Failed to start glassfish !" >> $SCRIPTLOGNAME
                else
                    log; echo "Started Glassfish" >> $SCRIPTLOGNAME
                fi
			else
				echo "$MSGINDENT Glassfish already running (PID = $GLASSPID)"
				log; echo "Attempted to start Glassfish, but process is already running" >> $SCRIPTLOGNAME
			fi
        else
			echo "$MSGINDENT Not starting glassfish: Executable $ABS_HOME/$GLASSFISHPATH/bin/asadmin not found !"
			log; echo "Not starting glassfish: Executable $ABS_HOME/$GLASSFISHPATH/bin/asadmin not found !" >> $SCRIPTLOGNAME
        fi
	else
		echo "$MSGINDENT Glassfish server disabled"
		log; echo "Attemped to start Glassfish server, but server is disabled" >> $SCRIPTLOGNAME
	fi
}

startglassfishapp()
{
    [ -z "$GLASSPID" ] && startglassfish
    getpids $ABS_SLOT
    if [ -z "$GLASSPID" ] 
    then
        echo "$MSGINDENT Glassfish not started !"
        log; echo "Glassfish not started !" >> $SCRIPTLOGNAME
    else
        case $1 in
            wpo)    WEBNAME="$WEBPORTALNAME";;
            gws)    WEBNAME="$GENERICWEBSERVICENAME";;
            ws2)    WEBNAME="$WEBSERVICE2NAME";;
            w4m)    WEBNAME="$MOBILEWEBSERVICENAME";;
            wp2)    WEBNAME="$WEBPORTAL2NAME";;
            wsr)    WEBNAME="$WEBRESTSERVICENAME";;
            wum)    WEBNAME="$WEBUHFMONNAME";;
            wcl)    WEBNAME="$WEBCLIENTNAME";;
            wua)    WEBNAME="$WEBCLIENTMYUNIFORMASSISTANT";;
            wrd)    WEBNAME="$WEBCLIENTRESIDENTIALDELIVERY";;
            wrr)    WEBNAME="$WEBCLIENTROUTERETURNS";;
            wsb)    WEBNAME="$WEBCLIENTREPLACESMARTBARCODE";;
            wmr)    WEBNAME="$MOBILERESTSERVICENAME";;
            wfb)    WEBNAME="$MOBILEFIREBASESERVICESNAME";;
            ara)    WEBNAME="$ABSROUTEASSISTANDNAME";;
        esac
        GFADMINPORT="5"$ABS_SLOT"34"
        [ ${#GFADMINPORT} == 4 ] && GFADMINPORT="50"$ABS_SLOT"34"
        echo "$MSGINDENT Starting Glassfish application $1"
        PWFILE="$ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN/config/absgfpwd.txt"
        echo "AS_ADMIN_PASSWORD=$GLASSADMINPWD" > $PWFILE
        $ABS_HOME/$GLASSFISHPATH/bin/asadmin --passwordfile=$PWFILE --port $GFADMINPORT --user $GLASSADMINUSR enable $WEBNAME
        log; echo "Started Glassfish application $WEBNAME" >> $SCRIPTLOGNAME
        rm -f $PWFILE
    fi
}

delete_domain()
{
    [ "$ABS_TYPE" != "glass" ] && exit 1
    [ ! -x "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ] && exit 1
    [ ! -d "$ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN" ] && exit 1

    [ "$GLASSPID" ] && stopglassfish

    echo -n "$MSGINDENT Deleting Glassfish domain $GFDOMAIN"
    $ABS_HOME/$GLASSFISHPATH/bin/asadmin delete-domain $GFDOMAIN
    log; echo "Deleted Glassfish domain $GFDOMAIN" >> $SCRIPTLOGNAME
}

create_domain()
{
    [ "$ABS_TYPE" != "glass" ] && exit 1
    [ ! -x "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ] && exit 1
    [ -d "$ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN" ] && exit 1

    echo "$MSGINDENT Create Glassfish domain $GFDOMAIN"

    echo "AS_ADMIN_PASSWORD=$GLASSADMINPWD" > $ABS_HOME/$GLASSFISHPATH/abspasswd.txt
    ABS_2SLOT="0"$ABS_SLOT
    ABS_2SLOT=${ABS_2SLOT:(-2)}

    GFRESPONSE=`$ABS_HOME/$GLASSFISHPATH/bin/asadmin --host $ABSHOST --port 5"$ABS_2SLOT"34 version --local`
    
    if [ "`echo $GFRESPONSE | grep 5.`" ]
    then
    $ABS_HOME/$GLASSFISHPATH/bin/asadmin --user $GLASSADMINUSR --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt create-domain --instanceport 5"$ABS_2SLOT"40 --adminport 5"$ABS_2SLOT"34 --domainproperties http.ssl.port=5"$ABS_2SLOT"32:domain.jmxPort=5"$ABS_2SLOT"33:jms.port=5"$ABS_2SLOT"35:orb.listener.port=5"$ABS_2SLOT"36:orb.ssl.port=5"$ABS_2SLOT"37:orb.mutualauth.port=5"$ABS_2SLOT"38:osgi.shell.telnet.port=5"$ABS_2SLOT"39 --template $ABS_HOME/$GLASSFISHPATH/glassfish/common/templates/gf/production-domain.jar $GFDOMAIN
    else
     $ABS_HOME/$GLASSFISHPATH/bin/asadmin --user $GLASSADMINUSR --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt create-domain --instanceport 5"$ABS_2SLOT"40 --adminport 5"$ABS_2SLOT"34 --domainproperties http.ssl.port=5"$ABS_2SLOT"32:domain.jmxPort=5"$ABS_2SLOT"33:jms.port=5"$ABS_2SLOT"35:orb.listener.port=5"$ABS_2SLOT"36:orb.ssl.port=5"$ABS_2SLOT"37:orb.mutualauth.port=5"$ABS_2SLOT"38:osgi.shell.telnet.port=5"$ABS_2SLOT"39 $GFDOMAIN
    fi
    sleep 5
    
    startglassfish
    getpids $ABS_SLOT
    $ABS_HOME/$GLASSFISHPATH/bin/asadmin --port 5"$ABS_2SLOT"34 --user $GLASSADMINUSR --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt enable-secure-admin 
	
    if [ -z "$GLASSPID" ] 
    then
        echo "$MSGINDENT Unable to start glassfish !"
        log; echo "Unable to start glassfish !" >> $SCRIPTLOGNAME
    else
        if [ $ABSSOLUTENUMVERSION -lt 90000 ]
        then
            $ABS_HOME/$GLASSFISHPATH/bin/asadmin --user $GLASSADMINUSR --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt --port 5"$ABS_2SLOT"34 delete-jvm-options "-XX\:MaxPermSize=192m"
            $ABS_HOME/$GLASSFISHPATH/bin/asadmin --user $GLASSADMINUSR --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt --port 5"$ABS_2SLOT"34 create-jvm-options "-XX\:MaxPermSize=$GFMAXPERM"
        fi
        $ABS_HOME/$GLASSFISHPATH/bin/asadmin --user $GLASSADMINUSR --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt --port 5"$ABS_2SLOT"34 create-jvm-options "-Djava.util.prefs.userRoot=$JAVA_HOME/userprefs.gf"
        $ABS_HOME/$GLASSFISHPATH/bin/asadmin --user $GLASSADMINUSR --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt --port 5"$ABS_2SLOT"34 create-jvm-options "-Djava.util.prefs.systemRoot=$JAVA_HOME/systemprefs.gf"
        sleep 1
        log; echo "Created Glassfish domain $GFDOMAIN" >> $SCRIPTLOGNAME
        stopglassfish
        getpids $ABS_SLOT
        startglassfish
    fi
    rm -f $ABS_HOME/$GLASSFISHPATH/abspasswd.txt
}

status_domain()
{
    [ "$ABS_TYPE" != "glass" ] && exit 1
    [ ! -x "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ] && exit 1
    [ ! -d "$ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN" ] && exit 1

    ABS_2SLOT="0"$ABS_SLOT
    ABS_2SLOT=${ABS_2SLOT:(-2)}
	
	STATUS=$($ABS_HOME/$GLASSFISHPATH/bin/asadmin --port 5"$ABS_2SLOT"34 list-domains | grep $GFDOMAIN | awk ' { print $2 } ' )
	echo "$STATUS"
}

list_domain()
{
	[ "$ABS_TYPE" != "glass" ] && exit 1
    	[ ! -x "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ] && exit 1
    	[ ! -d "$ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN" ] && exit 1
	
	echo "AS_ADMIN_PASSWORD=$GLASSADMINPWD" > $ABS_HOME/$GLASSFISHPATH/abspasswd.txt
	ABS_2SLOT="0"$ABS_SLOT
   	ABS_2SLOT=${ABS_2SLOT:(-2)}
	
	$ABS_HOME/$GLASSFISHPATH/bin/asadmin --user $GLASSADMINUSR --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt --port 5"$ABS_2SLOT"34 list-applications
	
	rm -f $ABS_HOME/$GLASSFISHPATH/abspasswd.txt
}

deploy_war()
{
    [ "$ABS_TYPE" != "glass" ] && exit 1
	[ ! "$WEBLIST" ] && exit 1
    [ ! -x "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ] && exit 1
    [ ! -d "$ABS_HOME/$GLASSFISHPATH/glassfish/domains/$GFDOMAIN" ] && exit 1

# weblist (commandline option -w) -- webtype:webname:webcontext:weblib
#     webtype - wpo, wpon, ws2, mws or gws => wpo|gws|ws2|w4m|wp2|wsr|wum|wcl|wua|wrd|wrr|wsb|wmr|wfb
#     webname - name as it appears in glassfish
#     webcontext - webpath of the webapp.  i.e. / or webportal (root / is the only one with a slash "/" sign)
#     weblib - extra needed libraries

    WEBTYPE=$(echo $WEBLIST | sed 's/\(.*\):\(.*\):\(.*\):\(.*\)/\1/')
	WEBNAME=$(echo $WEBLIST | sed 's/\(.*\):\(.*\):\(.*\):\(.*\)/\2/')
	WEBCONTEXT=$(echo $WEBLIST | sed 's/\(.*\):\(.*\):\(.*\):\(.*\)/\3/')
	WEBLIB=$(echo $WEBLIST | sed 's/\(.*\):\(.*\):\(.*\):\(.*\)/\4/')

    [ ! -e "$ABS_HOME/deploy/$WEBTYPE/$WEBNAME.war" ] && exit 1
    [ ! $WEBTYPE ] && exit 1
	[ ! $WEBNAME ] && exit 1
	[ ! $WEBCONTEXT ] && exit 1
	[ ! $WEBLIB ] && exit 1

    echo "$MSGINDENT Deploy web application $WEBTYPE in context $WEBCONTEXT"

    ABS_2SLOT="0"$ABS_SLOT
    ABS_2SLOT=${ABS_2SLOT:(-2)}
	
    echo "AS_ADMIN_PASSWORD=$GLASSADMINPWD" > $ABS_HOME/$GLASSFISHPATH/abspasswd.txt
	
    if [ "$WEBLIB" == X -o "$WEBLIB" == x ]
    then
        $ABS_HOME/$GLASSFISHPATH/bin/asadmin --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt --host $ABSHOST --port 5"$ABS_2SLOT"34 --user $GLASSADMINUSR deploy --contextroot $WEBCONTEXT --force --name $WEBNAME $ABS_HOME/deploy/$WEBTYPE/$WEBNAME.war
    else
        $ABS_HOME/$GLASSFISHPATH/bin/asadmin --passwordfile $ABS_HOME/$GLASSFISHPATH/abspasswd.txt --host $ABSHOST --port 5"$ABS_2SLOT"34 --user $GLASSADMINUSR deploy --contextroot $WEBCONTEXT --force --name $WEBNAME --libraries $WEBLIB $ABS_HOME/deploy/$WEBTYPE/$WEBNAME.war
    fi
	
	RETURNCODE=$?
	
    rm -f $ABS_HOME/$GLASSFISHPATH/abspasswd.txt
	
	exit $RETURNCODE
}

show_gf_version()
{
    [ "$ABS_TYPE" != "glass" ] && exit 1
    [ ! -x "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ] && exit 1

    ABS_2SLOT="0"$ABS_SLOT
    ABS_2SLOT=${ABS_2SLOT:(-2)}

    GFRESPONSE=`$ABS_HOME/$GLASSFISHPATH/bin/asadmin --host $ABSHOST --port 5"$ABS_2SLOT"34 version --local`
    if [ "`echo $GFRESPONSE | grep 3.1`" ]
    then
        GFVERSION="312"
    else
        if [ "`echo $GFRESPONSE | grep 4.1.1`" ]
        then
            GFVERSION="411"
        else
			if [ "`echo $GFRESPONSE | grep 4.1`" ]
				then
					GFVERSION="4"
				else
					if [ "`echo $GFRESPONSE | grep 5.`" ]
					then
						GFVERSION="5"
					else
						GFVERSION="301"
                                        fi
			fi
        fi
    fi
    echo $GFVERSION
}

show_version_abs()
{
    [ "$ABS_TYPE" == "glass" ] && show_gf_version
}

startappgui()
{
    if [ "$APPGUIPID" ] 
    then
        echo "Application GUI is already running (PID = $APPGUIPID)" 
        log; echo "Attempted to start application GUI, but application GUI is already running (PID = $APPGUIPID)" >> $SCRIPTLOGNAME
    else
		[ -z "$APPSRVPID" ] && echo "$MSGINDENT Warning: ABSSolute Applicationserver not started !"
        export DISPLAY=$ORG_DISPLAY
        setclasspath app
        echo "$MSGINDENT Starting ABSSolute applicationserver GUI"
        cd $ABS_HOME/Server/Lib
		
		[ $ABSSOLUTENUMVERSION -ge 90504 ] && APPSRVGUICLASS="-cp server-impl.jar -Dloader.main=absgroup.forms.MainUI org.springframework.boot.loader.PropertiesLauncher --spring.config.location=$ABS_HOME/Server/Config/application.properties"
		
        if [ "$JAVA_TMP_DIR" ]
        then
            nohup java -Djava.io.tmpdir="$JAVA_TMP_DIR" $VERBOSE -Dsun.java2d.noddraw=true $APPSRVGUICLASS -I $ABS_HOME/Server/Lib < /dev/null > /dev/null 2>&1 &
        else
            nohup java $VERBOSE -Dsun.java2d.noddraw=true $APPSRVGUICLASS -I $ABS_HOME/Server/Lib < /dev/null > /dev/null 2>&1 &
        fi
        cd $ABS_HOME
        log; echo "Started ABSSolute applicationserver GUI" >> $SCRIPTLOGNAME
    fi
}

startrepgui()
{
    if [ "$REPGUIPID" ] 
    then
        echo "Reportserver GUI is already running (PID = $REPGUIPID)" 
        log; echo "Attempted to start reportserver GUI, but reportserver GUI is already running (PID = $REPGUIPID)" >> $SCRIPTLOGNAME
    else
		[ -z "$REPSRVPID" ] && echo "$MSGINDENT Warning: ABSSolute Reportserver not started !"
        export DISPLAY=$ORG_DISPLAY
        setclasspath rep

        echo "$MSGINDENT Starting ABSSolute reportserver GUI"
        cd $ABS_HOME/Reportserver/Lib
        if [ "$JAVA_TMP_DIR" ]
        then
            java -Djava.io.tmpdir="$JAVA_TMP_DIR" $VERBOSE -Dsun.java2d.noddraw=true $REPSRVGUICLASS -i $ABS_HOME/Reportserver/Lib < /dev/null > /dev/null 2>&1 &
        else
            java $VERBOSE -Dsun.java2d.noddraw=true $REPSRVGUICLASS -i $ABS_HOME/Reportserver/Lib < /dev/null > /dev/null 2>&1 &
        fi
        cd $ABS_HOME
        log; echo "Started ABSSolute reportserver GUI" >> $SCRIPTLOGNAME
    fi
}

stopclient()
{
    if [ "$CLIENTPID" ] 
    then
        echo -n "$MSGINDENT Stopping CLIENT (pid $CLIENTPID)"
        kill_abs_process "$CLIENTPID"
        log; echo "Stopped client" >> $SCRIPTLOGNAME
    fi
}

startclient()
{
    if [ "$CLIENTPID" ] 
    then
        echo "ABSSolute client is already running (PID = $CLIENTPID)" 
        log; echo "Attempted to start client, but client is already running (PID = $CLIENTPID)" >> $SCRIPTLOGNAME
    else
		CHKAPP=`grep CLIENT $STARTFLAGFILE | awk '{ print $1,$2 }' `
		if [ "$CHKAPP" == "CLIENT 1" ]
		then
            echo "$MSGINDENT Starting ABSSolute $1"
            export DISPLAY=$ORG_DISPLAY
            export ABS_USER=local$ABS_SLOT

            # start client.sh
            ./startclient.sh -s $ABS_SLOT -t $1
            log; echo "Started ABSSolute $1 in slot $ABS_SLOT" >> $SCRIPTLOGNAME
        else
            echo "$MSGINDENT Clients are disabled !"
            log; echo "Attemped to start a client application, but clients are disabled" >> $SCRIPTLOGNAME
        fi
    fi
}

enablesrv()
{
    checkstartflagfile
    case "$ABS_TYPE" in
		app)	SRVTXT="Application server";			sed -i 's/APP 0/APP 1/g' $STARTFLAGFILE;;
		rep)	SRVTXT="Report server";					sed -i 's/REP 0/REP 1/g' $STARTFLAGFILE;;
        glass)	SRVTXT="Glassfish server";				sed -i 's/GLASS 0/GLASS 1/g' $STARTFLAGFILE;;
		all)	SRVTXT="App, rep and glassfish server"; sed -i 's/APP 0/APP 1/g' $STARTFLAGFILE;
                                                        sed -i 's/REP 0/REP 1/g' $STARTFLAGFILE;
                                                        sed -i 's/GLASS 0/GLASS 1/g' $STARTFLAGFILE;
                                                        sed -i 's/CLIENT 0/CLIENT 1/g' $STARTFLAGFILE;;
		client)	SRVTXT="Client";                        sed -i 's/CLIENT 0/CLIENT 1/g' $STARTFLAGFILE;;
	esac

    echo "$MSGINDENT Enabled $SRVTXT in slot $ABS_SLOT"
    log; echo "Enabled $SRVTXT in slot $ABS_SLOT" >> $SCRIPTLOGNAME
}

disablesrv()
{
    checkstartflagfile
    case "$ABS_TYPE" in
		app)	SRVTXT="Application server";			sed -i 's/APP 1/APP 0/g' $STARTFLAGFILE;	    [ "$APPSRVPID" ]   && stopappserver;;
		rep)	SRVTXT="Report server";					sed -i 's/REP 1/REP 0/g' $STARTFLAGFILE;	    [ "$REPSRVPID" ]   && stoprepserver;;
        glass)	SRVTXT="Glassfish server";				sed -i 's/GLASS 1/GLASS 0/g' $STARTFLAGFILE;    [ "$GLASSPID" ]    && stopglassfish;;
		all)	SRVTXT="App, rep, glassfish server and client"; 
                                                        sed -i 's/APP 1/APP 0/g' $STARTFLAGFILE; 		[ "$APPSRVPID" ]   && stopappserver;
                                                        sed -i 's/REP 1/REP 0/g' $STARTFLAGFILE;		[ "$REPSRVPID" ]   && stoprepserver;
                                                        sed -i 's/GLASS 1/GLASS 0/g' $STARTFLAGFILE;	[ "$GLASSPID" ]	   && stopglassfish;
                                                        sed -i 's/CLIENT 1/CLIENT 0/g' $STARTFLAGFILE;	[ "$CLIENTPID"]    && stopclient;;
		client)	SRVTXT="Client";                        sed -i 's/CLIENT 1/CLIENT 0/g' $STARTFLAGFILE;  [ "$CLIENTPID" ]   && stopclient;;
	esac
    echo "$MSGINDENT Disabled $SRVTXT in slot $ABS_SLOT"
    log; echo "Disabled $SRVTXT server in slot $ABS_SLOT" >> $SCRIPTLOGNAME
}

stopsrv()
{
    checkstartflagfile
    case "$ABS_TYPE" in
		app)	                    stopappserver;;
		rep)	                    stoprepserver;;
        glass)                      stopglassfish;;
        wpo|gws|ws2|w4m|wp2|wsr|wum|wcl|wua|wrd|wrr|wsb|wmr|wfb|ara)   stopglassfishapp "$ABS_TYPE";;
		all)	                    stopclient all; stopglassfish; stoprepserver; stopappserver;;
        tsmats|tsoutscansort|tspresortscan|tssoilreceiving|tsspecialscans|tsweigh|client)
                                    stopclient "$ABS_TYPE";;
	esac	
}

startsrv()
{
    checkstartflagfile
    getpids $ABS_SLOT
    case "$ABS_TYPE" in
		app)	                    export ABS_TMP_TYPE=app;   startappserver;;
		rep)	                    export ABS_TMP_TYPE=rep;   startrepserver;;
		glass)	                    export ABS_TMP_TYPE=glass; startglassfish;;
        wpo|gws|ws2|w4m|wp2|wsr|wum|wcl|wua|wrd|wrr|wsb|wmr|wfb|ara)   startglassfishapp "$ABS_TYPE";;
		all)	                    export ABS_TMP_TYPE=app;   startappserver;
									export ABS_TMP_TYPE=rep;   startrepserver;
                                    export ABS_TMP_TYPE=glass; startglassfish;;
        tsmats|tsoutscansort|tspresortscan|tssoilreceiving|tsspecialscans|tsweigh|client)
                                    startclient "$ABS_TYPE";;
	esac	
}

commandallsrv()
{
	echo "Performing $ABS_CMD on all slots for $ABS_TYPE component(s)"
	log; echo "Performing $ABS_CMD on all slots for $ABS_TYPE component(s)" >> $SCRIPTLOGNAME 
	
	find $ABS_ROOTDIR -maxdepth 1 -type d -regextype posix-extended -regex "$ABS_ROOTDIR/[0-9]{1,${#MAX_SLOT}}" | sort | while read SLOTDIR
	do
		[ -f "$SLOTDIR/startable.abs" ] && $ABS_ROOTDIR/abs.sh -s $(basename $SLOTDIR) -c $ABS_CMD -t $ABS_TYPE
	done	
}

startgui()
{
    case "$ABS_TYPE" in
		app)	    startappgui;;
		rep)	    startrepgui;;
		glass)      echo "$MSGINDENT No startable server gui available. Please use your webbrowser...";;
		all)	    startappgui; startrepgui;;
	esac
}

# set maximal heapspace in the startable.abs
maxheap()
{
	if [ -z "$MEMORYVALUE" ] || [[ "$ABS_TYPE" != "app" && "$ABS_TYPE" != "rep" ]] || [[ $MEMORYVALUE =~ [^[:digit:]] ]]
	then
		echo "The heapspace cannot be set, input parameters are invalid"
		log; echo "The heapspace cannot be set, input parameters are invalid" >> $SCRIPTLOGNAME 
	else
		case "$ABS_TYPE" in
			app)	                    sed -i "/APP/c\\$(grep APP $STARTFLAGFILE | awk -v a="$MEMORYVALUE" '{ print $1,$2,a }')" $STARTFLAGFILE;;
			rep)	                    sed -i "/REP/c\\$(grep REP $STARTFLAGFILE | awk -v a="$MEMORYVALUE" '{ print $1,$2,a }')" $STARTFLAGFILE;;
		esac
		echo "The maximal heapspace is configured for $ABS_TYPE in $STARTFLAGFILE"
		log; echo "The maximal heapspace is configured for $ABS_TYPE in $STARTFLAGFILE" >> $SCRIPTLOGNAME
	fi
}

# Reset custom maximal heapspace settting in the startable.abs
resetheap()
{
	[ "$ABS_TYPE" != "app" ] && [ "$ABS_TYPE" != "rep" ] && return 0
	case "$ABS_TYPE" in
			app)	                    sed -i "/APP/c\\$(grep APP $STARTFLAGFILE | awk '{ print $1,$2 }')" $STARTFLAGFILE;;
			rep)	                    sed -i "/REP/c\\$(grep REP $STARTFLAGFILE | awk '{ print $1,$2 }')" $STARTFLAGFILE;;
	esac
	echo "The custom maximal heapspace is removed for $ABS_TYPE in $STARTFLAGFILE"
	log; echo "The custom maximal heapspace is removed for $ABS_TYPE in $STARTFLAGFILE" >> $SCRIPTLOGNAME
}

#### Special / advanced commands ###################################################################
do_threaddump()
{
    if [ -z $STRACE_FOUND ]
    then
        echo "$MSGINDENT STRACE not found in any of the standard directories !"
        echo "$MSGINDENT The threaddump function needs the STRACE package to be installed."
        echo "$MSGINDENT If STRACE is installed then please change the standard location in this script."
        log; echo "Tried to start STRACE, but STRACE program was not found" >> $SCRIPTLOGNAME
    else
        if [ $1 ]
        then
            log; echo -n "$MSGINDENT Creating threaddump of " >> $SCRIPTLOGNAME
            if [ "$2" = "Reportserver" ] 
            then
                echo "report server" >> $SCRIPTLOGNAME
            else
                echo "application server" >> $SCRIPTLOGNAME
            fi

            THREADLOGNAME=`date +$ABS_ROOTDIR/log/threaddump_$2_$3_%Y%m%d_%H%M%S.txt`
            echo -n "$MSGINDENT Creating threaddump of " 
            if [ "$2" = "Reportserver" ] 
            then
                echo -n "report "
            else
                echo -n "application "
            fi
            echo -n "server in $THREADLOGNAME        "
            
            $STRACEPATH/strace -q -s 1024 -f -e trace=write -p $1 2>&1 | grep 'write(1,' | awk '{ gsub(/^[^\"]*\"|\"[^\"]*$/,""); gsub(/\\n/,""); gsub(/\\t/,"\t"); gsub(/\\\"/,"\""); print }' > $THREADLOGNAME &
            sleep 2
            STRACEPID=`ps --cols 2000 -eo pid,cmd | grep -i strace | grep $1 | awk '{print $1}'`
            kill -QUIT $1
            sleep 8
            kill -9 $STRACEPID
            echo "done"
            echo
            log; echo "Thread dump file --> $HEAPLOGNAME" >> $SCRIPTLOGNAME
        else
            echo "$MSGINDENT ABSSolute $2 is not running"
            log; echo -n "Attempted to create threaddump of " >> $SCRIPTLOGNAME
            if [ "$2" = "Reportserver" ] 
            then
                echo -n "report" >> $SCRIPTLOGNAME
            else
                echo -n "application" >> $SCRIPTLOGNAME
            fi
            echo " server, but process is not running" >> $SCRIPTLOGNAME
        fi
    fi
}

do_heapdump()
{
    if [ $1 ]
    then
        HEAPLOGNAME=`date +$ABS_ROOTDIR/log/heapdump_$2_$3_%Y%m%d_%H%M%S.bin`
        echo -n "$MSGINDENT"         
        $JAVA_HOME/bin/jmap -dump:format=b,file=$HEAPLOGNAME $1
        log; echo "Heap dump file --> $HEAPLOGNAME" >> $SCRIPTLOGNAME
    else
        echo "$MSGINDENT $2 is not running"
        log; echo "Attempted to create heapdump of $2, but process is not running" >> $SCRIPTLOGNAME
    fi 
}

threaddump()
{  
	case "$ABS_TYPE" in
        app)    do_threaddump $APPSRVPID Server $ABS_SLOT;;
        rep)    do_threaddump $REPSRVPID Reportserver $ABS_SLOT;;
        all)    do_threaddump $APPSRVPID Server $ABS_SLOT; 
                do_threaddump $REPSRVPID Reportserver $ABS_SLOT;;
        *)      echo "$MSGINDENT Use type app, rep or all for a thread dump";;
	esac
}

heapdump()
{
    if [ -z "$JMAP_FOUND" ]
    then
        echo
        echo "jmap program not found in the used java tree ($JAVA_HOME)"
        echo "heapdump needs JDK 1.6 or higher"
        echo
    else    
        case "$ABS_TYPE" in
            app)    do_heapdump $APPSRVPID Server $ABS_SLOT;;
            rep)    do_heapdump $REPSRVPID Reportserver $ABS_SLOT;;
            glass)  do_heapdump $GLASSFISHPID Glassfish $ABS_SLOT;;
            all)    do_heapdump $APPSRVPID Server $ABS_SLOT; 
                    do_heapdump $REPSRVPID Reportserver $ABS_SLOT;
                    do_heapdump $GLASSFISHPID Glassfish $ABS_SLOT;;
            *)      echo "$MSGINDENT Use type app, rep or all for a thread dump";;
        esac
    fi
}

do_remote_monitor()
{
    STARTMON="yes"
    if [ -z $JVISUALVM_FOUND ]
    then
        echo
        echo "jVisualVM program not found in the used java tree ($JAVA_HOME)."
        if [ -z $JCONSOLE_FOUND ]
        then
            echo "jConsole program not found in the used java tree ($JAVA_HOME)"
            echo "Monitoring needs JDK 1.6 update 7 or higher"
            STARTMON="no"
        else
            echo "Using jConsole program instead"
        fi    
        echo
    fi
    if [ "$STARTMON" == "yes" ]
    then
        if [ -z $MONHOST ]
        then
            echo "$MSGINDENT No connector string given. Please provide host:port when using the -r option."
            log; echo "No connector string given when starting jVisualVM remote monitor." >> $SCRIPTLOGNAME
        else
            if [ "$JVISUALVM_FOUND" ]
            then
                $MONITORAPP --jdkhome $JAVA_HOME --userdir $ABS_ROOTDIR/visualvm_userdir --openjmx $MONHOST &
            else
                if [ "$MONNOTILE"=="yes" ]
                then
                    [ -z $MONPLUGINPATH ] && $MONITORAPP -interval=$MONINTERVAL -notile $MONHOST &
                    [ $MONPLUGINPATH ] && $MONITORAPP -interval=$MONINTERVAL -notile -pluginpath $MONPLUGINPATH $MONHOST &
                else
                    [ -z $MONPLUGINPATH ] && $MONITORAPP -interval=$MONINTERVAL $MONHOST &
                    [ $MONPLUGINPATH ] && $MONITORAPP -interval=$MONINTERVAL -pluginpath $MONPLUGINPATH $MONHOST &
                fi
            fi
        fi
    fi
}

local_mon()
{
    if [ "$JVISUALVM_FOUND" ]
    then
        $MONITORAPP --jdkhome $JAVA_HOME --userdir $ABS_ROOTDIR/visualvm_userdir --openpid $1 &
    else
        if [ "$MONNOTILE"=="yes" ]
        then
            [ -z $MONPLUGINPATH ] && $MONITORAPP -interval=$MONINTERVAL -notile $1 &
            [ $MONPLUGINPATH ] && $MONITORAPP -interval=$MONINTERVAL -notile -pluginpath $MONPLUGINPATH $1 &
        else
            [ -z $MONPLUGINPATH ] && $MONITORAPP -interval=$MONINTERVAL $1 &
            [ $MONPLUGINPATH ] && $MONITORAPP -interval=$MONINTERVAL -pluginpath $MONPLUGINPATH $1 &
        fi
    fi
}

do_local_monitor()
{
    STARTMON="yes"
    if [ -z $JVISUALVM_FOUND ]
    then
        echo
        echo "jVisualVM program not found in the used java tree ($JAVA_HOME)"
        if [ -z $JCONSOLE_FOUND ]
        then
            echo "jConsole program not found in the used java tree ($JAVA_HOME)"
            echo "Monitoring needs JDK 1.6 update 7 or higher"
            STARTMON="no"
        else
            echo "Using jConsole program instead (jVisualVM needs JDK 1.6 update 7 or higher)"
        fi    
        echo
    fi
    if [ "$STARTMON" == "yes" ]
    then
        case "$ABS_TYPE" in
            app)    local_mon $APPSRVPID;;
            rep)    local_mon $REPSRVPID;;
            glass)  local_mon $GLASSFISHPID;;
            all)    local_mon $APPSRVPID; 
                    local_mon $REPSRVPID;
                    local_mon $GLASSFISHPID;;
            *)      echo "$MSGINDENT Use type app, rep, glass or all for monitoring";;
        esac
    fi
}

#### Crash Recovery #####################################################################
crash_recovery()
{
    case $CRASHFLAG in
        X)                                              ;; # do nothing
        H)     heapdump                                 ;; # heapdump
        T)     threaddump                               ;; # threaddump
        HT)    heapdump; threaddump                     ;; # heapdump + threaddump
        R)     stopsrv; startsrv                        ;; # restart
        RH)    heapdump; stopsrv; startsrv              ;; # heapdump + restart
        RT)    threaddump; stopsrv; startsrv            ;; # threaddump + restart
        RHT)   heapdump; threaddump; stopsrv; startsrv  ;; # heapdump + threaddump + restart
        K)     stopsrv                                  ;; # kill
        KH)    heapdump; threaddump; stopsrv            ;; # heapdump + kill
        KT)    threaddump; stopsrv                      ;; # threaddump + kill
        KHT)   heapdump; threaddump; stopsrv            ;; # heapdump + threaddump + kill
    esac
}

#### E-mail status reports ##############################################################
send_email()
{
# $1 = maint --> send email to $EMAIL_TO_MAINT about script usage
# $1 = crash --> email $SCRIPTLOGNAME to $EMAIL_TO_OOM_FATAL about OOM or FATAL crash
    case $1 in
        maint|crash)
            [ "$1" == "maint" ] && EMAILRECIP="$EMAIL_TO_MAINT"
            [ "$1" == "crash" ] && EMAILRECIP="$EMAIL_TO_OOM_FATAL"

            FROMFIELD="$EMAILFROM@$EMAILDOMAIN"
            EMAILDATE=`date +%Y%m%d-%H:%M:%S`

            EMAIL_LOG="$ABS_LOGDIR/abs_email.log"
            echo "FROM:$FROMFIELD" > "$EMAIL_LOG"
            echo "TO:$EMAILRECIP" >> "$EMAIL_LOG"

            case "$ABS_CMD" in
                status)     ACTION="list the status of servers";;
                start)      ACTION="start";;
                stop)       ACTION="stop";;
                restart)    ACTION="restart";;
                gui)        ACTION="start the GUI of";;
                monitor)    ACTION="monitor";;
                thread)     ACTION="make a threaddump of";;
                dump)       ACTION="make a heapdump of";;
                log)        ACTION="show the logfile of";;
                collect)    ACTION="collect info about";;
            esac
            case "$ABS_TYPE" in
                app)        TARGET="application server";;
                rep)        TARGET="report server";;
                glass)      TARGET="glassfish server";;
                all)        TARGET="app, rep and glassfish servers";;
            esac

            [ "$1" == "maint" ] && EMAILSUBJ="ABSSolute usage report ($EMAILDATE)"
            [ "$1" == "crash" ] && EMAILSUBJ="ABSSolute CRASH report ($EMAILDATE)"
            echo "SUBJECT:$EMAILSUBJ" >> "$EMAIL_LOG"
            echo >> "$EMAIL_LOG"
            if [ "$ABS_CMD" == "status" ]
            then
                echo "ABSSolute maintenance script was used to $ACTION" >> "$EMAIL_LOG"
            else
                [ "$1" == "maint" ] && echo "ABSSolute maintenance script was used to $ACTION the $TARGET in slot $ABS_SLOT on machine $(hostname)" >> "$EMAIL_LOG"
                [ "$1" == "crash" ] && echo "ABSSolute $TARGET in slot $ABS_SLOT on machine $(hostname) crashed !" >> "$EMAIL_LOG"
            fi
            echo "See attached logfile for details" >> "$EMAIL_LOG"
            echo >> "$EMAIL_LOG"
            echo >> "$EMAIL_LOG"

            NEEDPWD="no"
            [ "$SMTPUSER" ] && [ "$SMTPPWD" ] && [ "$AUTH_SMTP" == "yes" ] && NEEDPWD="yes"

            if [ -z "$SMTPSRV" ]
            then
                # use local mailserver (sendmail) to handle the e-mail
                if [ "$EMAIL_SEND_SCRIPT_LOG" == "yes" ]
                then
                #    $MAILBINPATH -n -s "$EMAILSUBJ" -a "$SCRIPTLOGNAME" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                    $MAILBINPATH "$EMAILSUBJ" -a "$SCRIPTLOGNAME" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                else
                #    $MAILBINPATH -n -s "$EMAILSUBJ" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                    $MAILBINPATH "$EMAILSUBJ" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                fi
            else
                # use nail and an external SMTP server to handle the e-mail
                if [ "$EMAIL_SEND_SCRIPT_LOG" == "yes" ]
                then
                    if [ "$NEEDPWD" == "no" ]
                    then
                        #env MAILRC="/dev/null" SMTP="$SMTPSRV" $MAILBINPATH -n -s "$EMAILSUBJ" -a "$SCRIPTLOGNAME" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                        env MAILRC="/dev/null" SMTP="$SMTPSRV" $MAILBINPATH "$EMAILSUBJ" -a "$SCRIPTLOGNAME" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                    else
                        #env MAILRC="/dev/null" SMTP="$SMTPSRV" SMTP-AUTH=login SMTP-AUTH-USER="$SMTPUSER" SMTP-AUTH-PASSWORD="$SMTPPWD" $MAILBINPATH -n -s "$EMAILSUBJ" -a "$SCRIPTLOGNAME" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                        env MAILRC="/dev/null" SMTP="$SMTPSRV" SMTP-AUTH=login SMTP-AUTH-USER="$SMTPUSER" SMTP-AUTH-PASSWORD="$SMTPPWD" $MAILBINPATH -n -s "$EMAILSUBJ" -a "$SCRIPTLOGNAME" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                    fi
                else
                    if [ "$NEEDPWD" == "no" ]
                    then
                        #env MAILRC="/dev/null" SMTP="$SMTPSRV" $MAILBINPATH -n -s "$EMAILSUBJ" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                        env MAILRC="/dev/null" SMTP="$SMTPSRV" $MAILBINPATH "$EMAILSUBJ" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                    else
                        env MAILRC="/dev/null" SMTP="$SMTPSRV" SMTP-AUTH=login SMTP-AUTH-USER="$SMTPUSER" SMTP-AUTH-PASSWORD="$SMTPPWD" $MAILBINPATH -n -s "$EMAILSUBJ" -r "$FROMFIELD" "$EMAILRECIP" < "$EMAIL_LOG"
                    fi
                fi
            fi
            echo "E-mail sent to system administrator";;
        *)  echo "wrong e-mail option";;
    esac
}

#### Process list #######################################################################
show_head_info()
{
    echo
    date
    uptime
    uname -psn
    top -b -n 1 -s | head -n 5 | tail -n 2
}

show_absheader()
{   
# if short list and no processes running --> skip slot
    [ "$LISTOPTION" == "s" ] && [ -z "$XVIRTPID" ] && [ -z "$APPSRVPID" ] && [ -z "$REPSRVPID" ] && [ -z "$APPGUIPID" ] && [ -z "$REPGUIPID" ] && return 0
    
# create headerline for current slot
	if [ -e $ABS_HOME/Server/Config/application.properties ]
	then
        SIDLST=`grep -i DBSID= $ABS_HOME/Server/Config/application.properties | grep -v ^# | sed 's/\(.*\)\(DBSID=\)\(.*\)\(\r\)/\3/'`
        DBUSER=`grep -i DBUSER $ABS_HOME/Server/Config/application.properties | grep -v ^# | sed 's/\(.*\)\(DBUser=\)\(.*\)\(\r\)/\3/'`
    elif [ -e $ABS_HOME/Server/Config/absserver.properties ]
    then
        SIDLST=`grep -i DBSID= $ABS_HOME/Server/Config/absserver.properties | grep -v ^# | sed 's/\(.*\)\(DBSID=\)\(.*\)\(\r\)/\3/'`
        DBUSER=`grep -i DBUSER $ABS_HOME/Server/Config/absserver.properties | grep -v ^# | sed 's/\(.*\)\(DBUser=\)\(.*\)\(\r\)/\3/'`
    elif [ -e $ABS_HOME/Reportserver/Config/crystalclear.properties ]
	then
		SIDLST="DBSID=$(grep -i oracle.url= $ABS_HOME/Reportserver/Config/crystalclear.properties | grep -v ^# | tr '@:/' ' ' | awk '{print $NF}')"
		DBUSER="DBUser=$(grep -i oracle.properties= $ABS_HOME/Reportserver/Config/crystalclear.properties | grep -v ^# | tr '=&' ' ' | awk '{print $3}')"
	fi

    if [ -e $ABS_HOME/Server/License/License*.??? ]
	then
		LICCODE=`ls $ABS_HOME/Server/License/License*.???`
		if [ $? -eq 0 ]
		then
			LICCODE=`echo $LICCODE | sed 's/\(.*\)\(...\)$/\2/'`
		else
			LICCODE="???"
		fi
	else
		LICCODE="???"
	fi

    DBSCHVRS="-[ SLOT $1 DB:$SIDLST Schema:$DBUSER Version:$ABSSOLUTEVERSION $LICCODE ]-"
    echo 
	echo "Java Home $JAVA_HOME "
    echo 
    echo -n -e "-------------------------------------------------------------------------------\r"
    echo "$DBSCHVRS"
    echo "process                PID   mem (MB)    %CPU   start   monpt   Build date/time"
    echo "-------------------------------------------------------------------------------"
 }

show_absfooter()
{   echo "-------------------------------------------------------------------------------"
    echo
}

print_process_line()
{
# $1 - starting text
# $2 - listoption ("s" or "n")
# $3 - "down" text
# $4 - PID
# $5 - used memory
# $6 - CPU usage
# $7 - starttime
# $8 - jvisualvm monitoring port
# $9 - build date/time

    if [ "$4" ] 
    then
        echo -n "$1   "
        echo "$4 $5 $6 $7 $8 $9" | awk '{printf "%5s   %8s   %5s   %5s   %5s   %s\n",$1,$2,$3,$4,$5,$6}'
        if [ "$SHOWPORTLIST" ]
        then
            PRTLST=`$LSOFPATH/lsof -i -nP 2> /dev/null | grep java | grep LISTEN | grep $4 | awk '{ print $8 }' | sort`
            [ "$PRTLST" ] && echo "      Ports: " $PRTLST
        fi
    else
        [ "$2" == "n" ] && echo "$1   $3"
    fi
}

show_statuslist()
{  
# if short list and no processes running --> skip slot
    [ "$LISTOPTION" == "s" ] && [ -z "$XVIRTPID" ] && [ -z "$APPSRVPID" ] && [ -z "$REPSRVPID" ] && [ -z "$APPGUIPID" ] && [ -z "$REPGUIPID" ] && return 0

    JARNAME="$SERVERJARFORCHECKS"
    [ -e "$PATCHSERVERJARFORCHECKS" ] && JARNAME="$PATCHSERVERJARFORCHECKS"
    
    STARTLINE="Xvfb              "; print_process_line "$STARTLINE" "$2" "$DOWNSTRING" "$XVIRTPID" "$XVIRTMB" "$XVIRTCPU" "$XVIRTSTIME" "" ""
    APPBUILD="unknown"
    [ -e "$JARNAME" ] && APPBUILD=`unzip -l $JARNAME | grep META-INF/MANIFEST.MF | awk '{ print $2 "/" $3 }'`
	STARTLINE="Application server"; print_process_line "$STARTLINE" "$2" "$DOWNSTRING" "$APPSRVPID" "$APPSRVMB" "$APPSRVCPU" "$APPSRVSTIME" "$JMONAPPPORT" "$APPBUILD"
    APPBUILD="unknown"
    if [ $ABSSOLUTENUMVERSION -ge 80306 ]
    then
        REPORTSERVERJARNAME=reportserver.jar
    else
        REPORTSERVERJARNAME=ReportServer.jar
    fi
    [ -e "$ABS_HOME/Reportserver/Lib/$REPORTSERVERJARNAME" ] && APPBUILD=`unzip -l $ABS_HOME/Reportserver/Lib/$REPORTSERVERJARNAME | grep META-INF/MANIFEST.MF | awk '{ print $2 "/" $3 }'`
	STARTLINE="Report server     "; print_process_line "$STARTLINE" "$2" "$DOWNSTRING" "$REPSRVPID" "$REPSRVMB" "$REPSRVCPU" "$REPSRVSTIME" "$JMONREPPORT" "$APPBUILD"
	STARTLINE="Appl. server GUI  "; print_process_line "$STARTLINE" "$2" "$DOWNSTRING" "$APPGUIPID" "$APPGUIMB" "$APPGUICPU" "$APPGUISTIME" "" ""
    STARTLINE="Report server GUI "; print_process_line "$STARTLINE" "$2" "$DOWNSTRING" "$REPGUIPID" "$REPGUIMB" "$REPGUICPU" "$REPGUISTIME" "" ""
    STARTLINE="Glassfish server  "; print_process_line "$STARTLINE" "$2" "$DOWNSTRING" "$GLASSPID" "$GLASSMB" "$GLASSCPU" "$GLASSSTIME" "" ""
    STARTLINE="Client            "; print_process_line "$STARTLINE" "$2" "$DOWNSTRING" "$CLIENTPID" "$CLIENTMB" "$CLIENTCPU" "$CLIENTTIME" "" ""
}

#### Misc. functions ####################################################################
showlogfile()
{
   	case "$ABS_TYPE" in
   		app)	LOGFILENAME=`ls $ABS_HOME/Server/Log/ServerErrorLog_$LOGDATE.txt | tail -n 1`;;
   		rep)	LOGFILENAME=`ls $ABS_HOME/Reportserver/Log/ReportServerErrorLog_$LOGDATE.txt | tail -n 1`;;
        glass)  LOGFILENAME=`ls $ABS_HOME/$GLASSFISHPATH/domains/$GFDOMAIN/logs/server.log | tail -n 1`;;
   	esac
    if [ $FOLLOWLOG ]
    then
        log; echo "Following logfile $LOGFILENAME" >> $SCRIPTLOGNAME
        tail -f $LOGFILENAME
    else
        log; echo "Viewing logfile $LOGFILENAME" >> $SCRIPTLOGNAME
        $PAGER $LOGFILENAME
    fi
}

collect_logs()
{
    LOGYEAR=`date +%Y`
    TARFNAME=`date +$ABS_ROOTDIR/log/threaddump_conf_log_$1_%Y%m%d_%H%M%S.tgz`
    tar czf $TARFNAME --newer-mtime=`date -d "today -$LOGWEEKS weeks"` $ABS_ROOTDIR/log/* $SCRIPTLOGNAME $ABS_HOME/Server/Config/*.properties $ABS_HOME/Server/Log/ServerErrorLog_"$LOGYEAR"*.txt $ABS_HOME/Reportserver/Config/*.properties $ABS_HOME/Reportserver/Log/ReportServerErrorLog_"$LOGYEAR"*.txt $LOGFILENAME 

    echo "$MSGINDENT Please send the archived threadump and logfiles to ABS (helpdesk@abslbs.com)"
    echo "$MSGINDENT The files are archived in $TARFNAME"
    log; echo "Threaddump, logfiles and configs archived in $TARFNAME" >> $SCRIPTLOGNAME
}

check_slotnr()
{
    # returns: 0 = ok, 1 = slotnr < MIN, 2 = slotnr > MAX
    SLOTOK=0
    [ "$ABS_SLOT" -lt "$MIN_SLOT" ] && SLOTOK=1
    [ "$ABS_SLOT" -gt "$MAX_SLOT" ] && SLOTOK=2
    return $SLOTOK
}

get_pre_90000b_version()
{
    export SERVERJARFORCHECKS="$ABS_HOME/Server/Lib/server.jar"
    export PATCHSERVERJARFORCHECKS="$ABS_HOME/Server/Lib/patchserver.jar"
	if [ -e $SERVERJARFORCHECKS ]
	then
		SERVERJARVERSION=`unzip -ca $SERVERJARFORCHECKS META-INF/MANIFEST.MF | grep 'Name: absgroup/bl/' -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
		# the next echo is needed. do not remove....
		export SERVERJARVERSION=`echo $SERVERJARVERSION`
		export PATCHJARMAJORVERSION=$SERVERJARVERSION
		export ABSSOLUTEVERSION=$SERVERJARVERSION
	fi
	if [ -e $PATCHSERVERJARFORCHECKS ]
	then
        export PATCHJARVERSION=`unzip -ca $PATCHSERVERJARFORCHECKS META-INF/MANIFEST.MF | grep 'Name: absgroup/patchserver/' -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
		export PATCHJARMAJORVERSION=`echo $PATCHJARVERSION | sed 's/\(.*\)\(\.p..\)/\1/'`
		export ABSSOLUTEVERSION=$PATCHJARVERSION
	fi
	export ABSSOLUTENUMVERSION=`echo $SERVERJARVERSION | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\1\2\3/'`
	export ABSSOLUTELETTERVERSION=`echo $SERVERJARVERSION | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\4/'`
}

get_post_90000b_version()
{
    export SERVERJARFORCHECKS="$ABS_HOME/Server/Lib/server-impl.jar"
    export PATCHSERVERJARFORCHECKS="$ABS_HOME/Server/Lib/patchserver-impl.jar"
	if [ -e $SERVERJARFORCHECKS ]
	then
		SERVERJARVERSION=`unzip -ca $SERVERJARFORCHECKS META-INF/MANIFEST.MF | grep 'Name: absgroup/bl/' -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
		# the next echo is needed. do not remove....
		export SERVERJARVERSION=`echo $SERVERJARVERSION`
		export PATCHJARMAJORVERSION=$SERVERJARVERSION
		export ABSSOLUTEVERSION=$SERVERJARVERSION
	fi

# added for the full Patch providing	
#
	SUBSTRING=$(echo $ABSSOLUTEVERSION | cut -d'p' -f 1)

    if [ $ABSSOLUTEVERSION <> $SUBSTRING ]
	then 
		SERVERJARVERSION=`unzip -ca $SERVERJARFORCHECKS META-INF/MANIFEST.MF | grep 'Name: absgroup/bl/' -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
		# the next echo is needed. do not remove....
		export SERVERJARVERSION=`echo $SERVERJARVERSION | sed 's/\(.*\)\(\.p..\)/\1/'`
		export PATCHJARMAJORVERSION=`echo $SERVERJARVERSION | sed 's/\(.*\)\(\.p..\)/\1/'`
		export ABSSOLUTEVERSION=$ABSSOLUTEVERSION
	fi
#
# for backwards compatibility 90000d until 90304
	
	if [ -e $PATCHSERVERJARFORCHECKS ]
	then
		export PATCHJARVERSION=`unzip -ca $PATCHSERVERJARFORCHECKS META-INF/MANIFEST.MF | grep 'Name: absgroup/patchserverimpl/' -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
		export PATCHJARMAJORVERSION=`echo $PATCHJARVERSION | sed 's/\(.*\)\(\.p..\)/\1/'`
		export ABSSOLUTEVERSION=$PATCHJARVERSION
	fi
	export ABSSOLUTENUMVERSION=`echo $SERVERJARVERSION | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\1\2\3/'`
	export ABSSOLUTELETTERVERSION=`echo $SERVERJARVERSION | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\4/'`
}

get_abs_version()
{
	export ABSSOLUTEVERSION=unknown
	export SERVERJARVERSION=0
    
    if [ -e $ABS_HOME/Server/Lib/server-impl.jar ]
    then
        get_post_90000b_version
    else
        get_pre_90000b_version
    fi
}

set_java_home()
{
# set JAVA_HOME depending on ABSSolute version or command line parameter
	if [ "$JAVAPAR_HOME" ]
	then
		export JAVA_HOME=$JAVAPAR_HOME
		export JAVAVERSION=0
	else
		if [ $ABSSOLUTENUMVERSION -ge 90400 ]
		then
			export JAVA_HOME=$JAVA11_HOME
			export JAVAVERSION=11
			export GLASSFISHPATH="payara5"
		else
			if [ $ABSSOLUTENUMVERSION -ge 90103 ]
			then
				export JAVA_HOME=$JAVA8_131_HOME
				export JAVAVERSION=8
				export GLASSFISHPATH="glassfish4"
			else
				if [ $ABSSOLUTENUMVERSION -ge 90000 ]
				then
					export JAVA_HOME=$JAVA8_HOME
					export JAVAVERSION=8
					export GLASSFISHPATH="glassfish4"
				else
					export GLASSFISHPATH="glassfishv3"
					if [ $ABSSOLUTENUMVERSION -ge 80308 ]
					then
						export JAVA_HOME=$JAVA7_HOME
						export JAVAVERSION=7
					else
						export JAVA_HOME=$JAVA6_HOME
						export JAVAVERSION=6
					fi
				fi
			fi
		fi
	fi

	if [ ! -d "$JAVA_HOME" ]
	then
		echo "The JAVA_HOME setting ($JAVA_HOME) points to a non-existing"
		if [ $JAVAVERSION == 0 ]
		then
			echo "directory. Please check the commandline option."
		else
			echo "directory. Please check the configuration script."
		fi
		log; echo "JAVA_HOME ($JAVA_HOME) not found" >> $SCRIPTLOGNAME
		JAVAVERSION="err"
	else
		# try to locate the jmap executable. Needed to create a heapdump
		[ -x "$JAVA_HOME/bin/jmap" ] && JMAP_FOUND="yes"

		# try to locate the jVisualVM or jConsole executable
		MONITORAPP="$VISUALVM_HOME/bin/visualvm"
		if [ -x "$MONITORAPP" ] 
		then
			JVISUALVM_FOUND="yes"
		else
			MONITORAPP="$JAVA_HOME/bin/jvisualvm"
			if [ -x "$MONITORAPP" ] 
			then
				JVISUALVM_FOUND="yes"
			else
				export MONITORAPP="$JAVA_HOME/bin/jconsole"
				[ -x "$MONITORAPP" ] && export JCONSOLE_FOUND="yes"
			fi
		fi
		[ ! -d $JAVA_HOME/userprefs.app ] && mkdir -p $JAVA_HOME/userprefs.app
		[ ! -d $JAVA_HOME/systemprefs.app ] && mkdir -p $JAVA_HOME/systemprefs.app
		[ ! -d $JAVA_HOME/userprefs.rep ] && mkdir -p $JAVA_HOME/userprefs.rep
		[ ! -d $JAVA_HOME/systemprefs.rep ] && mkdir -p $JAVA_HOME/systemprefs.rep
		[ ! -d $JAVA_HOME/userprefs.gf ] && mkdir -p $JAVA_HOME/userprefs.gf
		[ ! -d $JAVA_HOME/systemprefs.gf ] && mkdir -p $JAVA_HOME/systemprefs.gf
	fi
}

generateCommandFile()
{
	# Subsequence of patchrun function. Values reqruied: MAINSQLDIR; SCRIPTFILE; ORA_SCHEMA; 
	# SCRIPTDIR and CONTINUEONERROR can be an empty string
	if [ "$CONTINUEONERROR" != "true" ]
	then
		echo "whenever sqlerror exit sql.sqlcode" > $MAINSQLDIR/command.file
	else
		echo "" > $MAINSQLDIR/command.file
	fi
	echo "set echo on" >> $MAINSQLDIR/command.file
	echo "set define off" >> $MAINSQLDIR/command.file
	if [ -z $SCRIPTDIR ] 
	then
		echo "spool $MAINSQLDIRLOG/$( basename $SCRIPTFILE .sql).log" >> $MAINSQLDIR/command.file
	else
		echo "spool $MAINSQLDIRLOG/$(basename $SCRIPTDIR)/$( basename $SCRIPTFILE .sql).log" >> $MAINSQLDIR/command.file
	fi
	echo "" >> $MAINSQLDIR/command.file
	echo "declare" >> $MAINSQLDIR/command.file
	echo "begin" >> $MAINSQLDIR/command.file
	echo "insert into $ORA_SCHEMA.sqlscript (scriptname, issuccessful, createddate) values ('$(basename $SCRIPTFILE)', 'N', sysdate);" >> $MAINSQLDIR/command.file
	echo "exception when dup_val_on_index then" >> $MAINSQLDIR/command.file
	echo "update $ORA_SCHEMA.sqlscript set issuccessful = 'N', modifieddate = sysdate where scriptname = '$(basename $SCRIPTFILE)';" >> $MAINSQLDIR/command.file
	echo "end;" >> $MAINSQLDIR/command.file
	echo "/" >> $MAINSQLDIR/command.file
	echo "commit;" >> $MAINSQLDIR/command.file
	echo "" >> $MAINSQLDIR/command.file
	if [ -z $SCRIPTDIR ] 
	then
		echo "@\"$MAINSQLDIR/$(basename $SCRIPTFILE)\"" >> $MAINSQLDIR/command.file
	else
		echo "@\"$SCRIPTDIR/$(basename $SCRIPTFILE)\"" >> $MAINSQLDIR/command.file
	fi
	echo "" >> $MAINSQLDIR/command.file
	echo "update $ORA_SCHEMA.sqlscript set issuccessful = 'Y' where scriptname = '$(basename $SCRIPTFILE)';" >> $MAINSQLDIR/command.file
	echo "commit;" >> $MAINSQLDIR/command.file
	echo "set define on" >> $MAINSQLDIR/command.file
	echo "set echo off" >> $MAINSQLDIR/command.file
	echo "spool off" >> $MAINSQLDIR/command.file
	echo "exit" >> $MAINSQLDIR/command.file
}

retreiveoraclevars()
{
	PROPERTIESFILE="$ABS_HOME/Server/Config/absserver.properties"
	[ -e $ABS_HOME/Server/Config/sample_application.properties ] && PROPERTIESFILE="$ABS_HOME/Server/Config/application.properties"

	export ORA_SID=$(cat $PROPERTIESFILE | grep 'DBSID=' | tail -1 | sed "s/DBSID=//g")
	export ORA_SCHEMA=$(cat $PROPERTIESFILE | grep 'DBUser=' | tail -1 | sed "s/DBUser=//g")
	export ORA_PASSWORD=$(cat $PROPERTIESFILE | grep 'DBPassword=' | tail -1 | sed "s/DBPassword=//g")
	export MAINSQLDIR=$ABS_HOME/Server/sqlscripts
	export PATH=$ORACLE_HOME/bin:$PATH
	
	if [ -d $MAINSQLDIR/log ]
	then
		export MAINSQLDIRLOG=$MAINSQLDIR/log
	elif [ -d $MAINSQLDIR/Log ]
	then
		export MAINSQLDIRLOG=$MAINSQLDIR/Log
	else
		export MAINSQLDIRLOG=$MAINSQLDIR/Log
		mkdir -p $MAINSQLDIR/Log
	fi
	
	if [ -d $MAINSQLDIR/completed ]
	then
		export MAINSQLDIRCOMP=$MAINSQLDIR/completed
	elif [ -d $MAINSQLDIR/Completed ]
	then
		export MAINSQLDIRCOMP=$MAINSQLDIR/Completed
	else
		export MAINSQLDIRCOMP=$MAINSQLDIR/Completed
		mkdir -p $MAINSQLDIR/Completed
	fi
	
	if [ -d $MAINSQLDIR/conversion ]
	then
		export MAINSQLDIRCONV=$MAINSQLDIR/conversion
	elif [ -d $MAINSQLDIR/Conversion ]
	then
		export MAINSQLDIRCONV=$MAINSQLDIR/Conversion
	else
		export MAINSQLDIRCONV=$MAINSQLDIR/Conversion
		mkdir -p $MAINSQLDIR/Conversion
	fi
}

patchrun()
{
	retreiveoraclevars
	
	if [ ! -z $ORACLE_HOME ] && [ -d $ORACLE_HOME ] && [ ! -z $ORA_SCHEMA ] && [ ! -z $ORA_PASSWORD ] && [ ! -z $ORA_SID ] && [ -d $MAINSQLDIR ]
	then
	
		if [ "$PATCHRUN_PREVIOUSPATCH" != "-" ]
		then 
			# Move old script directories that are already run 
			for (( c=1; c<=$PATCHRUN_PREVIOUSPATCH; c++ ))
			do
				if [ $c -lt 10 ]
				then
					[ -d $MAINSQLDIR/_p_0$(echo $c)_sql ] && mv $MAINSQLDIR/_p_0$(echo $c)_sql $MAINSQLDIRCOMP
				fi
				[ -d $MAINSQLDIR/_p_$(echo $c)_sql ] && mv $MAINSQLDIR/_p_$(echo $c)_sql $MAINSQLDIRCOMP
			done
		fi

		# Run all patch scripts
		[ $(find $MAINSQLDIR -maxdepth 1 -type d -name '_p_*_sql' | wc -l) -eq 0 ] && echo "No execution of patch related database scripts necessary"
		for SCRIPTDIR in $(find $MAINSQLDIR -maxdepth 1 -type d -name "_p_*_sql" | sort) ; do
		
			for SCRIPTFILE in $(find $SCRIPTDIR -maxdepth 1 -type f -name "*.sql") ; do		
				[ ! -d $MAINSQLDIRLOG/$(basename $SCRIPTDIR) ] && mkdir -p $MAINSQLDIRLOG/$(basename $SCRIPTDIR)
				
				if [ ! -d $MAINSQLDIRCOMP/$(basename $SCRIPTDIR) ] 
				then 
					mkdir  $MAINSQLDIRCOMP/$(basename $SCRIPTDIR)
					logline "Starting patchrun for scripts in directory $(basename $SCRIPTDIR) on SID ($ORA_SID) and SCHEMA ($ORA_SCHEMA)"
				fi
				
				generateCommandFile			
				sqlplus $ORA_SCHEMA/$ORA_PASSWORD@$ORA_SID @$MAINSQLDIR/command.file
				
				[ $? -eq 0 ] && mv $SCRIPTFILE $MAINSQLDIRCOMP/$(basename $SCRIPTDIR)
			done
					
			# Retry script that failed in case there are order dependencies to other scripts in same patch
			for SCRIPTFILE in $(find $SCRIPTDIR -maxdepth 1 -type f -name "*.sql") ; do	
				echo > $MAINSQLDIRLOG/$(basename $SCRIPTDIR)/$( basename $SCRIPTFILE | sed 's/.sql/.log/g')
				generateCommandFile			
				sqlplus $ORA_SCHEMA/$ORA_PASSWORD@$ORA_SID @$MAINSQLDIR/command.file
				if [ $? -ne 0 ]
				then
					logline "The patch script file $SCRIPTFILE did not run succesfull. Continue the scriptrun manually."
					echo "The patch script file $SCRIPTFILE did not run succesfull. Continue the scriptrun manually."
					exit 1
				fi
				mv $SCRIPTFILE $MAINSQLDIRCOMP/$(basename $SCRIPTDIR)
			done
			
			logline "The patch scripts from directory $SCRIPTDIR ran succesfull."
			rm -r $SCRIPTDIR	
		done
		exit 0
	else
		[ -z $ORACLE_HOME ] && logline "ORACLE_HOME value is empty" 
		[ ! -d $ORACLE_HOME ] && logline "ORACLE_HOME directory does not exists"
		[ -z $ORA_SCHEMA ] && logline "ORA_SCHEMA value is empty"
		[ -z $ORA_PASSWORD ] && logline "ORA_PASSWORD value is empty"
		[ -z $ORA_SID ] && logline "ORA_SID value is empty";
		[ ! -d $MAINSQLDIR ] && logline "MAINSQLDIR directory does not exists"
		exit 1
	fi
}

convrun()
{
	# Subsequence of patchrun function. Values reqruied: MAINSQLDIR; SCRIPTDIR; SCRIPTFILE; ORA_SCHEMA;
	retreiveoraclevars

	if [ ! -z $ORACLE_HOME ] && [ -d $ORACLE_HOME ] && [ ! -z $ORA_SCHEMA ] && [ ! -z $ORA_PASSWORD ] && [ ! -z $ORA_SID ] && [ -d $MAINSQLDIR ]
	then
		export ABS_ENV_SERVERJAR_VERSION=`unzip -ca $ABS_HOME/Server/Lib/server-impl.jar META-INF/MANIFEST.MF | grep "Name: absgroup/bl/" -A 2  | grep Specification-Version | grep -Eo '[[:digit:]]{1}.[[:digit:]]{2}.[[:digit:]]{2}'`
		export ABS_ENV_SERVERJAR_HASHREV=`unzip -ca $ABS_HOME/Server/Lib/server-impl.jar META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'`
		[ "$ABS_ENV_SERVERJAR_HASHREV" == "" ] && export ABS_ENV_SERVERJAR_HASHREV=`unzip -ca $ABS_HOME/Server/Lib/server-impl.jar META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'`

		export IDELETESCRIPT="deleteITables.$ABS_ENV_SERVERJAR_HASHREV.sql"
		export ICREATESCRIPT="EmptyITableDatabaseScript.$ABS_ENV_SERVERJAR_HASHREV.sql"
		export IPACKSCRIPT="Plsql_import_packages_$ABS_ENV_SERVERJAR_VERSION.$ABS_ENV_SERVERJAR_HASHREV.sql"
		
		[ ! -f "$MAINSQLDIR/$IDELETESCRIPT" ] && cp "$MAINSQLDIRCONV/$IDELETESCRIPT" "$MAINSQLDIR/$IDELETESCRIPT"
		[ ! -f "$MAINSQLDIR/$ICREATESCRIPT" ] && cp "$MAINSQLDIRCONV/$ICREATESCRIPT" "$MAINSQLDIR/$ICREATESCRIPT"
		[ ! -f "$MAINSQLDIR/$IPACKSCRIPT" ] && cp "$MAINSQLDIRCONV/$IPACKSCRIPT" "$MAINSQLDIR/$IPACKSCRIPT"
		
		if [ -f "$MAINSQLDIR/$IDELETESCRIPT"  ] && [ -f "$MAINSQLDIR/$ICREATESCRIPT" ] && [ -f "$MAINSQLDIR/$IPACKSCRIPT" ] 
		then
			SCRIPTDIR=""
			CONTINUEONERROR="true"
			
			SCRIPTFILE="$MAINSQLDIR/$IDELETESCRIPT"
			generateCommandFile
			sqlplus $ORA_SCHEMA/$ORA_PASSWORD@$ORA_SID @$MAINSQLDIR/command.file
			mv $SCRIPTFILE $MAINSQLDIRCOMP/$IDELETESCRIPT
			
			SCRIPTFILE="$MAINSQLDIR/$ICREATESCRIPT"
			generateCommandFile
			sqlplus $ORA_SCHEMA/$ORA_PASSWORD@$ORA_SID @$MAINSQLDIR/command.file
			mv $SCRIPTFILE $MAINSQLDIRCOMP/$IDELETESCRIPT
			
			SCRIPTFILE="$MAINSQLDIR/$IPACKSCRIPT"
			generateCommandFile
			sqlplus $ORA_SCHEMA/$ORA_PASSWORD@$ORA_SID @$MAINSQLDIR/command.file
			mv $SCRIPTFILE $MAINSQLDIRCOMP/$IDELETESCRIPT
			
		else
			echo "Conversion scripts will not be run because not all three scripts could be found"
		fi
	fi
}

############################################################################
############################################################################
#####                #######################################################
##### Main (kind of) #######################################################
#####                #######################################################
############################################################################
############################################################################

[ "$LISTOPTION" != "s" ] && LISTOPTION="n"
export ABS_HOME="$ABS_ROOTDIR/$ABS_SLOT"

# Retrieve the Oracle client directory in the absserver.properties / application.properties file
# This will overwrite the ORACLE_HOME value from the abs_config.sh 

PROPERTIESFILE="$ABS_HOME/Server/Config/absserver.properties"
[ -e $ABS_HOME/Server/Config/application.properties ] && PROPERTIESFILE="$ABS_HOME/Server/Config/application.properties"

if [ -f $PROPERTIESFILE ]
then
	dos2unix -q $PROPERTIESFILE
	ORACLE_CLIENT_DIR=$(cat $PROPERTIESFILE | grep "^[^#;]" | grep 'OracleHome' | cut -d '=' -f 2 | cut -d ' ' -f 1)
	if [ -d "$ORACLE_CLIENT_DIR" ]
	then
		export ORACLE_HOME="$ORACLE_CLIENT_DIR"
		export LD_LIBRARY_PATH="$ORACLE_HOME/lib"
	fi
fi

# Start patchrun
if [ ! -z "$PATCHRUN_PREVIOUSPATCH" ]
then 
	patchrun
	exit $?
fi

# try to creat the java tmp dir. if it fails to create, don't use java tmp dir
[ "$JAVA_TMP_DIR" ] && [ ! -d "$JAVA_TMP_DIR" ] && mkdir -p "$JAVA_TMP_DIR"
[ "$JAVA_TMP_DIR" ] && [ ! -d "$JAVA_TMP_DIR" ] && unset JAVA_TMP_DIR

if [ $SCRIPTUSER == 'root' -o $SCRIPTUSERID == 0 ]
then
    echo "User root is not allowed to use this script, and you are root..."
else
    if [ $SHOWVERSION ] 
    then 
        show_version
    else
        if [ $REMOTEMON ]
        then
            do_remote_monitor
            EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_MONITOR
		elif [ "$ABS_SLOT" == "all" ]
		then
			commandallsrv
        else
            if [ -z $LISTALLPIDS ]
            then
                case "$ABS_CMD" in
                    start|stop|restart|status|gui|thread|dump|monitor|collect|crash|deletedomain|createdomain|statusdomain|listdomain|deploy|enable|disable|isrunning|showversion|maxheap|resetheap|convrun)  
                            SLEN=${#ABS_SLOT}
                            while [ $SLEN -lt $SLOTPOS ]
                            do
                                export ABS_SLOT="0$ABS_SLOT"
                                SLEN=${#ABS_SLOT}
                            done    
                            TMPSLOT="$ABS_SLOT"
                            SLEN=${#ABS_SLOT}
                            if [ $SLEN -gt $SLOTPOS ]
                            then
                                export MSGINDENT="   "
                            else
                                export MSGINDENT=""
                            fi
                            for ((i=0; i<$SLEN; i+=$SLOTPOS))
                            do
                                export ABS_SLOT=${TMPSLOT:$i:$SLOTPOS}
                                check_slotnr
                                if [ $? == 0 ] 
                                then
                                    export ABS_HOME="$ABS_ROOTDIR/$ABS_SLOT"
                                    if [ -d $ABS_HOME ]
                                    then
                                        get_abs_version
                                        set_java_home
                                        if [ ! "$JAVAVERSION" == "err" ]
                                        then
                                            checkstartflagfile
                                            getpids $ABS_SLOT
                                            export LD_LIBRARY_PATH="$ORACLE_HOME/lib":"$ABS_HOME/Server/Lib":"$LD_LIBRARY_PATH"
                                            export TNS_ADMIN="$ORACLE_HOME/network/admin"
                                            export PATH="$ABS_HOME/Server/Bin":"$ABS_HOME/Reportserver/Lib":"$PATH"
                                            export PATH="$JAVA_HOME/bin":"$ORACLE_HOME/bin":"$PATH"
                                            [ $SLEN -gt 1 ] && [ $ABS_CMD != "showversion" ] && echo "** Slot $ABS_SLOT"
                                            case "$ABS_CMD" in
                                                start)          startsrv;               EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_START;;
                                                stop)           stopsrv; checkXvfb;     EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_STOP;;
                                                restart)        stopsrv; startsrv;      EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_RESTART;;
                                                status)         if [ -e "$SERVERJARFORCHECKS" -o -e "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ]
                                                                then
                                                                    show_head_info
                                                                    show_absheader $ABS_SLOT;
                                                                    show_statuslist $ABS_SLOT $LISTOPTION;
                                                                    show_absfooter;     EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_STATUS
                                                                fi;;
                                                gui)            startgui;               EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_GUI;;
                                                thread)         threaddump;             EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_THREAD;;
                                                dump)           heapdump;               EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_DUMP;;
                                                monitor)        do_local_monitor;       EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_MONITOR;;
                                                collect)        collect_logs $ABS_SLOT; EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_COLLECT;;
                                                crash)          crash_recovery;;
                                                deletedomain)   delete_domain;;
                                                createdomain)   create_domain;;
						statusdomain)   status_domain;;
						listdomain)	list_domain;;
                                                deploy)         deploy_war;;
                                                enable)         enablesrv;;
                                                disable)        disablesrv;;
                                                isrunning)      checkifrunning;;
                                                showversion)    show_version_abs;;
						maxheap)	maxheap;;
						resetheap)	resetheap;;
					        convrun)	convrun;;
                                            esac
                                        fi
                                    fi
                                else
                                    echo "$MSGINDENT Slotnumber $ABS_SLOT out of range ($MIN_SLOT .. $MAX_SLOT)"
                                fi
                            done;;
                    log)    check_slotnr
                            if [ $? = 0 ] 
                            then
                                showlogfile
                            else
                                echo "Slotnumber $ABS_SLOT out of range ($MIN_SLOT .. $MAX_SLOT)"
                            fi
                            EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_LOG;;
                    *|h)    show_usage;;
                esac
            else
                show_head_info
                for ((a=MIN_SLOT; a<=MAX_SLOT; a++))
                do
                    make_wideslot $a
                    export ABS_SLOT=$WIDESLOT
                    export ABS_HOME="$ABS_ROOTDIR/$ABS_SLOT"
                    if [ -d $ABS_HOME ]
                    then
                        get_abs_version
                        if [ -e "$SERVERJARFORCHECKS" -o -e "$ABS_HOME/$GLASSFISHPATH/bin/asadmin" ]
                        then
                            getpids $ABS_SLOT
                            show_absheader $ABS_SLOT; 
                            show_statuslist $ABS_SLOT $LISTOPTION
                        fi    
                    fi    
                done
                show_absfooter
                EMAIL_USAGE_FLAG=$EMAIL_SEND_ON_STATUS
            fi    
        fi
    fi
fi

# If needed, send logfile by email when actions where caused by OOM or fatal error
if [ "$EMAIL_ON_OOM_FATAL" == "on" ]
then
    [ "$CAUSEDBYMEMERR" ] && SENDCRASHMAIL="yes"
    [ "$CAUSEDBYFATALERR" ] && SENDCRASHMAIL="yes"
    if [ "$SENDCRASHMAIL" ]
    then
        send_email crash
        EMAIL_ON_MAINT="off"
    fi
fi
# send logfile by email if needed when script is used and no crashmail has been sent
[ "$EMAIL_ON_MAINT" == "on" ] && [ "$EMAIL_USAGE_FLAG" == "yes" ] && send_email maint
