#!/usr/bin/awk -f
# HP 20000212
# usage:
#  dao2toc FILE=king1_44.wav TIME=2719.4 king1_48.dao > king1_44.toc
#  dao2toc FILE=king2_44.wav TIME=2872.9 king2_48.dao > king2_44.toc

function printidx (sta, dur) {
  if (dur>0) {
    track++;
    printf "\n// Track %d\n",track;
    printf "TRACK AUDIO\n";
    printf "NO COPY\n";
    printf "PRE_EMPHASIS\n";
    printf "TWO_CHANNEL_AUDIO\n";
    stafrm = int(sta*75);
    durfrm = int(dur*75);
    printf "FILE \"%s\" %02d:%02d:%02d %02d:%02d:%02d\n",FILE,\
      stafrm/4500,(stafrm/75)%60,stafrm%75,\
      durfrm/4500,(durfrm/75)%60,durfrm%75;
    printf "START 00:00:00\n\n";
  }
}

BEGIN {
  last = 0;
  track = 0;
  printf "CD_DA\n\n";
}

{
  if ($1 == "INDEX") {
    split($3,timestr,":");
    time = timestr[1]*60+timestr[2]*1+timestr[3]/75;
    printidx(last,time-last);
    last = time;
  }
}

END {
  printidx(last,TIME-last);
}


