##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## Gestion de la sélection d'une version de Java
##@cooked nocomments
##@require base
##@require sysinfos
uprovide java
urequire base sysinfos

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

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

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

    local vms vm v
    if check_sysinfos -s linux; then
        # D'abord chercher dans les JVMs installées manuellement dans
        # ~/opt/jvm{32,64}
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm32" "sun-jdk-*")"
        for vm in "${vms[@]}"; do
            v="${vm#sun-jdk-}"
            if [ "$v" == 1.4 ]; then
                __java_add VMS14 "$vm" 32
                __java_add HOMES14 "$HOME/opt/jvm32/$vm" 32
            elif [ "$v" == 1.5 ]; then
                __java_add VMS5 "$vm" 32
                __java_add HOMES5 "$HOME/opt/jvm32/$vm" 32
            elif [ "$v" == 1.6 ]; then
                __java_add VMS6 "$vm" 32
                __java_add HOMES6 "$HOME/opt/jvm32/$vm" 32
            elif [ "$v" == 1.7 ]; then
                __java_add VMS7 "$vm" 32
                __java_add HOMES7 "$HOME/opt/jvm32/$vm" 32
            elif [ "$v" == 1.8 ]; then
                __java_add VMS8 "$vm" 32
                __java_add HOMES8 "$HOME/opt/jvm32/$vm" 32
            fi
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm32" "j2sdk1.4.2*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS14 "$vm" 32
            __java_add HOMES14 "$HOME/opt/jvm32/$vm" 32
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm32" "jdk1.5.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS5 "$vm" 32
            __java_add HOMES5 "$HOME/opt/jvm32/$vm" 32
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm32" "jdk1.6.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS6 "$vm" 32
            __java_add HOMES6 "$HOME/opt/jvm32/$vm" 32
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm32" "jdk1.7.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS7 "$vm" 32
            __java_add HOMES7 "$HOME/opt/jvm32/$vm" 32
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm32" "jdk1.8.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS8 "$vm" 32
            __java_add HOMES8 "$HOME/opt/jvm32/$vm" 32
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm64" "sun-jdk-*")"
        for vm in "${vms[@]}"; do
            v="${vm#sun-jdk-}"
            if [ "$v" == 1.5 ]; then
                __java_add VMS5 "$vm" 64
                __java_add HOMES5 "$HOME/opt/jvm64/$vm" 64
            elif [ "$v" == 1.6 ]; then
                __java_add VMS6 "$vm" 64
                __java_add HOMES6 "$HOME/opt/jvm64/$vm" 64
            elif [ "$v" == 1.7 ]; then
                __java_add VMS7 "$vm" 64
                __java_add HOMES7 "$HOME/opt/jvm64/$vm" 64
            elif [ "$v" == 1.8 ]; then
                __java_add VMS8 "$vm" 64
                __java_add HOMES8 "$HOME/opt/jvm64/$vm" 64
            fi
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm64" "jdk1.5.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS5 "$vm" 64
            __java_add HOMES5 "$HOME/opt/jvm64/$vm" 64
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm64" "jdk1.6.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS6 "$vm" 64
            __java_add HOMES6 "$HOME/opt/jvm64/$vm" 64
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm64" "jdk1.7.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS7 "$vm" 64
            __java_add HOMES7 "$HOME/opt/jvm64/$vm" 64
        done
        array_from_lines vms "$(list_dirs "$HOME/opt/jvm64" "jdk1.8.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS8 "$vm" 64
            __java_add HOMES8 "$HOME/opt/jvm64/$vm" 64
        done
        # Puis chercher dans les JVMs installées manuellement dans ~/opt
        array_from_lines vms "$(list_dirs "$HOME/opt" "sun-jdk-*")"
        for vm in "${vms[@]}"; do
            v="${vm#sun-jdk-}"
            if [ "$v" == 1.4 ]; then
                __java_add VMS14 "$vm"
                __java_add HOMES14 "$HOME/opt/$vm"
            elif [ "$v" == 1.5 ]; then
                __java_add VMS5 "$vm"
                __java_add HOMES5 "$HOME/opt/$vm"
            elif [ "$v" == 1.6 ]; then
                __java_add VMS6 "$vm"
                __java_add HOMES6 "$HOME/opt/$vm"
            elif [ "$v" == 1.7 ]; then
                __java_add VMS7 "$vm"
                __java_add HOMES7 "$HOME/opt/$vm"
            elif [ "$v" == 1.8 ]; then
                __java_add VMS8 "$vm"
                __java_add HOMES8 "$HOME/opt/$vm"
            fi
        done
        array_from_lines vms "$(list_dirs "$HOME/opt" "j2sdk1.4.2*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS14 "$vm"
            __java_add HOMES14 "$HOME/opt/$vm"
        done
        array_from_lines vms "$(list_dirs "$HOME/opt" "jdk1.5.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS5 "$vm"
            __java_add HOMES5 "$HOME/opt/$vm"
        done
        array_from_lines vms "$(list_dirs "$HOME/opt" "jdk1.6.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS6 "$vm"
            __java_add HOMES6 "$HOME/opt/$vm"
        done
        array_from_lines vms "$(list_dirs "$HOME/opt" "jdk1.7.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS7 "$vm"
            __java_add HOMES7 "$HOME/opt/$vm"
        done
        array_from_lines vms "$(list_dirs "$HOME/opt" "jdk1.8.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS8 "$vm"
            __java_add HOMES8 "$HOME/opt/$vm"
        done
        # Puis chercher dans les JVMs installées manuellement dans /opt
        array_from_lines vms "$(list_dirs "/opt" "sun-jdk-*")"
        for vm in "${vms[@]}"; do
            v="${vm#sun-jdk-}"
            if [ "$v" == 1.4 ]; then
                __java_add VMS14 "$vm"
                __java_add HOMES14 "/opt/$vm"
            elif [ "$v" == 1.5 ]; then
                __java_add VMS5 "$vm"
                __java_add HOMES5 "/opt/$vm"
            elif [ "$v" == 1.6 ]; then
                __java_add VMS6 "$vm"
                __java_add HOMES6 "/opt/$vm"
            elif [ "$v" == 1.7 ]; then
                __java_add VMS7 "$vm"
                __java_add HOMES7 "/opt/$vm"
            elif [ "$v" == 1.8 ]; then
                __java_add VMS8 "$vm"
                __java_add HOMES8 "/opt/$vm"
            fi
        done
        array_from_lines vms "$(list_dirs "/opt" "j2sdk1.4.2*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS14 "$vm"
            __java_add HOMES14 "/opt/$vm"
        done
        array_from_lines vms "$(list_dirs "/opt" "jdk1.5.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS5 "$vm"
            __java_add HOMES5 "/opt/$vm"
        done
        array_from_lines vms "$(list_dirs "/opt" "jdk1.6.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS6 "$vm"
            __java_add HOMES6 "/opt/$vm"
        done
        array_from_lines vms "$(list_dirs "/opt" "jdk1.7.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS7 "$vm"
            __java_add HOMES7 "/opt/$vm"
        done
        array_from_lines vms "$(list_dirs "/opt" "jdk1.8.*" | LANG=C sort -r)"
        for vm in "${vms[@]}"; do
            __java_add VMS8 "$vm"
            __java_add HOMES8 "/opt/$vm"
        done
        # Puis chercher dans les JVMs installées par le système dans
        # /usr/lib/jvm
        if check_sysinfos -d 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
                    __java_add VMS14 "$vm"
                    __java_add HOMES14 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.5 ]; then
                    __java_add VMS5 "$vm"
                    __java_add HOMES5 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.6 ]; then
                    __java_add VMS6 "$vm"
                    __java_add HOMES6 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.7 ]; then
                    __java_add VMS7 "$vm"
                    __java_add HOMES7 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.8 ]; then
                    __java_add VMS8 "$vm"
                    __java_add HOMES8 "/usr/lib/jvm/$vm"
                fi
            done
        elif check_sysinfos -d debianlike; 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
                    __java_add VMS14 "$vm"
                    __java_add HOMES14 "/usr/lib/jvm/$vm"
                elif [ "$v" == 1.5 -o "$v" == 1.5.0 ]; then
                    __java_add VMS5 "$vm"
                    __java_add HOMES5 "/usr/lib/jvm/$vm"
                elif [ "$v" == 6 -o "$v" == 1.6 ]; then
                    __java_add VMS6 "$vm"
                    __java_add HOMES6 "/usr/lib/jvm/$vm"
                elif [ "$v" == 7 -o "$v" == 1.7 ]; then
                    __java_add VMS7 "$vm"
                    __java_add HOMES7 "/usr/lib/jvm/$vm"
                elif [ "$v" == 8 -o "$v" == 1.8 ]; then
                    __java_add VMS8 "$vm"
                    __java_add HOMES8 "/usr/lib/jvm/$vm"
                fi
            done
            if [ -d /usr/lib/j2sdk1.4-sun ]; then
                __java_add VMS14 j2sdk1.4-sun
                __java_add HOMES14 /usr/lib/j2sdk1.4-sun
            fi
        elif check_sysinfos -d redhatlike; then
            # XXX sur redhat
            :
        fi
    elif check_sysinfos -s macosx; then
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.3" ]; then
            __java_add VMS13 "1.3"
            __java_add HOMES13 "/System/Library/Frameworks/JavaVM.framework/Versions/1.3/Home"
        fi
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.4" ]; then
            __java_add VMS14 "1.4"
            __java_add HOMES14 "/System/Library/Frameworks/JavaVM.framework/Versions/1.4/Home"
        fi
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.5" ]; then
            __java_add VMS5 "1.5"
            __java_add HOMES5 "/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home"
        fi
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.6" ]; then
            __java_add VMS6 "1.6"
            __java_add HOMES6 "/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home"
        fi
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.7" ]; then
            __java_add VMS7 "1.7"
            __java_add HOMES7 "/System/Library/Frameworks/JavaVM.framework/Versions/1.7/Home"
        fi
        if [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/1.8" ]; then
            __java_add VMS8 "1.8"
            __java_add HOMES8 "/System/Library/Frameworks/JavaVM.framework/Versions/1.8/Home"
        fi
    fi
    __COMPUTED_JAVA_VMS=1
}

