#!/usr/bin/env bash

# Display the configuration files for the "AVpres" packages.
#
# Copyright (c) 2025-2026 by Reto Kromer <https://reto.ch/>
#
# Released under a 3-Clause BSD License and provided "as is" without warranty
# or support of any kind.


# initialise constants
VERSION='2026-01-02'
SCRIPT_NAME="$(basename "$0")"
CONFIG_FOLDER="${HOME}/.config/AVpres"
CONFIG_BASH="${CONFIG_FOLDER}/Bash_AVpres"
RED='\033[1;31m'
BLUE='\033[1;34m'
NC='\033[0m'


# get date-and-time stamp
date_time() {
  TZ='UTC' date +'[%F %T %Z]'
}


# print an error message and exit with status 1
abort() {
  echo -e "${RED}${1:-An unknown error occurred.\a}${NC}"
  echo "$(date_time) ${1:-An unknown error occurred.}" >> "${LOG_FILE}"
  echo "$(date_time) END" >> "${LOG_FILE}"
  exit 1
}


# print a minimal help message and exit with status 1
print_prompt() {
  echo "$(date_time) print prompt" >> "${LOG_FILE}"
  cat << EOF
Help:
  ${SCRIPT_NAME} -h
EOF
  echo "$(date_time) END" >> "${LOG_FILE}"
  exit 1
}


# print the help message and exit with status 0
print_help() {
  echo "$(date_time) print help" >> "${LOG_FILE}"
  cat << EOF

Usage:
  ${SCRIPT_NAME} -l | -c | -a | -g | -h
Options:
  -l  list of the installed configuration files
  -c  show the content of one configuration file
  -a  show the content of all configuration files
  -g  list of the installed 'AVpres' software packages
  -h  this help
About:
  Abstract: Display installed the AVpres configuration files
  Version:  ${VERSION}

EOF
  echo "$(date_time) END" >> "${LOG_FILE}"
  exit 0
}


# list installed 'AVpres' software packages
list_pkgs() {
  echo "$(date_time) list installed packages" >> "${LOG_FILE}"
  cd "${CONFIG_FOLDER}" || abort
  echo -e "${BLUE}Installed 'AVpres' packages:${NC}"
  ls -d -- */ | sed 's/^/  /' | sed 's/\///' | tee -a "${LOG_FILE}"
}


# list of the installed configuration files
list_cmds() {
  echo "$(date_time) list installed configuration files" >> "${LOG_FILE}"
  cd "${CONFIG_BASH}" || abort
  if [[ ! "$(ls -A)" ]]; then
    echo -e "${BLUE}There is no configuration file."
  else
    echo -e "${BLUE}The installed configuration files are:${NC}"
    ls -f -- *.txt | sed 's/^/  /' | tee -a "${LOG_FILE}"
  fi
}


# show the content of one configuration file
show_one_cmd() {
  local cmd
  local pkg

  echo "$(date_time) show configuration file" >> "${LOG_FILE}"
  cd "${CONFIG_BASH}" || abort
  if [[ ! "$(ls -A)" ]]; then
    echo -e "${BLUE}There is no configuration file."
  else
    pkg="$(ls)"
    PS3='Select a number: '
    echo -e "${BLUE}Configuration to display:${NC}"
    select cmd in ${pkg}; do
      break
    done
    if [[ "${cmd}" == '' ]]; then
      abort "Error: The answer '$REPLY' is not valid."
    fi
    echo "  ${cmd}" >> "${LOG_FILE}"
    echo -e "${BLUE}The configuration file '${cmd}' is:${NC}"
    cat "${cmd}" | sed 's/^/  /'
  fi
}


# show the content of all configuration files
show_all_cmds() {
  local cmd
  local pkg

  echo "$(date_time) show all configuration files" >> "${LOG_FILE}"
  cd "${CONFIG_BASH}" || abort
  if [[ ! "$(ls -A)" ]]; then
    echo -e "${BLUE}There is no configuration file."
  else
    pkg="$(ls)"
    for cmd in ${pkg}; do
      echo "  ${cmd}" >> "${LOG_FILE}"
      echo -e "${BLUE}The configuration file '${cmd}' is:${NC}"
      cat "${cmd}" | sed 's/^/  /'
    done
  fi
}


# start log file
[[ -d '/tmp/AVpres' ]] || mkdir -p '/tmp/AVpres'
LOG_FILE="$(mktemp -q "/tmp/AVpres/${SCRIPT_NAME}.XXXXXXXXXX")"
echo "$(date_time) ${SCRIPT_NAME} ${VERSION}" > "${LOG_FILE}"
echo "$(date_time) $0 $*" >> "${LOG_FILE}"
echo "$(date_time) START" >> "${LOG_FILE}"

# check that Bash 3.2 or later is running
bash_version="$(bash -c 'echo ${BASH_VERSION}')"
echo "$(date_time) running bash version = '${bash_version}'" >> "${LOG_FILE}"
if ! printf '%s\n%s\n' "${bash_version}" "3.2" | sort -rVC; then
  echo -e "${BLUE}Warning: This 'bash' binary is very old."
  echo -e "We strongly recommend that you use the current version 5.3.${NC}"
fi

# create a configuration folder and write "config.txt", if necessary
if [[ ! -d "${CONFIG_BASH}" ]]; then
  echo "$(date_time) create '${CONFIG_BASH}'" >> "${LOG_FILE}"
  mkdir -p "${CONFIG_BASH}" || abort
fi
if [[ ! -f "${CONFIG_BASH}/config.txt" ]]; then
  echo "$(date_time) write '${CONFIG_BASH}/config.txt'" >> "${LOG_FILE}"
  echo "${CONFIG_BASH}/config.txt"
  cat << EOF > "${CONFIG_BASH}/config.txt"
# Please don't edit me.
# Installed by '${SCRIPT_NAME}' $(TZ='UTC' date +'on %F at %T %Z').
VERSION='${VERSION}'
EOF
fi

# parse and process provided input
(( $# == 0 )) && print_prompt
while getopts ":lcagh" opt; do
  case "${opt}" in
    l) list_cmds ;;
    c) show_one_cmd ;;
    a) show_all_cmds ;;
    g) list_pkgs ;;
    h) print_help ;;
    *) abort "Error: The option '-${OPTARG}' is not valid." ;;
  esac
done

# end log file
echo "$(date_time) END" >> "${LOG_FILE}"
