#!/bin/sh
# 
# To map jdk.  3 options are available:
# 1. Check dev_home for quality assured jdk
#       dev
#          star
#          jdk
#             linux-x86
#                j2sdk1.4.2_08
# 
# 2. Check install maparch directory for quality assured jdk
#       starccm+2.02.001
#          star
#       jdk
#          linux-x86-2.2.5
#             j2sdk1.4.2_08
#
# 3. Check install directory for quality assured jdk
#       starccm+2.02.001
#          star
#       jdk
#          linux-x86
#             j2sdk1.4.2_08
#
# 4. Check install maparch directory and scan for the latest version of jdk
#       starccm+2.02.001
#          star
#       jdk
#          linux-x86-2.2.5
#             j2sdk1.4.2_08
#             j2sdk1.4.2_10
#
# 5. Check install directory for the latest version of jdk
#       starccm+2.02.001
#          star
#       jdk
#          linux-x86
#             j2sdk1.4.2_08
#             j2sdk1.4.2_10
#       
# 6. Old option
#       starccm+2.02.001
#          star
#       java
#          bin
#    
verify_jdk() {
    if [ -r "$1/bin/java" ]; then
        echo "$1"
        exit
    fi
}


option_1() {
    verify_jdk `$STAR_BIN_DIR/getjdk -arch $star_arch_noversion`
}


option_2() {
    verify_jdk `$STAR_BIN_DIR/getjdk -installhome $install_home -maparch -arch $star_arch_noversion`
}


option_3() {
    verify_jdk `$STAR_BIN_DIR/getjdk -installhome $install_home -arch $star_arch_noversion`
}


option_4() {
    # Check with install directory with the actual star_arch
    find_latest_version "$install_home/jdk/$star_arch"
 }


option_5() {
    find_latest_version "$install_home/jdk/$star_arch_noversion"
}

std_option() {
case "$star_arch" in

    aix-powerpc-5*)
	verify_jdk "/usr/java6_64"
	verify_jdk "/usr/java16_64"
    ;;

esac
}

old_option() {
    verify_jdk "$dev_home/java"
    verify_jdk "$install_home/java"
}


find_latest_version() {
    j2sdk_dir="$1"
    arch=`basename $1`
    if [ -r  $j2sdk_dir ] ; then
    # the standard version prefix
        j2sdk_version="jdk1"
	JDK_HOME=

	case "$arch" in
	    hpux-* )
                for j2sdk in "$j2sdk_dir"/$j2sdk_version* ; do
                    j2sdk_basename=`basename $j2sdk`

                    case "$j2sdk_basename" in
                        jdk1.5.0_01 | jdk1.5.0_02 | jdk1.5.0_03 )
                        ;;

                        * )
                        JDK_HOME=$j2sdk
                        ;;
                    esac
                done
                verify_jdk "$JDK_HOME"
	    ;;

	    * )
                for j2sdk in "$j2sdk_dir"/$j2sdk_version* ; do
                    j2sdk_basename=`basename $j2sdk`

                    case "$j2sdk_basename" in
                       * )
                       JDK_HOME="$j2sdk"
                       ;;
                   esac
		done
		verify_jdk "$JDK_HOME"
	    ;;
	esac
    fi
}

STAR_BIN_DIR=`dirname "$0"`
dev_home=`cd $STAR_BIN_DIR/../.. && pwd`
install_home=`cd $STAR_BIN_DIR/../../.. && pwd`

if [ "$#" -eq 1 ] ; then
    star_arch=$1
else 
    star_arch=`$STAR_BIN_DIR/maparch`
fi

case "$star_arch" in

    linux-x86_64-2.*)   star_arch_noversion=linux-x86_64;;

    linux-x86-2.*)      star_arch_noversion=linux-x86;;

    linux-ia64-2.*)     star_arch_noversion=linux-ia64;;

    sunos-sparc-*)      star_arch_noversion=sunos-sparc;;

    sunos-x86_64-*)     star_arch_noversion=sunos-x86_64;;

    hpux-hppa-11.*)     star_arch_noversion=hpux-hppa;;

    hpux-ia64-11.*)     star_arch_noversion=hpux-ia64;;

    aix-powerpc-*)    star_arch_noversion=aix-powerpc;;

    *)                  star_arch_noversion="$star_arch";;

esac

option_1
option_2
option_3
option_4
option_5
std_option
old_option

