#!/usr/bin/perl
#

  $output_file = "integrate_2a.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 (months) ' \n";
  printf CMDFILE "set ylabel 'Wage (dollars per month)' \n";
  printf CMDFILE "set title 'Integrating the wages of a corporate banker' \n";
  
  $cmd = "plot [-1:13][-1e4:1.5e5] './integrate_2a.dat' using (\$1-0.5):(\$2*\$1) ";
  $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_2b.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 'Months since Jan 1, 2010 ' \n";
  printf CMDFILE "set ylabel 'Account balance (dollars)' \n";
  printf CMDFILE "set title 'The growing bank account of a corporate banker' \n";
  
  $cmd = "plot [-1:13][-5e4:0.9e6] './integrate_2b.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;
