#!/bin/bash

container_path_proxy=$(CreoleGet container_path_proxy)

# Répertoires de cache pour les 2 instances de squid (#6864, #16749)
for cache_dir in $(CreoleGet cache_dir) $(CreoleGet cache_dir_2 '');do
    if [ ! -d "$container_path_proxy$cache_dir" ];then
        mkdir -p "$container_path_proxy$cache_dir"
        CreoleRun "chown proxy:proxy $cache_dir" proxy
    fi
done

# Peuplement des répertoires de cache
CreoleRun "squid -f /etc/squid/squid.conf -z 2>/dev/null" proxy
if [ -f "$container_path_proxy/etc/squid/squid2.conf" ];then
    CreoleRun "squid -f /etc/squid/squid2.conf -z 2>/dev/null" proxy
fi

if [ "$(CreoleGet activer_squid_1_mitm non)" = 'oui' ] || [ $(CreoleGet activer_squid_2_mitm non) = 'oui' ]; then
    if [ ! -d "$container_path_proxy/var/spool/squid/ssl_db" ]; then
        CreoleRun "/usr/lib/squid/security_file_certgen -c -s /var/spool/squid/ssl_db -M 4MB" proxy
    fi
    if [ ! -f "$container_path_proxy/etc/squid/signingCA.key" ]; then
        openssl genrsa 4096 > "$container_path_proxy/etc/squid/signingCA.key"
        openssl req -new -x509 -days 3650 -key "$container_path_proxy/etc/squid/signingCA.key" -out "$container_path_proxy/etc/squid/signingCA.crt"
        ln -s "$container_path_proxy/etc/squid/signingCA.crt" /etc/eole/squid_CA.crt
    fi
fi

exit 0
