#!/usr/bin/env bash

# Bash Scripting for AVpres
# Fourth Session, course script
# 2022-02-03


# initialise constant
SCRIPT_NAME="$(basename "${0}")"

# initialise variable
unset opt


# display an error message and exit with 1
prompt_error() {
    echo "Error: ${1:-unknown error.}"
    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} -q | -s | -h
Parameters:
  -q  set the quality parameter
  -s  set the speed parameter
  -h  display this help

EOF
    exit 0
}


# generate the AV file and exit with 0
make_file() {
    ffmpeg \
        -i mandelbrot.avi \
        -c:v libx264 \
        -preset veryslow \
        -crf 18 \
        -pix_fmt yuv420p \
        -movflags +faststart \
        mandelbrot_H264.mp4
    echo "The passed parameter is: '${1}'."  # placeholder
    exit 0
}


# parse and process the provided input
(( "${#}" == 0 )) && prompt_mini_help
while getopts ":qsh" opt; do
    case "${opt}" in
        q) make_file "-q" ;;
        s) make_file "-s" ;;
        h) prompt_help ;;
        *) prompt_error "Option '-${OPTARG}' is not valid." ;;
    esac
done
