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

if [ "$SYSTEM_NAME" == "Darwin" ]; then
    # valeurs par défaut de NEXT_ROOT, WOROOT et LOCALROOT sur MacOS X
    DEFAULT_NEXT_ROOT=
    function get_default_woroot() { echo $NEXT_ROOT/System; }
    function get_default_localroot() { echo $NEXT_ROOT; }
else
    # valeurs par défaut de NEXT_ROOT, WOROOT et LOCALROOT sur les autres Unix
    DEFAULT_NEXT_ROOT=/opt/Apple
    function get_default_woroot() { echo $NEXT_ROOT; }
    function get_default_localroot() { echo $NEXT_ROOT/Local; }
fi

function get_default_javahome() {
    local GENTOO_VM JAVA_HOME JAVA JAVAC PATH="$PATH"
    __select_default_java
    local javahome
    javahome="$JAVA_HOME"

    [ -n "$javahome" ] && echo "$javahome"
}

function get_javaextdir() {
    local javaextdir
    if [ "$SYSTEM_NAME" == "Linux" ]; then
        if [ -n "$JAVA_HOME" ]; then
            javaextdir="$JAVA_HOME/jre/lib/ext"
            [ -d "$javaextdir" ] || javaextdir="$JAVA_HOME/lib/ext"
        fi
    elif [ "$SYSTEM_NAME" == "Darwin" ]; then
        javaextdir=/Library/Java/Extensions
    fi

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

function get_apachebin() {
    local i
    case "$SYSTEM_NAME" in
    Darwin|Linux)
        for i in /usr/sbin/{apache2,apache,httpd}; do
            if [ -x "$i" ]; then
                echo "$i"
                break
            fi
        done
        ;;
    SunOS)
        for i in /usr{/local,}/apache/bin/{apache2,apache,httpd}; do
            if [ -x "$i" ]; then
                echo "$i"
                break
            fi
        done
        ;;
    esac
}

function get_apachectl() {
    local i apachebin="${1:-$(get_apachebin)}"
    for i in "$(dirname "$apachebin")"/apache*ctl; do
        if [ -x "$i" ]; then
            echo "$i"
            break
        fi
    done
}

function get_apacheversion() {
    local apachebin="${1:-$(get_apachebin)}"
    if [ -n "$apachebin" ]; then
        local version="$($apachebin -v | grep version:)"
        if [[ "$version" == *1.3* ]]; then
            echo ""
        elif [[ "$version" == *2.0* ]]; then
            echo "2"
        elif [[ "$version" == *2.2* ]]; then
            echo "2.2"
        fi
    fi
}

function get_default_apacheconfdir() {
    # Calculer et afficher la valeur par défaut de APACHECONF_DIR, ou une chaine
    # vide si l'on n'a pas pu le détecter automatiquement.
    local i
    case "$SYSTEM_NAME" in
    Darwin)
        echo /etc/httpd
        ;;
    Linux)
        case "$LINUX_FLAVOUR" in
        debian)
            for i in /etc/{apache{2,,-ssl},httpd}; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        gentoo)
            for i in /etc/{apache{2,},httpd}; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        *)
            ;;
        esac
        ;;
    SunOS)
        for i in /usr{/local,}/apache/conf; do
            if [ -d "$i" ]; then
                echo "$i"
                break
            fi
        done
        ;;
    esac
}

function get_apacheconf() {
    local i apacheconfdir="${1:-$(get_default_apacheconfdir)}"
    for i in "$apacheconfdir"/{apache2,httpd}.conf; do
        if [ -f "$i" ]; then
            echo "$i"
            break
        fi
    done
}

function get_default_htdocsdir() {
    # Calculer et afficher la valeur par défaut de HTDOCS_DIR, ou une chaine
    # vide si l'on n'a pas pu le détecter automatiquement.
    local i
    case "$SYSTEM_NAME" in
    Darwin)
        echo "/Library/WebServer/Documents"
        ;;
    Linux)
        case "$LINUX_FLAVOUR" in
        debian)
            for i in /var/www; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        gentoo)
            for i in /var/www/localhost/htdocs; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        redhat)
            for i in /var/www/html; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        *)
            ;;
        esac
        ;;
    SunOS)
        for i in /usr{/local,}/apache/htdocs; do
            if [ -d "$i" ]; then
                echo "$i"
                break
            fi
        done
        ;;
    esac
}

