#!/bin/bash

# Extract megaboon .zip and .mp3 files into /mymount/sound/Music/<band>/<album>/*.mp3
# And					 .mp3 files into same

set -eu
set -o pipefail

original_cwd="$(pwd)"

compilation=""

function usage
{
    retval="$1"
    case "$retval" in
        0)
            ;;
        *)
            exec 1>&2
            ;;
    esac
    echo "Usage: $0 --all-compilation --all-studio --help -- file1.zip file2.zip"
    exit "$retval"
}

while echo "$1" | grep -q '^-'
do
    case "$1" in
        --all-studio)
            compilation=False
            ;;
        --all-compilation)
            compilation=True
            ;;
        --help|-h)
            usage 0
            ;;
        --)
            break
            ;;
        *)
            echo "$0: unrecognized option: $1" 1>&2
            usage 1
            ;;
    esac
    shift
done

case "$compilation" in
    True)
        ;;
    False)
        ;;
    "")
        echo "$0: You must specify --all-compilation or --all-studio" 1>&2
        usage 1
        ;;
    *)
        echo "$0: internal error: \$compilation has a strange value: $compilation" 1>&2
        exit 1
        ;;
esac

for file in "$@"
do
    case "$file" in
        *.zip)
            # shellcheck disable=SC2001
            # The\ Rolling\ Stones\ -\ 12\ x\ 5(1964).zip
            band=$(echo "$file" | sed 's/^\([^-]*\) - .*\.zip/\1/' )
            # shellcheck disable=SC2001
            album=$(echo "$file" | sed 's/^[^-]* - \(.*\)([0-9]*)\.zip/\1/' )
            # shellcheck disable=SC2001
            case "$compilation" in
                True)
                    year=Compilation
                    ;;
                False)
                    year=$(echo "$file" | sed 's/^.*(\([0-9][0-9]*\))\.zip/\1/' )
                    ;;
                *)
                    echo "$0: internal error: \$compilation has a strange value: $compilation" 1>&2
                    exit 1
                    ;;
            esac
            echo "$band" "$album" "$year"
            cd /mymount/sound/Music
            mkdir -p "$band"
            cd "$band"
            mkdir -p "$year-$album"
            cd "$year-$album"
            # -o says to overwrite - nice for reextraction
            unzip -o "$original_cwd"/"$file"
            rename 's/_/ /g' ./*.mp3
            rename "s#^./$band - ##g" ./*.mp3
            mp3-album-year --year "$year" --files ./*.mp3
            cd "$original_cwd"
            ;;
        *.mp3)
            # If we do not have ffprobe, do not continue
            if ! which ffprobe > /dev/null 2>&1
            then
                echo "$0: no ffprobe found" 1>&2
                exit 1
            fi
            # Strangely, ffprobe writes its output to stderr, not stdout.
            # We disable this shellcheck stuff, because I prefer a subprocess or 2 over the line noise that's
            # been getting into bash lately.
            # shellcheck disable=SC2001
            if ! band=$(ffprobe "$file" 2>&1 | grep -E '^ *artist ' | sed 's/^ *artist *: \(.*\)$/\1/')
            then
                band=$(ffprobe "$file" 2>&1 | grep -E '^ *album_artist ' | sed 's/^ *album_artist *: \(.*\)$/\1/')
            fi
            # shellcheck disable=SC2001
            album=$(ffprobe "$file" 2>&1 | grep -E '^ *album ' | sed 's/^ *album *: \(.*\)$/\1/')
            echo "$band" "$album"
            cd /mymount/sound/Music
            mkdir -p "$band"
            cd "$band"
            mkdir -p "$album"
            cd "$album"
            cp "$original_cwd"/"$file" "$file"
            cd "$original_cwd"
            ;;
        *)
            echo "$0: Unrecognized file extension on file $file" 1>&2
            exit 1
            ;;
    esac
done