#!/bin/bash
set -u

# Naming Convention
# gpVar - command line parameters. These can override gcVars with same
#         base name.
# cgVar - User config vars
# gVar  - global variables. Should be few. Use functions to reduce these.
# cVar  - These are global config vars, fixed values for the script.
# pVar  - a positional variable passed into a function (local).
# tVar  - a local variable used in a function.
# fFunction  - a function unique to this script
# fComFunction - Most functions in bash-com.inc begin with fCom

# ========================================
# Main

export cVer=2.3.5
export cName=${BASH_SOURCE##*/}

# Save original command line for fExample.  Done before any parsing so
# the example file shows exactly what the user typed.
export gpCmdLine="$cName $*"

# -------------------
# Set cCurDir because PWD may be unset under cron.
if [[ -z "$PWD" ]]; then
    PWD=$(pwd)
fi
export cCurDir=$PWD

# -------------------
# Locate the directory that contains this script (and bash-com.inc).
export cBin

#tBin=home
tBin=this
case $tBin in
    this) cBin=${BASH_SOURCE%/*} ;;
    current) cBin=$cCurDir ;;
    home) cBin=$HOME/bin ;;
    local) cBin=/usr/local/bin ;;
    system) cBin=/usr/bin ;;
esac

if [[ ! -e $cBin/bash-com.inc ]]; then
    echo "Error: could not find $cBin/bash-com.inc (Change cBin setting in script.) [$LINENO]"
    exit 1
fi
if [[ ! -x $cBin/bash-com.inc ]]; then
    chmod a+rx $cBin/bash-com.inc
fi
source $cBin/bash-com.inc

if [[ ! -e $cBin/vid-tag.inc ]]; then
    echo "Error: could not find $cBin/vid-tag.inc (Change cBin setting in script.) [$LINENO]"
    exit 1
fi
if [[ ! -x $cBin/vid-tag.inc ]]; then
    chmod a+rx $cBin/vid-tag.inc
fi
source $cBin/vid-tag.inc

# --------------------------------------
# Get Args Section

fSetGlobals

if [[ $# -eq 0 ]]; then
    fUsage usage
fi

# Handle help / version options BEFORE any I/O, so that -h / -H / -V
# have no side effects on vid-tag.conf.  fUsage calls fCleanUp which
# exits the script.
tPrev=""
for tArg in "$@"; do
    case "$tArg" in
        -h) fUsage long ;;
        -V)
            echo "vid-tag version: $cVer"
            exit 0
            ;;
    esac
    case "$tPrev" in
        -H) fUsage "$tArg" ;;
    esac
    tPrev="$tArg"
done
# Trailing -H with no value: fall through to fGetOpt which will error.

# If vid-tag.conf exists, source it (overrides defaults).
# If it does not exist, fSaveConf is called inside fLoadConf to
# create it with the current defaults.
fLoadConf

# Parse the command line – overrides defaults and conf values.
fGetOpt "$@"

# --------------------------------------
# Validate Args Section. Exit with fUsage if errors.
fValidate

# Write the final state (defaults + conf + cmd-line) back to
# vid-tag.conf.
fSaveConf

# -------------------
# Build vid-tag-example.txt (preview of renames + metadata).
# Runs in both -n (dummy) and full-execute modes so the user always
# has a record of the planned changes.
fExample

# -------------------
# No-execute / dry-run exit.
if [[ $gpNoExec -ne 0 ]]; then
    fLog -p notice \
        -m "No-execute (-n) set. Review $gpConfLocal and rerun without -n." \
        -l $LINENO
    fCleanUp
fi

# --------------------------------------
# Write Section

fCreateOutputDir
if [[ ! -d "$cOutputDir" ]]; then
    fError -m "Cannot create $cOutputDir" -l $LINENO
fi

cd $cOutputDir >/dev/null 2>&1
for gFile in $gpEventFileList; do
    fProcessFile "$gFile"
done
cd - >/dev/null 2>&1

# -------------------
# CleanUp Section

fLog -p notice -m "Done." -l $LINENO
fCleanUp
