#
# 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
#
#
# Functions for mpg321:
# FORMAT FILE: mp3

# DEPENDENCIES:
plugin_present_sox 2>/dev/null
if [ $? -ne 0 ]; then
  # This plugin require sox plugin!
  . $PLUGINDIR/general/sox
fi 

# Shared functions about mp3s
. $PLUGINDIR/general/mp3funcs

plugin_present_mpg321 () {
  return 0
}

parse_param_t_mpg321 () {
  shared_parse_param_t "mpg321"
  return 0
}

extract_time_mp3 () {
  $mpg321_DECODER -t -v -k 99999999 "$1" >$SONGDATA 2>&1
  TS=$(cat $SONGDATA|$GREP "Decoding of"|cut -d' ' -f1)
  TS=${TS/\[/}
  TS=${TS/\]/}
  # Devo aggiungere un eventuale 0 avanti...
  if [ ${TS:1:1} = ":" ]; then
    TS="0$TS"
  fi
  TS="$TS:00"
  # TODO: Method to understand if mp3 has some error...
  RET_WCL=0
}

extract_check_info_mp3() {
  RET_MONO=2
  if [ ! -z "$TRUST_EXT" ]; then
    $mpg321_DECODER -t -v -k 99999999 "$i" >$SONGDATA 2>&1
    RET_RATE=$(cat $SONGDATA|grep -w 'rate'|cut -d',' -f2|cut -d':' -f2|tr -d ' ')
    RET_KBITSTEREO=$(cat $SONGDATA|grep -w 'Kbits/s'|cut -d':' -f2|cut -d',' -f1)
    RET_KBITSTEREO="$RET_KBITSTEREO, $(cat $SONGDATA|grep -w 'mode:'|\
                    cut -d',' -f4|cut -d':' -f2)"
    if [ $(cat $SONGDATA|grep channels|cut -d',' -f 4|cut -d':' -f2) -ne 2 ]; then
      RET_MONO=1
    fi
    return
  fi
  shared_magic_check_info
}

check_file_type_mpg321 () {
  shared_check_file_type
}

setup_vars_mp3 () {
  FLYFMT="RAW"
  OUTPUT_FILE="$SPOOLDIR/$nomef.$MPGCMD"
}

to_stdout_buffered_mp3 () {
  $mpg321_DECODER $BUFFERED_DECODE "$1"
}

to_stdout_no_buffer_mp3 () {
  $mpg321_DECODER $DECODE_TO_STDOUT "$1"
}

decode_mp3 () {
  $mpg321_DECODER $NORMALDECODE $TOFMT "$OUTPUT_FILE" "$i"
}
