#! /usr/bin/perl
####  Run this program in the same directory as your data list
###   The data list should have three columns for the three dimensions
###   if not executable, type chmod a+x animateNLD.pl

system("mv animate.gif old.animated");system("clear");
print STDOUT "             "; 
system("echo -en \"\\033[0;46m\"");
print STDOUT "Animate";
system("echo -en \"\\033[0;47m\"");
print STDOUT "NLD";
system("echo -en \"\\033[0;45m\"");
print STDOUT " Version 0.1\n";

system("echo -en \"\\033[0;43m\"");
print STDOUT "#############################################################\n";
system("echo -en \"\\033[0;33m\"");
print STDOUT "########## Nonlinear Dynamics 3D Animation Program ##########\n";
system("echo -en \"\\033[0;42m\"");
print STDOUT "########## Department of Physics, Drexel University #########\n";
print STDOUT "#############################################################\n";
system("echo -en \"\\033[0;39m\"");
print STDOUT "\n";
print STDOUT "This is the non-premium ps version.\n";
print STDOUT "\n";
print STDOUT "\n";
print STDOUT "Hit Enter to Continue...";
$false=<STDIN>;
print STDOUT "The following files are in this directory:\n";
system("echo -en \"\\033[0;36m\"");
system("ls");
system("echo -en \"\\033[0;39m\"");
system("echo -en \"\\033[0;39m\"");

print STDOUT "\n";
print STDOUT "Enter file name: ";
$file = <STDIN>;
print STDOUT "Enter point size (0.1 smallest): ";
$ps = <STDIN>;
chomp($ps);
print STDOUT "\n";
print STDOUT "...working...\n";
chomp($file);
$file2 = '/"'.$file.'/"';


#############################PREPARE GNUPLOT SCRIPT

$stuff= readpipe "echo 'set pointsize $ps' > gnu0001.dat";
$stuff= readpipe "echo 'set grid' >> gnu0001.dat";
$stuff= readpipe "echo 'set terminal postscript' >> gnu0001.dat";
#$stuff= readpipe "echo 'set terminal pbm' >> gnu0001.dat";
#$stuff= readpipe "echo 'set noxtics' >> gnu0001.dat";
#$stuff= readpipe "echo 'set noytics' >> gnu0001.dat";
#$stuff= readpipe "echo 'set noborder' >> gnu0001.dat";

$b = 0;
$bb = 100;

while ($b < 180){
    $c = $b + 100;
    $cc = $b*2;
    $sview = "set view ".$b.", ".$cc.", 1";
    $soutput = $c.".ps";
    $sooutput = "a.".$c.".ps";
    $stuff = readpipe "echo '$sview' >> gnu0001.dat";
    $stuff = readpipe "echo 'set output \"$soutput\"' >> gnu0001.dat";
    $stuff = readpipe "echo 'splot \"$file\"' >> gnu0001.dat";
    #$stuff = readpipe "echo 'set output \"$sooutput\"' >> gnu0001.dat";
    #$stuff = readpipe "echo 'splot \"$file\"' >> gnu0001.dat";
    $b=$b+1;
    #system("cp $output $sooutput");
    
}
print STDOUT "...Running GNUplot...\n";

########FDAFDSF
$stuff= readpipe "gnuplot gnu0001.dat";

print STDOUT "...GNUplot                       [";
system("echo -en \"\\033[0;37m\"");
print STDOUT "DONE";
system("echo -en \"\\033[0;39m\"");
print STDOUT "]\n";

#print STDOUT "...cloning images...\n";
#$b=0;
#while ($b < 180){
#    $c = $b + 100;
#    $soutput = $c.".ps";
#    $sooutput = "a.".$c.".ps";
#    system("cp $soutput $sooutput");
#    $b = $b + 1;
#    }

#print STDOUT "...Cloning                       [";
#system("echo -en \"\\033[0;37m\"");
#print STDOUT "DONE";
#system("echo -en \"\\033[0;39m\"");
#print STDOUT "]\n";

####################################################

############################################CONVERT
#$a=1;
#$pw=readpipe "pwd";
print STDOUT "...rotating images...\n";
system("mogrify -rotate 90 *.ps");

print STDOUT "...Rotating Images               [";
system("echo -en \"\\033[0;37m\"");
print STDOUT "DONE";
system("echo -en \"\\033[0;39m\"");
print STDOUT "]\n";

#print STDOUT "...converting phase one...\n";
#system("mogrify -format tif *.ps");

#print STDOUT "...Phase One                     [";
#system("echo -en \"\\033[0;37m\"");
#print STDOUT "DONE";
#system("echo -en \"\\033[0;39m\"");
#print STDOUT "]\n";


#print STDOUT "...starting stereo formatting...\n";
#$a=0;
#while ($a < 180){
 #   $num = 100 + $a;
#    $stuff= readpipe "ppmtogif  $num.ppm > $num.gif";
 #   $n1 = $num.".tif";
 #   $n2 = "a.".$num.".tif";
 #   system("mogrify -roll +6+0 $n2");
 #   system("./anaglyph_i386_schlecht -tw $n1 $n2 -O $n1");
 #   $a=$a+1;
#}

#print STDOUT "...Stereographing                [";
#system("echo -en \"\\033[0;37m\"");
#print STDOUT "DONE";
#system("echo -en \"\\033[0;39m\"");
#print STDOUT "]\n";

#print STDOUT "\n";
print STDOUT "Final formatting...\n";
system("rm a.*.tif");
system("mogrify -format gif *.ps");
system ("echo 'Initiating animation...'");
$stuff = readpipe "gifsicle --colors 256 *.gif > animate";
system ("echo 'Removing excess data to junk.tar, erase as you wish...'");
system ("mkdir ./junk001");
system ("mv ./*.gif ./junk001");
system ("mv ./*.ps ./junk001");
system ("mv ./*.tif ./junk001");
system ("tar zcf ./junk002.tar ./junk001/*");
system ("rm -rf ./junk001");
system ("rm -f ./gnu0001.dat");
system ("mv ./animate ./animate.gif");
system ("gimp ./animate.gif &");
system ("echo Done!");
