#!/usr/bin/with-contenv bash

if [ ! -d /data/log ]; then
    echo -e "\t\t\033[1;33m* creating /data/log directory\033[0m"
    mv /var/log /data/log
else
    rm -Rf /var/log
fi
ln -s /data/log /var/log
chown root:root /var/log # for security requirements (and logrotate requires that)

if [ ! -d /data/log/s6 ]; then
    echo -e "\t\t\033[1;33m* creating /data/log/s6 directory\033[0m"
    mkdir -p /data/log/s6
fi
chown nobody:nogroup -R /data/log/s6

if [ ! -d /data/log/dmarc ]; then
    echo -e "\t\t\033[1;33m* initalizing /data/log/dmarc directory\033[0m"
    mkdir /data/log/dmarc
fi
chown delivery:mail -R /data/log/dmarc

if [ ! -d /data/log/delivery ]; then
    echo -e "\t\t\033[1;33m* initalizing /data/log/delivery directory\033[0m"
    mkdir /data/log/delivery
fi
# removed stat --format '%U' ... and stat --format '%G' ... to get rid of statx bug
user="$(/bin/core-username "/data/log/delivery")"
group="$(/bin/core-groupname "/data/log/delivery")"
if [ "delivery" != "${user}" ] || [ "mail" != "${group}" ] ; then
    chown delivery:mail -R /data/log/delivery
fi

# fix of NodeJS privs bug
# get first directory starting with 2 in /data/log/delivery
# and set it to delivery:mail
first_dir="$(find /data/log/delivery -maxdepth 1 -type f -name '2*' -print | sort | tail -n 1)"
user="$(/bin/core-username "$first_dir")"
group="$(/bin/core-groupname "$first_dir")"
if [ "delivery" != "${user}" ] || [ "mail" != "${group}" ] ; then
    echo -e "\t\t\033[1;33m* fixing wrong permissions due NodeJS bug\033[0m"
    chown delivery:mail -R /data/log/delivery
fi
# end of fix of NodeJS privs bug
# eventually remove this in future

if [ ! -d /data/log/nginx ]; then
    echo -e "\t\t\033[1;33m* initalizing /data/log/nginx directory\033[0m"
    mkdir /data/log/nginx
fi

if [ ! -d /data/redis ]; then
    echo -e "\t\t\033[1;33m* initalizing /data/redis directory\033[0m"
    mkdir /data/redis
fi
chown redis:redis -R /data/redis

if [ ! -d /var/log/redis ]; then
    mkdir /var/log/redis
fi
chown redis:redis -R /var/log/redis

if [ ! -d /data/quarantine ]; then
    echo -e "\t\t\033[1;33m* initalizing /data/quarantine directory\033[0m"
    mkdir /data/quarantine
fi
if [ ! -d /data/quarantine/tmp ]; then
    mkdir /data/quarantine/tmp
fi
chown delivery:mail -R /data/quarantine
find /data/quarantine -type d -exec chmod 775 {} \;
find /data/quarantine -type f -exec chmod 664 {} \;

if [ ! -d /data/var/ ] || [ ! -d /data/var/rspamd ]; then
    echo -e "\t\t\033[1;33m* initalizing /data/var/rspamd directory\033[0m"
    mkdir -p /data/var/rspamd
fi

if [ ! -f /data/var/rspamd/custom_regex_subject.map ]; then
    echo -e "/test spam message ř subject/iu\n" > /data/var/rspamd/custom_regex_subject.map
    chown _rspamd:_rspamd /data/var/rspamd/custom_regex_subject.map
    chmod 644 /data/var/rspamd/custom_regex_subject.map
fi

if [ ! -f /data/var/rspamd/custom_regex_content.map ]; then
    echo -e "/test spam message ř content/iu\n" > /data/var/rspamd/custom_regex_content.map
    chown _rspamd:_rspamd /data/var/rspamd/custom_regex_content.map
    chmod 644 /data/var/rspamd/custom_regex_content.map
fi

if [ ! -f /data/var/rspamd/custom_regex_from.map ]; then
    echo -e "/spam-test@poste\\.io/iu CUSTOM_REGEX_FROM_POSTE:10.0\n" > /data/var/rspamd/custom_regex_from.map
    chown _rspamd:_rspamd /data/var/rspamd/custom_regex_from.map
    chmod 644 /data/var/rspamd/custom_regex_from.map
fi

if [ ! -f /data/var/rspamd/custom_regex_full.map ]; then
    echo -e "/Test-header: itsmespam/iu CUSTOM_REGEX_FULL_POSTE:10.0\n" > /data/var/rspamd/custom_regex_full.map
    chown _rspamd:_rspamd /data/var/rspamd/custom_regex_full.map
    chmod 644 /data/var/rspamd/custom_regex_full.map
fi

touch /data/var/rspamd/rspamd_dynamic
chown _rspamd:_rspamd -R /data/var/rspamd
rm -Rf /var/lib/rspamd
ln -s /data/var/rspamd /var/lib/rspamd

if [ ! -d /var/log/rspamd ]; then
    mkdir -p /var/log/rspamd
fi
chown _rspamd:_rspamd -R /var/log/rspamd

if [ ! -d /run/rspamd ]; then
    mkdir /run/rspamd
fi
chown _rspamd:_rspamd -R /run/rspamd

if [ ! -d /data/queue ]; then
    mkdir /data/queue
fi
if [ ! -d /data/queue/smtp ]; then
    mkdir /data/queue/smtp
fi
if [ ! -d /data/queue/submission ]; then
    mkdir /data/queue/submission
fi
chown delivery:mail /data/queue -R

chmod 644 /etc/crontab
chown root:root /etc/crontab

if [ ! -d /data/var/clamav ]; then
    mkdir -p /data/var/clamav
fi
chown clamav:clamav /data/var/clamav -R

if [ ! -d /var/log/clamav ]; then
    mkdir -p /var/log/clamav
fi
# fixing some weird bug which prevents starting clamav with
# ERROR: /var/log/clamav/clamav.log is locked by another process
rm /var/log/clamav/clamav.log 2> /dev/null
chown clamav:clamav /var/log/clamav -R

# fix #470
chown mail:mail /etc/dovecot/sieve/

# we link submission to haraka-465
# mkdir -p /opt/haraka-465/config/
# ln -s /opt/haraka-submission/config/* /opt/haraka-465/config/
# rm /opt/haraka-465/config/smtp.ini
# ln -s /opt/haraka-465/config/smtp-override.ini /opt/haraka-465/config/smtp.ini
# ln -s /opt/haraka-smtp/plugins /opt/haraka-465/plugins

if [ -d /data ] && [ -e /data/server.ini ]; then
    chown mail:mail /data/server.ini 2> /dev/null
fi

chmod 644 /etc/logrotate.d/*


rm /opt/haraka-smtp/plugins/node_modules 2> /dev/null
ln -s /usr/lib/node_modules/Haraka/node_modules /opt/haraka-smtp/plugins/ 2> /dev/null || true

