]> diplodocus.org Git - flac-archive/blob - fa-tags
Rename, since i'm about to use some GNU make features.
[flac-archive] / fa-tags
1 #! /usr/bin/env zsh
2
3 # $Id$
4
5 # Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
6 # Copyright (c) 2003-2004 Jesus Climent <jesus.climent@hispalinux.es>
7 # Copyright (c) 2004 Eric Gillespie <epg@pretzelnet.org>
8 # This code is hereby licensed for public consumption under either the
9 # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
10
11 set -e
12
13 if [[ -z ${CDDBURL} ]]; then
14 CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
15 fi
16
17 get_cddb_protocol () {
18 for CDDBPROTO in 5 4 3; do
19 if [[ \
20 $(cddb-tool stat $CDDBURL $USER $HOST $CDDBPROTO \
21 | sed 's/^\([0-9][0-9]*\).*/\1/;q') = '210' ]]; then
22 return 0
23 fi
24 done
25
26 echo "${CDDBURL} does not work"
27 exit 2
28 }
29
30 handle_read=$(<<EOF
31 function rstrip_print(_s) {
32 sub(/[[:space:]]*\$/, "", _s)
33 print(_s)
34 }
35
36 BEGIN {
37 FS="="
38 }
39
40 /^DTITLE=/ {
41 split(\$2, a, /[[:blank:]]*\/[[:blank:]]*/)
42 rstrip_print("ARTIST=" a[1])
43 rstrip_print("ALBUM=" a[2])
44 }
45
46 /^DYEAR=/ {
47 rstrip_print("DATE=" \$2)
48 }
49
50 /^TTITLE[[:digit:]]+/ {
51 rstrip_print("TITLE=" \$2)
52 }
53 EOF
54 )
55
56 perl_handle_read=$(<<EOF
57 (\$tag, \$value) = split(/=/, \$_, 2);
58
59 if (/^DTITLE=/) {
60 (\$artist, \$album) = split(/\\s*\\/\\s*/, \$value);
61 print("ARTIST=\$artist\\n");
62 print("ALBUM=\$album\\n");
63
64 } elsif (/^DYEAR=/) {
65 print("DATE=\$value\\n");
66
67 } elsif (/^TTITLE\\d+/) {
68 print("TITLE=\$value\\n");
69 }
70 EOF
71 )
72
73 handle_queries () {
74 local F
75 local i=1
76
77 # First eat the status line.
78 read F
79
80 while read -A F; do
81 [[ $F = '.' ]] && break
82 cddb-tool read $CDDBURL $CDDBPROTO $USER $HOST $F[1] $F[2] \
83 | nawk $handle_read > candidate-tags-$i
84 # XXX The awk script has at least one problem; since we can't split to
85 # exactly two components, we break on track names with equal signs.
86 # Use Perl instead:
87 # cddb-tool read $CDDBURL $CDDBPROTO $USER $HOST $F[1] $F[2] \
88 # | perl -lne $handle_read > candidate-tags-$i
89 i=$(( $i + 1 ))
90 done
91 }
92
93 get_cddb_protocol
94 (cddb-tool query $CDDBURL $CDDBPROTO $USER $HOST "$@" | handle_queries) &
95
96 cat > candidate-tags-0 <<EOF
97 ARTIST=
98 ALBUM=
99 DATE=
100 EOF
101 for (( i = 1; i <= $2; i++ )); do
102 echo 'TITLE=' >> candidate-tags-0
103 done