function __java_dump_vms() {
    local v b i vms homes
    for v in 13 14 5 6 7 8; do
        for b in "" 32 64; do
            vms="JAVA${b}_VMS${v}[@]"; vms=("${!vms}")
            homes="JAVA${b}_HOMES${v}[@]"; homes=("${!homes}")
            i=0
            while [ $i -lt "${#vms[*]}" ]; do
                echo "$v${b:+-$b}:${vms[$i]}:${homes[$i]}"
                i=$(($i + 1))
            done
        done
    done
}

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

function __java_select() {
    # Sélectionner la version de java $1 (qui peut être 13, 14, 5, 6, 7 ou 8)
    # 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.
    __java_compute_vms

    local vms homes custom
    if [ -z "$2" ]; then
        __java_select_vms "$1" "$MYBITS" ||
        __java_select_vms "$1" ||
        __java_select_vms "$1" 32 ||
        __java_select_vms "$1" 64
    elif [ "$MYBITS" == "$2" ]; then
        __java_select_vms "$1" "$2" ||
        __java_select_vms "$1"
    else
        __java_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"
            udelpath "$JAVA_HOME/bin"
            uinspath "$JAVA_HOME/bin"
            export PATH
            return 0
        fi
    fi
    return 1
}

function __java_select_default() {
    # 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, 8, puis 14
    # la version 13 n'est jamais sélectionnée automatiquement
    if [ -n "$JAVA_HOME" ]; then
        export JAVA_HOME # s'assurer que cette variable est exportée
        export JAVA="$JAVA_HOME/bin/java"
        export JAVAC="$JAVA_HOME/bin/java"
        udelpath "$JAVA_HOME/bin"
        uinspath "$JAVA_HOME/bin"
        export PATH
        return 0
    fi
    local vm
    for vm in 5 6 7 8 14; do
        __java_select "$vm" && return 0
    done
    return 1
}

