#!/bin/bash
export LD_LIBRARY_PATH="/opt/deploy/die/die_lin64_portable/base:$LD_LIBRARY_PATH"
# without this makedirs the Dockerfile is not able to create new directories in volumes that already exist
mkdir -p /var/run/clamav "${LOG_PATH}" "${LOG_PATH}/clamav"
chown -R clamav:"${USER}" /var/lib/clamav /var/run/clamav "${LOG_PATH}"
chmod 755 /var/lib/clamav "${LOG_PATH}" /var/run/clamav

touch "${LOG_PATH}/gunicorn_access.log" "${LOG_PATH}/gunicorn_errors.log" \
      "${LOG_PATH}/malware_tools_analyzers.log" "${LOG_PATH}/malware_tools_analyzers_errors.log"

chown "${USER}:${USER}" "${LOG_PATH}/gunicorn_access.log" "${LOG_PATH}/gunicorn_errors.log" \
      "${LOG_PATH}/malware_tools_analyzers.log" "${LOG_PATH}/malware_tools_analyzers_errors.log"

echo "running freshclam"
freshclam # download db for first time
freshclam -d & # run updater in bg
echo "running clamd"
clamd --debug & # run daemon in bg
# change user
su malware_tools_analyzers-user -s /bin/bash

echo "running fangfrisch"
/opt/deploy/flask/venv/bin/fangfrisch --conf /etc/clamav/fangfrisch.conf initdb --force
/opt/deploy/flask/venv/bin/fangfrisch --conf /etc/clamav/fangfrisch.conf refresh &

echo "running gunicorn"
# start flask server
exec /opt/deploy/flask/venv/bin/gunicorn 'app:app' \
    --bind '0.0.0.0:4002' \
    --user malware_tools_analyzers-user \
    --log-level "${LOG_LEVEL}" \
    --access-logfile "${LOG_PATH}/gunicorn_access.log" \
    --error-logfile "${LOG_PATH}/gunicorn_errors.log"
