#!/bin/sh
PATH="$(dirname $0):$PATH"

CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
MYUSER="gutenbach"
MYHOST="zsr.mit.edu"

DISCID=$(cd-discid /dev/cdrom 2>/dev/null) || exit 1

set -- $DISCID

ID="$1"
TRACKS="$2"

mkdir -p /var/cache/gutenbach

CACHEFILE="/var/cache/gutenbach/$ID"

if [ ! -e "$CACHEFILE" ]; then

    CD=$(cddb-tool query "$CDDBURL" 3 "$MYUSER" "$MYHOST" $DISCID) \
	|| exit 2;

    file=$(tempfile)

    cddb-tool read "$CDDBURL" 3 "$MYUSER" "$MYHOST" \
	$(echo "$CD" | head -1 | awk '{print $2 " " $3}') > "$file"  \
	|| exit 3;

    echo "TRACKS=$TRACKS"   >  "$CACHEFILE"
    cddb-tool parse "$file" >> "$CACHEFILE"

    rm "$file"
fi

echo "$CACHEFILE"
exit 0;
