#!/usr/bin/awk -f
# Heiko Purnhagen <purnhage@tnt.uni-hannover.de>
# HP 20030508
#
# Convert cdrdao toc file to a script encoding all tracks as e.g. mp3 files
# from CD image file.
#
# usage:
#  toc2plax-trk FILE=title ENC="mp3enc -r" image.toc > image.enc
#  source image.enc

BEGIN {
  track = 0;
  printf "#!/bin/sh\n";
  FILE = "dflt";
  ENC = "mp3enc -r -h -v -V 3 -m j -";
}

{
  if ($1 == "FILE") {
    track++;
    filename = substr($2,2,length($2)-2);
    split($3,timestr,":");
    if (timestr[2]=="")
      sta = timestr[1]/44100;
    else
      sta = timestr[1]*60+timestr[2]*1+timestr[3]/75;
    split($4,timestr,":");
    if (timestr[2]=="")
      dur = timestr[1]/44100;
    else
      dur = timestr[1]*60+timestr[2]*1+timestr[3]/75;
    printf "plax -ts %8.3f -td %8.3f -f integer16,0,44100,big,2,1 %s -of be -o - | %s %s%02d%s\n",\
      sta,dur,filename,ENC,FILE "_",track,".mp3";
  }
}

