#!/bin/sh
# Check for Apps before starting
if [ `which oggenc| grep oggenc|wc -l` -eq 0 ]
then
  echo "oggenc not installed"
  exit 0
fi

if [ `which vorbisgain| grep vorbisgain|wc -l` -eq 0 ]
then
  echo "vorbisgain not installed, not applying .ogg replaygain tag support."
fi

# Create Direcotry of converted files
OUT_DIR="./wav2ogg"
[ ! -d $OUT_DIR ] && mkdir -p $OUT_DIR

# Modify Ogg preferences
# http://linux.die.net/man/1/oggenc
# ogg_opts="-q9"
# Default Values ^ Quality Lvl 9

ogg_opts="-q9"

# Loop every .wav file
for x in *.wav
do OGGFILE=`basename "${x%.wav}.ogg"`

# Then convert .wav to .ogg
echo "Converting ${x} to Ogg format"
oggenc $ogg_opts "$x" -o $OUT_DIR/"$OGGFILE"

# Apply .ogg replaygain tag support if vorbisgain is installed.
if [ `exec which vorbisgain = "" | wc -l` -eq 1 ]; then
echo "Applying ReplayGain tag to "$OUT_DIR/"$OGGFILE"" "
vorbisgain $OUT_DIR/"$OGGFILE"
fi
done