#!/usr/bin/perl -w
#
# assemble output from gramofile into some oggs

# datafile format:
#
# # is comment
# Author: specifies author
# Genera: specifies genre
# Album: specifies album
# Inputprefix: input file prefix (defaults to 'processed')
# anything else is, in 3 tab-delimited columns:
#  <track> <name> <filename, sans .ogg>
# blanklines are kinda handled





# default value
$inputprefix = 'processed';


while ($line = <>) {
  chomp $line;

  if ($line =~ /^\#/) {
    # Comment
    next;
  } elsif ($line =~ /^ *$/) {
    # blankline
    next;
  } elsif ($line =~ /^Author: (.*)$/ ) {
    # Author
    $author = $1;
    next;
  } elsif ($line =~ /^Genera: (.*)$/ ) {
    # Genera
    $genre = $1;
    next;
  } elsif ($line =~ /^Inputprefix: (.*)$/) {
    # Input file prefix
    $inputprefix = $1;
    next;
  } elsif ($line =~ /^Album: (.*)$/ ) {
    # Album
    $album = $1;
    next;
  } else {
    # And it's data!
    my @l = split /\t/, $line;
    push @data, $l[0];
    push @data, $l[1];
    push @data, $l[2];
    next;
  }
}

# Assemble the static part of the cmdline
push @cmdline, '/usr/bin/oggenc';
if (defined $author) {
  push @cmdline, '-a';
  push @cmdline, $author;
}
if (defined $genre) {
  push @cmdline, '-G';
  push @cmdline, $genre;
}
if (defined $album) {
  push @cmdline, '-l';
  push @cmdline, $album;
}

while ($num = shift @data) {
  my $name = shift @data;
  my $filename = shift @data;

  # assemble a cmdline
  # No real input sanitization is performed, but then, no untrusted
  # data is expected.
  @cmd = @cmdline;
  push @cmd, '-N';
  push @cmd, $num;
  push @cmd, '-t';
  push @cmd, $name;
  push @cmd, '-o';
  push @cmd, $filename . '.ogg';
  push @cmd, $inputprefix . $num . '.wav';

  #system @cmd;
  print join (' ', @cmd), "\n";

}


# leftover cruft
  #system ("/usr/bin/oggenc", "-a", $author, "-G", $genre, "-l", $album, "-N", $num, "-t", $name, "-o", $filename . ".ogg", "processed" . $num . ".wav");
  #print join (' ', ("/usr/bin/oggenc", "-a", $author, "-G", $genre, "-l", $album, "-N", $num, "-t", $name, "-o", $filename . ".ogg", "processed" . $num . ".wav"), "\n");





