#!/usr/bin/perl
#
# Series of commands needed to turn hand-made screencaptures
#   into GIF images which will align perfectly.
#
# MWR 7/26/2017

$debug = 1;

$temp = "./foo.gif";

@exptimes = ("0.5", "1.0", "2.5", "5.0", "10 ", "50 ");

$index = 0;
@globfiles = glob("trans_?_label.png");
foreach $file (@globfiles) {
  if ($debug > 0) {
    printf " next is %s \n", $file;
  }

  $new_file = $file;
  $new_file =~ s/png/gif/;

  $cmd = "convert -crop 698x600+8+25 +repage $file $temp";
  $ret = exec_cmd($cmd);

  $exptime = $exptimes[$index];
  $exptime_str = sprintf " %s sec", $exptime;

  $cmd = "convert -font helvetica -fill gold -pointsize 36 ";
  $cmd .= "   -draw \"text 50,80 ' $exptime_str ' \" ";
  $cmd .= "  $temp $new_file ";
  $ret = exec_cmd($cmd);


  $index++;

}

$cmd = "gifmerge -100 -l0 trans_a_label.gif ";
$cmd .= " trans_b_label.gif ";
$cmd .= " trans_c_label.gif ";
$cmd .= " trans_d_label.gif ";
$cmd .= " trans_e_label.gif ";
$cmd .= " trans_f_label.gif ";
$cmd .= " trans_f_label.gif ";
$cmd .= " trans_f_label.gif ";
$cmd .= "   > trans_anim_label.gif ";
$ret = exec_cmd($cmd);


exit 0;





##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


