#!/bin/bash
#
#########################################################################################
# Script: 		select_current_evironment.sh
# Description: 	Select environment for further processing 
# Author: 		ABS Laundry Business Solutions
# Version: 		4.5
#### Changelog of script ################################################################
# Date        Author    Version 	Change    
# 15-May-2011 JMI		1.0			Initial version
# 13-Jun-2011 JMI		1.2			Included displaying ABSSolute version
# 18-Jun-2011 JMI		1.3			Replaced rps to global repository setting
#									Included customer license code
# 08-Dec-2014 JMI		1.4			Changed egrep to grep to support more then 9 environment in selection
#########################################################################################

export NOABSREPOS=0

get_environment_details()
{
    # Load environment details for selected environment
    ABSMENU_CURRENT_ENV_SERVERJAR_VERSION=" -"
    ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME=" "
    ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION=" -"
    ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME=" "
    ABSMENU_FUTURE_ENV_SERVERJAR_VERSION=" -"
    ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME=" "
    ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION=" -"
    ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME=" "
    ABSMENU_CURRENT_ENV_LICCODE=" -"

    # Check repositories
    if [ "$ABS_ROOTSERVER" == "$ABSMENUREPOSITORYSERVER" ]
    then
        export NOABSREPOS=$([ -d "$ABSMENUREPOSITORYLOCATION/$1" ] && echo 0 || echo 1)
    else
        export NOABSREPOS=$(ssh -qn $ABSMENUREPOSITORYSERVER "[ -d \"$ABSMENUREPOSITORYLOCATION/$1\" ] && echo 0 || echo 1")
    fi

    if [ "$NOABSREPOS" -eq 0 ]
    then
        if [ "$ABS_ROOTSERVER" == "$ABSMENUREPOSITORYSERVER" ]
        then
            if [ -e "$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/server-impl.jar" ] # assume 9.00.00.b or higher
            then
                ABSCURVERSIONCHECKSERVER="$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/server-impl.jar"
                ABSCURVERSIONCHECKSERVERSTRING=bl
                ABSCURVERSIONCHECKPATCHSERVER="$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/patchserver-impl.jar"
                ABSCURVERSIONCHECKPATCHSERVERSTRING=patchserverimpl
            else
                ABSCURVERSIONCHECKSERVER="$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/server.jar"
                ABSCURVERSIONCHECKSERVERSTRING=bl
                ABSCURVERSIONCHECKPATCHSERVER="$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/patchserver.jar"
                ABSCURVERSIONCHECKPATCHSERVERSTRING=patchserver
            fi
            if [ -e "$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/server-impl.jar" ] # assume 9.00.00.b or higher
            then
                ABSFUTVERSIONCHECKSERVER="$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/server-impl.jar"
                ABSFUTVERSIONCHECKSERVERSTRING=bl
                ABSFUTVERSIONCHECKPATCHSERVER="$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/patchserver-impl.jar"
                ABSFUTVERSIONCHECKPATCHSERVERSTRING=patchserverimpl
            else
                ABSFUTVERSIONCHECKSERVER="$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/server.jar"
                ABSFUTVERSIONCHECKSERVERSTRING=bl
                ABSFUTVERSIONCHECKPATCHSERVER="$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/patchserver.jar"
                ABSFUTVERSIONCHECKPATCHSERVERSTRING=patchserver
            fi

            if [ -e $ABSCURVERSIONCHECKSERVER ]
            then
                ABSMENU_CURRENT_ENV_SERVERJAR_VERSION=`unzip -ca $ABSCURVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep "Name: absgroup/$ABSCURVERSIONCHECKSERVERSTRING/" -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
                ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME=`unzip -l $ABSCURVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep META-INF/MANIFEST.MF | awk '{ print $2 "_" $3 }'`
                ABSMENU_CURRENT_ENV_SERVERJAR_REVISION=`unzip -ca $ABSCURVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'`
				[ "$ABSMENU_CURRENT_ENV_SERVERJAR_REVISION" == "" ] && ABSMENU_CURRENT_ENV_SERVERJAR_REVISION=`unzip -ca $ABSCURVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'`
            fi
            ABSMENU_CURRENT_ENV_SERVERJAR_VERSION=${ABSMENU_CURRENT_ENV_SERVERJAR_VERSION:1}
            if [ -e $ABSCURVERSIONCHECKPATCHSERVER ]
            then
                ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION=`unzip -ca $ABSCURVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep "Name: absgroup/$ABSCURVERSIONCHECKPATCHSERVERSTRING/" -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
                ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME=`unzip -l $ABSCURVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep META-INF/MANIFEST.MF | awk '{ print $2 "_" $3 }'`
                ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION=`unzip -ca $ABSCURVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'`
				[ "$ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION" == "" ] && ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION=`unzip -ca $ABSCURVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'`
            fi
            ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION=${ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION:1}
            if [ -e $ABSMENUREPOSITORYLOCATION/$1/Current/Server/License/License*.??? ]
            then 
                ABSMENU_CURRENT_ENV_LICCODE=`ls $ABSMENUREPOSITORYLOCATION/$1/Current/Server/License/License*.??? | sed 's/\(.*\)\(...\)\$/\2/'`
			else
				if [ -e $ABSMENUREPOSITORYLOCATION/$1/Current/LICENSE/License*.??? ]
				then
					ABSMENU_CURRENT_ENV_LICCODE=`ls $ABSMENUREPOSITORYLOCATION/$1/Current/LICENSE/License*.??? | sed 's/\(.*\)\(...\)\$/\2/'`
				fi
            fi
            if [ -e $ABSFUTVERSIONCHECKSERVER ]
            then 
                ABSMENU_FUTURE_ENV_SERVERJAR_VERSION=`unzip -ca $ABSFUTVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep "Name: absgroup/$ABSFUTVERSIONCHECKSERVERSTRING/" -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
                ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME=`unzip -l $ABSFUTVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep META-INF/MANIFEST.MF | awk '{ print $2 "_" $3 }'`
                ABSMENU_FUTURE_ENV_SERVERJAR_REVISION=`unzip -ca $ABSFUTVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'`
				[ "$ABSMENU_FUTURE_ENV_SERVERJAR_REVISION" == "" ] && ABSMENU_FUTURE_ENV_SERVERJAR_REVISION=`unzip -ca $ABSFUTVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'`
            fi
            ABSMENU_FUTURE_ENV_SERVERJAR_VERSION=${ABSMENU_FUTURE_ENV_SERVERJAR_VERSION:1}
            if [ -e $ABSFUTVERSIONCHECKPATCHSERVER ]
            then
                ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION=`unzip -ca $ABSFUTVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep "Name: absgroup/$ABSFUTVERSIONCHECKPATCHSERVERSTRING/" -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'`
                ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME=`unzip -l $ABSFUTVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep META-INF/MANIFEST.MF | awk '{ print $2 "_" $3 }'`
                ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION=`unzip -ca $ABSFUTVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'`
				[ "$ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION" == "" ] && ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION=`unzip -ca $ABSFUTVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'`
            fi
            ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION=${ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION:1}
        else
            ABSCURVERSIONPRE90000B=$(ssh -qn $ABSMENUREPOSITORYSERVER "[ -e $ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/server-impl.jar ] && echo N || echo Y")
            ABSFUTVERSIONPRE90000B=$(ssh -qn $ABSMENUREPOSITORYSERVER "[ -e $ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/server-impl.jar ] && echo N || echo Y")
            if [ $ABSCURVERSIONPRE90000B == N ]
            then
                ABSCURVERSIONCHECKSERVER="$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/server-impl.jar"
                ABSCURVERSIONCHECKSERVERSTRING=bl
                ABSCURVERSIONCHECKPATCHSERVER="$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/patchserver-impl.jar"
                ABSCURVERSIONCHECKPATCHSERVERSTRING=patchserverimpl
            else
                ABSCURVERSIONCHECKSERVER="$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/server.jar"
                ABSCURVERSIONCHECKSERVERSTRING=bl
                ABSCURVERSIONCHECKPATCHSERVER="$ABSMENUREPOSITORYLOCATION/$1/Current/Server/Lib/patchserver.jar"
                ABSCURVERSIONCHECKPATCHSERVERSTRING=patchserver
            fi
            if [ $ABSFUTVERSIONPRE90000B == N ]
            then
                ABSFUTVERSIONCHECKSERVER="$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/server-impl.jar"
                ABSFUTVERSIONCHECKSERVERSTRING=bl
                ABSFUTVERSIONCHECKPATCHSERVER="$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/patchserver-impl.jar"
                ABSFUTVERSIONCHECKPATCHSERVERSTRING=patchserverimpl
            else
                ABSFUTVERSIONCHECKSERVER="$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/server.jar"
                ABSFUTVERSIONCHECKSERVERSTRING=bl
                ABSFUTVERSIONCHECKPATCHSERVER="$ABSMENUREPOSITORYLOCATION/$1/Future/Server/Lib/patchserver.jar"
                ABSFUTVERSIONCHECKPATCHSERVERSTRING=patchserver
            fi
            ABSMENU_CURRENT_ENV_SERVERJAR_VERSION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSCURVERSIONCHECKSERVER ]; then unzip -ca $ABSCURVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep "Name: absgroup/$ABSCURVERSIONCHECKSERVERSTRING/" -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'; else echo \" -\"; fi;")
            ABSMENU_CURRENT_ENV_SERVERJAR_VERSION=${ABSMENU_CURRENT_ENV_SERVERJAR_VERSION:1}
            ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSCURVERSIONCHECKSERVER ]; then unzip -l $ABSCURVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep META-INF/MANIFEST.MF; fi;" | awk '{ print $2 "_" $3 }')
            ABSMENU_CURRENT_ENV_SERVERJAR_REVISION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSCURVERSIONCHECKSERVER ]; then unzip -ca $ABSCURVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'; else echo \" -\"; fi;")
			[ "$ABSMENU_CURRENT_ENV_SERVERJAR_REVISION" == "" ] && ABSMENU_CURRENT_ENV_SERVERJAR_REVISION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSCURVERSIONCHECKSERVER ]; then unzip -ca $ABSCURVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'; else echo \" -\"; fi;")
            ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSCURVERSIONCHECKPATCHSERVER ]; then unzip -ca $ABSCURVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep "Name: absgroup/$ABSCURVERSIONCHECKPATCHSERVERSTRING/" -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'; else echo \" -\"; fi;")
            ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION=${ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION:1}
            ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSCURVERSIONCHECKPATCHSERVER ]; then unzip -l $ABSCURVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep META-INF/MANIFEST.MF; fi;" | awk '{ print $2 "_" $3 }')
            ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSCURVERSIONCHECKPATCHSERVER ]; then unzip -ca $ABSCURVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'; else echo \" -\"; fi;")
			[ "$ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION" == "" ] && ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSCURVERSIONCHECKPATCHSERVER ]; then unzip -ca $ABSCURVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'; else echo \" -\"; fi;")
			ABSMENU_CURRENT_ENV_LICCODE=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSMENUREPOSITORYLOCATION/$1/Current/Server/License/License*.??? ]; then ls $ABSMENUREPOSITORYLOCATION/$1/Current/Server/License/License*.??? | sed 's/\(.*\)\(...\)\$/\2/'; else if [ -e $ABSMENUREPOSITORYLOCATION/$1/Current/LICENSE/License*.??? ]; then ls $ABSMENUREPOSITORYLOCATION/$1/Current/LICENSE/License*.??? | sed 's/\(.*\)\(...\)\$/\2/'; else echo \"-\"; fi; fi;")
            ABSMENU_FUTURE_ENV_SERVERJAR_VERSION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSFUTVERSIONCHECKSERVER ]; then unzip -ca $ABSFUTVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep "Name: absgroup/$ABSFUTVERSIONCHECKSERVERSTRING/" -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'; else echo \" -\"; fi;")
            ABSMENU_FUTURE_ENV_SERVERJAR_VERSION=${ABSMENU_FUTURE_ENV_SERVERJAR_VERSION:1}
            ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSFUTVERSIONCHECKSERVER ]; then unzip -l $ABSFUTVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep META-INF/MANIFEST.MF; fi;" | awk '{ print $2 "_" $3 }')
            ABSMENU_FUTURE_ENV_SERVERJAR_REVISION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSFUTVERSIONCHECKSERVER ]; then unzip -ca $ABSFUTVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'; else echo \" -\"; fi;")
            [ "$ABSMENU_FUTURE_ENV_SERVERJAR_REVISION" == "" ] && ABSMENU_FUTURE_ENV_SERVERJAR_REVISION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSFUTVERSIONCHECKSERVER ]; then unzip -ca $ABSFUTVERSIONCHECKSERVER META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'; else echo \" -\"; fi;")
			ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSFUTVERSIONCHECKPATCHSERVER ]; then unzip -ca $ABSFUTVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep "Name: absgroup/$ABSFUTVERSIONCHECKPATCHSERVERSTRING/" -A 2 | grep Specification-Version | sed 's/\(.*:\)\(.*\)\(\r\)/\2/'; else echo \" -\"; fi;")
            ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION=${ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION:1}
            ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSFUTVERSIONCHECKPATCHSERVER ]; then unzip -l $ABSFUTVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep META-INF/MANIFEST.MF; fi;" | awk '{ print $2 "_" $3 }')
            ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSFUTVERSIONCHECKPATCHSERVER ]; then unzip -ca $ABSFUTVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep Revision | sed 's/Revision\: \(.*\)\(\r\)/\1/'; else echo \" -\"; fi;")
			[ "$ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION" == "" ] && ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION=$(ssh -qn $ABSMENUREPOSITORYSERVER "if [ -e $ABSFUTVERSIONCHECKPATCHSERVER ]; then unzip -ca $ABSFUTVERSIONCHECKPATCHSERVER META-INF/MANIFEST.MF | grep Hash | sed 's/Hash\: \(.*\)\(\r\)/\1/'; else echo \" -\"; fi;")
		fi
	else
		# message if repository does not exists
		ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION='(repository not found)'
        ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION='(repository not found)'
	fi

    ABSMENU_CURRENT_ENV_VERSION="$ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION"
    [ "$ABSMENU_CURRENT_ENV_VERSION" == "-" -o "$ABSMENU_CURRENT_ENV_VERSION" == "" ] && ABSMENU_CURRENT_ENV_VERSION="$ABSMENU_CURRENT_ENV_SERVERJAR_VERSION"
    ABSMENU_FUTURE_ENV_VERSION="$ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION"
    [ "$ABSMENU_FUTURE_ENV_VERSION" == "-" -o "$ABSMENU_FUTURE_ENV_VERSION" == "" ] && ABSMENU_FUTURE_ENV_VERSION="$ABSMENU_FUTURE_ENV_SERVERJAR_VERSION"

    [ "$ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION" == "-" ] && ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME="-"
    [ "$ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION" == "-" ] && ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME="-"
    [ "$ABSMENU_CURRENT_ENV_SERVERJAR_REVISION" == "" ] && ABSMENU_CURRENT_ENV_SERVERJAR_REVISION="-"
    [ "$ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION" == "" ] && ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION="-"
    [ "$ABSMENU_FUTURE_ENV_SERVERJAR_REVISION" == "" ] && ABSMENU_FUTURE_ENV_SERVERJAR_REVISION="-"
    [ "$ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION" == "" ] && ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION="-"

    export ABSMENU_CURRENT_ENV_SERVERJAR_VERSION ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION
    export ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME ABSMENU_FUTURE_ENV_SERVERJAR_VERSION ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME
    export ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME ABSMENU_CURRENT_ENV_LICCODE
    export ABSMENU_CURRENT_ENV_VERSION ABSMENU_CURRENT_ENV_SERVERJAR_REVISION ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION 
    export ABSMENU_FUTURE_ENV_SERVERJAR_REVISION ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION
}

