#!/bin/bash
#
#Server-Manager: ack_log (System Command: Acknowledge omoti log) [Power: 755]
#
#Version: 1.3.1
#Date:    2025.07.01
#Unix:    Debian 11
#Author:  P.Klapp
#Company: Omoti UG (haftungsbeschränkt)
#Website: https://omoti.de
#
#Path:    /Commands/


#Version
    version=$(sed -n '5p' ${0})
    version=${version#*\ }

#Temporary file
    tmp="/srv/omoti/tmp/ack_log.$(date +'%H%M%S')$(pwgen -s -0 -N 1 2).tmp"

#Parameters
    option=${1}

#Functions
#Dependencies
    #parameters
        if [[ ${option} ]] && ([[ ${option} == '-?' ]] || [[ ${option} == '--help' ]]); then
            echo 'Usage: ack_log [OPTION]...'
            echo "ack_log (v${version}) Acknowledge omoti log"
            echo ''
            echo 'Options:'
            echo '-s            show the pending log'
            echo '-? / --help   display this help and exit'
            echo ''
            echo 'Full documentation can be found in the Omoti Wiki'
            exit 0
        fi

        if [[ ${option} ]] && [[ ${option} != '-s' ]]; then
            unset option
        fi

# check for omoti-server
    has_omoti_server() {
        command -v omoti-server >/dev/null 2>&1
    }

#Variables
    #entries        -> Main
    i='0'
    #logCaller      -> Main
    #logCode        -> Main
    #logText        -> Main
    monFile='/srv/omoti/log/.mon.status'
    #option         -> Parameters
    #tmp            -> Temporary file
    #user           -> Main
    #version        -> Version

#Main
    # zusätzlicher omoti-server Befehl, falls installiert
    if has_omoti_server
    then
        omoti-server log -c "${USER}" info 'Log status acknowledged'
        echo -e "\033[32mOmoti-Server log status acknowledged\033[0m"
    fi

    if [[ -f ${monFile} ]] && [[ -z ${option} ]]; then
        rm ${monFile}

        echo -e "\033[32mLog status acknowledged\033[0m"
        o_log -i 'Log status acknowledged'

    elif [[ -f ${monFile} ]] && [[ ${option} ]]; then
        IFS=$'\n' entries=($(cat ${monFile}))

        echo ''
        echo "Pending log status (v${version})"
        echo $(date +'%Y-%m-%d')
        echo '--------------------------------------------------------------------------------------------------'
        echo 'Log-Code  User        Caller                Text'
        echo '--------------------------------------------------------------------------------------------------'
        echo ''

        while [[ ${i} != ${#entries[@]} ]]; do
            ((i++))
            logCode="$(printf '%-8s\n' ${entries[${i}]})"
            ((i++))
            user="$(printf '%-10s\n' ${entries[${i}]})"
            ((i++))
            logCaller="$(printf '%-20s\n' ${entries[${i}]})"
            ((i++))
            logText="${entries[${i}]}"

            echo "${logCode}  ${user}  ${logCaller}  ${logText}"
            ((i++))
        done
        echo ''

    else
        echo -e "\033[33mNo log status pending\033[0m"
    fi
