# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
##@require sysinc/base
##@require sysinc/functions

################################################################################
# versions installées sur le système
JAVA_VMS14=(); JAVA_HOMES14=()
JAVA_VMS5=(); JAVA_HOMES5=()
JAVA_VMS6=(); JAVA_HOMES6=()
JAVA_VMS7=(); JAVA_HOMES7=()
# versions locales 32 bits
JAVA32_VMS14=(); JAVA32_HOMES14=()
JAVA32_VMS5=(); JAVA32_HOMES5=()
JAVA32_VMS6=(); JAVA32_HOMES6=()
JAVA32_VMS7=(); JAVA32_HOMES7=()
# versions locales 64 bits
JAVA64_VMS5=(); JAVA64_HOMES5=()
JAVA64_VMS6=(); JAVA64_HOMES6=()
JAVA64_VMS7=(); JAVA64_HOMES7=()
# version sélectionnée
SELECTED_JAVA_VM=; SELECTED_JAVA_HOME=
SELECTED_JAVA=; SELECTED_JAVAC=

function __add() {
    # Ajouter à la liste JAVA$3_$1 la valeur $2
    array_add "JAVA${3}_${1}" "$2"
}

function __compute_java_vms() {
    # calculer la liste de VMs disponibles et initialiser les tableaux
    # JAVA{,32,64}_{VMS,HOMES}{14,5,6,7}
    [ -z "$__COMPUTED_JAVA_VMS" ] || return

    local vms vm v
    if [ "$SYSTEM_NAME" == "Linux" ]; then
        if [ "$LINUX_FLAVOUR" == "gentoo" ]; then
            # sur gentoo
            array_from_lines vms "$(list_dirs /usr/lib/jvm)"
            for vm in "${vms[@]}"; do
                v="${vm#sun-jdk-}"
                if [ "$v" == 1.4 ]; then
                    __add VMS14 "$vm"
                    __add HOMES14 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.5 ]; then
                    __add VMS5 "$vm"
                    __add HOMES5 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.6 ]; then
                    __add VMS6 "$vm"
                    __add HOMES6 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.7 ]; then
                    __add VMS7 "$vm"
                    __add HOMES7 "/usr/lib/jvm/$vm"
                fi
            done
        elif [ "$LINUX_FLAVOUR" == "debian" ]; then
        # sur debian
            array_from_lines vms "$(list_dirs /usr/lib/jvm)"
            for vm in "${vms[@]}"; do
                v="${vm%-sun}"
                v="${v#java-}"
                v="${v#j2sdk}"
                if [ "$v" == 1.4 ]; then
                    __add VMS14 "$vm"
                    __add HOMES14 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.5 -o "$v" == 1.5.0 ]; then
                    __add VMS5 "$vm"
                    __add HOMES5 "/usr/lib/jvm/$vm"
                elif [ "$v" == 6 -o "$v" == 1.6 ]; then
                    __add VMS6 "$vm"
                    __add HOMES6 "/usr/lib/jvm/$vm"
                elif [ "$v" == 7 -o "$v" == 1.7 ]; then
                    __add VMS7 "$vm"
                    __add HOMES7 "/usr/lib/jvm/$vm"
                fi
            done
            if [ -d /usr/lib/j2sdk1.4-sun ]; then
                __add VMS14 j2sdk1.4-sun
                __add HOMES14 /usr/lib/j2sdk1.4-sun
            fi
        fi
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm32")"
        for vm in "${vms[@]}"; do
            v="${vm#sun-jdk-}"
            if [ "$v" == 1.4 ]; then
                __add VMS14 "$vm" 32
                __add HOMES14 "$HOME/opt/jvm32/$vm" 32
            elif [ "$v" == 1.5 ]; then
                __add VMS5 "$vm" 32
                __add HOMES5 "$HOME/opt/jvm32/$vm" 32
            elif [ "$v" == 1.6 ]; then
                __add VMS6 "$vm" 32
                __add HOMES6 "$HOME/opt/jvm32/$vm" 32
            elif [ "$v" == 1.7 ]; then
                __add VMS7 "$vm" 32
                __add HOMES7 "$HOME/opt/jvm32/$vm" 32
            fi
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm64")"
        for vm in "${vms[@]}"; do
            v="${vm#sun-jdk-}"
            if [ "$v" == 1.5 ]; then
                __add VMS5 "$vm" 64
                __add HOMES5 "$HOME/opt/jvm64/$vm" 64
            elif [ "$v" == 1.6 ]; then
                __add VMS6 "$vm" 64
                __add HOMES6 "$HOME/opt/jvm64/$vm" 64
            elif [ "$v" == 1.7 ]; then
                __add VMS7 "$vm" 64
                __add HOMES7 "$HOME/opt/jvm64/$vm" 64
            fi
        done
    elif [ "$SYSTEM_NAME" == "Darwin" ]; then
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.4" ]; then
            __add VMS14 "1.4"
            __add HOMES14 "/System/Library/Frameworks/JavaVM.framework/Versions/1.4/Home"
        fi
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.5" ]; then
            __add VMS5 "1.5"
            __add HOMES5 "/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home"
        fi
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.6" ]; then
            __add VMS6 "1.6"
            __add HOMES6 "/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home"
        fi
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.7" ]; then
            __add VMS7 "1.7"
            __add HOMES7 "/System/Library/Frameworks/JavaVM.framework/Versions/1.7/Home"
        fi
    fi
    __COMPUTED_JAVA_VMS=1
}

