#!/usr/bin/perl -w

#######################################################
# expr_play
#
# try to permit a CS 131 student to "play" with C++
# arithmetic expressions easily. (written for Section 2, HTDP for C++)
# 
# written by: Sharon M. Tuttle, st10@humboldt.edu
# last modified: 6-2-03
# last modified: 8-29-03 (NEWER than version on sorrel!) HERE PLEASE - dbl ck!
#######################################################

use File::Basename;

chomp(my $who = `whoami`);
chomp(my $where_path = `pwd`);
my $where = basename $where_path;

print "Enter a C++ expression and type enter\n";
print "   (or type q to quit):\n";

chomp(my $expr = <STDIN>);

# shall we create each expression's program in a separate file,
#    or not? Hmm... For now, yes.

# create a file to eventually be mailed to me with a "record"
#    of this student's attempts
open RECORD, "> expr_play_record"
    or die "Cannot open expr_play_record for writing: $!";

print RECORD "Subject: [cs131] expr_play from $who\n";
print RECORD "\n";
print RECORD "$who is running expr_play in directory: $where\n";
print RECORD "   (whole path: $where_path)\n";

chomp(my $start_time = `date`);

print RECORD "   (start time: $start_time)\n";
print RECORD "\n";

my $expr_ct = 0;

# keep handling expressions until the user wishes to quit

while ($expr ne 'q')
{
    print "\n";
    $expr_ct++;

    # create a C++ program to execute this expression
    open TESTER, "> try_expr$expr_ct.cpp"
        or die "Cannot open try_expr$expr_ct.cpp for writing: $!";

    print TESTER "#include <iostream>\n";
    print TESTER "#include <cmath>\n";
    print TESTER "using namespace std;";
    print TESTER "\n";
    print TESTER "int main()\n";
    print TESTER "{\n";
    print TESTER "    cout << \"value of $expr: \" << endl;\n";
    print TESTER "    cout << $expr << endl;\n";
    print TESTER "    return 0;\n";
    print TESTER "}\n";
    print TESTER "\n";

    close TESTER;

    my $ret_val = system("g++ -o try_expr$expr_ct try_expr$expr_ct.cpp");

    # I am hoping very hard that a return value of 0 means the compilation
    #    was successful;
    if ($ret_val == 0)
    {
        system("try_expr$expr_ct");
    }
    else
    {
        print "*****************************************************\n";
        print "Are you sure that $expr is truly a C++ arithmetic\n";
        print "   expression (with no variables)?\n";
        print "Chances are good that it is not; the above are C++\n";
        print "   compiler messages. Save them and show them to \n";
        print "   your prof if you have questions.\n";
        print "*****************************************************\n";
    }

    # save a record of this attempt (to be emailed to me at the
    #    end of this loop...)
    my $when = `date`;

    print RECORD "$who tried expression #$expr_ct: $expr at $when\n";    

    # clean up --- remove latest expression's C++ files
    unlink "try_expr$expr_ct.cpp";
    if ($ret_val == 0)
    {
        unlink "try_expr$expr_ct";
    }

    print "\nEnter next C++ expression and type enter\n";
    print "   (or type q to quit):\n";
    chomp($expr = <STDIN>);
}   

close RECORD;

# thanks to Jeremy Miller for the how-to-email-in-Perl example...
open (MAIL, "|/usr/lib/sendmail st10\@humboldt.edu") 
     || &HTMLdie("Couldn't send the mail!");

open RECORD, "< expr_play_record"
    or die "Cannot open expr_play_record for reading: $!";

while (<RECORD>)
{
    chomp(my $line = $_);

    print MAIL "$line\n";
}

close (RECORD);
close (MAIL);

unlink expr_play_record;

print "\nQuitting ... goodbye.\n\n";

# end of expr_play
