##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## Gestion du service apache et de sa configuration
##@cooked nocomments
##@require base
##@require sysinfos
uprovide apache
urequire base sysinfos

# Si aucun chemin n'est trouvé, retourner la première valeur
APACHE_PREFIXES_CHECK_OR_FIRST=

function __apache_prefixes_checkdir() {
    local dir
    for dir in "$@"; do
        if [ -d "$dir" ]; then
            echo "$dir"
            return 0
        fi
    done
    [ -n "$APACHE_PREFIXES_CHECK_OR_FIRST" -a -n "$1" ] && echo "$1"
    return 1
}

function __apache_prefixes_checkfile() {
    local file
    for file in "$@"; do
        if [ -f "$file" ]; then
            echo "$file"
            return 0
        fi
    done
    [ -n "$APACHE_PREFIXES_CHECK_OR_FIRST" -a -n "$1" ] && echo "$1"
    return 1
}

function __apache_prefixes_checkexec() {
    local exec
    for exec in "$@"; do
        if [ -x "$exec" ]; then
            echo "$exec"
            return 0
        fi
    done
    [ -n "$APACHE_PREFIXES_CHECK_OR_FIRST" -a -n "$1" ] && echo "$1"
    return 1
}

function get_default_apachebin_prefix() {
    __apache_prefixes_checkexec /usr/sbin/{apache2,apache,httpd}
}

function get_default_apacheversion_prefix() {
    [ -n "$APACHEBIN" ] || return
    local version="$($APACHEBIN -v 2>/dev/null | grep version:)"
    if [[ "$version" == *1.3* ]]; then
        echo ""
    elif [[ "$version" == *2.0* ]]; then
        echo "2"
    elif [[ "$version" == *2.2* ]]; then
        echo "2.2"
    elif [[ "$version" == *2.4* ]]; then
        echo "2.4"
    fi
}

function get_default_apachectl_prefix() {
    [ -n "$APACHEBIN" ] || return
    __apache_prefixes_checkexec "$(dirname "$APACHEBIN")"/apache*ctl
}

function get_default_apachelogdir_prefix() {
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir /var/log/{apache2,httpd}
    elif check_sysinfos -s linux; then
        if check_sysinfos -d debianlike; then
            __apache_prefixes_checkdir /var/log/{apache2,apache}
        elif check_sysinfos -d redhatlike; then
            __apache_prefixes_checkdir /var/log/httpd
        elif check_sysinfos -d gentoo; then
            # XXX à vérifier
            __apache_prefixes_checkdir /var/log/{apache{2,},httpd}
        fi
    fi
}

function get_default_apachesslcertsdir_prefix() {
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir /etc/{apache2,httpd}/ssl.crt
    elif check_sysinfos -s linux; then
        __apache_prefixes_checkdir /etc/ssl/certs /etc/pki/tls/certs /etc/httpd/conf/ssl.crt
    fi
}

function get_default_apachesslkeysdir_prefix() {
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir /etc/{apache2,httpd}/ssl.key
    elif check_sysinfos -s linux; then
        __apache_prefixes_checkdir /etc/ssl/private /etc/pki/tls/private /etc/httpd/conf/ssl.key
    fi
}

function get_default_apacheconfdir_prefix() {
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir /etc/{apache2,httpd}
    elif check_sysinfos -s linux; then
        if check_sysinfos -d debianlike; then
            __apache_prefixes_checkdir /etc/{apache{2,,-ssl},httpd}
        elif check_sysinfos -d redhatlike; then
            __apache_prefixes_checkdir /etc/httpd
        elif check_sysinfos -d gentoo; then
            __apache_prefixes_checkdir /etc/{apache{2,},httpd}
        fi
    fi
}

function get_default_apacheconf_prefix() {
    [ -n "$APACHECONFDIR" ] || return
    __apache_prefixes_checkfile "$APACHECONFDIR"/{apache2,{,conf/}httpd}.conf
}

function get_default_apacheavsitesdir_prefix() {
    [ -n "$APACHECONFDIR" ] || return
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir "$APACHECONFDIR/sites_disabled"
    elif check_sysinfos -d debianlike gentoo; then
        __apache_prefixes_checkdir "$APACHECONFDIR/sites-available"
    fi
}

function get_default_apachesitesdir_prefix() {
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir "$APACHECONFDIR/sites"
    elif check_sysinfos -d debianlike gentoo; then
        __apache_prefixes_checkdir "$APACHECONFDIR/sites-enabled"
    fi
}

function get_default_htdocsbase_prefix() {
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir /Library/WebServer/Documents
    elif check_sysinfos -s linux; then
        __apache_prefixes_checkdir /var/www
    fi
}

