#!/usr/bin/env bash

# Check which LTFS library is installed.
#
# Copyright (c) 2017-2026 by Reto Kromer <https://reto.ch/>
#
# This Bash script is released under a 3-Clause BSD License and is provided
# "as is" without warranty or support of any kind.


# initialise constants
VERSION='2026-01-02'
SCRIPT_NAME="$(basename "$0")"
RED='\033[1;31m'
BLUE='\033[1;34m'
NC='\033[0m'


# 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
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


# 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} -c | -h
Options:
  -c  check LTFS library
  -h  this help
See also:
  man ${SCRIPT_NAME}
  https://avpres.net/Bash_AVpres/
About:
  Abstract: Check which LTFS library is installed
  Version:  ${VERSION}

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


# check which LTFS library is installed

check_library() {
  local ltfs_bin
  local ltfs_path
  local mkltfs_bin
  local mkltfs_path

  echo "$(date_time) ckeck library" >> "${LOG_FILE}"
  ltfs_path="$(which ltfs)"
  if [[ "${ltfs_path}" ]]; then
    ltfs_bin="$(ltfs --version 2>&1)"
    echo "$(date_time) ltfs = '${ltfs_bin}'" >> "${LOG_FILE}"
    echo -e "${BLUE}The 'ltfs' binary${NC}"
    echo "${ltfs_bin}" | sed 's/^/  /'
    echo "$(date_time) path = '${ltfs_path}'" >> "${LOG_FILE}"
    echo -e "${BLUE}was found and its path is:${NC}"
    echo -e "  $(dirname "${ltfs_path}")"
  else
    abort "Error: No 'ltfs' binary was found on this computer."
  fi

  mkltfs_path="$(which mkltfs)"
  if [[ "${mkltfs_path}" ]]; then
    mkltfs_bin="$(mkltfs --version 2>&1)"
    echo "$(date_time) mkltfs = '${mkltfs_bin}'" >> "${LOG_FILE}"
    echo -e "${BLUE}The 'mkltfs' binary${NC}"
    echo "${mkltfs_bin}" | sed 's/^/  /'
    echo "$(date_time) path = '${mkltfs_path}'" >> "${LOG_FILE}"
    echo -e "${BLUE}was found and its path is:${NC}"
    echo -e "  $(dirname "${mkltfs_path}")"
  else
    abort "Error: No 'mkltfs' binary was found on this computer."
  fi
}


# parse provided input
(( $# == 0 )) && print_prompt
while getopts ":ch-:" opt; do
  case "${opt}" in
    c) check_library ;;
    h) print_help ;;
    -) case "${OPTARG}" in
         check) check_library ;;
         help) print_help ;;
         *) abort "Error: The option '--${OPTARG}' is not valid." ;;
       esac ;;
    *) abort "Error: The option '-${OPTARG}' is not valid." ;;
  esac
done

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