#!/bin/bash

# shellcheck disable=SC1091

set -o errexit
set -o nounset
set -o pipefail

[ -n "${DEBUG:+1}" ] && set -x

# Load libraries
. /opt/easysoft/scripts/liblog.sh
. /opt/easysoft/scripts/libeasysoft.sh

print_welcome_page

#=========================#
# Control service startup #
#=========================#
ENABLE_CRON=${ENABLE_CRON:-false}

# Enable apache
ln -s /etc/s6/s6-available/apache /etc/s6/s6-enable/01-apache

# Prepare cron
if [ "$ENABLE_CRON" == "true" ];then
    info "Enable cron service."
    ln -s /etc/s6/s6-available/cron /etc/s6/s6-enable/04-cron
fi

if [ $# -gt 0 ]; then
    exec "$@"
else
    # Init service
    /etc/s6/s6-init/run || exit 1

    # Start s6 to manage service
    exec /usr/bin/s6-svscan /etc/s6/s6-enable
fi