function select_java() {
    # sélectionner la version *minimum* de java correspondant à $1
    # $1== 1.3|1.3+|1.4|1.4+|1.5|1.5+|1.6|1.6+|1.7|1.7+|1.8|1.8+
    # 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.3|1.3+|13|13+)
        for v in 13 14 5 6 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.4|1.4+|14|14+)
        for v in 14 5 6 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.5|1.5+|5|5+)
        for v in 5 6 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.6|1.6+|6|6+)
        for v in 6 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.7|1.7+|7|7+)
        for v in 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.8|1.8+|8|8+)
        for v in 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    esac
    return 1
}

function select_java_exact() {
    # sélectionner la version *exacte* de java correspondant à $1
    # $1== 1.3|1.4|1.5|1.6|1.7|1.8 pour une correspondance exacte
    # $1== 1.3+|1.4+|1.5+|1.6+|1.7+|1.8+ 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.3|13)
        __java_select 13 && return 0
        ;;
    1.3+|13+)
        for v in 13 14 5 6 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.4|14)
        __java_select 14 && return 0
        ;;
    1.4+|14+)
        for v in 14 5 6 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.5|5)
        __java_select 5 "$2" && return 0
        ;;
    1.5+|5+)
        for v in 5 6 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.6|6)
        __java_select 6 "$2" && return 0
        ;;
    1.6+|6+)
        for v in 6 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.7|7)
        __java_select 7 "$2" && return 0
        ;;
    1.7+|7+)
        for v in 7 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    1.8|8)
        __java_select 8 "$2" && return 0
        ;;
    1.8+|8+)
        for v in 8; do
            __java_select "$v" "$2" && return 0
        done
        ;;
    esac
    return 1
}

