#
# 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
#
#
# cdl-0.03 plugin

syntax_cdl () {
  echo "  --cdl"
  echo "    Makes a CD cover with cdl-0.03"
  echo "  --save-itemsfile"
  echo "    Don't remove items file (will be something like CD-cover_$$.txt)"
  echo "  --out-file <output_file>"
  echo "    Postscript output file"
  echo "    Default filename: $DEF_CDL_FILENAME"
}

parse_param_cdl () {
  case "$1" in
    --cdl)
          CDL=$DEF_CDL
          #CDL_SAVE_ITEMSFILE=""
          CDL_FILENAME=$DEF_CDL_FILENAME
          SHIFT=1
          return 1
          ;;
    --save-itemsfile)
          CDL_SAVE_ITEMSFILE=1
          SHIFT=1
          return 1
          ;;
    --out-file)
          if [ -z "$2" ] || [ "${2:0:1}" = "-" ]; then
            STRERR="No valid output file specified!"
            return -1
          else
            if [ -r "$2" ]; then
              echoc GREEN "WARNING: $2 already exist. Overwriting..."
            fi
            CDL_FILENAME="$2"
            SHIFT=2
          fi
          return 1
          ;;
  esac
}

end_cdl () {

  if [ -z "$CDL" ]; then
    return
  fi
  echo
  echoc CYAN "Making cover..."
  ITEMSFILE="CD-cover_$$.txt"
  (
  CDL_TEMP="cdl_tempfile-$$"

  echoc CYAN -n "Now will edit items file $ITEMSFILE with vi. Press ENTER"
  
  read X
  echo "Title: ">$ITEMSFILE
  echo "Artist: ">>$ITEMSFILE
  echo "Note: ">>$ITEMSFILE
  echo >>$ITEMSFILE
  
  count=0
  for i in $WROTE; do
  title=${i:3}
  title=${title/_/}
  echo "$title::${TIME[$count]}">>$ITEMSFILE
  count=$[ count + 1 ]
  done
  vi $ITEMSFILE

  ACTDIR=$PWD
  cd $CDL_DIR
  $CDL -c 1 -s <$ACTDIR/$ITEMSFILE >$CDL_TEMP.tex
  echoc CYAN "Running LaTeX..."
  latex $CDL_TEMP.tex 1>/dev/null
  echoc CYAN "Running dvips..."
  dvips $CDL_TEMP -o $ACTDIR/$CDL_FILENAME 2>/dev/null
  rm -f $CDL_TEMP.*
  )
  if [ -z $CDL_SAVE_ITEMSFILE ]; then
    rm -f $ITEMSFILE
  fi
  echoc CYAN "Done"
  echo
}

