#!/usr/bin/env bash

# Verify a frame MD5 checksum manifest of an audio-visual file or
# single-image files in a folder.
#
# Copyright (c) 2012-2026 by Reto Kromer <https://reto.ch/>
#
# This Bash script is released under a 3-Clause BSD License and provided
# "as is" without warranty or support of any kind.


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

# load configuration file if any and initialise default values
[[ -f "${CONFIG_FILE}" ]] && . "${CONFIG_FILE}"
suffix="${suffix:-_framemd5}"
extension="${extension:-txt}"
ffmpeg_bin="${ffmpeg_bin:-$(which ffmpeg)}"
ffprobe_bin="${ffprobe_bin:-$(which ffprobe)}"

# initialise other default values
extension_regex='^[A-Za-z0-9]+$'
ff_options='-y'

# initialise variables
unset input_path
unset input_manifest
unset input_file_regex


# 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 -q "/tmp/AVpres/${SCRIPT_NAME}.XXXXXXXXXX")"
TMP_MANIFEST="$(mktemp -q "/tmp/AVpres/tmp_${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}"
  rm "${TMP_MANIFEST}"
  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
  rm "${TMP_MANIFEST}"
  echo "$(date_time) END" >> "${LOG_FILE}"
  exit 1
}


# print the help message and exit with status 0
print_help() {
  local tmp
  if [[ -f "${CONFIG_FILE}" ]]; then
    tmp="local configuration file found and loaded"
  else
    tmp="no local configuration file found on this computer"
  fi

  echo "$(date_time) print help" >> "${LOG_FILE}"
  cat << EOF

Usage:
  ${SCRIPT_NAME} -i <input_path> [-m <manifest_file>]
  ${SCRIPT_NAME} -h | -x
Options:
  -i  input file for stream-based content or input folder with single images
  -m  manifest file (default is '<input_path>${suffix}.${extension}')
  -h  this help
  -x  advanced options with their default arguments
      (${tmp})
Dependencies:
  ffmpeg and ffprobe
See also:
  man ${SCRIPT_NAME}
  https://avpres.net/Bash_AVpres/
About:
  Abstract: Verify a frame MD5 checksum manifest of an audio-visual file or
            single-image files in a folder
  Version:  ${VERSION}

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


# print advanced options with their default arguments and exit with status 0
print_options() {
  echo "$(date_time) print options" >> "${LOG_FILE}"
  if [[ -f "${CONFIG_FILE}" ]]; then
    cat << EOF

Local configuration file
  '${CONFIG_FILE}'
found and loaded.
EOF
  else
    cat << EOF

No local configuration file for '${SCRIPT_NAME}' found on this computer.
EOF
  fi
  cat << EOF

Output file default parameters:
  --suffix='${suffix}'
  --extension='${extension}'

FFmpeg default binaries:
  --ffmpeg='${ffmpeg_bin}'
  --ffprobe='${ffprobe_bin}'

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


# verify input
verify_input() {
  local in_file="${1}"
  local tmp_framerate

  echo "$(date_time) verify input file" >> "${LOG_FILE}"
  if [[ ! "${in_file}" ]]; then
    abort "Error: No input file passed."
  elif [[ ! -f "${in_file}" ]]; then
    abort "Error: '${in_file}' is not an input file."
  elif "${ffprobe_bin}" "${in_file}" 2>&1 | grep "Invalid data found" > /dev/null; then
    abort "Error: '${in_file}' is not an AV file."
  elif grep "#format: frame checksums" > /dev/null < "${in_file}"; then
    abort "Error: '${in_file}' is a frame MD5 checksum file."
  fi
  tmp_framerate="$(ffprobe -show_streams -print_format flat -loglevel quiet ${in_file} \
    | grep 'r_frame_rate=' | grep -oE '[0-9]+/?[0-9]+')"
  if [[ "${tmp_framerate}" ]] ; then
    ff_options+=" -framerate ${tmp_framerate}"
  fi
  echo "$(date_time) input='${in_file}'" >> "${LOG_FILE}"

  return 0
}


# verify manifest
verify_manifest() {
  local in_file="${1}"

  echo "$(date_time) verify manifest file" >> "${LOG_FILE}"
  if [[ "${in_file}" ]]; then
    if [[ ! "${in_file##*.}" =~ ${extension_regex} ]]; then
      abort "Error: The extension '${in_file##*.}' is not valid."
    fi
    input_manifest="${in_file}"
  else
    if [[ ! "${extension}" =~ ${extension_regex} ]]; then
      abort "Error: The extension '${extension}' is not valid."
    fi
    if [[ "${suffix}" == '#' ]]; then
      suffix=''
    fi
    if [[ -d "${input_path}" ]]; then
      input_manifest="${input_path}${suffix}.${extension}"
    else
      input_manifest="${input_path%.*}_${input_path##*.}${suffix}.${extension}"
    fi
  fi
  if  [[ ! -f "${input_manifest}" ]]; then
    abort "Error: '${in_file}' is not a manifest file."
  elif ! grep "#format: frame checksums" > /dev/null < "${input_manifest}"; then
    abort "Error: '$(basename "${input_manifest}")' is not a frame MD5 checksum file."
  fi
  echo "$(date_time) output=${input_manifest}" >> "${LOG_FILE}"

  return 0
}


# check that external ffmpeg and ffprove commands are running
check_ffmpeg() {
  if command -v "${ffmpeg_bin}" &> /dev/null; then
    echo "$(date_time) '${ffmpeg_bin}' found" >> "${LOG_FILE}"
  else
    abort "Error: 'ffmpeg' binary not found."
  fi
  if command -v "${ffprobe_bin}" &> /dev/null; then
    echo "$(date_time) '${ffprobe_bin}' found" >> "${LOG_FILE}"
  else
    abort "Error: 'ffprobe' binary not found."
  fi

  return 0
}


# generate frame MD5 checksums
make_frame_MD5() {
  local in_file="${1}"
  local out_file="${2}"

  echo "$(date_time) generate frame MD5 checksum manifest" >> "${LOG_FILE}"
  echo -e "${BLUE}Please wait while generating frame MD5 checksum manifest...${NC}"
  echo "$(date_time) ${ffmpeg_bin} ${ff_options} -i ${in_file} -f framemd5 ${out_file}" >> "${LOG_FILE}"
  if ! "${ffmpeg_bin}" ${ff_options} -i "${in_file}" -f framemd5 \
    "${out_file}" &> /dev/null
  then
    abort "Fatal error, see '${LOG_FILE}'."
  fi

  return 0
}


# parse and process provided input
(( $# == 0 )) && print_prompt
while getopts ":i:m:-:hx" opt; do
  case "${opt}" in
    i) if [[ "${OPTARG:0:1}" == '-' ]]; then
         abort "Error: The option '-i' requires an argument."
       else
         input_path="${OPTARG}"
       fi ;;
    m) if [[ "${OPTARG:0:1}" == '-' ]]; then
         abort "Error: The option '-m' requires an argument."
       else
         input_manifest="${OPTARG}"
       fi ;;
    -) case "${OPTARG}" in
         input=?*) input_path="${OPTARG#*=}" ;;
         manifest=?*) input_manifest="${OPTARG#*=}" ;;
         suffix=?*) suffix="${OPTARG#*=}" ;;
         extension=?*) extension="${OPTARG#*=}" ;;
         ffmpeg=?*) ffmpeg_bin="${OPTARG#*=}" ;;
         ffprobe=?*) ffprobe_bin="${OPTARG#*=}" ;;
         help) print_help ;;
         options) print_options ;;
         *) abort "Error: The option '--${OPTARG}' is not valid." ;;
       esac ;;
    h) print_help ;;
    x) print_options ;;
    :) abort "Error: The option '-${OPTARG}' requires an argument." ;;
    *) abort "Error: The option '-${OPTARG}' is not valid." ;;
  esac
