#!/bin/sh
##
##  java-toolkit.sh -- Java toolkit frontend
##  Copyright (c) 2007 Ralf S. Engelschall <rse@engelschall.com>
##

#   make sure variables are exported
export JAVA_PLATFORM
export JAVA_HOME

#   determine requested platform and location
determine_variables () {
    if [ ".$JAVA_PLATFORM" = . ] && [ ".$JAVA_HOME" = . ]; then
        #   use default platform
        entry=`grep '^\+ ' @l_prefix@/etc/java/toolkits`
        if [ ".$entry" != . ]; then
            JAVA_PLATFORM=`echo ".$entry" | awk '{ print $2; }'`
            JAVA_HOME=`echo ".$entry" | awk '{ print $3; }'`
        fi
    elif [ ".$JAVA_PLATFORM" = . ] && [ ".$JAVA_HOME" != . ]; then
        #   use user supplied platform
        if [ -f "$JAVA_HOME/lib/tools.jar" ] && [ -f "$JAVA_HOME/bin/javac" ]; then
            JAVA_PLATFORM="sun-jdk"
        elif [ -f "$JAVA_HOME/lib/rt.jar" ] && [ -f "$JAVA_HOME/bin/java" ]; then
            JAVA_PLATFORM="sun-jre"
        elif [ ".`(grep 'GCJ' $JAVA_HOME/README) 2>/dev/null`" != . ]; then
            JAVA_PLATFORM="gnu-gcj"
        else
            JAVA_PLATFORM="unknown-unknown"
        fi
        version=`($JAVA_HOME/bin/java -version 2>&1; echo "") 2>/dev/null | \
            grep "^java version" | sed -e '1q' | \
            sed -e 's;^;X;' -e 's;^X.*"\([0-9][0-9.]*\)".*$;\1;' -e 's;^X.*;0.0.0;'`
        JAVA_PLATFORM="$JAVA_PLATFORM-$version"
    elif [ ".$JAVA_PLATFORM" != . ] && [ ".$JAVA_HOME" = . ]; then
        #   use installed platform
        entry=`grep "^[-+] $JAVA_PLATFORM" @l_prefix@/etc/java/toolkits | sed -e '1q'`
        if [ ".$entry" != . ]; then
            JAVA_PLATFORM=`echo ".$entry" | awk '{ print $2; }'`
            JAVA_HOME=`echo ".$entry" | awk '{ print $3; }'`
        fi
    elif [ ".$JAVA_PLATFORM" != . ] && [ ".$JAVA_HOME" != . ]; then
        #   use user supplied platform (but optionally extend with version)
        case "$JAVA_PLATFORM" in
            *-*-[0-9]* )
                ;;
            * )
                version=`($JAVA_HOME/bin/java -version 2>&1; echo "") 2>/dev/null | \
                    grep "^java version" | sed -e '1q' | \
                    sed -e 's;^;X;' -e 's;^X.*"\([0-9][0-9.]*\)".*$;\1;' -e 's;^X.*;0.0.0;'`
                JAVA_PLATFORM="$JAVA_PLATFORM-$version"
                ;;
        esac
    fi
}

