#!/usr/bin/env bash

# Bash Scripting for AVpres
# Fifth Session: make checksum manifest, course script
# 2022-02-08

# initialise constants
SCRIPT_NAME="$(basename "${0}")"
RED='\033[1;31m'
BLUE='\033[1;34m'
NC='\033[0m'

# initialise variables
unset input_file
unset output_file
unset algorithm
unset opt


# display an error message and exit with 1
prompt_error() {
    echo -e "${RED}Error: ${1:-Unknown error.\a}${NC}"
    exit 1
}


# display a minimal help message and exit with 1
prompt_mini_help() {
    cat << EOF
Help:
  ${SCRIPT_NAME} -h
EOF
    exit 1
}


# display a help message and exit with 0
prompt_help() {
    cat << EOF

Syntax:
  ${SCRIPT_NAME} -i <input_file> [-o <output_file>] [-a <algorithm>]
  ${SCRIPT_NAME} -h
Parameters:
  -i  input file
  -o  output file
  -a  checksum algorithm: md5, sha1, sha256, sha512 or xxh
  -h  display this help

EOF
    exit 0
}


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

    if [[ "${in_file}" == "" ]]; then
        prompt_error "no file passed."
    elif [[ ! -f "${in_file}" ]]; then
        prompt_error "'${in_file}' is not a file."
    fi

    return 0
}


# verify output file
verify_output() {
    local out_file="${1}"

    ########  TO DO  ########

    return 0
}


# verify that checksum tool for chosen algorithm is running
verify_algorithm() {
    local algo="${1,,}"  # needs Bash 4.0 or newer

    if ! command -v "${algo}sum" &> /dev/null; then
        prompt_error "'${algo}sum' not found."
    fi

    return 0
}


# generate the checksum of the file and exit with 0
make_checksum() {
    local in_file="${1}"
    local out_file="${2}"
    local algo="${3,,}"  # needs Bash 4.0 or newer

    echo -e "${BLUE}Please wait...${NC}"
    ${algo}sum "${in_file}" | sed "s#${in_file%/*}/##" > "${out_file}"
    if (( $? != 0 )); then
        prompt_error "The checksum manifest could not be generated."
    else
        echo -e "${BLUE}... done.${NC}"
        exit 0
    fi
}


# parse the provided input
(( $# == 0 )) && prompt_mini_help
while getopts ":i:o:a:h" opt; do
    case "${opt}" in
        i) if [[ "${OPTARG:0:1}" == '-' ]]; then
              prompt_error "Error: The option '-i' requires an argument."
           else
              input_file="${OPTARG}"
           fi ;;
        o) if [[ "${OPTARG:0:1}" == '-' ]]; then
              prompt_error "Error: The option '-o' requires an argument."
           else
              output_file="${OPTARG}"
           fi ;;
        a) if [[ "${OPTARG:0:1}" == '-' ]]; then
              prompt_error "Error: The option '-a' requires an argument."
           else
              algorithm="${OPTARG}"
           fi ;;
        h) prompt_help ;;
        :) prompt_error "Option '-${OPTARG}' requires an argument." ;;
        *) prompt_error "Option '-${OPTARG}' is not valid." ;;
    esac
done

# check if provided input file is valid
verify_input "${input_file}"
if  [[ ! "${algorithm}" ]]; then
    algorithm='md5'
fi
verify_algorithm "${algorithm}"
if [[ ! "${output_file}" ]]; then
    output_file="${input_file%.*}_${input_file##*.}_${algorithm}.txt"
fi
verify_output "${output_file}"

# generate the checksum manifest
make_checksum "${input_file}" "${output_file}" "${algorithm}"
