#!/bin/bash

# Environment Variables
. ../../common/ajax_common

# SYS ENV
PGPATH=${PGPATH:-/usr/panabit}
PGETC=${PGETC:-/usr/panaetc}
RAMDISK=${RAMDISK:-/usr/ramdisk}
DATAPATH=${DATAPATH:-/usr/panalog}

# APP ENV
APP_NAME="unisase-agent"
APP_ROOT_DIR="${PGPATH}/app/${APP_NAME}"

RAM_ROOT_DIR="${RAMDISK}/app/${APP_NAME}"
RAM_TMP_DIR="${RAM_ROOT_DIR}/tmp"
RAM_APP_CTRL="${RAM_ROOT_DIR}/appctrl"

# APP LOG
APP_LOG_DIR="${DATAPATH}/App/${APP_NAME}"
AGENT_DEFAULT_LOG_DIR="/usr/unisase/log/${APP_NAME}"
AGENT_DEFAULT_LOG_FILE="${AGENT_DEFAULT_LOG_DIR}/agent.log"
AGENT_ARCHIVE_LOG_DIR="/usr/unisase/archivelog/${APP_NAME}"

# AGENT BIN
AGENT_BIN_DIR="/usr/unisase/bin"
AGENT_BIN="${AGENT_BIN_DIR}/unisase-agent"

# AGENT CONF
AGENT_CONF_DIR="/usr/unisase/etc/${APP_NAME}"
AGENT_CONF_BACKUP_DIR="${AGENT_CONF_DIR}/backup"
AGENT_CONF_FILE="${AGENT_CONF_DIR}/config.yaml"

# AGENT CERTS
AGENT_CERTS_DIR="/usr/unisase/etc/certs"
AGENT_CA_CERT_FILE="${AGENT_CERTS_DIR}/cacert.pem"

# Get log file path from config file
# Returns the log file path configured in YAML, or default path if not configured
get_log_file_path() {
    local log_file_path="$AGENT_DEFAULT_LOG_FILE"

    if [ ! -f "$AGENT_CONF_FILE" ]; then
        echo "$log_file_path"
        return
    fi

    # Parse YAML config file to get log file path
    local parsed_path=$(awk '
    BEGIN { in_logging=0; in_outputs=0; in_file=0 }

    # Enter logging section
    /^logging:/ { in_logging=1; next }

    # Exit logging section
    /^[a-zA-Z]/ && in_logging { in_logging=0; in_outputs=0; in_file=0 }

    # Enter outputs section
    in_logging && /^  outputs:/ { in_outputs=1; next }

    # Exit outputs section
    in_logging && /^  [a-zA-Z]/ && in_outputs { in_outputs=0; in_file=0 }

    # Detect file type output
    in_outputs && /^    - type: "file"/ { in_file=1; next }

    # Detect console type output - ignore console config in daemon mode
    in_outputs && /^    - type: "console"/ { in_file=0; next }

    # Get file path
    in_outputs && in_file && /^      file:/ {
        gsub("#.*", "");  # Remove comments
        gsub("^      file: *\"?", "");
        gsub("\"? *$", "");
        gsub(/[ \t]+$/, "");  # Remove trailing spaces
        if (length($0) > 0) {
            print $0;
            exit
        }
    }
    ' "$AGENT_CONF_FILE")

    # Use parsed path if valid, otherwise use default path
    if [ -n "$parsed_path" ] && [ "$parsed_path" != "" ]; then
        log_file_path="$parsed_path"
    fi

    echo "$log_file_path"
}

# Get archive log directory from config file
# Returns the archive directory path configured in YAML, or default path if not configured
get_archive_log_dir() {
    local archive_dir=""

    if [ -f "$AGENT_CONF_FILE" ]; then
        archive_dir=$(awk '
        BEGIN { in_logging=0; in_outputs=0; in_file=0; in_archive=0 }

        # Enter logging section
        /^logging:/ { in_logging=1; next }

        # Exit logging section
        /^[a-zA-Z]/ && in_logging { in_logging=0; in_outputs=0; in_file=0; in_archive=0 }

        # Enter outputs section
        in_logging && /^  outputs:/ { in_outputs=1; next }

        # Exit outputs section
        in_logging && /^  [a-zA-Z]/ && in_outputs { in_outputs=0; in_file=0; in_archive=0 }

        # Detect file type output
        in_outputs && /^    - type: "file"/ { in_file=1; next }

        # Detect console type output
        in_outputs && /^    - type: "console"/ { in_file=0; in_archive=0; next }

        # Enter archive section
        in_outputs && in_file && /^      archive:/ { in_archive=1; next }

        # Exit archive section
        in_outputs && in_file && /^      [a-zA-Z]/ && in_archive { in_archive=0 }

        # Get archive dir
        in_outputs && in_file && in_archive && /^        dir:/ {
            gsub("#.*", "");  # Remove comments
            gsub("^        dir: *\"?", "");
            gsub("\"? *$", "");
            gsub(/[ \t]+$/, "");  # Remove trailing spaces
            if (length($0) > 0) {
                print $0;
                exit
            }
        }
        ' "$AGENT_CONF_FILE")
    fi

    # Use default archive directory if not configured
    if [ -z "$archive_dir" ]; then
        archive_dir="$AGENT_ARCHIVE_LOG_DIR"
    fi

    echo "$archive_dir"
}