#!/bin/sh
set -e

if [ -z "$CDROM" ];
then
  CDROM=/dev/cdrom
fi

if [ $# = "1" ]
then
	FILENAME="$1"
	
	CDID=`discid $CDROM`
	if [ -z "${CDID}" ]
	then
		echo "Failed to get disc ID from ${CDROM}"
		exit 1
	fi

	FLACID=`flactag --discid "${FILENAME}" | rev | cut -d' ' -f1 | rev`
	if [ -z "${FLACID}" ]
	then
		echo "Failed to get disc ID from FLAC file ${FILENAME}"
		exit 1
	fi

	if [ "$FLACID" != "$CDID" ]
	then
		echo "**********"
		echo "**********"
		echo "**********"
                echo "********** Disc ID of CD:   ${CDID}"
                echo "********** Disc ID of FLAC: ${FLACID} * INVALID *"
                echo "**********"
		echo "********** Disc ID of FLAC doesn't match Disc ID of CD   ***********"
		echo "********** Please re-rip this CD and remove any invalid  ***********"
		echo "********** disc IDs from the MusicBrainz service         ***********"
		echo "**********"
		echo "**********"
		echo "**********"
		exit 2
	else
		echo "IDs match, FLAC file is OK"
		exit 0
	fi
else
	echo "Usage: $0 flacfile"
	exit 1
fi

