#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# Album Name Fix
# 
# 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.0.0"
declare -g          NAME="album_name_fix "
declare -g          HELP="usage: [-h | --help] | [-V | --version] | [-T | --test] [-v | --verbose] [-a <album_dir>] [-p|--music_pattern <music_pattern>] [-s|--sed_substitute_pattern <sed_substitue_pattern>]"

declare -g     ALBUM_DIR="."
declare -g MUSIC_PATTERN="."
declare -g           SSP=""

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





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

anf_version_print()
{
    version_print
}





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

anf_help_print()
{
    anf_version_print
    help_print
}





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

anf_args_parse()
{
    tmp_args=$(getopt -o a:p:s:ThVv --long album_dir:,music_pattern:,sed_substitute_pattern:,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
	    # Path options
            -a|--album_dir)                   shift;       ALBUM_DIR="$1";                     shift;;

	    # Options
            -p|--music_pattern)               shift;   MUSIC_PATTERN="$1";                     shift;;
            -s|--sed_substitute_pattern)      shift;             SSP="$1";                     shift;;

	    # Mode switches
            -T|--test)                                         MODE="TEST";                    shift;;
	    -h|--help)                                         MODE="EXIT"; anf_help_print;    shift;;
	    -V|--version)                                      MODE="EXIT"; anf_version_print; shift;;

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

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





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

anf_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}]   Album_Dir: [${ALBUM_DIR}]   Music_Pattern: [${MUSIC_PATTERN}]   Sed_Substitue_Pattern: [${SSP}]"

cd "${ALBUM_DIR}"

IFS="\n"
find . -maxdepth 1 -name '*.flac' -o -name '*.FLAC' -o -name '*.ogg' -o -name '*.OGG' -o -name '*.mp3' -o -name '*.MP3' -o -name '*.m4a' -o -name '*.M4A' -o -name '*.ape' -o -name '*.APE' -o -name '*.wav' -o -name '*.WAV' -o -name '*.mp4' -o -name '*.MP4' -o -name '*.mkv' -o -name '*.MKV' | sed -e 's/^.\///' | grep -e "${MUSIC_PATTERN}" | sort |
    while read track_file
    do
        IFS=" "
        new_track_file="$(mt_fix_file_name "${track_file}" "${SSP}")"

	if [[ "${new_track_file}" != "${track_file}" ]]
	then
	    echo "Moving '${track_file}' into '${new_track_file}'..."
            
            cmd_exec mv -- "${track_file}" "${new_track_file}"
	else
	    echo "Skiping '${track_file}'..."
	fi

	IFS="\n"
    done
