#
# 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
#
#
# Shared Functions for MP3s:

# 1st parameter = plugin's name
# 2nd parameter = filetype to handle
shared_parse_param_t () {
  eval TOFMT=\$TO_${2}

  if [ -z "$TOFMT" ]; then
    lechoc GREEN $"Type %s is not supported by $1. Using sox's auto-conversion." "$2"
    mp3_OUT_TYPE="$2"
    TOFMT=$TO_wav
  fi

  MPGCMD="$2"
  T_SUPP="$T_SUPP $2"

  return 0
  
}

shared_check_file_type () {
  if [ ! -z "$TRUST_EXT" ]; then
    if [ "$(basename "$tempf" .mp3)" != "$(basename "$tempf")" ]; then
      ISTYPE="mp3"
      FILETYPE="MPEG 1.0 audio stream data"
    fi
  else 
    if [ "${TIPOFILE:$MPEGS1_1:$MPEGS2_1}" = "$MPEGCRITERIA" ]; then
      ISTYPE="mp3"
      FILETYPE="${TIPOFILE:$MPEGS1_1:$MPEGS2_1}"
    elif [ "${TIPOFILE:$MPEGS1_2:$MPEGS2_2}" = "$MPEGCRITERIA_2" ]; then
      ISTYPE="mp3"
      FILETYPE="${TIPOFILE:$MPEGS1_2:$MPEGS2_2}"
    fi
  fi
}

shared_magic_check_info() {
  local RATEFILE=$(echo "$TIPOFILE"|cut -d',' -f3|cut -d' ' -f2)
  local INTRATEF=$(echo "$RATEFILE"|cut -d'.' -f1)
  local DOTRATEF=$(echo "$RATEFILE"|cut -d'.' -f2)

  [ $(echo "$TIPOFILE"|cut -d',' -f4) = mono ] && RET_MONO=1 || RET_MONO=2

  RET_KBITSTEREO=$(echo "$TIPOFILE"|cut -d',' -f2,4|cut -d' ' -f2-)

  if [ "$DOTRATEF" = "$INTRATEF" ]; then
    MULT=1000
  else
    MULT=100
  fi

  # Remove dot (if necessary)
  RATEFILE=${RATEFILE/./}

  RET_RATE=$[ RATEFILE * MULT ]
}
