#
# 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
#
#
# Plugin atip

atip_cd() {
  local ATIPTOT=$($ATIP 2>&1|$GREP "lead out"|cut -d'(' -f2)
  if [ -z "$ATIPTOT" ]; then
    lechoc STDERR "Could'nt read ATIP from CD! Perhaps CD in not in the writer?"
    USCITA=$ERR_NO_ATIP; exit $USCITA
  fi
  #ATIPTOT="74:45/00)"
  #ATIPTOT=" 9:00/00)"
  ATIPMIN=${ATIPTOT:0:2}
  ATIPSEC=${ATIPTOT:3:2}
}

syntax_atip () {
  echo  "  -a, --atip"
  echo $"    Read ATIP data from cd in the mastering device and compares the"
  echo $"    lead out time with the duration of all audio files."
  echo $"    (Implies -w)"
}

parse_param_atip () {
  if [ "$1" = "--atip" ] || [ "$1" = "-a" ]; then
    SHIFT=1
    if [ "$USER" = "$PRIVUSER" ]; then
      ATIP=$DEF_ATIP
      return 1
    else
      STRERR="--atip option is only for $PRIVUSER!"
      USCITA=$ERR_BAD_PARAM
      return -1
    fi
  fi
}

sanity_check_atip () {
  if [ ! -z "$ATIP" ] && [ -z "$WRITER" ]; then
    STRERROR="Option --atip implies -w!"
    USCITA=1
    return 1
  fi
}

end_atip () {
  if [ ! -z "$ATIP" ]; then
    atip_cd
    [ $ATIPSEC -gt $TOTSEC ] && SEC=$[ ATIPSEC - TOTSEC ] || SEC=$[ TOTSEC - ATIPSEC ]
    if [ $TOTMIN -gt $ATIPMIN ]; then
      lechoc STDERR $"Songs don't fit on CD!" 
      lechoc STDERR $"Total song time exceed maximum time recordable on this CD"
      lechoc STDERR $"Time exceeding: %0.2d minutes" "$[ TOTMIN - ATIPMIN ]"
      USCITA=$ERR_OVERTIME; exit $USCITA
    fi
    if [ $TOTMIN -eq $ATIPMIN ] && [ $TOTSEC -gt $ATIPSEC ]; then
      lechoc GREEN $"Songs don't fit on CD!"
      lechoc GREEN $"Total song time exceed maximum time recordable on this CD"
      lechoc GREEN $"Time exceeding: %0.2d seconds" "$SEC"
      echo ""
      lechoc GREEN $"Some CD writer may overburn"
      quest $"Did you want to try to record this cd?" $"Songs will be converted" $"You must edit your playlist!" 1
      OVRBURN=$?
      if [ $OVRBURN -eq 0 ]; then
        USCITA=$ERR_OVERTIME; exit $USCITA
      else
        SEC="-$SEC"
      fi
    fi
    lechoc CYAN $"Total time for cd    : %0.2d:%0.2d" "$ATIPMIN" "$ATIPSEC"
    lechoc CYAN $"Time remaining on cd : %0.2d:%0.2d" "$[ ATIPMIN - TOTMIN ]" "$SEC"
  fi
}