function get_default_htdocsdir_prefix() {
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir /Library/WebServer/Documents
    elif check_sysinfos -s linux; then
        if check_sysinfos -d debian -v jessie+; then
            __apache_prefixes_checkdir /var/www/html
        elif check_sysinfos -d debianlike; then
            __apache_prefixes_checkdir /var/www
        elif check_sysinfos -d redhatlike; then
            __apache_prefixes_checkdir /var/www/html
        elif check_sysinfos -d gentoo; then
            __apache_prefixes_checkdir /var/www/localhost/htdocs
        fi
    fi
}

function get_default_cgibindir_prefix() {
    if check_sysinfos -s darwin; then
        __apache_prefixes_checkdir /Library/WebServer/CGI-Executables
    elif check_sysinfos -s linux; then
        if check_sysinfos -d debianlike; then
            __apache_prefixes_checkdir /usr/lib/cgi-bin
        elif check_sysinfos -d redhatlike; then
            __apache_prefixes_checkdir /var/www/cgi-bin
        elif check_sysinfos -d gentoo; then
            __apache_prefixes_checkdir /var/www/localhost/cgi-bin
        fi
    fi
}

__apache_prefixes=
function __compute_apache_prefixes() {
    [ -n "$__apache_prefixes" ] && return
    APACHEBIN="${APACHEBIN:-$(get_default_apachebin_prefix)}"
    APACHEVERSION="${APACHEVERSION:-$(get_default_apacheversion_prefix)}"
    APACHECTL="${APACHECTL:-$(get_default_apachectl_prefix)}"
    APACHELOGDIR="${APACHELOGDIR:-$(get_default_apachelogdir_prefix)}"
    APACHESSLCERTSDIR="${APACHESSLCERTSDIR:-$(get_default_apachesslcertsdir_prefix)}"
    APACHESSLKEYSDIR="${APACHESSLKEYSDIR:-$(get_default_apachesslkeysdir_prefix)}"
    APACHECONFDIR="${APACHECONFDIR:-$(get_default_apacheconfdir_prefix)}"
    APACHECONF="${APACHECONF:-$(get_default_apacheconf_prefix)}"
    APACHEAVSITESDIR="${APACHEAVSITESDIR:-$(get_default_apacheavsitesdir_prefix)}"
    APACHESITESDIR="${APACHESITESDIR:-$(get_default_apachesitesdir_prefix)}"
    HTDOCSBASE="${HTDOCSBASE:-$(get_default_htdocsbase_prefix)}"
    HTDOCSDIR="${HTDOCSDIR:-$(get_default_htdocsdir_prefix)}"
    CGIBINDIR="${CGIBINDIR:-$(get_default_cgibindir_prefix)}"
    __apache_prefixes=1
}

APACHE_PREFIXES=(APACHEBIN APACHEVERSION APACHECTL APACHELOGDIR APACHESSLCERTSDIR APACHESSLKEYSDIR APACHECONFDIR APACHECONF APACHEAVSITESDIR APACHESITESDIR HTDOCSBASE HTDOCSDIR CGIBINDIR)
UTOOLS_PREFIXES=("${UTOOLS_PREFIXES[@]}" "${APACHE_PREFIXES[@]}")

function compute_apache_prefixes() {
    __compute_apache_prefixes
}

function recompute_apache_prefixes() {
    local v
    for v in "${APACHE_PREFIXES[@]}"; do
        eval "$v="
    done
    __apache_prefixes=
    __compute_apache_prefixes
}

function get_APACHEBIN_prefix() {
    __compute_apache_prefixes
    echo "$APACHEBIN"
}

function get_APACHEVERSION_prefix() {
    __compute_apache_prefixes
    echo "$APACHEVERSION"
}

function get_APACHECTL_prefix() {
    __compute_apache_prefixes
    echo "$APACHECTL"
}

function get_APACHELOGDIR_prefix() {
    __compute_apache_prefixes
    echo "$APACHELOGDIR"
}

function get_APACHESSLCERTSDIR_prefix() {
    __compute_apache_prefixes
    echo "$APACHESSLCERTSDIR"
}

function get_APACHESSLKEYSDIR_prefix() {
    __compute_apache_prefixes
    echo "$APACHESSLKEYSDIR"
}

function get_APACHECONFDIR_prefix() {
    __compute_apache_prefixes
    echo "$APACHECONFDIR"
}

function get_APACHECONF_prefix() {
    __compute_apache_prefixes
    echo "$APACHECONF"
}

function get_APACHEAVSITESDIR_prefix() {
    __compute_apache_prefixes
    echo "$APACHEAVSITESDIR"
}

function get_APACHESITESDIR_prefix() {
    __compute_apache_prefixes
    echo "$APACHESITESDIR"
}

function get_HTDOCSBASE_prefix() {
    __compute_apache_prefixes
    echo "$HTDOCSBASE"
}

function get_HTDOCSDIR_prefix() {
    __compute_apache_prefixes
    echo "$HTDOCSDIR"
}

function get_CGIBINDIR_prefix() {
    __compute_apache_prefixes
    echo "$CGIBINDIR"
}