#!/bin/sh
# podrecast_mp3gain

mp3gain='mp3gain'

# ###########################################################################
# If ~/.podrecast_mp3gain exists and is executable, then it is sourced.
#

rc="$HOME/.podrecast_mp3gain"

if [ -f $rc ] && [ -s $rc ] && [ -x $rc ]
then
   echo "source: $rc"
   . $rc
fi

# ###########################################################################
# Do mp3gain...

if which aacgain > /dev/null
then
   mp3gain='aacgain'
fi

if ! which $mp3gain > /dev/null
then
   echo "error: could not find mp3gain or aacgain executable" >&2
   exit 1
fi

gain=3

for file in `podrecast --downloaded`
do
   (
      if flock -x -n 9
      then
         nice nice $mp3gain -r -c -p -t -m $gain $file \
            && podrecast --ready $file
      fi
   ) 9< $file
done