#!/usr/bin/env bash

# Bash Scripting for AVpres
# Sixth Session: make access file, course script
# 2022-02-10

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

# initialise presets
access=(veryslow 18)
web=(veryfast 28)
quality_presets=()

# initialise variables
unset input_file
unset output_file
unset file_quality


# if no input is provided, then display a minimal help message and exit with 1
if (( "${#}" == 0 )); then
    cat << EOF
Help:
  ${SCRIPT_NAME} -h
EOF
    exit 1
fi


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


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

Syntax:
  ${SCRIPT_NAME} -i <input_file> -o <output_file> [-q <file_quality>]
  ${SCRIPT_NAME} -h

Parameters:
  -i  path and name of the input file
  -o  path and name of the output file
  -q  file quality: 'access' (default) or 'web'
  -h  display this help

EOF
    exit 0
}


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

    if [[ "${in_file}" == '' ]]; then
        prompt_error "No input file passed."
    elif [[ ! -f "${in_file}" ]]; then
        prompt_error "'${in_file}' is not a file."
    elif ffprobe "${input_file}" 2>&1 | grep "Invalid data found" >/dev/null; then
        prompt_error "'$(basename ${input_file})' is not an AV file."
    fi

    return 0
}


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

    if [[ "${out_file}" == '' ]]; then
        prompt_error "No output file passed."
    elif echo "${out_file##*.}" | grep -E "^(mov|mp4)$"; (( $? != 0 )); then
        prompt_error "'${out_file##*.}' is not a valid file format."
    elif echo '' > "${out_file}"; (( $? != 0 )); then
        prompt_error "Cannot create an output file '${out_file}'."
    fi

    return 0
}


# verify file quality
verify_quality() {
    local file_q="${1}"

    if [[ "${file_q}" == '' ]]; then
        quality_presets=("${access[@]}")
    elif [[ "${file_q}" == 'access' ]]; then
        quality_presets=("${access[@]}")
    elif [[ "${file_q}" == 'web' ]]; then
        quality_presets=("${web[@]}")
    else
        prompt_error "'${file_q}' is not a valid preset."
    fi

    return 0
}


# generate the AV file and exit with 0
make_file() {
    local in_file="${1}"
    local out_file="${2}"

    echo -e "${BLUE}Please wait...${NC}"
    ffmpeg \
        -i "${in_file}" \
        -c:v libx264 \
        -preset "${quality_presets[0]}" \
        -crf "${quality_presets[1]}" \
        -pix_fmt yuv420p \
        -movflags +faststart \
        -y \
        "${out_file}"
    if (( $? != 0 )); then
        prompt_error "The file '$(basename ${out_file})' could not be generated."
    else
        echo -e "${BLUE}... done.${NC}"
        exit 0
    fi
}


# parse and process the provided input
while getopts ":i:o:q: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 ;;
        q) if [[ "${OPTARG:0:1}" == '-' ]]; then
              prompt_error "Error: The option '-q' requires an argument."
           else
              file_quality="${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}"
verify_output "${output_file}"
verify_quality "${file_quality}"

# generate the access file
make_file "${input_file}" "${output_file}"
