#
# Copyright (C) by Stefano Falsetto
# e-mail contact ....: mailto:stefko5@inwind.it
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or   
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# Config plugin for: sox
#

begin_check sox
find_prg sox
SOX=$FOUND_PRG
EXTENSIONS="wav au cdr"

if [ ! -z $SOX ]; then
  QueryVersion $($SOX -V 2>&1|$GREP Version|cut -d' ' -f3) sox "12.16" "12.17.1"
  OUTPUT_FORMATS_sox="almost all"
else
  missing sox "http://home.sprynet.com/~cbagwell/sox.html"
  lechoc GREEN $"Install sox as soon as possible.\
  In the meantime will be used a reduced version of this plugin."
  OUTPUT_FORMATS_sox="reduced version"
fi
  AUDIO_PLUGINS="$AUDIO_PLUGINS sox"
  echo
  begin_check "WAV description string in $MAGICFILE"
  echo
  WAVP1=$($EGREP "^0[[:space:]].*string[[:space:]]+RIFF" $MAGICFILE|\
          tr -s '\t' ' '|cut -d' ' -f4-)
  WAVCRITERIA=$($EGREP "^>8[[:space:]]+string[[:space:]]+WAVE" $MAGICFILE|\
                head -n 1|tr -s '\t'|tr -s ' '|cut -f4|cut -d',' -f2-)
  WAVS1=${#WAVP1}
  WAVS1=$[ WAVS1 + 1 ]
  WAVS2=${#WAVCRITERIA}
  if [ $WAVS2 -eq 0 ]; then
    #want_feedback "magic file parsing (wav criteria)" "Can't find wav criteria description in magic file"
    force_trust "wav criteria"
    #USCITA=$ERR_BAD_MAGICFILE; exit $USCITA
  fi
  lechoc YELLOW $" -> WAV criteria: %s" "$WAVCRITERIA"
  EXTENSIONS="wav"

  begin_check "AU description string in $MAGICFILE"
  AUCRITERIA=$($EGREP "^0[[:space:]]+string[[:space:]]+\.snd" $MAGICFILE|\
               tr -s '\t' ' '| cut -d' ' -f4-)
  AUS1=0
  AUS2=${#AUCRITERIA}
  if [ $AUS2 -eq 0 ]; then
     #want_feedback "magic file parsing (au criteria)" "Can't find au criteria description in magic file" warning
     force_trust "au criteria"
    #USCITA=$ERR_BAD_MAGICFILE; exit $USCITA
  else
    echo
    lechoc YELLOW $" -> AU criteria: %s" "$AUCRITERIA" 
    EXTENSIONS="$EXTENSIONS au"
  fi

  begin_check "RAW audio data (cdr) description string in $MAGICFILE"
  CDRCRITERIA=$($EGREP "^0[[:space:]].*short[[:space:]]+0x0100" $MAGICFILE|\
                tr -s '\t' ' '|cut -d' ' -f4-)
  CDRS1=0
  CDRS2=${#CDRCRITERIA}
  if [ $CDRS2 -eq 0 ]; then
    #want_feedback "magic file parsing (cdr criteria)" "Can't find cdr criteria description in magic file" warning
    #USCITA=$ERR_BAD_MAGICFILE; exit $USCITA
    force_trust "au criteria"
  else
    echo
    lechoc YELLOW $" -> RAW audio data criteria: %s" "$CDRCRITERIA"
    EXTENSIONS="$EXTENSIONS cdr"
  fi
  SUPPORTED_FORMATS="$SUPPORTED_FORMATS $EXTENSIONS"
  

cat_variables_sox () {  
  # Prevent multiple inclusion
  sox_counter=$[ sox_counter + 1 ]
  if [ $sox_counter -gt 1 ]; then
    return
  fi
  cat <<EOF
# Complete path for sox
SOX="$SOX" 

# Default format file to output
SOX_TOFMT="wav"

# Handled format file in input
OUTPUT_FORMATS_sox=( "$OUTPUT_FORMATS_sox" )

# Bound and criteria to check file type using magic file
WAVS1=$WAVS1
WAVS2=$WAVS2
WAVCRITERIA="$WAVCRITERIA"
EOF
if [ $AUS2 -ne 0 ]; then
  echo "AUS1=$AUS1" 
  echo "AUS2=$AUS2"
  echo "AUCRITERIA=\"$AUCRITERIA\""
fi
if [ $CDRS2 -ne 0 ]; then
  echo "CDRS1=$CDRS1"
  echo "CDRS2=$CDRS2"
  echo "CDRCRITERIA=\"$CDRCRITERIA\""
fi
}

