#
# 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 mpg321
#

cat_variables_mpg321 () {

  cat <<EOF 
# Complete path for mp3 decoder
mpg321_DECODER="$mpg321_DECODER"

# Parameters to only show a time line
EXTRACT_TIME="$EXTRACT_TIME"

# Parameters to output to stdout with buffered mode
BUFFERED_DECODE="$BUFFERED_DECODE"

# Parameters to output to stdout without buffer
DECODE_TO_STDOUT="$DECODE_TO_STDOUT"

# Parameters to simply decode
NORMALDECODE="$NORMALDECODE"

# Array of output supported formats files
OUTPUT_FORMATS_mpg321=( $OUTPUT_FORMATS_mpg321 )

# Parameters to output to file in wav format
TO_wav="$TO_wav"

# Parameters to output to file in cdr format
TO_cdr="$TO_cdr"

# Parameters to output to file in au format
TO_au="$TO_au"

# default output file format
TOFMT="$TOFMT"

# Default conversion file extension
MPGCMD="$MPGCMD"

# Bound and criteria to check file type using magic file
# Layer 3
MPEGS1_1=$MPEGS1_1
MPEGS2_1=$MPEGS2_1
MPEGCRITERIA="$MPEGCRITERIA"
# Layer 2
MPEGS1_2=$MPEGS1_2
MPEGS2_2=$MPEGS2_2
MPEGCRITERIA_2="$MPEGCRITERIA_2"

EOF

}


###################
# MAIN
###################

if [ $(expr "$SUPPORTED_FORMATS" : ".*mp3.*") -ne ${#SUPPORTED_FORMATS} ]; then
  begin_check mpg321
  find_prg mpg321
  mpg321_DECODER=$FOUND_PRG
  if [ -z "$mpg321_DECODER" ]; then
    missing mpg321 "http://sourceforge.net/projects/mpg321"
    end_check
  else
    QueryVersion $($mpg321_DECODER --version 2>&1|head -n 1|cut -d' ' -f3) mpg321 "0.2.3."
    echo
    begin_check $"MP3 description string in $MAGICFILE"
    MPEGS1_1=0
    L1=$($EGREP "^0[[:space:]].*beshort[[:space:]].*\&0xffe0" $MAGICFILE| \
         tr -s '\t' ' '|cut -d' ' -f4)
    if [ -z "$L1" ]; then
      L1=$($EGREP "^0[[:space:]].*beshort[[:space:]].*\&0xfff0" $MAGICFILE| \
           tr -s '\t' ' '|cut -d' ' -f4)
    fi
    L2=$($EGREP "^>1[[:space:]].*byte&0x08[[:space:]].*=0x08" $MAGICFILE| \
         tr -s '\t' ' '|cut -d' ' -f4-|cut -b3-)
    L3_1=$($EGREP "^>>1[[:space:]].*byte[[:space:]].*&0x02" $MAGICFILE| \
         tr -s '\t' ' '|cut -d' ' -f4-|cut -b3-|head -n 1)
    L3_2=$($EGREP "^>>1[[:space:]].*byte[[:space:]].*&0x04" $MAGICFILE| \
           tr -s '\t' ' '|cut -d' ' -f4-|cut -b3-|head -n 1)

    LONGD="${L1}${L2}${L3_1}"
    MPEGS2_1=${#LONGD}
    if [ $MPEGS2_1 -eq 0 ]; then
      #want_feedback "magic file parsing (mpeg criteria)" "Can't find mpeg criteria description in magic file" warning
      #EXTENSIONS=
      #USCITA=$ERR_BAD_MAGICFILE
      #exit $USCITA
      force_trust "mpeg criteria layer 3"
    fi
    LONGD_2="${L1}${L2}${L3_2}"
    MPEGS1_2=0
    MPEGS2_2=${#LONGD_2}
    if [ $MPEGS2_2 -eq 0 ]; then
      #want_feedback "magic file parsing (mpeg criteria)" "Can't find mpeg criteria for layer 2 in magic file" warning
      force_trust "mpeg criteria layer 2"
    fi
    EXTENSIONS="mp3"
    EXTRACT_TIME=" -t -v -k 99999999 "
    BUFFERED_DECODE=" -v -s "     # -b 10000 non funzia in ver 0.2.[23]
    DECODE_TO_STDOUT=" --cdr - " 
    NORMALDECODE=" -v -q "
    OUTPUT_FORMATS_mpg321="wav cdr au"
    TO_wav="--wav"
    TO_cdr="--cdr"
    TO_au="--au"
    TOFMT=$TO_wav
    MPGCMD="wav"
    AUDIO_PLUGINS="$AUDIO_PLUGINS mpg321"
    MPEGCRITERIA=$LONGD
    MPEGCRITERIA_2=$LONGD_2
  
    echo
    lechoc CYAN $"Summary of command used by $mpg321_DECODER:"
    lechoc CYAN $"Extract time:"
    lechoc YELLOW " -> $mpg321_DECODER $EXTRACT_TIME song.mp3"
    lechoc CYAN $"Buffered decode:"
    lechoc YELLOW " -> $mpg321_DECODER $BUFFERED_DECODE song.mp3 | ..."
    lechoc CYAN $"Decoding to stdout:"
    lechoc YELLOW " -> $mpg321_DECODER $DECODE_TO_STDOUT song.mp3 | ..."
    lechoc CYAN $"Normal decoding to wav (default):"
    lechoc YELLOW " -> $mpg321_DECODER $NORMALDECODE $TO_wav file.wav song.mp3"
    lechoc CYAN $"Normal decoding to cdr:"
    lechoc YELLOW " -> $mpg321_DECODER $NORMALDECODE $TO_cdr file.cdr song.mp3"
    lechoc CYAN $"Normal decoding to au:"
    lechoc YELLOW " -> $mpg321_DECODER $NORMALDECODE $TO_au file.au song.mp3"
    lechoc CYAN $"MP3 description string:"
    lechoc YELLOW $" -> MP3 criteria: %s" "$MPEGCRITERIA"
    end_check 
    SUPPORTED_FORMATS="$SUPPORTED_FORMATS $EXTENSIONS"
  fi
fi

