#!/bin/bash

if [[ $1 == "-h" || $1 == "--help" ]]; then
  echo "mp3gain interface (C) 2005 by Georg Hennig"
  echo " - gaining recursivly all mp3's in the current directory"
  echo " - passing all arguments to mp3gain"
  echo " - if no arguments are given, use 'mp3gain -p -k -r \"filename\"'"
  echo ""
  echo "Usage: cd /your/desired/directory"
  echo "       mp3gain_here [mp3gain switches]"
  echo ""
  echo "To get an overview over the mp3gain switches, type"
  echo "  'mp3gain -?' or 'mp3gain_here -?'"
  exit 0
fi

if [[ $1 == "-?" ]]; then
  mp3gain -?
  exit 0
fi

echo "Building index of current directory..."

find . -type f -regex ".*.*3" -printf "%h/%f\n" > gain_tmp 2>/dev/null
LINES=`(set \`wc -l gain_tmp\`; echo $1 )`

if [[ $@  == "" ]]; then
  OPTIONS="-p -k -r"
else
  OPTIONS="$@"
fi

exec < gain_tmp

echo "Running mp3gain on $LINES files..."

for (( i=1; i<=LINES; i++ ))
{
  read FILE
  echo "Processing file #$i of $LINES..."
  OUT=`mp3gain $OPTIONS "$FILE"`
  echo "$OUT" | grep "Can't open"
}

rm gain_tmp

echo "mp3gain_here finished."

exit 0
