#! /bin/bash set -e ###-------------------------------------------------------------------------- ### Table of contents representation. ### ### We need a simple table of contents representation. A toc file consists ### of a number of records, one per line; each record is a number of ### blank-delimited fields. Fields don't contain whitespace, so no quoting ### is needed. The first field of each record is a type which explains how ### to decode the rest. ### ### `T' START Audio track starts at START frames. ### ### `D' START Data track, starts at START frames. ### ### `E' END Leadout at END frames. ###-------------------------------------------------------------------------- ### Command line options. hidden=0 data=none while getopts "d:h:" opt; do case "$opt" in h) hidden=$OPTARG ;; d) data=$OPTARG ;; *) exit 1 ;; esac done shift $((OPTIND - 1)) case "$#" in 1) ;; *) echo >&2 "Usage: $0 [-d DATA] [-h HIDDEN] DIR|CD-DEVICE"; exit 1 ;; esac source=$1 ###-------------------------------------------------------------------------- ### Work out what to do. if [ -d "$source" ]; then ## Intuit what's going on from a directory full of FLAC files. cd "$source" for i in [0-9][0-9][-.\ ]*.flac; do metaflac --show-total-samples "$i" done | { tn=1 tot=$hidden while read samples; do frames=$((samples/588)) echo "T $tot" tot=$((tot + frames)) done case "$data" in none) ;; *) tot=$((tot + 11400)) echo "D $tot" tot=$((tot + data)) ;; esac echo "E $tot" } elif [ -b "$source" ]; then ## Read a table of contents from a CD. wodim dev="$source" -toc | sed ' /^track:/ !d s/^track:\(.*\) lba: *\([0-9][0-9]*\) (.*mode: *\([-0-9][0-9]*\) *$/\1 \3 \2/ ' | while read track mode offset; do case "$track,$mode" in lout,-1) echo "E $offset" ;; *,0 | *,-1) echo "T $offset" ;; *,1 | *,2) echo "D $offset" ;; esac done else echo >&2 "$0: don't know how to read a toc from $source" fi