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

cat_variables_madplay () {

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

# Program to only show a time line
MADTIME="$MADTIME"

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

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

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

# Parameters to output to file in aiff format
TO_aiff="$TO_aiff"

# Parameters to output to file in snd format
TO_snd="$TO_snd"

# Parameters to output to file in snd format
TO_raw="$TO_raw"

# 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 madplay
  find_prg madplay
  madplay_DECODER=$FOUND_PRG
  find_prg madtime
  MADTIME=$FOUND_PRG
  if [ -z "$madplay_DECODER" ] || [ -z "$MADTIME" ]; then
    missing madplay "http://www.mars.org/home/rob/proj/mpeg/"
    echoc GREEN "You must remember that 'madtime' program must be also compiled and installed"
    end_check
  else
    QueryVersion $($madplay_DECODER --version 2>&1|head -n 1|cut -d' ' -f4) madplay "0.14.2"
    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
    #else
    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"
    OUTPUT_FORMATS_madplay="wav cdr aiff snd raw"
    TO_wav="wave"
    TO_cdr="cdda"
    TO_aiff="aiff"
    TO_snd="snd"
    TO_raw="raw"
    TOFMT=$TO_wav
    MPGCMD="wav"
    AUDIO_PLUGINS="$AUDIO_PLUGINS madplay"
    MPEGCRITERIA=$LONGD
    MPEGCRITERIA_2=$LONGD_2
    
    echo
    echoc CYAN "Summary of command used by $madplay_DECODER:"
    echoc CYAN "Extract time:"
    echoc YELLOW " -> $MADTIME song.mp3"
    echoc CYAN "Decoding to stdout:"
    echoc YELLOW " -> $madplay_DECODER -v --no-tty-control -o cdda:/dev/stdout song.mp3 | ..."
    echoc CYAN "Normal decoding to wav (default):"
    echoc YELLOW " -> $madplay_DECODER -v --no-tty-control -o $TO_wav:file.wav song.mp3"
    echoc CYAN "Normal decoding to cdr:"
    echoc YELLOW " -> $madplay_DECODER -v --no-tty-control -o $TO_cdr:file.wav song.mp3"
    echoc CYAN "Normal decoding to aiff:"
    echoc YELLOW " -> $madplay_DECODER -v --no-tty-control -o $TO_aiff:file.wav song.mp3"
    echoc CYAN "Normal decoding to snd:"
    echoc YELLOW " -> $madplay_DECODER -v --no-tty-control -o $TO_snd:file.wav song.mp3"
    echoc CYAN "Normal decoding to raw:"
    echoc YELLOW " -> $madplay_DECODER -v --no-tty-control -o $TO_raw:file.wav song.mp3"
    echoc CYAN "MP3 description string:"
    echoc YELLOW " -> MP3 criteria: $MPEGCRITERIA"
    end_check
    SUPPORTED_FORMATS="$SUPPORTED_FORMATS $EXTENSIONS"
  fi
fi

