#!/bin/sh -u # # Submit a CD description to the FreeDB CD Database. # Also write a cache entry into ~/.cddb for use by programs. # - Cameron Simpson 25aug2001 # : ${TMPDIR:=/tmp} : ${EDITOR:=vi} cmd=`basename "$0"` version=1.0 cachedir=$HOME/.cddb categories='blues classical country data folk jazz misc newage raggae rock soundtrack' realsubmit=freedb-submit@freedb.org testsubmit=test-submit@freedb.org submitto=$realsubmit cachesave=1 doedit= note= usage="Usage: $cmd [-entf] [-m addr] category \"category\" one of: $categories -e Invoke editor to enter information. -n No action. Emit submission email on standard output. Don't make cache entry. -t Test submit. Submit to the test submission address. Don't make cache entry. -f FreeDB submit. Submit to the real submission address. -m addr Submit to the mail address addr. Default: $submitto Valid: $testsubmit, $realsubmit" badopts= while [ $# -gt 0 ] do case $1 in -e) doedit=1 ;; -n) submitto= cachesave= ;; -t) submitto=$testsubmit cachesave= ;; -f) submitto=$realsubmit cachesave=1;; -m) submitto=$2; shift ;; --) shift; break ;; -?*) echo "$cmd: unrecognised option: $1" >&2 badopts=1 ;; *) break ;; esac shift done if [ $# = 0 ] then echo "$cmd: missing category" >&2 badopts=1 else category=$1; shift case " $categories " in *" $category "*) ;; *) echo "$cmd: unsupported category \"$category\"; I know: $categories" >&2 badopts=1 ;; esac fi [ $# = 0 ] || { echo "$cmd: extra arguments: $*" >&2; badopts=1; } [ $badopts ] && { echo "$usage" >&2; exit 2; } xit=0 desc=$TMPDIR/cdsub$$d tmp=$TMPDIR/cdsub$$t cddb=$TMPDIR/cdsub$$c trap 'rm -f $desc $cddb $tmp' 0 trap 'rm -f $desc $cddb $tmp; exit 1' 1 15 # gather basic disc info cddiscinfo ${CDRIP_DEV:-/dev/cdrom} >"$tmp" || exit 1 # save stdin, read from cddiscinfo exec 3<&0 0<"$tmp" read discid || { echo "$cmd: no discid" >&2; exit 1; } case "$discid" in [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ;; *) echo "$cmd: invalid discid \"$discid\"" >&2; exit 1 ;; esac read ntracks || { echo "$cmd: no track count" >&2; exit 1; } # stash offsets in $@ totallen=0 discidqry= while read tn len offset do set ${1+"$@"} "$offset" totallen=`expr $totallen + $len` [ $# = $tn ] || { echo "$cmd: bad track number $tn: expected $#" >&2 exit 1 } done # restore stdin exec 0<&3 3<&- # construct description file if [ $doedit ] then ( echo "Discid: $discid" echo "Artist: " echo "Title: " echo "Year: " tn=1 for offset do echo "Track #$tn: " tn=`expr $tn + 1` done ) >"$desc" # edit file and then clean it up for easy parsing $EDITOR "$desc" || { echo "$cmd: edit fails, aborting" >&2; exit 1; } else ( echo "Discid: $discid" >"$desc" necho "Artist: " >&2; read -r artist || exit 1 echo "Artist: $artist" necho "Title: " >&2; read -r title || exit 1 echo "Title: $title" necho "Year: " >&2; read -r dyear || exit 1 echo "Year: $dyear" tn=1 for offset do while : do necho "Track #$tn: " >&2; read -r ttitle || exit 1 if [ -n "$ttitle" ] then echo "track #$tn: $ttitle" tn=`expr $tn + 1` break else echo "Each track must have a title." >&2 fi done done ) >"$desc" \ || exit 1 echo >&2 fi # strip header tags sed '1,/^$/s/^[^:]*: *//' "$desc" >"$tmp" || exit 1 # load data from file # and construct CDDB entry exec 3<&0 0<"$tmp" 4>&1 1>"$cddb" ok=1 # start the CDDB stuff echo "# xmcd CD database file generated by $cmd" echo "#" echo "# Track frame offsets:" for offset do echo "# $offset" done echo "#" echo "# Disc length: $totallen seconds" echo "#" echo "# Revision: 0" echo "# Submitted via: $cmd $version by Cameron Simpson " echo "#" read -r dummydiscid || exit 1 [ "x$discid" = "x$dummydiscid" ] || { echo "$cmd: discid damaged" >&2; exit 1; } echo "Discid: $discid" >&2 echo "DISCID=$discid" read -r artist || exit 1 echo "Artist: $artist" >&2 [ -n "$artist" ] || { echo "Artist may not be empty." >&2; ok=; } read -r title || exit 1 echo "Title: $title" >&2 [ -n "$title" ] || { echo "Title may not be empty." >&2; ok=; } echo "DTITLE=$artist / $title" read -r dyear || exit 1 echo "Year: $dyear" >&2 echo "DYEAR=$dyear" n=0 tn=1 for offset do read -r ttitle || exit 1 echo "Track #$tn: $ttitle" >&2 if [ -n "$ttitle" ] then echo "TTITLE$n=$ttitle" n=`expr $n + 1` tn=`expr $tn + 1` else echo "Each track must have a title." >&2 ok= fi done # anything else on the input is extended data # NB: this used to be a plain sed invocation: # sed -n '/[^ ]/s/^/EXTD=/p' # but there's a great bug in GNU sed # where they've stuffed up mmap()ed I/O # and lose the read pointer :-( # Bug report and patch submitted! # But a sullen silence from the maintainers :-( needEXTD=1 while read -r line do echo "EXTD=$line" needEXTD= done [ $needEXTD ] && echo EXTD= n=0 for offset do echo "EXTT$n=" n=`expr $n + 1` done echo "PLAYORDER=" # restore stdin and stdout exec 0<&3 3<&- 1>&4 4>&- [ $ok ] || exit 1 # sanity check ask "Is the above all ok" \ || { echo "$cmd: aborting, cddb saved in $cddb, desc saved in $desc" >&2 exit 1 } if ask "Edit the cddb record before submission" then $EDITOR "$cddb" || { echo "$cmd: edit fails, aborting" >&2; exit 1; } ask "Still ok" \ || { echo "$cmd: aborting, cddb saved in $cddb, desc saved in $desc" >&2 exit 1 } fi if [ $cachesave ] then pathdir=$cachedir/$category path=$pathdir/$discid if [ -d "$pathdir/." ] || mkdir -p "$pathdir" then if [ -s "$path" ] then echo "$cmd: already exists: $path" >&2 xit=1 else (set -x; cp "$cddb" "$path") || xit=1 fi else echo "$cmd: can't mkdir $pathdir, no cache entry made" >&2 xit=1 fi fi ( (set -x; echo "From: $NAME <$EMAIL>") (set -x; echo "To: $submitto") (set -x; echo "Subject: cddb $category $discid") [ -n "$note" ] && echo "X-Cddbd-Note: $note" ## echo "MIME-Version: 1.0" ## echo "Content-Transfer-Encoding: quoted-printable" ## echo "Content-Type: text/plain; charset=ISO-8859-1" echo cat "$cddb" ## quoted-printable <"$cddb" ) | if [ -n "$submitto" ] then (set -x; sendmail -oi "$submitto") else exec cat fi [ $? = 0 ] || xit=1