| 1 | #! /bin/bash |
| 2 | |
| 3 | set -e |
| 4 | : ${JBDIR=/mnt/jb} |
| 5 | |
| 6 | force=nil |
| 7 | while 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 |
| 14 | done |
| 15 | shift $((OPTIND - 1)) |
| 16 | |
| 17 | dir=$1; shift; cd "$dir" |
| 18 | case $# in |
| 19 | 0) |
| 20 | set $(ls | sed ' |
| 21 | /^\([0-9][0-9]\)[-.\ ].*\.flac$/!d |
| 22 | s//\1/ |
| 23 | ') |
| 24 | ;; |
| 25 | esac |
| 26 | |
| 27 | case "${offset+t}" in |
| 28 | t) ;; |
| 29 | *) if [ -r .offset ]; then read offset <.offset; else offset=0; fi ;; |
| 30 | esac |
| 31 | |
| 32 | : ${id=$($JBDIR/bin/flaccrip-discid -a .)} |
| 33 | ntr=$(echo "$id" | sed 's:^0*\([1-9][0-9]*\)-.*$:\1:') |
| 34 | ar=$($JBDIR/bin/flaccrip-arfetch $id) |
| 35 | npress=0 |
| 36 | while read type rest; do |
| 37 | case "$type" in |
| 38 | H) npress=$((npress + 1)) ;; |
| 39 | *) ;; |
| 40 | esac |
| 41 | done <<EOF |
| 42 | $ar |
| 43 | EOF |
| 44 | case $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 | ;; |
| 52 | esac |
| 53 | |
| 54 | cks="" ntrack=$# ngood=0 |
| 55 | for 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 |
| 104 | EOF |
| 105 | case $found in |
| 106 | nil) |
| 107 | printf "Track %2d (%s): NO MATCH\n" $t $ck |
| 108 | ;; |
| 109 | esac |
| 110 | done |
| 111 | |
| 112 | if ((ngood == ntrack)); then |
| 113 | echo "All tracks match: GOOD RIP" |
| 114 | exit 0 |
| 115 | elif ((ngood == 0)); then |
| 116 | echo "No matches: new pressing, incorrect offset or wrong discid?" |
| 117 | exit 2 |
| 118 | else |
| 119 | echo "Matched $ngood/$ntrack: time to re-rip :-(" |
| 120 | exit 3 |
| 121 | fi |