build_environment_list()
{
    # build new environment list
    [ -e "$ABSMENUPATH/tmp/all_environments.tmp" ] && rm -f "$ABSMENUPATH/tmp/all_environments.tmp"
    touch "$ABSMENUPATH/tmp/all_environments.tmp"
    COUNTER=1
    while read ABSM_ENVIRONMENT_NAME
    do 
        get_environment_details "$ABSM_ENVIRONMENT_NAME"
        if [ "$NOABSREPOS" -eq 0 ]
        then
			if [ "$UNATTENDED_ENABLED" == "yes" ] && [ "$1" == "$ABSM_ENVIRONMENT_NAME" ]
			then
				export UNATTENDED_COUNTER=$COUNTER
			fi
			# Display a list on-screen (number, environment name, current version)
            echo $COUNTER $ABSM_ENVIRONMENT_NAME $ABSMENU_CURRENT_ENV_VERSION 
            # Create an internal selection-list (number, environment name, active instance)
            echo $COUNTER $ABSM_ENVIRONMENT_NAME $ABSMENU_CURRENT_ENV_LICCODE $ABSMENU_CURRENT_ENV_SERVERJAR_VERSION $ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME $ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION $ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME $ABSMENU_FUTURE_ENV_SERVERJAR_VERSION $ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME $ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION $ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME $ABSMENU_CURRENT_ENV_SERVERJAR_REVISION $ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION $ABSMENU_FUTURE_ENV_SERVERJAR_REVISION $ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION >> $ABSMENUPATH/tmp/all_environments.tmp
            COUNTER=$(($COUNTER + 1))
        else
            echo "- $ABSM_ENVIRONMENT_NAME $ABSMENU_CURRENT_ENV_VERSION "
        fi
    done< <( grep -v '^ *:' $ABSMENUGLOBALCONFIG | awk ' { print $1 } ' | sort -u )
}

