gremlin/: Start on making it be a proper built thing.
[autoys] / flaccrip / flaccrip-toc
1 #! /bin/bash
2
3 set -e
4
5 ###--------------------------------------------------------------------------
6 ### Table of contents representation.
7 ###
8 ### We need a simple table of contents representation. A toc file consists
9 ### of a number of records, one per line; each record is a number of
10 ### blank-delimited fields. Fields don't contain whitespace, so no quoting
11 ### is needed. The first field of each record is a type which explains how
12 ### to decode the rest.
13 ###
14 ### `T' START Audio track starts at START frames.
15 ###
16 ### `D' START Data track, starts at START frames.
17 ###
18 ### `E' END Leadout at END frames.
19
20 ###--------------------------------------------------------------------------
21 ### Command line options.
22
23 hidden=0
24 data=none
25 while getopts "d:h:" opt; do
26 case "$opt" in
27 h) hidden=$OPTARG ;;
28 d) data=$OPTARG ;;
29 *) exit 1 ;;
30 esac
31 done
32 shift $((OPTIND - 1))
33
34 case "$#" in
35 1) ;;
36 *) echo >&2 "Usage: $0 [-d DATA] [-h HIDDEN] DIR|CD-DEVICE"; exit 1 ;;
37 esac
38 source=$1
39
40 ###--------------------------------------------------------------------------
41 ### Work out what to do.
42
43 if [ -d "$source" ]; then
44
45 ## Intuit what's going on from a directory full of FLAC files.
46 cd "$source"
47 for i in [0-9][0-9][-.\ ]*.flac; do
48 metaflac --show-total-samples "$i"
49 done | {
50 tn=1 tot=$hidden
51 while read samples; do
52 frames=$((samples/588))
53 echo "T $tot"
54 tot=$((tot + frames))
55 done
56 case "$data" in
57 none)
58 ;;
59 *)
60 tot=$((tot + 11400))
61 echo "D $tot"
62 tot=$((tot + data))
63 ;;
64 esac
65 echo "E $tot"
66 }
67
68 elif [ -b "$source" ]; then
69
70 ## Read a table of contents from a CD.
71 wodim dev="$source" -toc | sed '
72 /^track:/ !d
73 s/^track:\(.*\) lba: *\([0-9][0-9]*\) (.*mode: *\([-0-9][0-9]*\) *$/\1 \3 \2/
74 ' | while read track mode offset; do
75 case "$track,$mode" in
76 lout,-1) echo "E $offset" ;;
77 *,0 | *,-1) echo "T $offset" ;;
78 *,1 | *,2) echo "D $offset" ;;
79 esac
80 done
81
82 else
83 echo >&2 "$0: don't know how to read a toc from $source"
84 fi