#!/usr/bin/with-contenv bash

nginx_confs=( \
    authelia-location.conf \
    authelia-server.conf \
    geoip2.conf \
    ldap.conf \
    nginx.conf \
    proxy.conf \
    site-confs/default \
    ssl.conf )

for i in ${nginx_confs[@]}; do
    if [ "$(sed -nE 's|^## Version ([0-9]{4}\/[0-9]{2}\/[0-9]{2}).*|\1|p' /config/nginx/${i})" != "$(sed -nE 's|^## Version ([0-9]{4}\/[0-9]{2}\/[0-9]{2}).*|\1|p' /defaults/$(basename ${i}))" ]; then
        nginx_confs_changed="/config/nginx/${i}\n${nginx_confs_changed}"
    fi
done

if [ -n "$nginx_confs_changed" ]; then
    echo "**** The following nginx confs have different version dates than the defaults that are shipped. ****"
    echo "**** This may be due to user customization or an update to the defaults. ****"
    echo "**** To update them to the latest defaults shipped within the image, delete these files and restart the container. ****"
    echo "**** If they are user customized, check the date version at the top and compare to the upstream changelog via the link. ****"
    echo -e "${nginx_confs_changed}"
fi

proxy_confs=$(ls /config/nginx/proxy-confs/*.conf 2>/dev/null)

for i in $proxy_confs; do
    if [ -f "${i}.sample" ]; then
        if [ "$(sed -nE 's|^## Version ([0-9]{4}\/[0-9]{2}\/[0-9]{2}).*|\1|p' ${i})" != "$(sed -nE 's|^## Version ([0-9]{4}\/[0-9]{2}\/[0-9]{2}).*|\1|p' ${i}.sample)" ]; then
            proxy_confs_changed="${i}\n${proxy_confs_changed}"
        fi
    fi
done

if [ -n "$proxy_confs_changed" ]; then
    echo "**** The following reverse proxy confs have different version dates than the samples that are shipped. ****"
    echo "**** This may be due to user customization or an update to the samples. ****"
    echo "**** You should compare them to the samples in the same folder to make sure you have the latest updates. ****"
    echo -e "${proxy_confs_changed}"
fi
