#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# Music Info
#
# 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_info"
declare -g     HELP="usage: [-h | --help] | [-V | --version] | [-l | --list] [-r | --root_dir <root_dir>]"

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

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





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

mi_version_print()
{
    version_print
}





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

mi_help_print()
{
    mi_version_print
    help_print
}





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

mi_args_parse()
{
    tmp_args=$(getopt -o r:lhVv --long root_dir:,list,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;;

	    # Mode switches
            -l|--list)                                      MODE="LIST";                   shift;;
	    -h|--help)                                      MODE="EXIT"; mi_help_print;    shift;;
	    -V|--version)                                   MODE="EXIT"; mi_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 [[ "${#}" -gt "0" ]]
	then
	    echo_error "Too many args!"
            
	    MODE="EXIT"
	    mi_help_print
	fi
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# List
#-----------------------------------------------------------------------------------------------------------------------------------

mi_list()
{
    local   type="$1"
    local suffix="$2"


    case "${type}"
    in
        track)
            find "${ROOT_DIR}" -type f | grep "\\.${suffix}$"
        ;;

        lp|ep|single|live|compilation|tribute)
            find "${ROOT_DIR}" -type f | grep -v -e "/child/" -e "/original_soundtrack/" -e "/various_artists/" | grep -e "/${type}/" | grep "\\.${suffix}$" | sed -e 's/\/[^/]*$//' | sort -u
        ;;

        child)
            find "${ROOT_DIR}" -type f | grep -e "/child/" | grep "\\.${suffix}$" | sed -e 's/\/[^/]*$//' | sort -u
        ;;

        ost)
            find "${ROOT_DIR}" -type f | grep -e "/original_soundtrack/" | grep "\\.${suffix}$" | sed -e 's/\/[^/]*$//' | sort -u
        ;;

        va)
            find "${ROOT_DIR}" -type f | grep -e "/various_artists/" | grep "\\.${suffix}$" | sed -e 's/\/[^/]*$//' | sort -u
        ;;

        us)
            find "${ROOT_DIR}" -type f | grep -v -e "/various_artists/" -e "/original_soundtrack/" -e "/lp/" -e "/ep/" -e "/single/" -e "/live/" -e "/compilation/" -e "/misc/" -e "/tribute/" | grep "\\.${suffix}$" | sed -e 's/\/[^/]*$//' | sort -u
        ;;
    esac
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Count
#-----------------------------------------------------------------------------------------------------------------------------------

