#!/bin/bash

###########################################################################
#
# MODULE:       Configurator
# COPYRIGHT:    (C) 2009-2025 by CacheGuard Technologies Ltd (UK)
# COPYRIGHT:    (C) 2026-2026 by CacheGuard Technologies SAS (FR)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

log-reset()
{
    echo -n > ${APPLY_LOG}
    chmod 640 ${APPLY_LOG}
    chown ${ADMIN_NAME}:${GROUP_NAME} ${APPLY_LOG}
}

log-abort-begin()
{
    echo >> ${APPLY_LOG}
    log "Begining the Abort operation"
    log-ret 0
}

log-abort-end()
{
    log "Ending the abort operation"
    log-result 0
}

log-msg()
{
    test -n "${1}" || return 1
    local message=${1}

    stdbuf --error=0 echo -ne "${message}" >> ${APPLY_LOG}
}

log()
{
    test -n "${1}" || return 1
    local message=${1}
    local tag=${2}

    test -n "${tag}" || tag=0
    local date=$(date +"%Y/%m/%d-%T" 2> /dev/null)
    log-msg "${tag} ${date} ${message}"
}

log-ret()
{
    test -n "${1}" || return 1
    local err_code=${1}

    local ok="[OK]"
    local ko="[KO]"

    if test ${err_code} -eq 0 ; then
	log-msg "${ok}\n" >> ${APPLY_LOG}
    else
	log-msg "${ko}\n" >> ${APPLY_LOG}
    fi
}

log-result()
{
    test -n "${1}" || return 1
    local code=${1}

    if test ${code} -eq 0 ; then
	log-ret ${code}
    else
	log-msg " (${code})"
	log-ret ${code}
	return 1
    fi
}

LIB_APL_LOG=Yes
