#!/usr/bin/perl
# cardpack
# by Sam Watkins <sam@shallow.net>
# This software is in the public domain.
# see http://cards.sourceforge.net/ for details

$cols = 2; # don't change this!! but maybe ...
$rows = 4;

$unit_num = 1; $unit_unit = "cm";
$cellw = 8.2; $cellh = 5.6;
$line_space = "8pt";
$unit = $unit_num . $unit_unit;
$cell_margin = 0.3;
$cell_v_margin = 0.15;

$cell_inside_w = $cellw - $cell_margin * 2;
$cell_inside_h = $cellh - $cell_v_margin * 2;
$cellrealw = (($cellw-$cell_margin*2) * $unit_num) . $unit_unit;
$totw = $cellw * $cols; $toth = $cellh * $rows;
$dashes = 36;
$dashh = $cellh / $dashes;

print
"\\documentclass[10pt]{article}
\\usepackage[pdftex,a4paper]{geometry}
\\usepackage{texdraw}
\\begin{document}
\\pagestyle{empty}
\\Large
\\begin{center}
\\setlength{\\unitlength}{$unit}\n\n";

while (@ARGV) {
	$card_file = shift;
	$number = @ARGV ? shift : $rows;
	open IN, $card_file or die "cannot open $card_file\n";
	$card = [split /^-+\n/m, join "", <IN>];
	push @cards, $card;
	push @reps, $number;
	close IN;
}

$page = 1;
$card_i = 0;
$row = -1;
for $card (@cards) { for $i (1..shift(@reps)) { for $side (reverse @$card) {
	if ($row < 0) {
		print "\\section*{page ",$page++,"}\n\\begin{picture}($totw,$toth)\n\n";
		for $n (0,$cols) {
			print "\\put(",$n*$cellw,",0){\\line(0,1){$toth}}\n";
		}
		for $n (1,$cols-1) {
			for $m (0..$rows-1) {
				for $d (0..$dashes-1) {
					print "\\put(",$n*$cellw,",",$m*$cellh+$dashh*($d+1/3),"){\\line(0,1){",$dashh/3,"}}\n";
				}
			}
		}
		for $n (0..$rows) {
			print "\\put(0,",$n*$cellh,"){\\line(1,0){$totw}}\n";
		}
		$col = 0; $row = $rows - 1;
	}
	print
"\\put(",$col*$cellw+$cell_margin,",",$row*$cellh+$cell_v_margin,"){%
\\makebox($cell_inside_w,$cell_inside_h)[t]
{\\parbox[t]{$cellrealw}
$side}}\n";
	++$col; if ($col == $cols) { $col = 0; --$row;
	if ($row < 0) {
		print "\\end{picture}\n\n";
	}}
}}}
if ($row >= 0) {
	print "\\end{picture}\n\n";
}

print "\\end{center}\n\\end{document}\n";
