#!/bin/bash

set -eu
set -o pipefail

if [ -d /digital-assets/sound/Phone-Music ]
then
    cd /digital-assets/sound/Phone-Music
elif [ -d ~/sound/Phone-Music ]
then
    cd ~/sound/Phone-Music
elif [ -d /mymount/sound/Phone-Music ]
then
    cd /mymount/sound/Phone-Music
else
    echo "$0: Could not find Phone-Music" 1>&2
    exit 1
fi

if [ -t 1 ]
then
    count_options=-c
else
    count_options='-n -q'
fi
    
# shellcheck disable=SC2016,SC2086
find . -name '*.mp3' -print0 \
    | xargs -0 mp3info -p '%S\n' \
    | count $count_options \
    | stddev \
    | mtee -q -s \
        'cat' \
        'modunits -t time -d all -u unabbreviated -c -a -n $(grep "^sum " | sed "s/^sum //") -r'