#!/usr/bin/env bash
#
# Last Update: Fri May 22 14:07:26 CDT 2015
# Name: sox-process_masters
# License: GPLv3 or >
# Description: This script helps me process my master files and generat egg files; which are to be uploaded to our website. It's
#              kind of cool since it uses parallel to encode the files; which reduces the time it takes to do it.
# Usage: sox-process_masters <origin> <destination>

# variables
origin=${1:-"${HOME}/Bitwig Studio/Masters"}
origin=$( readlink -f "$origin" )
destination=${2:-"${HOME}/result"}
destination=$( readlink -f "$destination" )

## metadata
artist='Renich'
album='drafts'
year=2019
genre=17 # rock = 17
image='/home/renich/Pictures/renich/renich_con_sombrero.jpg'
comments='Composed, recorded and produced by: Renich Bon Ciric <renich@woralelandia.com>'
composer='Renich Bon Ciric <renich@woralelandia.com>'
copyright='This music is distributed under the CC-BY-SA 4.0 license.'

# tests
## dependencies
if [[ ! $( which parallel ) ]]; then
    echo 'please, install GNU Parallel before you continue'
    exit 1
elif [[ ! $( which sox ) ]]; then
    echo 'please, install SoX before you continue'
    exit 2
fi

## directories
if [[ ! -d "${origin}" ]]; then
    echo "the origin directory: ${origin}, does not exist."
    exit 3
elif [[ ! -d "${destination}" ]]; then
    echo "creating directory: ${destination}"
    mkdir -p "${destination}"
fi


# do
## go
cd "${origin}"

## process
parallel \
    sox --norm=0 "{}" --compression 10 "${destination}/{.}.ogg" silence 1 0.5 0% reverse silence 1 0.5 0% reverse '&&' \
    id3v2 \
        -2 \
        --artist "$artist" \
        --album "$album" \
        --genre $genre \
        --year $year \
        --APIC "$image" \
        "${destination}/{.}.ogg" \
::: *.wav