function select_java_any() {
    # Sélectionner la version exacte de java correspondant aux arguments, dans
    # l'ordre, jusqu'à ce qu'un argument corresponde. DEFAULT correspond à la
    # valeur actuelle de JAVA_HOME, si elle est définie.
    # Si aucun argument n'est défini, on assume "DEFAULT 5 6 7 8 1.4"
    local v
    [ -n "$*" ] || set -- DEFAULT 5 6 7 8 1.4
    for v in "$@"; do
        if [ "$v" == DEFAULT ]; then
            if [ -n "$JAVA_HOME" ]; then
                __java_select_default && return 0
            fi
        else
            select_java_exact "$v" && return 0
        fi
    done
    return 1
}

################################################################################
# Gestion des préfixes

function get_default_javahome_prefix() {
    local GENTOO_VM JAVA_HOME JAVA JAVAC PATH="$PATH"
    __java_select_default
    [ -n "$JAVA_HOME" ] && echo "$JAVA_HOME"
}

function get_javaextensions_prefix() {
    local javaextensions
    if check_sysinfos -s linux; then
        if [ -n "$JAVA_HOME" ]; then
            javaextensions="$JAVA_HOME/jre/lib/ext"
            [ -d "$javaextensions" ] || javaextensions="$JAVA_HOME/lib/ext"
        fi
    elif check_sysinfos -s darwin; then
        javaextensions=/Library/Java/Extensions
    fi

    [ -n "$javaextensions" -a -d "$javaextensions" ] && echo "$javaextensions"
}

__java_prefixes=
function __compute_java_prefixes() {
    [ -n "$__java_prefixes" ] && return
    [ -n "$JAVA_HOME" ] || JAVA_HOME="$(get_default_javahome_prefix)"
    JAVAEXTENSIONS="${JAVAEXTENSIONS:-$(get_javaextensions_prefix)}"
    __java_prefixes=1
}

UTOOLS_PREFIXES=("${UTOOLS_PREFIXES[@]}" JAVA_HOME JAVAEXTENSIONS)

function compute_java_prefixes() {
    __compute_java_prefixes
}

function recompute_java_prefixes() {
    __java_prefixes=
    __compute_java_prefixes
}

function get_JAVA_HOME_prefix() {
    __compute_java_prefixes
    echo "$JAVA_HOME"
}

function get_JAVAEXTENSIONS_prefix() {
    __compute_java_prefixes
    echo "$JAVAEXTENSIONS"
}