#!/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.1.3"
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       EXIT_CODE=0
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()
{
    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="HELP";                    shift;;
	    -V|--version)                                         MODE="VERSION";                 shift;;

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

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





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

vsl_add

mfm_args_parse "$@"


case "${MODE}" in        
    "EXIT")
        exit ${EXIT_CODE}
    ;;
    
    "HELP")
        mfm_help_print
        exit ${EXIT_CODE}
    ;;

    "TEST")
	DRY_RUN="TRUE"
    ;;
    
    "VERSION")
        mfm_version_print
        exit ${EXIT_CODE}
    ;;
esac


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_CODE=1
else
    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 ""
fi


exit ${EXIT_CODE}