mi_count()
{
    local   type="$1"
    local suffix="$2"


    echo "$(mi_list "${type}" "${suffix}" | wc -l)"
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Stats Print
#-----------------------------------------------------------------------------------------------------------------------------------

mi_stats_print()
{
    local    track_flac=$(mi_count track flac)
    local     track_ogg=$(mi_count track ogg)
    local     track_mp3=$(mi_count track mp3)
    local   track_music=$(( track_flac + track_ogg + track_mp3 ))

    local     track_avi=$(mi_count track avi)
    local     track_mp4=$(mi_count track mp4)
    local     track_mkv=$(mi_count track mkv)
    local   track_video=$(( track_avi + track_mp4 + track_mkv ))

    local     track_jpg=$(mi_count track jpg)
    local     track_png=$(mi_count track png)
    local track_picture=$(( track_jpg + track_png ))

    local   track_total=$(( track_music + track_video + track_picture ))

    local      lp_flac=$(mi_count lp flac)
    local       lp_ogg=$(mi_count lp ogg)
    local       lp_mp3=$(mi_count lp mp3)
    local     lp_music=$(( lp_flac + lp_ogg + lp_mp3 ))

    local      ep_flac=$(mi_count ep flac)
    local       ep_ogg=$(mi_count ep ogg)
    local       ep_mp3=$(mi_count ep mp3)
    local     ep_music=$(( ep_flac + ep_ogg + ep_mp3 ))

    local  single_flac=$(mi_count single flac)
    local   single_ogg=$(mi_count single ogg)
    local   single_mp3=$(mi_count single mp3)
    local single_music=$(( single_flac + single_ogg + single_mp3 ))

    local    live_flac=$(mi_count live flac)
    local     live_ogg=$(mi_count live ogg)
    local     live_mp3=$(mi_count live mp3)
    local   live_music=$(( live_flac + live_ogg + live_mp3 ))

    local compilation_flac=$(mi_count compilation flac)
    local  compilation_ogg=$(mi_count compilation ogg)
    local  compilation_mp3=$(mi_count compilation mp3)
    local compilation_music=$(( compilation_flac + compilation_ogg + compilation_mp3 ))

    local  tribute_flac=$(mi_count tribute flac)
    local   tribute_ogg=$(mi_count tribute ogg)
    local   tribute_mp3=$(mi_count tribute mp3)
    local tribute_music=$(( tribute_flac + tribute_ogg + tribute_mp3 ))

    local   child_flac=$(mi_count child flac)
    local    child_ogg=$(mi_count child ogg)
    local    child_mp3=$(mi_count child mp3)
    local  child_music=$(( child_flac + child_ogg + child_mp3 ))

    local     ost_flac=$(mi_count ost flac)
    local      ost_ogg=$(mi_count ost ogg)
    local      ost_mp3=$(mi_count ost mp3)
    local    ost_music=$(( ost_flac + ost_ogg + ost_mp3 ))

    local      va_flac=$(mi_count va flac)
    local       va_ogg=$(mi_count va ogg)
    local       va_mp3=$(mi_count va mp3)
    local     va_music=$(( va_flac + va_ogg + va_mp3 ))

    local      us_flac=$(mi_count us flac)
    local       us_ogg=$(mi_count us ogg)
    local       us_mp3=$(mi_count us mp3)
    local     us_music=$(( us_flac + us_ogg + us_mp3 ))

    local set_flac_total=$(( lp_flac  + ep_flac  + single_flac  + live_flac  + compilation_flac  + tribute_flac  + child_flac  + ost_flac  + va_flac  + us_flac  ))
    local  set_ogg_total=$(( lp_ogg   + ep_ogg   + single_ogg   + live_ogg   + compilation_ogg   + tribute_ogg   + child_ogg   + ost_ogg   + va_ogg   + us_ogg   ))
    local  set_mp3_total=$(( lp_mp3   + ep_mp3   + single_mp3   + live_mp3   + compilation_mp3   + tribute_mp3   + child_mp3   + ost_mp3   + va_mp3   + us_mp3   ))
    local      set_total=$(( lp_music + ep_music + single_music + live_music + compilation_music + tribute_music + child_music + ost_music + va_music + us_music ))

    
    echo "Music Statistics from ${ROOT_DIR}:"
    echo "Tracks:"
    printf "Music:   Flac: %5s   OGG: %5s   MP3: %5s   Total: %6s\n" "${track_flac}"       "${track_ogg}"       "${track_mp3}"       "${track_music}"
    printf "Video:    AVI: %5s   mp4: %5s   MKV: %5s   Total: %6s\n" "${track_avi}"        "${track_mp4}"        "${track_mkv}"        "${track_video}"
    printf "Picture:  JPG: %5s   PNG: %5s        %5s   Total: %6s\n" "${track_jpg}"        "${track_png}"        ""                    "${track_picture}"
    printf "               %5s        %5s        %5s   Total: %6s\n" ""                    ""                    ""                    "${track_total}"

    echo ""
    echo   "Album    |  Flac |   OGG |   MP3 || Total"
    echo   "---------+-------+-------+-------++------"
    printf "LP       | %5s | %5s | %5s || %5s\n" "${lp_flac}"          "${lp_ogg}"          "${lp_mp3}"          "${lp_music}"
    printf "EP       | %5s | %5s | %5s || %5s\n" "${ep_flac}"          "${ep_ogg}"          "${ep_mp3}"          "${ep_music}"
    printf "Single   | %5s | %5s | %5s || %5s\n" "${single_flac}"      "${single_ogg}"      "${single_mp3}"      "${single_music}"
    printf "Live     | %5s | %5s | %5s || %5s\n" "${live_flac}"        "${live_ogg}"        "${live_mp3}"        "${live_music}"
    printf "Compil   | %5s | %5s | %5s || %5s\n" "${compilation_flac}" "${compilation_ogg}" "${compilation_mp3}" "${compilation_music}"
    printf "Tribute  | %5s | %5s | %5s || %5s\n" "${tribute_flac}"     "${tribute_ogg}"     "${tribute_mp3}"     "${tribute_music}"
    printf "Child    | %5s | %5s | %5s || %5s\n" "${child_flac}"       "${child_ogg}"       "${child_mp3}"       "${child_music}"
    printf "OST      | %5s | %5s | %5s || %5s\n" "${ost_flac}"         "${ost_ogg}"         "${ost_mp3}"         "${ost_music}"
    printf "VA       | %5s | %5s | %5s || %5s\n" "${va_flac}"          "${va_ogg}"          "${va_mp3}"          "${va_music}"
    printf "UnSorted | %5s | %5s | %5s || %5s\n" "${us_flac}"          "${us_ogg}"          "${us_mp3}"          "${us_music}"
    echo   "---------+-------+-------+-------++------"
    printf "Total    | %5s | %5s | %5s || %5s\n" "${set_flac_total}"   "${set_ogg_total}"   "${set_mp3_total}"   "${set_total}"
}





#-----------------------------------------------------------------------------------------------------------------------------------
# List Print
#-----------------------------------------------------------------------------------------------------------------------------------

mi_list_print()
{
    local suffix type

    
    for suffix in flac ogg mp3 avi mp4 mkv jpg png
    do
	echo "Track ${suffix}:"
	mi_list track "${suffix}" | sed -e "s/^/track /"
	echo
    done

    for type in lp ep single live compilation tribute va ost us
    do
	for suffix in flac ogg mp3
	do
	    echo "Set ${type} ${suffix}:"
	    mi_list "${type}" "${suffix}" | sed -e "s/^/${type} /"
	    echo
	done
    done
}





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

mi_args_parse "$@"



if [[ "${MODE}" == "EXIT" ]]
then
    exit 0
fi



echo_error "${NAME}: Mode: [${MODE}]   Verbose: [${VERBOSE}]   Root_Dir: [${ROOT_DIR}]"

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

case "${MODE}"
in
    STATS)
        mi_stats_print
    ;;

    LIST)
        mi_list_print
    ;;
esac
