#!/usr/bin/env bash
# shellcheck disable=SC2001

# Source the script 'common-functions.sh'.
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
ROOT_DIR=$(grep --only-matching "^.*scripts[^/]*" <<<"$SCRIPT_DIR")
source "$ROOT_DIR/common-functions.sh"

_main() {
    local input_files=""
    local output_dir=""

    # Execute initial checks.
    _check_dependencies ""
    _display_wait_box "2"
    input_files=$(_get_files "par_type=all")

    # Execute the function '_main_task' for each file in parallel.
    _run_task_parallel "$input_files" "$output_dir"
    _display_result_box ""
}

_main_task() {
    local input_file=$1
    local output_dir=$2
    local output_file=""
    local std_output=""

    local dir=""
    local filename=""
    local filename_extension=""
    dir=$(dirname -- "$input_file")
    filename=$(basename -- "$input_file")
    filename_extension=$(_get_filename_extension "$input_file")
    output_file=$filename

    # Transform similar ' characters.
    output_file=$(sed "s|"$'\u0060'"|'|g" <<<"$output_file") # Grave accent.
    output_file=$(sed "s|"$'\u00b4'"|'|g" <<<"$output_file") # Acute accent.
    output_file=$(sed "s|"$'\u2018'"|'|g" <<<"$output_file") # Left single quotation.
    output_file=$(sed "s|"$'\u2019'"|'|g" <<<"$output_file") # Right single quotation.

    # Transform to title case.
    output_file=$(sed "s|\(\w\)\(\w*\)|\U\1\L\2|g" <<<"$output_file")

    # Words to lowercase.
    local words=""
    words="a|an|and|as|at|but|by|for|from|if|in|nor|of|on|or|per|so|the|to|via|with|yet"
    words+="|a|as|e|o|os|um|uns|uma|umas|com|da|das|de|desde|do|dos|em|na|nas|no|nos|ou|pra|para|pela|pelas|pelo|pelos|por|se|sob|sobre"
    words+="|von"
    output_file=$(sed -E "s/ ($words)\b/ \L\1/gI" <<<"$output_file")

    # Uppercase the beginning of sentences.
    output_file=$(sed -E "s|([-\.])(\s\w)|\1\U\2|g" <<<"$output_file")

    # Fix words with ' character.
    output_file=$(sed "s|\('\w\)|\L\1|gI" <<<"$output_file")
    output_file=$(sed "s| \(d'\w\)| \U\1|gI" <<<"$output_file")
    output_file=$(sed "s| \(o'\w\)| \U\1|gI" <<<"$output_file")
    output_file=$(sed "s| o' | O' |gI" <<<"$output_file")
    output_file=$(sed "s|'em |'Em |gI" <<<"$output_file")
    output_file=$(sed "s| '\(\w\)| '\U\1|gI" <<<"$output_file")

    # Fix common uppercase word abbreviations.
    words="[0-9]\w|ac|api|sql|aka|asap|ceo|cfo|cto|dc|diy|dna|faq|isbn|ko|mtv|rpg|rpm|tv|usa|vip"
    words+="|bios|cmos|fifa|gba|nba|nes|nfl|nhl|rom|snes|uml"
    output_file=$(sed -E "s/\b($words)\b/\U\1/gI" <<<"$output_file")

    # Fix the some specific cases.
    output_file=$(sed "s|-in-|-in-|gI" <<<"$output_file")
    output_file=$(sed "s|\bphd\b|PhD|gI" <<<"$output_file")
    output_file=$(sed "s|\bno\. |No. |gI" <<<"$output_file")

    # Fix the some specific cases: 'featuring' word.
    output_file=$(sed "s|\bfeat\. |feat. |gI" <<<"$output_file")
    output_file=$(sed "s|\bfeaturing |featuring |gI" <<<"$output_file")

    # Fix the some specific cases: article 'the'.
    output_file=$(sed "s|, a |, A |gI" <<<"$output_file")
    output_file=$(sed "s|, an |, An |gI" <<<"$output_file")
    output_file=$(sed "s|, the |, The |gI" <<<"$output_file")
    output_file=$(sed "s|\& the |\& The |gI" <<<"$output_file")
    output_file=$(sed "s|\band the |and The |gI" <<<"$output_file")

    # Fix the some specific cases: some roman numbers.
    output_file=$(sed -E "s/ (IX|IV|V?I{0,3})\b/ \U\1/gI" <<<"$output_file")

    # Fix the some specific cases: classical songs.
    output_file=$(sed "s| in a minor| in A Minor|gI" <<<"$output_file")
    output_file=$(sed "s| in e minor| in E Minor|gI" <<<"$output_file")
    output_file=$(sed "s| in a major| in A Major|gI" <<<"$output_file")
    output_file=$(sed "s| in e major| in E Major|gI" <<<"$output_file")
    output_file=$(sed "s| in a flat| in A Flat|gI" <<<"$output_file")
    output_file=$(sed "s| in e flat| in E Flat|gI" <<<"$output_file")
    output_file=$(sed "s| in a sharp| in A Sharp|gI" <<<"$output_file")
    output_file=$(sed "s| in e sharp| in E Sharp|gI" <<<"$output_file")
    words="bwv|hwv|rv|wwv"
    output_file=$(sed -E "s/\b($words)\b/\U\1/gI" <<<"$output_file")
    output_file=$(sed "s|\bwoo\b|WoO|gI" <<<"$output_file")

    # Fix abbreviations with one letter character.
    output_file=$(sed "s| \(\w\.\)| \U\1|gI" <<<"$output_file")

    # Strip the extension.
    output_file=$(_strip_filename_extension "$output_file")

    # Run the main process.
    std_output=$(_move_file "skip" "$input_file" "$dir/$output_file${filename_extension,,}" 2>&1)
    _check_output "$?" "$std_output" "$input_file" "" || return 1
}

_main "$@"
