#!/bin/sh
# Audio decoding/encoding engine for Grafpup
# This script uses primarily ffmpeg to handle
# ogg, mp3, and wav formats but can also handle
# .ape, .flac, .aac, .mp4, and .m4a formats if
# the proper extra programs are installed.

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

PROFILE=$HOME/.config/grafburnrc
. $PROFILE

case $CHANNELS in
	mono)
	S_M="1"
	;;
	stereo)
	S_M="2"
	;;
esac

OUTPUT=$1

# First get the ogg files from the audiodirectory
ogg_encode() {
if [ ! $OUTPUT = "ogg" ];then
  for OGG in `ls *.ogg`
    do
    BASENAME=`basename $OGG .ogg`
    case $OUTPUT in
     mp3)
      ffmpeg -i $OGG -ac $S_M -ar $SAMPLING $BASENAME.mp3
      [ "$?" -eq 0 ] && rm -f $OGG
     ;;
     wav)
      ffmpeg -i $OGG -ac $S_M -ar $SAMPLING $BASENAME.wav
      [ "$?" -eq 0 ] && rm -f $OGG
     ;;
     flac)
     if [ $FLAC = "flac" ];then
      # first convert to wav
      ffmpeg -i $OGG -ac $S_M -ar $SAMPLING $BASENAME.wav
      [ "$?" -eq 0 ] && rm -f $OGG
      # now compress to flac
      flac -6 $BASENAME.wav
      [ "$?" -eq 0 ] && rm -f $BASENAME.wav
     fi
     ;;
     mac)
     if [ $MAC = "mac" ];then
      # first convert to wav
      ffmpeg -i $OGG -ac $S_M -ar $SAMPLING $BASENAME.wav
      [ "$?" -eq 0 ] && rm -f $OGG
      # now compress to mac
      mac $BASENAME.wav $BASENAME.ape -c2000
      [ "$?" -eq 0 ] && rm -f $BASENAME.wav
     fi
     ;;
    esac
  done
fi
}

# Now do the mp3 files
mp3_encode() {
if [ ! $OUTPUT = "mp3" ];then
  for MP3 in `ls *.mp3`
    do
    BASENAME=`basename $MP3 .mp3`
    case $OUTPUT in
      wav)
       ffmpeg -i $MP3 -ac $S_M -ar $SAMPLING $BASENAME.wav
       [ "$?" -eq 0 ] && rm -f $MP3
      ;;
      ogg)
       ffmpeg -i $MP3 -ac $S_M -ar $SAMPLING $BASENAME.ogg
       [ "$?" -eq 0 ] && rm -f $MP3
      ;;
      flac)
      if [ $FLAC = "flac" ];then
       ffmpeg -i $MP3 -ac $S_M -ar $SAMPLING $BASENAME.wav
       [ "$?" -eq 0 ] && rm -f $MP3
      fi
      ;;
      mac)
      if [ $MAC = "mac" ];then
       ffmpeg -i $MP3 -ac $S_M -ar $SAMPLING $BASENAME.wav
       [ "$?" -eq 0 ] && rm -f $MP3
      fi
      ;;
    esac
  done
fi
}

# Do the wav files
wav_encode() {
if [ ! $OUTPUT = wav ];then
  for WAV in `ls *.wav`
   do
    BASENAME=`basename $WAV .wav`
    case "$OUTPUT" in
      mp3)
       ffmpeg -i "$WAV" -ac $S_M -ar $SAMPLING $BASENAME.mp3
       [ "$?" -eq 0 ] && rm -f $WAV
      ;;
      ogg)
       ffmpeg -i $WAV -ac $S_M -ar $SAMPLING $BASENAME.ogg
       [ "$?" -eq 0 ] && rm -f $WAV
      ;;
      flac)
      if [ ! "`which flac`" = "" ];then
       flac -6 $WAV
       [ "$?" -eq 0 ] && rm -f $WAV
      fi
      ;;
      mac)
      if [ ! "`which mac`" = "" ];then
       mac $WAV $BASENAME.ape -c2000
       [ "$?" -eq 0 ] && rm -f $WAV
      fi
      ;;
    esac
  done
fi
}

# Any wma files
wma_encode() {
for WMA in `ls *.wma`
 do
  BASENAME=`basename $WMA .wma`
  case $OUTPUT in
   mp3)
   ffmpeg -i $WMA -ac $S_M -ar $SAMPLING $BASENAME.mp3
   [ "$?" -eq 0 ] && rm -f $WMA
   ;;
   ogg)
   ffmpeg -i $WMA -ac $S_M -ar $SAMPLING $BASENAME.ogg
   [ "$?" -eq 0 ] && rm -f $WMA
   ;;
   wav)
   ffmpeg -i $WMA -ac $S_M -ar $SAMPLING $BASENAME.wav
   [ "$?" -eq 0 ] && rm -f $WMA
   ;;
   flac)
   ffmpeg -i $WMA -ac $S_M -ar $SAMPLING $BASENAME.wav
   [ "$?" -eq 0 ] && rm -f $WMA
   ;;
   mac)
   ffmpeg -i $WMA -ac $S_M -ar $SAMPLING $BASENAME.wav
   [ "$?" -eq 0 ] && rm -f $WMA
   ;;
  esac
 done
}

# Catch any flac files (only if flac is installed)
flac_decode() {
if [ ! $OUTPUT = "flac" ];then
  flac -d --delete-input-file --verify *.flac
fi
}

# On to any mac files (.ape) (only if mac is installed)
mac_decode() {
if [ ! $OUTPUT = "ape" ];then
  for MAC in `ls *.ape`
  do
   BASENAME=`basename $MAC .ape`
   mac $MAC $BASENAME.wav -d
   [ "$?" -eq 0 ] && rm -f $MAC
  done
fi
}

# m4a and mp4 (only if faad is installed)
faad_decode() {
for MP4 in "`ls *.mp4`"
do
 faad $MP4
 [ "$?" -eq 0 ] && rm -f $MP4
done
for M4A in "`ls *.m4a`"
do
 faad $M4A
 [ "$?" -eq 0 ] && rm -f $M4A
done
}


#-----------------------------------------------
# The work actually starts here, all of above are
# just defining functions. We start by decoding
# all flac, mac, or mp4 files to wav, which is
# just an intermediate stage unless wav is the
# target output. These parts depend on packages
# not part of official Puppy, so we have to check
# for the executables first.
if [ ! "`which flac`" = "" ];then
  flac_decode
fi
if [ ! "`which mac`" = "" ];then
  mac_decode
fi
if [ ! "`which faad`" = "" ];then
  faad_decode
fi
# now process all ogg, mp3, wav, and wma files
wma_encode
ogg_encode
mp3_encode
wav_encode
     

