#
# 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 ogg123:
# FORMAT FILE: ogg

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

plugin_present_ogg123 () {
  return 0
}

parse_param_t_ogg123 () {
  eval OGG_TOFMT=\$OGG_TO_${1}
   
  if [ -z "$OGG_TOFMT" ]; then
    lechoc GREEN "Type %s is not supported by ogg123. Using sox's auto-conversion." "$1"
    ogg_OUT_TYPE="$1"
    #OGG_TOFMT="$OGG_TO_wav"
  fi

  OGG_MPGCMD="$1"
  T_SUPP="$T_SUPP $1"

  return 0
}

extract_time_ogg () {
  $ogg123_DECODER -v -d null -k 9999999 "$1" >$SONGDATA 2>&1
  TS=$(cat $SONGDATA|$GREP Time|cut -d' ' -f2)
  RET_WCL=0 # TODO: vedere quando c' un errore nel file...
}

extract_check_info_ogg() {
  RET_MONO=2
  local DATI=$($ogg123_DECODER -v -d null -k 9999999 "$i" 2>&1|$GREP "Bitstream")

  RET_RATE=$(echo $DATI|tr -d ' '|cut -d',' -f2|cut -d'H' -f1)
  RET_KBITSTEREO="Variable"
  if [ $(echo $DATI|cut -d ' ' -f 3) -ne 2 ]; then
    RET_MONO=1
  fi
}

check_file_type_ogg123 () {
  if [ ! -z "$TRUST_EXT" ]; then
    if [ "$(basename "$tempf" .ogg)" != "$(basename "$tempf")" ]; then
      ISTYPE="ogg"
      FILETYPE="Ogg/Vorbis"
    fi
  else
    if [ "$(strings "$i"|head -c 3)" = "Ogg" ]; then
      ISTYPE="ogg"
      FILETYPE="Ogg/Vorbis"
    fi
  fi
}

setup_vars_ogg() {
  FLYFMT="raw"
  OUTPUT_FILE="$SPOOLDIR/$nomef.$OGG_MPGCMD"
}

to_stdout_buffered_ogg () {
  $ogg123_DECODER $OGG_BUFFERED_DECODE "$1"
}

to_stdout_no_buffer_ogg () {
  $ogg123_DECODER $OGG_DECODE_TO_STDOUT "$1"
}

decode_ogg () {
  if [ "$OGG_MPGCMD" = "raw" ]; then
    $ogg123_DECODER $OGG_NORMALDECODE "$i" $OGG_TOFMT >"$OUTPUT_FILE"
  else
    $ogg123_DECODER $OGG_NORMALDECODE $OGG_TOFMT"$OUTPUT_FILE" "$i"
  fi
}
