#!/bin/perl

# This program makes a single series of mugshots for a single file.

use strict;
use warnings;
use File::Slurp;
use File::Path;
use File::Copy;
use Data::Dumper;
use Cwd;

my $cwd;
$cwd = getcwd;

# require "${cwd}/bin/tightknot_common.pl";

print STDERR "mugshot 1.0: Extracts a VECT or vects from ./tars and makes a set of mugshots.\n";

my $cwd;
$cwd = getcwd;

my @files;
@files = @ARGV;
my $filename;

NEWFILE: foreach $filename (@files) {

  my $tubename;
  my $pngname;
  my $imgname;
  my $throttle;
  my $axis;
  my $dirname;
  my $thickness;
  my $cvtoutput;
  my $borderW;
  my $borderH;
  my $jpgname;
  my $cream   = "--red=0.9647  --green=0.9098 --blue=0.7647";
  my $dkbrown = "--red=0.00392 --green=0.4    --blue=0.3686";
  my $dkgreen = "--red=0.5490  --green=0.3176 --blue=0.0392";
  my $ltbrown = "--red=0.8470  --green=0.7019 --blue=0.3960";
  my $ltgreen = "--red=0.78039 --green=0.9176 --blue=0.8980";

  print " $filename should be in ";
  my $tarfile;
  unless ($filename =~ /(.+)\_(.+)\.vect/) { die ("Couldn't parse vect name $filename.\n"); }

  my $knot_type = $1;
  my $tag = $2;

  print "${cwd}/tars/${knot_type}.tar.gz -> ";

  # Now we look for the tar file.

  chdir("${cwd}/tars/") or die("Couldn't change working directory to ${cwd}/tars");

  unless (-f "${knot_type}.tar.gz") {

    print "couldn't find ${knot_type}.tar.gz. Skipping.\n";
    next NEWFILE;

  }

  print "found tar -> ";
  `tar  -xzf ${knot_type}.tar.gz`;
  
  unless (-f "./${knot_type}/${knot_type}_${tag}.vect") { 
    
    print "Couldn't find ./${knot_type}/${knot_type}_${tag}.vect. Skipping. \n";
    rmtree("${cwd}/tars/${knot_type}/") or die("Couldn't rmtree the directory ${cwd}/tars/${knot_type}\n");
    next NEWFILE;
    
  }
  
  copy("./${knot_type}/$filename","${cwd}/mugshots/$filename") or die("Couldn't copy ./${knot_type}/$filename to ${cwd}/mugshots/$filename");
  rmtree("${cwd}/tars/${knot_type}/") or die("Couldn't rmtree the directory ${cwd}/tars/${knot_type}\n");

  print "copied to ${cwd}/mugshots\n";

  chdir("${cwd}/mugshots/") or die("Couldn't chdir to ${cwd}/mugshots\n");
 
  print "Generating tube...";

  $tubename = $filename;
  $tubename =~ s/vect/tube\.off/;
  
  `tube -r 0.5 $filename`;

  print "done\n";
 
  for ($axis=0;$axis<3;$axis++) {

    print "Orienting to axis $axis.\n";
    $imgname = $filename;
    $imgname =~ s/.vect//;
	$imgname = $imgname.".a".$axis.".png";
    print "Will output image file $imgname.\n";
    
    `orient -a $axis $tubename`;
    
    print "Now running povsnap.\n";
    
    $pngname = `povsnap -qp $tubename`;
    chomp($pngname);
    
    print "Povsnap generated file $pngname.\n";

    #`convert -trim $pngname $imgname`;
    #`convert -resize 768x768 $imgname $imgname`;
    `convert -contrast -trim $pngname $imgname`;
    $cvtoutput = `convert -resize 768x768 -verbose $imgname $imgname`;
    
    #print "Convert output is: $cvtoutput";
    $cvtoutput =~ /\=\>(\d+)x(\d+)/;
    print "Read new image dimensions as $1 x $2\n";
    
    $borderW = (768 - $1)/2.0;
    $borderH = (768 - $2)/2.0;
    
    $cvtoutput = `convert -border ${borderW}x${borderH} -bordercolor white -verbose $imgname $imgname`;
    
    #print "Convert output is: $cvtoutput";
    $cvtoutput =~ /\=\>(\d+)x(\d+)/;
    print "After adding border, image is $1 x $2\n";
    
    $jpgname = $imgname;
    $jpgname =~ s/png/jpg/;
    `convert $imgname $jpgname`;
    
    print "Converted $imgname to $jpgname.\n"; 
    
    $dirname = $tubename;
    $dirname =~ s/.off//;
    
    print "Will now attempt to remove povsnap directory $dirname.\n";
    
    unlink($imgname) or die("Couldn't remove $imgname: $_");
    unlink($pngname) or die("Couldn't remove $pngname: $_");
    rmtree($dirname) or die("Couldn't remove $dirname: $_");
    
  }

}
