#!/usr/bin/perl
#

  $output_file = "integrate_1a.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
  printf CMDFILE "set xlabel 'Time (hours) ' \n";
  printf CMDFILE "set ylabel 'Wage (dollars per hour)' \n";
  printf CMDFILE "set title 'Integrating the wages of a faceless drone' \n";
  
  $cmd = "plot [-1:10][-1:9] './integrate_1a.dat' using (\$1+0.5):2 ";
  $cmd = $cmd . "   with boxes lw 3 fs solid 0.2 t '' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . " 0 with lines lt -1 lw 1 t '' ";
  

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



#  Now show the running sum of all money earned so far
  $output_file = "integrate_1b.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
  printf CMDFILE "set xlabel 'Time since 9 AM (hours) ' \n";
  printf CMDFILE "set ylabel 'Account balance (dollars)' \n";
  printf CMDFILE "set title 'The growing bank account of a drone worker' \n";
  
  $cmd = "plot [-1:10][-5:65] './integrate_1a.dat' using (\$1):3 ";
  $cmd = $cmd . "   with points ps 2 pt 5 lw 3  t '' ";
  $cmd = $cmd . " , ";
  $cmd = $cmd . " 0 with lines lt -1 lw 1 t '' ";
  

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


exit 0;
