#!/bin/bash

container_path_proxy=$(CreoleGet container_path_proxy)
activer_filtrage_proxy=$(CreoleGet activer_filtrage_proxy)
proxyuser="proxy"
proxygroup="proxy"
conf_root="/etc/guardian/"

rm -f /etc/logrotate.d/e2guardian

## DANSGUARDIAN ##
if [ $activer_filtrage_proxy = 'oui' ];then
    set -e

    # Create empty not existing authexception list files included by e2guardian
    for noauth_list in domaines_noauth_user domaines_noauth_acad
    do
        noauth_file="${container_path_proxy}/etc/guardian/common/${noauth_list}"
        if [ ! -f "${noauth_file}" ]
        then
            touch "$noauth_file"
        fi
    done
    danspath="/usr/share/eole/dansguardian"
    # mise à niveau de la configuration
    for zone in 0 1 2
    do
        if [ "$(CreoleGet dans_instance_$((zone+1))_active)" = "oui" ]; then
            rm -f /etc/amon_ifaces.dat
            DANS_ETC="$container_path_proxy/etc/guardian/guardian$zone"
            DANS_BLACK="$container_path_proxy/var/lib/blacklists/dansguardian$zone"
            DANS_WHITE="$container_path_proxy/var/lib/blacklists/white"
            for dir in downloadmanagers authplugins lists
            do
                cp -a "$danspath/$dir" "$DANS_ETC/"
            done
            mkdir -p "$DANS_BLACK/common"
            for filename in spusers users hosts filtergroupslist urls ipgroupslist domains_acad urls_acad
            do
                touch $DANS_BLACK/common/$filename
            done
            for pol in 1 3 4 5 6 7 8
            do
                mkdir -p $DANS_BLACK/f$pol
                for filename in whites domains types_mime extensions
                do
                    touch $DANS_BLACK/f$pol/$filename
                done
                #
                mkdir -p "$DANS_ETC/listsf$pol"
                cp -a "$danspath/templates/"* "$DANS_ETC/listsf$pol"
                [ "$pol" = "4" ] && cp -f "$danspath/whitelist_templates/"* "$DANS_ETC/listsf$pol"
                [ "$pol" = "3" ] && cp -f "$danspath/interdit_templates/"* "$DANS_ETC/listsf$pol"
                to_sed=$(find $DANS_ETC/listsf$pol/ -type 'f')
                sed -e "s/\$s/$pol/g" -e "s/\$z/$zone/g" -i $to_sed
                cp -a "$danspath/lists/"* "$DANS_ETC/listsf$pol"
            done
            #whitelist and other
            for pol in 1 3 5 6 7 8
            do
                for filename in bannedsitelist bannedurllist
                do
                    mkdir -p $DANS_BLACK/f$pol
                    [ ! -f "$DANS_BLACK/f$pol/$filename" ] && cp -f "$danspath/local_templates/$filename" "$DANS_BLACK/f$pol/$filename"
                done
            done
            #add only for whitelist
            pol=4
            for filename in site_liste_blanche url_liste_blanche exceptionsitelist exceptionurllist
            do
                touch $DANS_BLACK/f$pol/$filename
            done

	    for filemane in freelist
	    do
		    touch $DANS_WHITE/$filemane
	    done

            #répertoire de cache (#14035)
            for rep in $(CreoleGet dans_cachedir$((zone+1)))
            do
                if [[ ! -d ${container_path_proxy}/${rep} ]]
                then
                    mkdir -p ${container_path_proxy}/${rep}
                    CreoleRun "chown -R ${proxyuser}:${proxygroup} ${rep}" proxy
                fi
            done
        fi
    done
    $danspath/init_dans.py
    #Configuration files (#30441)
    CreoleRun "chown -R ${proxyuser}:${proxygroup} ${conf_root}" proxy
    set +e
fi
exit 0
