#!/bin/bash


#######################################################################################################################
# Variables                                                                                                           #
#######################################################################################################################

LIGHTTPD="/usr/sbin/lighttpd"
LIBRARIES="/usr/lib/lighttpd/"
SERVER_NAME='Webui'
APPLICATION_UID_GID="www-data:www-data"
APPLICATION_PERMISSION="775"
declare -a DMS_LOG_FILES=("access.log lighttpd.breakage.log lighttpd.error.log")

#######################################################################################################################
# Functions                                                                                                           #
#######################################################################################################################

create_server_log_files()
{
    for logfile in $DMS_LOG_FILES; do
        newfile=$WEBUI_WORK_FILES_DIR/log/$logfile
        
        if [ ! -f "$newfile" ]; then
            touch $newfile
            chown $APPLICATION_UID_GID $newfile
            chmod $APPLICATION_PERMISSION $newfile
        fi
    done
}

#######################################################################################################################
# Main entry                                                                                                          #
#######################################################################################################################

create_server_log_files

# Start the application - *The semaphore is green*
echo "Webui Server started listening -> http."
# echo "Device Maintenance Server started listening on port 8889 -> https."
exec -a $SERVER_NAME $LIGHTTPD -D -f $WEBUI_APP_FILES_DIR/config/lighttpd.conf -m $LIBRARIES