function __select_vms() {
    if [ -z "$2" ]; then
        vms="JAVA_VMS$1[@]"; vms=("${!vms}")
        if [ -n "${vms[*]}" ]; then
            homes="JAVA_HOMES$1[@]"; homes=("${!homes}")
            return 0
        else
            return 1
        fi
    else
        vms="JAVA${2}_VMS$1[@]"; vms=("${!vms}")
        homes="JAVA${2}_HOMES$1[@]"; homes=("${!homes}")
        custom=1
        [ -n "${vms[*]}" ] && return 0 || return 1
    fi
}

function __select_java() {
    # Sélectionner la version de java $1 (qui peut être 14, 5, 6 ou 7)
    # Si $2 est défini, il peut s'agit de 32 ou 64 selon que l'on requière la
    # version 32bits ou 64 bits. Sinon, la version sélectionnée peut être 32bits
    # ou 64bits selon ce qui est disponible.
    __compute_java_vms

    local vms homes custom
    if [ -z "$2" ]; then
        __select_vms "$1" ||
        __select_vms "$1" "$SYSTEM_BITS" ||
        __select_vms "$1" 32 ||
        __select_vms "$1" 64
    else
        __select_vms "$1" "$2"
    fi
    if [ -n "${vms[*]}" ]; then
        SELECTED_JAVA_VM="${vms[0]}"
        SELECTED_JAVA_HOME="${homes[0]}"
        SELECTED_JAVA="$SELECTED_JAVA_HOME/bin/java"
        SELECTED_JAVAC="$SELECTED_JAVA_HOME/bin/javac"
        if [ -z "$custom" -a "$LINUX_FLAVOUR" == "gentoo" ]; then
            export GENTOO_VM="$SELECTED_JAVA_VM"
            export JAVA=java
            export JAVAC=javac
            return 0
        else
            [ "$LINUX_FLAVOUR" == "gentoo" ] && unset GENTOO_VM
            export JAVA_HOME="$SELECTED_JAVA_HOME"
            export JAVA="$SELECTED_JAVA"
            export JAVAC="$SELECTED_JAVAC"
            export PATH="$JAVA_HOME/bin:$PATH"
            return 0
        fi
    fi
    return 1
}

function __select_default_java() {
    # Sélectionner la version de java par défaut. Si JAVA_HOME est défini,
    # prendre cette valeur. Sinon, essayer dans l'ordre 5, 6, 7, puis 14
    if [ -n "$JAVA_HOME" ]; then
        export JAVA="$JAVA_HOME/bin/java"
        export JAVAC="$JAVA_HOME/bin/java"
        return 0
    fi
    local vm
    for vm in 5 6 7 14; do
        __select_java "$vm" && return 0
    done
    return 1
}

function select_java() {
    # sélectionner la version *minimum* de java correspondant à $1
    # $1== 1.4|1.4+|1.5|1.5+|1.6|1.6+|1.7|1.7+
    # Si $2 est défini, il peut s'agit de 32 ou 64 selon que l'on requière la
    # version 32bits ou 64 bits
    local v vms homes

    case "$1" in
    1.4|1.4+)
        for v in 14 5 6 7; do
            __select_java "$v" "$2" && return 0
        done
        ;;
    1.5|1.5+)
        for v in 5 6 7; do
            __select_java "$v" "$2" && return 0
        done
        ;;
    1.6|1.6+)
        for v in 6 7; do
            __select_java "$v" "$2" && return 0
        done
        ;;
    1.7|1.7+)
        for v in 7; do
            __select_java "$v" "$2" && return 0
        done
        ;;
    esac
    return 1
}

function select_java_exact() {
    # sélectionner la version *exacte* de javac correspondant à $1
    # $1== 1.4|1.5|1.6|1.7 pour une correspondance exacte
    # $1== 1.4+|1.5+|1.6+|1.7+ pour une version minimum
    # Si $2 est défini, il peut s'agit de 32 ou 64 selon que l'on requière la
    # version 32bits ou 64 bits
    local v vms homes

    case "$1" in
    1.4)
        __select_java 14 && return 0
        ;;
    1.4+)
        for v in 14 5 6 7; do
            __select_java "$v" "$2" && return 0
        done
        ;;
    1.5)
        __select_java 5 "$2" && return 0
        ;;
    1.5+)
        for v in 5 6 7; do
            __select_java "$v" "$2" && return 0
        done
        ;;
    1.6)
        __select_java 6 "$2" && return 0
        ;;
    1.6+)
        for v in 6 7; do
            __select_java "$v" "$2" && return 0
        done
        ;;
    1.7)
        __select_java 7 "$2" && return 0
        ;;
    1.7+)
        for v in 7; do
            __select_java "$v" "$2" && return 0
        done
        ;;
    esac
    return 1
}