function get_default_cgibindir() {
    # Calculer et afficher la valeur par défaut de CGIBIN_DIR, ou une chaine
    # vide si l'on n'a pas pu le détecter automatiquement.
    local i
    case "$SYSTEM_NAME" in
    Darwin)
        echo "/Library/WebServer/CGI-Executables"
        ;;
    Linux)
        case "$LINUX_FLAVOUR" in
        debian)
            for i in /usr/lib/cgi-bin; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        gentoo)
            for i in /var/www/localhost/cgi-bin; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        *)
            ;;
        esac
        ;;
    SunOS)
        for i in /usr{/local,}/apache/cgi-bin; do
            if [ -d "$i" ]; then
                echo "$i"
                break
            fi
        done
        ;;
    esac
}

function get_default_ldapconfdir() {
    # Calculer et afficher la valeur par défaut de LDAPCONF_DIR, ou une chaine
    # vide si l'on n'a pas pu le détecter automatiquement.
    local i
    case "$SYSTEM_NAME" in
    Darwin)
        ;;
    Linux)
        case "$LINUX_FLAVOUR" in
        debian)
            for i in /etc/ldap; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        gentoo)
            for i in /etc/openldap; do
                if [ -d "$i" ]; then
                    echo "$i"
                    break
                fi
            done
            ;;
        *)
            ;;
        esac
        ;;
    SunOS)
        ;;
    esac
}

function get_default_ldapowner() {
    # Calculer et afficher la valeur par défaut de LDAPOWNER, ou une chaine
    # vide si l'on n'a pas pu le détecter automatiquement.
    local i
    case "$SYSTEM_NAME" in
    Darwin)
        ;;
    Linux)
        case "$LINUX_FLAVOUR" in
        debian)
            echo "openldap:openldap"
            ;;
        gentoo)
            echo "ldap:ldap"
            ;;
        *)
            ;;
        esac
        ;;
    SunOS)
        ;;
    esac
}

function compute_prefixes() {
    NEXT_ROOT="${NEXT_ROOT:-$DEFAULT_NEXT_ROOT}"
    WOROOT="${WOROOT:-$(get_default_woroot)}"
    LOCALROOT="${LOCALROOT:-$(get_default_localroot)}"
    SYSTEMFRAMEWORKS="$WOROOT/Library/Frameworks"
    WOEXTENSIONS="$LOCALROOT/Library/WebObjects/Extensions"
    WOFRAMEWORKS="$LOCALROOT/Library/WebObjects/Applications/Frameworks"
    WOAPPLICATIONS="$LOCALROOT/Library/WebObjects/Applications"
    WOCONFIGURATION="$LOCALROOT/Library/WebObjects/Configuration"

    JAVA_HOME="${JAVA_HOME:-$(get_default_javahome)}"
    JAVAEXTENSIONS="$(get_javaextdir)"

    APACHEBIN="${APACHEBIN:-$(get_apachebin)}"
    APACHEVERSION="${APACHEVERSION:-$(get_apacheversion "$APACHEBIN")}"
    APACHECTL="${APACHECTL:-$(get_apachectl "$APACHEBIN")}"
    APACHECONF_DIR="${APACHECONF_DIR:-$(get_default_apacheconfdir)}"
    APACHECONF="${APACHECONF:-$(get_apacheconf "$APACHECONF_DIR")}"
    HTDOCS_DIR="${HTDOCS_DIR:-$(get_default_htdocsdir)}"
    CGIBIN_DIR="${CGIBIN_DIR:-$(get_default_cgibindir)}"

    LDAPCONF_DIR="${LDAPCONF_DIR:-$(get_default_ldapconfdir)}"
    LDAPOWNER="${LDAPOWNER:-$(get_default_ldapowner)}"
}

