
BEGIN {
  open SAVESTDOUT, ">&STDOUT" or die $!;
  open     STDOUT, ">", "/dev/null" or die $!;
  open     STDIN, "<", "/dev/null" or die $!;
}

END {
  print STDERR "$0\n";
  for my $tfile (<templates/*>) {
    print STDERR "  $tfile\n";
    open F, "<", $tfile or die "Couldn't read $tfile: $!";
    chomp(my @template = <F>);
    close F;

    for my $sfile (<glyphs/*>) {
      print STDERR "    $sfile\n";
      open S, "<", $sfile or die "Couldn't read $sfile: $!";
      chomp(my @squares = <S>);
      close S;
      { my $i = 0;
        undef @empty_square; undef @full_square;
        my @mref = (\@empty_square, \@full_square);  # globals
        for (@squares) {
          if (/^\s*$/) { $i++; last if $i == @mref; next }
          push @{$mref[$i]}, $_;
        }
      }

      my $outdir = "outputs/$0";
      -d $outdir or mkdir($outdir, 0777) or die "mkdir $outdir: $!";
      s|.*/|| for $tfile, $sfile;
      open STDOUT, ">", "$outdir/$tfile-$sfile" 
        or die "$outdir/$tfile-$sfile: $!";
      @output = eval { layout_crossword(@template) };
      if ($@) {
        print "FAILED: $@\n";
      } else {
        print join "\n", @output, "";
      }
      close STDOUT;
    }
  }
}

1;
