#!/bin/bash

container_path_proxy=$(CreoleGet container_path_proxy)
mode_conteneur_actif=$(CreoleGet mode_conteneur_actif)
activer_filtrage_proxy=$(CreoleGet activer_filtrage_proxy)
activer_cache_pere=$(CreoleGet activer_cache_pere)
SQUID_PATH="$container_path_proxy/etc/squid"

## SQUID ##
touch "$SQUID_PATH/domaines_noauth_user"
touch "$SQUID_PATH/domaines_nocache_user"
touch "$SQUID_PATH/src_noauth_user"
touch "$SQUID_PATH/src_nocache_user"
touch "$SQUID_PATH/domaines_noauth_acad"
touch "$SQUID_PATH/domaines_nocache_acad"
touch "$SQUID_PATH/src_noauth_acad"
touch "$SQUID_PATH/src_nocache_acad"
touch "$SQUID_PATH/users"
touch "$SQUID_PATH/domaines_nopeerproxy_regex"
if [ ! -f "$SQUID_PATH/src_noauth" ];then
    echo "127.0.0.1" > "$SQUID_PATH/src_noauth"
fi
if [ ! -f "$SQUID_PATH/src_nocache" ];then
    echo "127.0.0.1" > "$SQUID_PATH/src_nocache"
fi
if [ "$activer_cache_pere" == "oui" ];then
    if [ ! -f "$SQUID_PATH/domaines_nopeerproxy" ];then
        touch "$SQUID_PATH/domaines_nopeerproxy"
    fi
fi

# correction des droits sur winbindd_privileged (#13740)
mkdir -p "$container_path_proxy/var/lib/samba/winbindd_privileged"
chmod 0750 "$container_path_proxy/var/lib/samba/winbindd_privileged"
CreoleRun "chgrp proxy /var/lib/samba/winbindd_privileged" proxy

LOG_SQUID=/var/log/squid

if [ "$mode_conteneur_actif" = "oui" ]; then
    #remonter des logs en mode conteneur
    if [ ! -L $LOG_SQUID ]; then
        rm -rf $LOG_SQUID
        ln -s "$container_path_proxy$LOG_SQUID" "$LOG_SQUID"
    fi
fi



## GUARDIAN ##
if [ "$activer_filtrage_proxy" = 'oui' ]; then
    if [ "$(CreoleGet dans_instance_1_active)" = "oui" ]; then
        LOG_E2GUARDIANS="/var/log/e2guardian/dstats0.log"
    fi
    if [ "$(CreoleGet dans_instance_2_active)" = "oui" ]; then
        LOG_E2GUARDIANS="$LOG_E2GUARDIANS /var/log/e2guardian/dstats1.log"
    fi
    if [ "$(CreoleGet dans_instance_3_active)" = "oui" ]; then
        LOG_E2GUARDIANS="$LOG_E2GUARDIANS /var/log/e2guardian/dstats2.log"
    fi
    for log in $LOG_E2GUARDIANS; do
        touch "$container_path_proxy$log"
        CreoleRun "chown proxy: $log" proxy
        if [ "$mode_conteneur_actif" = "oui" ]; then
            #remonter des logs en mode conteneur
            mkdir -p "$(dirname "$log")"
            if [ ! -L "$log" ]; then
                rm -rf "$log"
                ln -s "$container_path_proxy$log" "$log"
            fi
        fi
    done
    set -e
    # /!\ ne pas supprimer /etc/guardian/languages /!\
    rm -rf "$container_path_proxy/etc/guardian/guardian"*
    rm -rf "$container_path_proxy/etc/guardian/authplugins"
    rm -rf "$container_path_proxy/etc/guardian/contentscanners"
    rm -rf "$container_path_proxy/etc/guardian/downloadmanagers"
    rm -rf "$container_path_proxy/etc/guardian/lists"
fi

## LIGHTSQUID ##
# mise à disposition de lightsquid
if ! [ -d /var/www/html ];then
    mkdir -p /var/www/html
fi
ln -nsf /usr/share/lightsquid /var/www/html/

# déplacement des statistiques dans /var/log
OLD_REPORTS="/var/www/html/lightsquid/report"
NEW_REPORTS="/var/log/lightsquid/reports"
[ ! -d "$NEW_REPORTS" ] && mkdir -p "$NEW_REPORTS"
if [ -d "${OLD_REPORTS}" ]
then
    rsync -r ${OLD_REPORTS}/ ${NEW_REPORTS}
    rm -rf "$OLD_REPORTS"
fi