PREFIXES=(NEXT_ROOT WOROOT LOCALROOT SYSTEMFRAMEWORKS WOEXTENSIONS WOFRAMEWORKS WOAPPLICATIONS WOCONFIGURATION
JAVA_HOME JAVAEXTENSIONS
APACHEBIN APACHEVERSION APACHECTL APACHECONF_DIR APACHECONF HTDOCS_DIR CGIBIN_DIR
LDAPCONF_DIR LDAPOWNER)
function NEXT_ROOT_value() {
    [ -z "$NEXT_ROOT" ] && compute_prefixes
    echo "$NEXT_ROOT"
}
function WOROOT_value() {
    [ -z "$WOROOT" ] && compute_prefixes
    echo "$WOROOT"
}
function LOCALROOT_value() {
    [ -z "$LOCALROOT" ] && compute_prefixes
    echo "$LOCALROOT"
}
function SYSTEMFRAMEWORKS_value() {
    [ -z "$SYSTEMFRAMEWORKS" ] && compute_prefixes
    echo "$SYSTEMFRAMEWORKS"
}
function WOEXTENSIONS_value() {
    [ -z "$WOEXTENSIONS" ] && compute_prefixes
    echo "$WOEXTENSIONS"
}
function WOFRAMEWORKS_value() {
    [ -z "$WOFRAMEWORKS" ] && compute_prefixes
    echo "$WOFRAMEWORKS"
}
function WOAPPLICATIONS_value() {
    [ -z "$WOAPPLICATIONS" ] && compute_prefixes
    echo "$WOAPPLICATIONS"
}
function WOCONFIGURATION_value() {
    [ -z "$WOCONFIGURATION" ] && compute_prefixes
    echo "$WOCONFIGURATION"
}
function JAVA_HOME_value() {
    [ -z "$JAVA_HOME" ] && compute_prefixes
    echo "$JAVA_HOME"
}
function JAVAEXTENSIONS_value() {
    [ -z "$JAVAEXTENSIONS" ] && compute_prefixes
    echo "$JAVAEXTENSIONS"
}
function APACHEBIN_value() {
    [ -z "$APACHEBIN" ] && compute_prefixes
    echo "$APACHEBIN"
}
function APACHEVERSION_value() {
    [ -z "$APACHEVERSION" ] && compute_prefixes
    echo "$APACHEVERSION"
}
function APACHECTL_value() {
    [ -z "$APACHECTL" ] && compute_prefixes
    echo "$APACHECTL"
}
function APACHECONF_DIR_value() {
    [ -z "$APACHECONF_DIR" ] && compute_prefixes
    echo "$APACHECONF_DIR"
}
function APACHECONF_value() {
    [ -z "$APACHECONF" ] && compute_prefixes
    echo "$APACHECONF"
}
function HTDOCS_DIR_value() {
    [ -z "$HTDOCS_DIR" ] && compute_prefixes
    echo "$HTDOCS_DIR"
}
function CGIBIN_DIR_value() {
    [ -z "$CGIBIN_DIR" ] && compute_prefixes
    echo "$CGIBIN_DIR"
}
function LDAPCONF_DIR_value() {
    [ -z "$LDAPCONF_DIR" ] && compute_prefixes
    echo "$LDAPCONF_DIR"
}
function LDAPOWNER_value() {
    [ -z "$LDAPOWNER" ] && compute_prefixes
    echo "$LDAPOWNER"
}

function list_prefixes() {
    local prefix
    for prefix in "${PREFIXES[@]}"; do
        echo "$prefix"
    done
}

function expand_prefix() {
    local prefix
    for prefix in "${PREFIXES[@]}"; do
        if beginswith "$1" "$prefix/" || [ "$1" == "$prefix" ]; then
            echo "$(${prefix}_value)${1#$prefix}"
            return
        fi
    done
    echo "$1"
}

function dump_prefixes() {
    local prefix
    for prefix in "${PREFIXES[@]}"; do
        echo "$prefix=$(expand_prefix "$prefix")"
    done
}
