#!/usr/bin/perl
#

  
  my($output_file, $term_options);

  # plot of OPS+ versus time
  $output_file = "offense_2016_a.ps";
  $term_options = "postscript color enhanced '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 "unset key \n";
  printf CMDFILE "set ylabel 'OPS+'  \n";
  printf CMDFILE "set xlabel 'Year' \n";
  printf CMDFILE "set title 'All Major League Teams, OPS+' \n";
  printf CMDFILE "set datafile separator ',' \n";
  
  $cmd = "plot [1999:2017][50:130]  ";
  $cmd .= "   './all.csv' using (\$30+0.0*rand(0)):22 ";
  $cmd .= "       with points pt 3 ps 0.7 lc 3 lw 3  t '' ";
  $cmd .= " , ";
  $cmd .= "   './boston.csv' using 30:22 with points pt 6 ps 2 lc 1 lw 3 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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }

  # plot of RPG versus time
  $output_file = "offense_2016_b.ps";
  $term_options = "postscript color enhanced '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 "unset key \n";
  printf CMDFILE "set ylabel 'Runs per game'  \n";
  printf CMDFILE "set xlabel 'Year' \n";
  printf CMDFILE "set title 'All Major League Teams, Runs scored per game' \n";
  printf CMDFILE "set datafile separator ',' \n";
  
  $cmd = "plot [1999:2017][]  ";
  $cmd .= "   './all.csv' using (\$30+0.0*rand(0)):4 ";
  $cmd .= "       with points pt 3 ps 0.7 lc 3 lw 3  t '' ";
  $cmd .= " , ";
  $cmd .= "   './boston.csv' using 30:4 with points pt 6 ps 2 lc 1 lw 3 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 -flatten -background white -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




exit 0;
