#!/bin/sh

if [ _"$1" = _"" ] ; then
   echo "Usage: $0 <filename>"
   exit
fi

# file=$1
for file in $@; do  ### das geht irgendwie schief :-(
   echo "Processing $file ..."
   if [ ! -e "$file" ] ; then
      echo "$file does not exist."
      exit
   fi

   FREQ=`file "$file"`
   TYPE=`echo $FREQ | grep -c "16 bit, mono"`
   FREQ=`echo $FREQ | grep -c "16 bit, .* 16000 Hz"`
   if [ $TYPE -lt 1 ] ; then
      echo "$file is not mono"
      FREQ=`file "$file"`
      FREQ=`echo $FREQ | sed -e "s/.*16 bit, stereo \([0-9]*\) Hz/\1/"`
      if [ $FREQ -ne 44100 ] ; then
         TYPE=""
      else
         echo "$file has $FREQ Hz - OK"
	 continue
      fi
   else
      TYPE="-c 2 "
   fi
   
   if [ $FREQ -lt 1 ] ; then
      FREQ=""
   else
      FREQ="-r 44100 "
   fi

   mv "$file" tmp_"$file"
   sox tmp_"$file" $FREQ $TYPE "$file"
   rm tmp_"$file"
done
