#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# Music Folder Make
#
# 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="music_folder_make"
declare -g            HELP="usage: [-h | --help] | [-V | --version] | [-T | --test] [-v | --verbose] [-p | --music_pattern <music_pattern>] [-r | --root_dir <music_root_dir>]"

declare -g        ROOT_DIR="/opt/music/src"
declare -g   MUSIC_PATTERN="/"

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





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

mfm_version_print()
{
    version_print
}





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

mfm_help_print()
{
    mfm_version_print
    help_print
}





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

mfm_args_parse()
{
    tmp_args=$(getopt -o r:p:ThVv --long root_dir:,music_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
	    # Options
            -r|--root_dir)                        shift;      ROOT_DIR="$1";                      shift;;
            -p|--music_pattern)                   shift; MUSIC_PATTERN="$1";                      shift;;

	    # Mode switches
            -T|--test)                                            MODE="TEST";                    shift;;
	    -h|--help)                                            MODE="EXIT"; mfm_help_print;    shift;;
	    -V|--version)                                         MODE="EXIT"; mfm_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
}





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

mfm_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}]   Root_Dir: [${ROOT_DIR}]   Music_Pattern: [${MUSIC_PATTERN}]"

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

i=1

while read -r dir
do
    if [[ "$(basename "${dir}")" == "misc" ]]
    then
	echo_line "${dir}: Misc\r" "${i}"
    else
	src=""
	dst="${dir}/folder.jpg"

	for file in "${dir}/cover-front" "${dir}/slipcase-front" "${dir}/box-front"
	do
	    for suffix in png jpg
	    do
		if [[ "${src}" == "" ]]
		then
		    if [[ -f "${file}.${suffix}" ]]
		    then
			src="${file}.${suffix}"
		    fi
		fi
	    done
	done

	if [[ "${src}" == "" ]]
	then
	    echo_line "${dir}: BAD!\n" "${i}"
	else
	    if [[ -f "${dst}" ]] && [[ -s "${dst}" ]] && [[ "${dst}" -nt "${src}" ]]
	    then
		echo_line "${dir}: Skipping\r" "${i}"
	    else
		echo_line "${dir}: Processing...\n" "${i}"
		sh_exec "anytopnm \"${src}\" | pamscale -xyfit 1024 1024 | pnmtojpeg -quality=95 -optimize >\"${dst}\""
	    fi
	fi
    fi

    i=$(( i + 1 ))

done < <(find "${ROOT_DIR}" -type d -links 2 | grep -e "${MUSIC_PATTERN}" | grep -v -e "/video" -e "/photo" | sort)

echo_line ""