done

# check that FFmpeg is running
check_ffmpeg

# check that input provided is valid
if [[ -d "${input_path}" ]]; then
  first_file=$(ls "${input_path}" | sort | head -n 1)
  start_number=$(echo "${first_file}" | grep -oE '_[0-9]+\.' | grep -oE '[0-9]+')
  if [[ ! "${start_number}" ]]; then
    start_number=$(echo "${first_file}" | grep -oE '[0-9]+')
  fi
  if [[ "${start_number}" == $(basename "${first_file%.*}") ]]; then
    verify_input "${input_path}/${start_number}.${first_file##*.}"
  else
    verify_input "${input_path}/${first_file%_*}_${start_number}.${first_file##*.}"
  fi
else
  verify_input "${input_path}"
fi
verify_manifest "${input_manifest}"

# generate frame MD5 checksum manifest
if [[ -d "${input_path}" ]]; then
  if [[ "${start_number}" == $(basename "${first_file%.*}") ]]; then
    input_file_regex="%0${#start_number}d.${first_file##*.}"
  else
    input_file_regex="${first_file%_*}_%0${#start_number}d.${first_file##*.}"
  fi
  if (( $(echo "${start_number}" | bc -l) > 1 )); then
    FF_OPTIONS+=" -start_number ${start_number}"
  fi
fi
if [[ -d "${input_path}" ]]; then
  input_file="${input_path}/${input_file_regex}"
else
  input_file="${input_path}"
fi
make_frame_MD5 "${input_file}" "${TMP_MANIFEST}"

# compare the old with the new frame MD5 checksum manifests
if ! diff <(grep -v '^#' < "${input_manifest}") \
  <(grep -v '^#' < "${TMP_MANIFEST}") \
  >> "${LOG_FILE}" 2>&1
then
  abort "Error: The file and the manifest don't match, see '${LOG_FILE}'."
fi
rm "${TMP_MANIFEST}"
echo -e "${BLUE}... all is okay!${NC}"

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