#
# 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
#
#
# this is multiple_cd plugin

ERR_NO_EJECT=1
ERR_PURGE=2
ERR_NO_WRITE=3

syntax_multiple_cd () {
  echo "  --copies <number>"
  echo "    Makes total <number> of copies of CD"
}

sanity_check_multiple_cd () {
  # devo pretendere --eject, --write, e che non ci sia --purge
  if [ ! -z "$COPIES_CD" ]; then
    if [ -z "$EJECT_CD" ]; then
      STRERROR="This plugin require --eject option!"
      USCITA=$ERR_NO_EJECT
      return
    fi
    if [ ! -z "$DELFILES" ]; then
      STRERROR="This plugin can't be used with --purge option!"
      USCITA=$ERR_PURGE
      return
    fi
    if [ -z "$WRITER" ]; then
      STRERROR="This plugin require -w option!"
      USCITA=$ERR_NO_WRITE
      return
    fi
  fi
}

parse_param_multiple_cd () {
  if [ "$1" = "--copies" ]; then
    if [ $(expr "$2" : ".*[[:digit:]].*") -ne ${#2} ]; then
      STRERR="Bad copy number"
      USCITA=$ERR_BAD_COPY_NUMBER
      return -1
    fi
    if [ "$2" -eq 1 ]; then
      COPIES_CD=
    else
      COPIES_CD=$[ $2 - 1 ]
    fi
    SHIFT=2
    return 1
  fi
}

end_multiple_cd () {
  local count=0
  local PAUSE
  
  if [ ! -z "$COPIES_CD" ]; then
    while [ $count -lt $COPIES_CD ]; do
      lechoc CYAN "----------------------------------------------------------------------"
      lechoc CYAN "Copy #%d" $[ count + 1 ]
      lechoc CYAN -n $"Insert a blank CD-R in mastering device and press ENTER "
      read PAUSE
      writecd "$WRITER" "$OPZDUMMY" "$OPZWRITER" "$WHAT_TO_WRITE"
      if [ $? -ne 0 ]; then
        exit $ERR_BAD_WRITE
      fi
      count=$[ count + 1 ]
    done
  fi
}


