#
# 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 dagrab:
# FORMAT FILE: CD audio track

plugin_present_dagrab () {
  return 0
}

ERR_BAD_DEVICE=1

parse_param_t_dagrab () {
  eval dagrab_TOFMT=\$dagrab_TO_${1}

  if [ -z "$dagrab_TOFMT" ]; then
    lechoc GREEN $"Type %s is not supported by dagrab. Using sox's auto-conversion." "$1"
    dagrab_TOFMT="$1"
    audiocd_OUT_TYPE="$1"
  fi
  
  T_SUPP="$T_SUPP $1"

  return 0
  
}

syntax_dagrab () {
  echo "  cdda:<#trk1,#trk2,#trkS+#trkE...>"
  echo "    Extract audio cd track from /dev/cdrom using dagrab"
  echo "    Can be used a list of track (i.e. 1,2,3) or ranges (i.e. 3+7),"
  echo "    or mixed ranges track (i.e. 1,2,5+7,8,10+12)"
}

parse_param_dagrab () {
  case "$1" in
    cdda:*)
       PLUG="dagrab"
       if [ -z "$DAGRAB_MAX_TRACK" ]; then
         DAGRAB_MAX_TRACK=$($dagrab_DECODER -i|$EGREP \
                         "[[:space:]]*[[:digit:]]+[[:space:]]*audio*" |\
                          tail -n 1|tr -s ' ' |cut -d' ' -f2)
         if [ -z "$DAGRAB_MAX_TRACK" ]; then
           STRERR="There is some problem with CD device"
           USCITA=$ERR_BAD_DEVICE
           return -1
         fi
       fi
       TRACKS=$(echo "$1"|cut -d':' -f2|tr ',' ' ')
       if [ -z "$TRACKS" ]; then
         STRERR="Bad track number(s) $TRACKS"
         USCITA=$ERR_BAD_TRK_NUMBER
         return -1
       fi
       if [ -z "$TRACKS" ]; then
         STRERR="Bad track number(s)"
         USCITA=$ERR_BAD_TRK_NUMBER
         return -1
       fi
       local try_syn=$TRACKS
       try_syn=${try_syn//[[:digit:]]/}
       try_syn=${try_syn//,/}
       try_syn=${try_syn//+/}
       try_syn=${try_syn// /}
       if [ ! -z "$try_syn" ]; then
         STRERR="Bad char in track number(s): $try_syn"
         USCITA=$ERR_BAD_TRK_NUMBER
         return -1
       fi
       if [ "$(expr "$TRACKS" : ".*+.*")" -ne 0 ]; then
         for T in $TRACKS; do
           if [ "$(expr "$T" : ".*+.*")" -ne 0 ]; then
             START_T=$(echo "$T"|cut -d'+' -f1)
             END_T=$(echo "$T"|cut -d'+' -f2)
             ERR_T=$(echo "$T"|cut -d'+' -f3-)
             
             [ -z $END_T ] && END_T=$DAGRAB_MAX_TRACK
             if [ ! -z "$ERR_T" ] || [ -z "$START_T" ] || \
                [ $START_T -gt $END_T ]; then
               STRERR="Bad track number(s) $T"
               USCITA=$ERR_BAD_TRK_NUMBER
               return -1
             fi
             while [ $START_T -le $END_T ]; do
               NEWTRACK="$NEWTRACK $START_T"
               START_T=$[ START_T + 1 ]
             done
           else
             NEWTRACK="$NEWTRACK $T"
           fi
         done
         TRACKS="$NEWTRACK"
       fi
       (
       for TRK in $TRACKS; do
         TRKNUM=$(printf "%.2d" "$TRK")
         echo "$TEMPDIR/$TRKNUM.audiocd">>"$TEMPDATA"
         touch "$TEMPDIR/$TRKNUM.audiocd"
       done
       )
       SHIFT=1
       return 1
      ;;
   esac
}

sanity_check_dagrab () {
  if [ ! -z "$TRACKS" ]; then
    #if [ ! -z "$ALVOLO" ] || [ ! -z "$ONE_BY_ONE" ]; then
      # DA VEDERE COME FARE!!
      #if [ $(expr "$CDRECORD" : ".*$DAGRAB_DEVICE.*") -eq ${#CDRECORD} ]; then
      #  STRERROR="Can't read and write at the same time on the same CD-R device!"
      #  USCITA=$ERR_BAD_DEVICE
      # return
      #fi
    #fi
    DAGRAB_TMPFILE="$TEMPDIR/cdda-tmpfile.$$"
    $dagrab_DECODER -v -i 2>&1|$EGREP \
           "[[:space:]]*[[:digit:]]+[[:space:]]*audio*">$DAGRAB_TMPFILE 2>&1
    if [ $? -ne 0 ]; then
      STRERROR="There is some problem with dagrab on device /dev/cdrom"
      USCITA=1
    fi
  fi
}

extract_time_audiocd () {
  AUDIO_TRACK="$(basename $1 .audiocd)"

  TS=$(cat $DAGRAB_TMPFILE|head -n $AUDIO_TRACK|tail -1|tr -s ' '|cut -d' ' -f6)
  
  if [ -z "$TS" ]; then
    RET_WCL=1
  else
    if [ ${TS:1:1} = ":" ]; then
      TS="0$TS"
    fi
    RET_WCL=0
  fi
}

extract_check_info_audiocd () {
  RET_KBITSTEREO="128Kb/s, stereo"
  RET_RATE="44100"
}

check_file_type_dagrab () {
  if [ "$(basename "$tempf" .audiocd)" != "$(basename "$tempf")" ]; then
    if [ -z "$WRITER" ]; then
      lechoc GREEN "WARNING: CD audio tracks ignored without -w option!"
    else
      ISTYPE="audiocd"
      FILETYPE="CD audio track"
    fi
  fi
}

setup_vars_audiocd () {
  TRACK="$(echo "$nomef"|cut -d'-' -f2)"
  OUTPUT_FILE="$SPOOLDIR/${nomef}-audiocd.${dagrab_TOFMT}"
  FLYFMT="wav"
}

to_stdout_buffered_audiocd () {
  to_stdout_no_buffer_audiocd
}

to_stdout_no_buffer_audiocd () {
  $dagrab_DECODER $DAGRAB_OPT "$TRACK" -f -
}

decode_audiocd () {
  $dagrab_DECODER $DAGRAB_OPT -v "$TRACK" -f "$OUTPUT_FILE"
}