#########################################################################################
## main()
#########################################################################################

case $1 in
	"get_details")

		get_environment_details "$ABSM_ENVIRONMENT_NAME"
		;;
		
	"select")
		build_environment_list $2
		V_MENU_CHOICE=$UNATTENDED_COUNTER

		#Get environment from second parameter
		read COUNTER ABSM_ENVIRONMENT_NAME ABSMENU_CURRENT_ENV_LICCODE ABSMENU_CURRENT_ENV_SERVERJAR_VERSION ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME ABSMENU_FUTURE_ENV_SERVERJAR_VERSION ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME ABSMENU_CURRENT_ENV_SERVERJAR_REVISION ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION ABSMENU_FUTURE_ENV_SERVERJAR_REVISION ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION < <(grep -w ^$V_MENU_CHOICE $ABSMENUPATH/tmp/all_environments.tmp)

		export ABSM_ENVIRONMENT_NAME 
		;;
		
	*)

		clear
		echo
		echo Please select the environment you want to manage
		echo
		build_environment_list
		echo
		read -p "Make a choice from the menu, or press ENTER to quit: " V_MENU_CHOICE
		echo
		if [ ! "$V_MENU_CHOICE" ]
		then
			echo
			echo "Terminating script..."
			echo
			exit 1
		fi

		# Get selected environment name and details by user input
		read COUNTER ABSM_ENVIRONMENT_NAME ABSMENU_CURRENT_ENV_LICCODE ABSMENU_CURRENT_ENV_SERVERJAR_VERSION ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME ABSMENU_FUTURE_ENV_SERVERJAR_VERSION ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME ABSMENU_CURRENT_ENV_SERVERJAR_REVISION ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION ABSMENU_FUTURE_ENV_SERVERJAR_REVISION ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION < <(grep -w ^$V_MENU_CHOICE $ABSMENUPATH/tmp/all_environments.tmp)

		export ABSM_ENVIRONMENT_NAME 
		;;
