chiark / gitweb /
gremlin/gremlin.in: Use `https' scheme for Wikipedia link.
[autoys] / flaccrip / flaccrip-check
CommitLineData
583b7e4a
MW
1#! /bin/bash
2
3set -e
4: ${JBDIR=/mnt/jb}
5
6force=nil
7while getopts fo:i: opt; do
8 case "$opt" in
9 o) offset=$OPTARG ;;
10 f) force=t ;;
11 i) id=$OPTARG ;;
12 *) exit 1 ;;
13 esac
14done
15shift $((OPTIND - 1))
16
17dir=$1; shift; cd "$dir"
18case $# in
19 0)
20 set $(ls | sed '
21 /^\([0-9][0-9]\)[-.\ ].*\.flac$/!d
22 s//\1/
23 ')
24 ;;
25esac
26
27case "${offset+t}" in
28 t) ;;
29 *) if [ -r .offset ]; then read offset <.offset; else offset=0; fi ;;
30esac
31
32: ${id=$($JBDIR/bin/flaccrip-discid -a .)}
33ntr=$(echo "$id" | sed 's:^0*\([1-9][0-9]*\)-.*$:\1:')
34ar=$($JBDIR/bin/flaccrip-arfetch $id)
35npress=0
36while read type rest; do
37 case "$type" in
38 H) npress=$((npress + 1)) ;;
39 *) ;;
40 esac
41done <<EOF
42$ar
43EOF
44case $npress,$force in
45 0,nil)
46 echo >&2 "$0: no AccurateRip record found"
47 exit 1
48 ;;
49 *)
50 echo "Found $npress pressings"
51 ;;
52esac
53
54cks="" ntrack=$# ngood=0
55for t in "$@"; do
56 while :; do case "$t" in 0*) t=${t#0} ;; *) break ;; esac; done
57 l=$((t - 1)) h=$((t + 1))
58 tt=$(printf %02d "$t")
59 ll=$(printf %02d "$l")
60 hh=$(printf %02d "$h")
61 flags=""
62 unset before after
63
64 if ((t == 1)); then
65 flags="${flags+$flags }-f"
66 elif [ -f "$ll"[-.\ ]*.flac ]; then
67 before=$(echo "$ll"[-.\ ]*.flac)
68 elif ((offset < 0)); then
69 echo >&2 "$0: warning: -ve offset, but track $l missing; using silence"
70 fi
71
72 if ((t == ntr)); then
73 flags="${flags+$flags }-l"
74 elif [ -f "$hh"[-.\ ]*.flac ]; then
75 after=$(echo "$hh"[-.\ ]*.flac)
76 elif ((offset > 0)); then
77 echo >&2 "$0: warning: +ve offset, but track $h missing; using silence"
78 fi
79
80 ck=$($JBDIR/bin/flaccrip-offset -o"$offset" \
81 $flags ${before+-b "$before"} ${after+-a "$after"} "$tt"[-.\ ]*.flac |
82 $JBDIR/bin/flaccrip-compute)
83
84 press=0 found=nil
85 while read arty art arconf arck; do
86 case "$arty" in
87 H)
88 press=$((press + 1))
89 ;;
90 T)
91 case $art,$arck in
92 $t,$ck)
93 found=t
94 printf "Track %2d (%s): match pressing %d; confidence %d\n" \
95 $t $ck $press $arconf
96 ngood=$((ngood + 1))
97 break
98 ;;
99 esac
100 ;;
101 esac
102 done <<EOF
103$ar
104EOF
105 case $found in
106 nil)
107 printf "Track %2d (%s): NO MATCH\n" $t $ck
108 ;;
109 esac
110done
111
112if ((ngood == ntrack)); then
113 echo "All tracks match: GOOD RIP"
114 exit 0
115elif ((ngood == 0)); then
116 echo "No matches: new pressing, incorrect offset or wrong discid?"
117 exit 2
118else
119 echo "Matched $ngood/$ntrack: time to re-rip :-("
120 exit 3
121fi