#!/usr/bin/env bash

# Manage the Nano configuration files in the '${HOME}/.config/nano/' folder.
#
# Copyright (c) 2017-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='2025-12-04'
SCRIPT_NAME="$(basename "$0")"
RED='\033[1;31m'
BLUE='\033[1;34m'
NC='\033[0m'

# initialise default value
folder="${HOME}/.config/nano/"

# initialise variables
unset input_file
unset mode


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


# start log file
[[ -d '/tmp/AVpres' ]] || mkdir -p '/tmp/AVpres'
LOG_FILE="$(mktemp "/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
if ! printf '%s\n%s\n' "$(bash -c 'echo ${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


# 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 | (-a|-d|-i) [<config_file>] | -p
  ${SCRIPT_NAME} -h
Options:
  -l  list the installed local Nano configuration files
  -a  add a local Nano configuration file
  -d  delete a local Nano configuration file
  -i  initialise the local Nano configuration files
  -p  print the local Nano configuration files
  -h  this help
See also:
  man ${SCRIPT_NAME}
  https://avpres.net/Bash_AVpres/
About:
  Abstract: Manage local Nano configuration files
  Version:  ${VERSION}
  Folder:   ${folder}

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


# add a Nano configuration file
add_config() {
  echo "$(date_time) add config" >> "${LOG_FILE}"
  echo "Sorry, not implemented yet."
}


# delete a local Nano configuration file
delete_config() {
  local kbd

  echo "$(date_time) delete config" >> "${LOG_FILE}"
  echo -en "${BLUE}Delete Nano configuration files? (y|N) ${NC}"
  read -n 1 kbd
  echo
  if [[ "${kbd}" != [Yy] ]]; then
    abort "Aborted by user."
  else
    echo -e "${BLUE}Deleting the Nano configuration files one by one:${NC}"
    rm -i "${folder}"*
  fi
}


# list the local Nano configuration files
list_config() {
  echo "$(date_time) list config" >> "${LOG_FILE}"
  cd "${folder}" || abort "Error: Could not change directory to '${folder}'."
  if [[ -z "$(ls -A "${folder}")" ]]; then
    echo -e "${BLUE}The folder '${folder}' is empty.${NC}"
  else
    echo -e "${BLUE}Installed 'Nano' configuration files at '${folder}':${NC}"
    find . -type f | sort | sed 's/^.\//  /'
  fi
}


# initialise the local Nano configuration files
init_config() {
  echo "$(date_time) init config" >> "${LOG_FILE}"
  if [[ ! -f "${HOME}/.config/nano/nanorc" ]]; then
    echo -e "${BLUE}Installing 'nanorc' configuration file...${NC}"
    cat << EOF >> "${folder}nanorc"
# Generated on $(TZ='UTC' date +'%F %T %Z') by ${SCRIPT_NAME} ${VERSION}
#
# Copyright (c) 2017-2026 by Reto Kromer <https://reto.ch/>
#
# This Nano configuration file is released under a 3-Clause BSD Licence and is
# provided "as is" without warranty or support of any kind.

set autoindent
#set linenumbers
#set nohelp
set softwrap

include ${folder}*.nanorc
#include /usr/local/share/nano/*.nanorc
EOF
  else
    echo -e "${RED}There is already a 'nanorc' configuration file.${NC}"
  fi
  if [[ ! -f "${folder}default.nanorc" ]]; then
    echo -e "${BLUE}Installing 'default.nanorc' configuration file...${NC}"
    cat << EOF >> "${folder}default.nanorc"
# Generated on $(TZ='UTC' date +'%F %T %Z') by ${SCRIPT_NAME} ${VERSION}
#
# Copyright (c) 2017-2026 by Reto Kromer <https://reto.ch/>
#
# This Nano configuration file is released under a 3-Clause BSD Licence and is
# provided "as is" without warranty or support of any kind.

# This default syntax is used for files that do not match any other syntax.

syntax default
comment "#"

# comments
color cyan "^[[:space:]]*#.*"
color cyan "[[:space:]]#.*"

# tabs
color ,cyan "	+"

# trailing whitespace
color ,cyan "[[:space:]]+$"
EOF
  else
    echo -e "${RED}There is already a 'default.nanorc' configuration file.${NC}"
  fi
}


# print Nano configuration files
print_config() {
  local file_list="$(ls "${folder}")"

  echo "$(date_time) print config" >> "${LOG_FILE}"
  for file_name in ${file_list}; do
    echo -e "${BLUE}Current '${file_name}' configuration file at '${folder}':${NC}"
    cat "${folder}${file_name}"
  done
}


# parse and process provided input
(( $# == 0 )) && print_prompt
[[ -d "${folder}" ]] || mkdir -p "${folder}"
while getopts ":ladip-:h" opt; do
  case "${opt}" in
    l) mode='list' ;;
    a) mode='add' ;;
    d) mode='delete' ;;
    i) mode='init' ;;
    p) mode='print' ;;
    -) case "${OPTARG}" in
         list) mode='list' ;;
         add=?*) input_file="${OPTARG#*=}"; mode='add' ;;
         delete=?*) input_file="${OPTARG#*=}"; mode='delete' ;;
         init) input_file="${OPTARG#*=}"; mode='init' ;;
         print) input_file="${OPTARG#*=}"; mode='print' ;;
         help) print_help ;;
         *) abort "Error: The option '--${OPTARG}' is not valid or requires an argument." ;;
       esac ;;
    h) print_help ;;
    *) abort "Error: The option '-${OPTARG}' is not valid." ;;
  esac
done

[[ -d "${folder}" ]] || mkdir -p "${folder}"
${mode}_config
echo "$(date_time) END" >> "${LOG_FILE}"
exit 0