esac
    
export ABSMENU_CURRENT_ENV_LICCODE 
export ABSMENU_CURRENT_ENV_SERVERJAR_VERSION 
export ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME 
export ABSMENU_CURRENT_ENV_SERVERJAR_REVISION 
export ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION 
export ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME 
export ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION 
export ABSMENU_FUTURE_ENV_SERVERJAR_VERSION 
export ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME 
export ABSMENU_FUTURE_ENV_SERVERJAR_REVISION 
export ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION 
export ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME 
export ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION
export ABSMENU_SELECTED_ENVIRONMENT="$ABSM_ENVIRONMENT_NAME"
export ABSMENU_CURRENT_ENVIRONMENT="$ABSM_ENVIRONMENT_NAME"

export ABSMESSAGE="Selected environment   : $ABSMENU_CURRENT_ENVIRONMENT";                                                                                                              absmenu_log
export ABSMESSAGE="Customer code          : $ABSMENU_CURRENT_ENV_LICCODE";                                                                                                              absmenu_log
export ABSMESSAGE="Current Server version : $ABSMENU_CURRENT_ENV_SERVERJAR_VERSION $ABSMENU_CURRENT_ENV_SERVERJAR_BUILDTIME $ABSMENU_CURRENT_ENV_SERVERJAR_REVISION";                   absmenu_log
export ABSMESSAGE="Current Patch version  : $ABSMENU_CURRENT_ENV_PATCHSERVERJAR_VERSION $ABSMENU_CURRENT_ENV_PATCHSERVERJAR_BUILDTIME $ABSMENU_CURRENT_ENV_PATCHSERVERJAR_REVISION";    absmenu_log
export ABSMESSAGE="Repository home        : $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT";                                                          absmenu_log
export ABSMESSAGE="Backup home            : $ABSMENUBACKUPSERVER:$ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT";                                                                  absmenu_log
export ABSMESSAGE="Future Server version  : $ABSMENU_FUTURE_ENV_SERVERJAR_VERSION $ABSMENU_FUTURE_ENV_SERVERJAR_BUILDTIME $ABSMENU_FUTURE_ENV_SERVERJAR_REVISION";                      absmenu_log
export ABSMESSAGE="Future Patch version   : $ABSMENU_FUTURE_ENV_PATCHSERVERJAR_VERSION $ABSMENU_FUTURE_ENV_PATCHSERVERJAR_BUILDTIME $ABSMENU_FUTURE_ENV_PATCHSERVERJAR_REVISION";       absmenu_log