#!/bin/sh

# This file is part of Mkat
#
# Copyright (C) 2004, 2005 Dmitry Maksyoma <ledestin at amur.ru>.
#
# Mkat 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.

#1. obtain audio CD id info
#2. grep audiocd.list for match
#3. report whether CD is in the db
#4. add to collection unless -c option specified

RCFILE=mkatrc 
[ -f /etc/$RCFILE ] && source /etc/$RCFILE
[ -z "$MKAT_LIBPATH" ] && \
  { echo >&2 "MKAT_LIBPATH not set (edit /etc/$RCFILE)"; exit 1; }
. "$MKAT_LIBPATH/helpers.sh" || exit 1
load_config /etc/$RCFILE
load_config ~/.$RCFILE

function usage() {
  echo "  -c		check if the CD is in database"
  echo "  -h, --help	print this help"
  exit $1
}

#process options
case $1 in
  "-c") CHECK_ONLY=1 ;;
  "-h"|"--help") usage 0 ;;
esac

LIST=$LISTDIR/audiocd.list
entry=`cd-discid $DRIVE`
if [ -f $LIST ]; then
  grep "$entry" $LIST > /dev/null
  case $? in
    0) echo -e >&2 "the CD already exists in the database:\n$entry"; exit 1 ;;
    2) echo >&2 "grep error"; exit 1 ;;
    *) echo "the CD isn't in the database" ;;
  esac
else
  > $LIST
fi
[ ! $CHECK_ONLY ] && \
  { echo "adding the CD to the database..."; echo "$entry" >> $LIST; }
