#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# Music Check
#
# Copyright (C) 2016-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see
# <https://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------------------------------------------------------------



#-----------------------------------------------------------------------------------------------------------------------------------
# Includes
#-----------------------------------------------------------------------------------------------------------------------------------

: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/music_tools.bash"





#-----------------------------------------------------------------------------------------------------------------------------------
# Global Variables
#-----------------------------------------------------------------------------------------------------------------------------------

declare -g    VERSION="1.1.0"
declare -g       NAME="music_check"
declare -g       HELP="usage: [-h | --help] | [-V | --version] | [-T | --test] [-v | --verbose] <source_dir>"

declare -g SOURCE_DIR=""

declare -g       MODE="DEFAULT"
declare -g    VERBOSE="FALSE"
declare -g    DRY_RUN="FALSE"





#-----------------------------------------------------------------------------------------------------------------------------------
# Version Print
#-----------------------------------------------------------------------------------------------------------------------------------

mc_version_print()
{
    version_print
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Help Print
#-----------------------------------------------------------------------------------------------------------------------------------

mc_help_print()
{
    mc_version_print
    help_print
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Arg Parse
#-----------------------------------------------------------------------------------------------------------------------------------

mc_args_parse()
{
    tmp_args=$(getopt -o ThVv --long test,help,version,verbose -n "${NAME}" -- "$@")

    if [ $? != 0 ]
    then
        echo "Terminating..." >&2
        exit 1
    fi

    # Note the quotes around `$TEMP': they are essential!
    eval set -- "${tmp_args}"

    while true
    do
	case "$1" in
	    # Options

	    # Mode switches
            -T|--test)                              MODE="TEST";                   shift;;

	    -h|--help)                              MODE="EXIT"; mc_help_print;    shift;;
	    -V|--version)                           MODE="EXIT"; mc_version_print; shift;;

	    # Global options
            -v|--verbose)                        VERBOSE="TRUE";                   shift;;

            # End of options
	    --)                                                                    shift;  break;;
	    *) echo "args_parse internal error [$1] !";                                    exit 1;;
	esac
    done

    if [[ "${MODE}" != "EXIT" ]]
    then
        if [[ "${#}" != "1" ]]
	then
	    if [[ "${#}" -lt "1" ]]
	    then
		echo_error "Not enough args!"
	    else
		echo_error "Too many args!"
	    fi

	    MODE="EXIT"
	    mc_help_print
	else
	   SOURCE_DIR="$1"
	fi
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------------------------------------------------------------------

mc_args_parse "$@"



if [[ "${MODE}" == "EXIT" ]]
then
    exit 0
else
    if [[ "${MODE}" == "TEST" ]]
    then
	DRY_RUN="TRUE"
    fi
fi


echo_error "${NAME}: Mode: [${MODE}]   Verbose: [${VERBOSE}]   Source_Dir: [${SOURCE_DIR}]"

if [[ ! -d "${SOURCE_DIR}" ]]
then
    echo_error "Source directory doesn't exist!"
    exit -1
fi

i=1

find "${SOURCE_DIR}" -name '*.flac' -print0 | sort -z | while IFS= read -r -d '' file
do
    echo_line "${file}: " "${i}"
    cmd_exec flac --test "${file}" >/dev/null 2>&1
    if [[ $? -eq 0 ]]
    then
        echo "OK"
    else
        echo "KO"
    fi
    i=$((i+1))
done
