gremlin/: Start on making it be a proper built thing.
[autoys] / flaccrip / flaccrip-guessoffset
1 #! /bin/bash
2
3 set -e
4 : ${JBDIR=/mnt/jb}
5
6 while getopts i: opt; do
7 case "$opt" in
8 i) id=$OPTARG ;;
9 *) exit 1 ;;
10 esac
11 done
12 shift $((OPTIND - 1))
13
14 dir=$1; shift; cd "$dir"
15
16 case $# in
17 0)
18 set $(ls | sed '
19 /^\([0-9][0-9]\)[-. ].*\.flac$/!d
20 s//\1/
21 ')
22 ;;
23 esac
24
25 : ${id=$($JBDIR/bin/flaccrip-discid -a .)}
26 ntr=$(echo "$id" | sed 's:^0*\([1-9][0-9]*\)-.*$:\1:')
27 ar=$($JBDIR/bin/flaccrip-arfetch $id)
28 npress=0
29 while read type rest; do
30 case "$type" in
31 H) npress=$((npress + 1)) ;;
32 *) ;;
33 esac
34 done <<EOF
35 $ar
36 EOF
37 case $npress in
38 0)
39 echo >&2 "$0: no AccurateRip record found: this isn't going to work"
40 exit 1
41 ;;
42 *)
43 echo "Found $npress pressings"
44 ;;
45 esac
46
47 for t in "$@"; do
48 while :; do case "$t" in 0*) t=${t#0} ;; *) break ;; esac; done
49 l=$((t - 1)) h=$((t + 1))
50 tt=$(printf %02d "$t")
51 ll=$(printf %02d "$l")
52 hh=$(printf %02d "$h")
53 flags=""
54 unset before after
55
56 if ((t == 1)); then
57 flags="${flags+$flags }-f"
58 elif [ -f "$ll"[-.\ ]*.flac ]; then
59 before=$(echo "$ll"[-.\ ]*.flac)
60 else
61 echo >&2 "$0: warning: track $l missing; using silence"
62 fi
63
64 if ((t == ntr)); then
65 flags="${flags+$flags }-l"
66 elif [ -f "$hh"[-.\ ]*.flac ]; then
67 after=$(echo "$hh"[-.\ ]*.flac)
68 else
69 echo >&2 "$0: warning: track $h missing; using silence"
70 fi
71
72 echo "T $t"
73
74 cks=""
75 while read arty art arconf arck; do
76 case "$arty,$art" in
77 T,$t)
78 cks="${cks:+$cks }$arck"
79 echo "C $arck $arconf"
80 ;;
81 esac
82 done <<EOF
83 $ar
84 EOF
85 $JBDIR/bin/flaccrip-trackoffsets \
86 $flags ${before+-b "$before"} ${after+-a "$after"} "$tt"[-.\ ]*.flac $cks
87
88 done | awk '
89 BEGIN {
90
91 }
92
93 $1 == "T" {
94 delete CK;
95 }
96
97 $1 == "C" {
98 CK[$2] = $3;
99 }
100
101 $1 == "M" {
102 m = $2; ck = $3;
103 c = CK[ck];
104 MM[m]++;
105 if (!(m in MC) || MC[m] > c) MC[m] = c;
106 }
107
108 END {
109 for (m in MM)
110 print m " " MM[m] " " MC[m];
111 }' |
112 sort -rn -k2 -k3