#!/usr/bin/tini /bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright (C) 2021 Olliver Schinagl <oliver@schinagl.nl>
# Copyright (C) 2021-2023 Cisco Systems, Inc. and/or its affiliates. All rights reserved.

set -eu

if [ "${#}" -gt 0 ] && \
   [ "${1#-}" = "${1}" ] && \
   command -v "${1}" > "/dev/null" 2>&1; then
        CLAMAV_NO_CLAMD="true" exec "${@}"
else
        if [ "${#}" -ge 1 ] && \
           [ "${1#-}" != "${1}" ]; then
                exec clamd "${@}"
        fi

        if [ ! -f "/var/lib/clamav/main.cvd" ]; then
                echo "Updating initial database"
                sed -e 's|^\(TestDatabases \)|\#\1|' \
                        -e '$a TestDatabases no' \
                        -e 's|^\(NotifyClamd \)|\#\1|' \
                        /etc/clamav/freshclam.conf > /tmp/freshclam_initial.conf
                freshclam --foreground --stdout --config-file=/tmp/freshclam_initial.conf
                rm /tmp/freshclam_initial.conf
        fi

        if [ "${CLAMAV_NO_FRESHCLAMD:-false}" != "true" ]; then
                echo "Starting Freshclamd"
                freshclam \
                          --checks="${FRESHCLAM_CHECKS:-1}" \
                          --daemon \
                          --foreground \
                          --stdout \
                          --user="clamav" \
                          &
        fi

        if [ "${CLAMAV_NO_CLAMD:-false}" != "true" ]; then
                echo "Starting ClamAV"
                if [ -S "/tmp/clamd.sock" ]; then
                        unlink "/tmp/clamd.sock"
                fi
                clamd --foreground &
                while [ ! -S "/tmp/clamd.sock" ]; do
                        if [ "${_timeout:=0}" -gt "${CLAMD_STARTUP_TIMEOUT:=1800}" ]; then
                                echo
                                echo "Failed to start clamd"
                                exit 1
                        fi
                        printf "\r%s" "Socket for clamd not found yet, retrying (${_timeout}/${CLAMD_STARTUP_TIMEOUT}) ..."
                        sleep 1
                        _timeout="$((_timeout + 1))"
                done
                echo "socket found, clamd started."
        fi

        if [ "${CLAMAV_NO_MILTERD:-true}" != "true" ]; then
                echo "Starting clamav milterd"
                clamav-milter &
        fi

        exec tail -f "/dev/null"
fi

exit 0
