#!/usr/bin/perl -W

# Recover options of line command
$help = 0;

foreach $opt (@ARGV) {
    $help = 1 if $opt eq "-h";
    $random = $1 if $opt =~ /-r=([1-9][0-9]*)/;
    $dest = $1 if $opt =~ /-dest=(.*)/;
    $src = $1 if $opt =~ /^([^-].*)/;
    die "Unvalid option. Use ". $0 ." -h for any help.\n" if $opt =~ /-[^hrd]/;
}

if($help == 1) {
    print "TikzCubes : help.

Usage : ". $0 ." [-h | [-r=n][-dest=\"path\"]] src_path
where src_path is the path of the file containing the Rote sequences.

Options :
    -h               Print this help page and exit.
    -r=n             Do not generate all m,n-cubes of src_path but 
                     only n randomly.
    -dest=\"path\"   Print the LaTeX/TikZ code in path. Default is 
                     ./tikzcubes_m_n.tex.
";
    exit 0;
}

# Recover m and n from the source file
open(S, $src);
if(<S> =~ /^.*(\d+),(\d+).*$/) {
    $m = $1;
    $n = $2;
} else { chomp; die "$_ does not contain formatted digits.\n"; }

# Format a TeX file to generate Tikz code
$dest = "tikzcubes_".$m."_".$n.".tex" unless defined($dest);
open(F,">".$dest) || die "opening file : $!";

print F "\\documentclass{article}

\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage[english]{babel}
\\usepackage{lmodern}
\\usepackage{amsmath}
\\usepackage{amssymb}
\\usepackage{tikz}

\\newcommand{\\dx}{0.866025403784438597}
\\newcommand{\\dy}{0.5}

% \\cube{edge, x, y, color_up, color_left, color_right}
%                
%               / \\                  'x' is the position point (x,y)
%             /     \\ 
%           /         \\ 
%         /  color_up   \\   ^
%        |\\             /|  |
%        |  \\         /  |  e 
%        |    \\     /    |  d   
%        |      \\ /      |  g 
%        | color x color |  e
%        | left  | right |  |
%         \\      |      /   √
%           \\    |    /   
%             \\  |  /     
%               \\|/
%
\\newcommand{\\cube}[6]{
  \\draw[fill=#4] (#2*#1 pt, #3*#1 pt) -- (#2*#1+\\dx*#1 pt, #3*#1+\\dy*#1 pt) -- (#2*#1 pt, #3*#1+#1 pt) 
  -- (#2*#1-\\dx*#1 pt, #3*#1+\\dy*#1 pt) -- cycle;
  \\draw[fill=#5] (#2*#1 pt, #3*#1 pt) -- (#2*#1-\\dx*#1 pt, #3*#1+\\dy*#1 pt) -- (#2*#1-\\dx*#1 pt, #3*#1-\\dy*#1 pt) 
  -- (#2*#1 pt, #3*#1-#1 pt) -- cycle;
  \\draw[fill=#6] (#2*#1 pt, #3*#1 pt) -- (#2*#1 pt, #3*#1-#1 pt) -- (#2*#1+\\dx*#1 pt, #3*#1-\\dy*#1 pt) 
  -- (#2*#1+\\dx*#1 pt, #3*#1+\\dy*#1 pt) -- cycle;
}


\\title{".$m.",".$n."-cubes drawing}
\\author{Generated by Ti\\emph{k}zCubes} \\date{}

\\begin{document}
\\maketitle
\\newpage

";

$dx = 0.866025403784438597;
$dy = 0.5;

if(defined $random) {
    my $n_tot = 0;
    while(<S>) {
	$n_tot = $1 if m/^.*:\s*(\d+)\s*$/;
    }
    close(S); open(S, $src);
    for(my $j=0; $j < $random; ++$j) { 
	$rand[$j] = 1+rand($n_tot);
    }
    @rand = sort(@rand);
    $rand = shift(@rand);
}

$i = 0;
$count = 0;

while(<S>) {
    chomp;
    if($_ =~ /\[([01])\]/) {
	if ($i == 0) {
	    if(defined($random)) {
		if(defined($rand) && $count == $rand) {
		    print F "\\begin{tikzpicture}\n";
		}
	    } else { print F "\\begin{tikzpicture}\n"; }
	    ++$count;
	} 
	if(defined($random)) {
	    if(defined($rand) && $count == $rand) {
		&tikz_segment($_, -$i*$dx, -$i*$dy, $1);
		$rand = shift(@rand);
	    }
	} else { &tikz_segment($_, -$i*$dx, -$i*$dy, $1); }
    }
    else {
	if($i > 0) {
	    if(defined($random)) {
		if(defined($rand) && $count == $rand) {
		    print F "\\end{tikzpicture}\\hspace{10pt}\n";
		}
	    } else { print F "\\end{tikzpicture}\\hspace{10pt}\n"; }
	    $i = 0;
	}
    }
}

print F "\\end{document}";

sub tikz_segment {
    my ($line, $posx, $posy, $last) = @_;
    $line =~ s/\[([01])\]// || return ++$i;
    
    my $stepy = -$dy;
    $stepy -= 1 if $1 == 1 - $last;

    my $printx = sprintf("%.50f", ($posx+$dx));
    my $printy = sprintf("%.50f", ($posy+$stepy));
    
    print F "\\cube{10}{".$printx."}{".$printy."}{blue!30}{red!30}{green!30}\n";
    &tikz_segment($line, $posx+$dx, $posy+$stepy, $1);
}