#   management functionality
do_manage () {
    if [ $# -eq 0 ]; then
        echo "USAGE: $0 [-e|--env] [-l|--list] [--register=<platform>:<home>] [--unregister=<platform>] [--activate=<platform>] [--deactivate=<platform>]"
        exit 0
    fi
    case "$1" in
        -e | --env )
            determine_variables
            echo "JAVA_PLATFORM=\"$JAVA_PLATFORM\"; export JAVA_PLATFORM"
            echo "JAVA_HOME=\"$JAVA_HOME\"; export JAVA_HOME"
            ;;
        -l | --list )
            sort <@l_prefix@/etc/java/toolkits |\
            awk '{ printf("%s %-15s %s\n", $1, $2, $3); }' 
            ;;
        --register=* )
            eval `echo "$1" | sed -e 's%^--register=\(.*\):\(.*\)$%_platform="\1"; _home="\2"%'`
            ( egrep -v "^[-+] $_platform" @l_prefix@/etc/java/toolkits
              echo "- $_platform $_home" 
            ) >@l_prefix@/etc/java/toolkits.new
            if [ ".`(cat @l_prefix@/etc/java/toolkits.new) 2>/dev/null`" != . ]; then
                cp @l_prefix@/etc/java/toolkits.new @l_prefix@/etc/java/toolkits
                rm -f @l_prefix@/etc/java/toolkits.new
            fi
            ;;
        --unregister=* )
            eval `echo "$1" | sed -e 's%^--unregister=\(.*\)$%_platform="\1"%'`
            ( egrep -v "^[-+] $_platform" @l_prefix@/etc/java/toolkits
            ) >@l_prefix@/etc/java/toolkits.new
            if [ -f @l_prefix@/etc/java/toolkits.new ]; then
                cp @l_prefix@/etc/java/toolkits.new @l_prefix@/etc/java/toolkits
                rm -f @l_prefix@/etc/java/toolkits.new
            fi
            ;;
        --activate=* )
            eval `echo "$1" | sed -e 's%^--activate=\(.*\)$%_platform="\1"%'`
            sed -e "s%^[-+] \(.*\)%- \1%" \
                -e "s%^[-+] \($_platform \)%+ \1%" \
                <@l_prefix@/etc/java/toolkits \
                >@l_prefix@/etc/java/toolkits.new
            if [ ".`(cat @l_prefix@/etc/java/toolkits.new) 2>/dev/null`" != . ]; then
                cp @l_prefix@/etc/java/toolkits.new @l_prefix@/etc/java/toolkits
                rm -f @l_prefix@/etc/java/toolkits.new
            fi
            ;;
        --deactivate=* )
            eval `echo "$1" | sed -e 's%^--deactivate=\(.*\)$%_platform="\1"%'`
            sed -e "s%^[-+] \($_platform \)%- \1%" \
                <@l_prefix@/etc/java/toolkits \
                >@l_prefix@/etc/java/toolkits.new
            if [ ".`(cat @l_prefix@/etc/java/toolkits.new) 2>/dev/null`" != . ]; then
                cp @l_prefix@/etc/java/toolkits.new @l_prefix@/etc/java/toolkits
                rm -f @l_prefix@/etc/java/toolkits.new
            fi
            ;;
    esac
}

#   execution functionality
do_execute () {
    #   allow explicit platform selection on the command-line
    case "$1:$2" in
        -V*:* )
            JAVA_PLATFORM=`echo "X$1" | sed -e 's;^X-V;;'`
            shift
            ;;
        -V:* )
            JAVA_PLATFORM="$2"
            shift; shift
            ;;
        --platform=*:* )
            JAVA_PLATFORM=`echo "X$1" | sed -e 's;^X--platform=;;'`
            shift
            ;;
        --platform:* )
            JAVA_PLATFORM="$2"
            shift; shift
            ;;
    esac

    #   determine requested platform and location
    determine_variables

    #   sanity check platform and location
    if [ ".$JAVA_PLATFORM" = . ]; then
        if [ ".`(cat @l_prefix@/etc/java/toolkits) 2>/dev/null`" = . ]; then
            echo "$0:ERROR: still no backend toolkits registered" 1>&2
        else
            echo "$0:ERROR: unable to determine JAVA_PLATFORM" 1>&2
        fi
        exit 1
    fi
    if [ ".$JAVA_HOME" = . ]; then
        if [ ".`(cat @l_prefix@/etc/java/toolkits) 2>/dev/null`" = . ]; then
            echo "$0:ERROR: still no backend toolkits registered" 1>&2
        else
            echo "$0:ERROR: unable to determine JAVA_HOME" 1>&2
        fi
        exit 1
    fi
    case "$JAVA_PLATFORM" in
        *-*-[0-9]* )
            ;;
        * )
            echo "$0:ERROR: invalid JAVA_PLATFORM: \"$JAVA_PLATFORM\"" 1>&2
            exit 1
            ;;
    esac
    if [ ! -d $JAVA_HOME ]; then
        echo "$0:ERROR: invalid JAVA_HOME: \"$JAVA_HOME\"" 1>&2
        exit 1
    fi

    #   determine Java class path
    if [ ".$CLASSPATH" = . ]; then
        CLASSPATH="@l_prefix@/lib/java"
    else
        CLASSPATH="$CLASSPATH:@l_prefix@/lib/java"
    fi
    for jar in @l_prefix@/lib/*/*.jar @l_prefix@/lib/*.jar; do
        CLASSPATH="$CLASSPATH:$jar"
    done
    export CLASSPATH

    #   pass-through execution to requested tool of particular platform
    if [ -x $JAVA_HOME/bin/$tool ]; then
        exec $JAVA_HOME/bin/$tool ${1+"$@"}
        echo "$0:ERROR: tool \"$JAVA_HOME/bin/$tool\" failed to execute" 1>&2
        exit 1
    else
        echo "$0:ERROR: tool \"$tool\" not provided by platform \"$JAVA_PLATFORM\" under \"$JAVA_HOME\"" 1>&2
        exit 1
    fi
}

#   determine requested tool
tool=`echo "$0" | sed -e 's;^.*/\([^/]*\);\1;'`

#   dispatch according to requested tool
case "$tool" in
    java-toolkit ) do_manage  "$@" ;;
    *            ) do_execute "$@" ;;
esac

