Index: other-projects/nightly-tasks/diffcol/trunk/task.pl
===================================================================
--- other-projects/nightly-tasks/diffcol/trunk/task.pl	(revision 27621)
+++ other-projects/nightly-tasks/diffcol/trunk/task.pl	(revision 27628)
@@ -6,10 +6,14 @@
 # For windows, need to REMEMBER to set the correct shebangs at the top
 
+package diffcoltask;
 
 use Cwd; 
+use Switch; # for switch(val) { case: ; ...}
 use File::Path; # for rmdir and mkdir type functions
 use File::Copy; # for recursive copying of folders but skipping .svn
 use File::Basename;
 
+use strict;
+no strict 'subs'; # allow barewords (eg STDERR) as function arguments
 
 my $isWin = ($^O =~ m/mswin/i) ? 1 : 0;
@@ -27,4 +31,5 @@
 $ENV{'UPLOAD_DIR'} = filename_concat($ENV{'TASK_HOME'}, "diffcol-reports");
 $ENV{'MONITOR_EMAIL'} = "greenstone_team\@cs.waikato.ac.nz"; # need to escape @ sign
+$ENV{'GSDL_SMTP'} = "";
 ##print STDERR "@@@ email: ".$ENV{'MONITOR_EMAIL'}."\n";
 
@@ -35,4 +40,28 @@
 #export GSDL3SRCHOME=
 
+
+
+#parse arguments
+my $action = "all";
+if(scalar(@ARGV) > 1) {
+    &printusage();
+    exit 0;
+}
+
+if(scalar(@ARGV) == 0) {
+    $action="all";
+}
+else {    
+    switch ($ARGV[0]) {
+	case qr/^(-h|-help|help)$/i { &printusage; exit 0; }
+	case qr/^(setup_greenstone|run_test|summarise|upload|all)$/ { $action=$ARGV[0]; }
+	else { 
+	    print STDERR "Bad subcommand.\n"; 
+	    &printusage; 
+	    exit -1;
+	}
+    }
+}
+
 #check key environment vars are set
 if(!defined $ENV{'UPLOAD_DIR'}) {
@@ -56,5 +85,4 @@
 print STDOUT "DATA_DIR: ".$ENV{'DATA_DIR'}."\n";
 print STDOUT "UPLOAD_DIR: ".$ENV{'UPLOAD_DIR'}."\n";
-
 
 #create an id for this test
@@ -74,5 +102,5 @@
 ##print STDERR "@@@ $jar_lib_path\n";
 my @files = <$jar_lib_path/*.jar>; # /full/path/to/diffcol/lib/*jar
-foreach $file (@files) {
+foreach my $file (@files) {
     $ENV{'CLASSPATH'}=$file.$pathsep.$ENV{'CLASSPATH'};
 }
@@ -101,11 +129,32 @@
 ##print STDERR "GSHOME: $greenstone_home\n";
 
-#&setup_greenstone;
-&run_test;
-&summarise;
-&upload;
+#do the requested action
+if($action eq "setup_greenstone") {
+    &setup_greenstone;
+}
+elsif ($action eq "run_test") { 
+    &run_test;
+}
+elsif ($action eq "summarise") {
+    &summarise;
+}
+elsif ($action eq "upload") { 
+    &upload;
+    &mail_with_report_attached;
+}
+elsif ($action eq "all") {
+    &setup_greenstone;
+    &run_test;
+    &summarise;
+    &upload;
+    &mail_with_report_attached;
+}
 
 ##********************************
 
+sub printusage
+{
+    print STDERR "Run as: $0 (help|setup_greenstone|run_test|summarise|upload|all)\n";
+}
 
 
@@ -115,10 +164,10 @@
 {
     #clean up from previous tests
-    print STDOUT "about to clean up any old tests (Ctrl-C to cancel)"; # no newline
+    print STDERR "about to clean up any old tests (Ctrl-C to cancel)"; # no newline
     for my $i ( 1..5 ) {
 	sleep 1; # 1 second
-	print STDOUT ".";
-    }
-    print STDOUT "\n";
+	print STDERR ".";
+    }
+    print STDERR "\n";
 
     # http://perldoc.perl.org/File/Path.html
@@ -298,6 +347,8 @@
 	print STDOUT "$collection - Importing:\n";
 	print $xml_fh "<import>\n";
-	$cmd = "import.pl -OIDtype hash_on_full_filename $collection -removeold";
-	&run_build_script($cmd);
+#	$cmd = "import.pl -OIDtype hash_on_full_filename $collection -removeold";
+#	&run_build_script($cmd);
+	$cmd = "bash -c \"export GSDLHOME=&& source $setup_script.bash && import.pl -OIDtype hash_on_full_filename -removeold $collection\"";
+	system($cmd);
 	print $xml_fh "</import>\n";
 	print STDOUT "done\n";	
@@ -306,6 +357,8 @@
 	print STDOUT "$collection - Building:\n";
 	print $xml_fh "<build>\n";
-	$cmd = "buildcol.pl $collection -removeold";
-	&run_build_script($cmd);
+#	$cmd = "buildcol.pl $collection -removeold";
+#	&run_build_script($cmd);
+	$cmd = "bash -c \"export GSDLHOME=&& source $setup_script.bash && buildcol.pl -removeold $collection\"";
+	system($cmd);
 	print $xml_fh "</build>\n";
 	print STDOUT "done\n";
@@ -370,5 +423,4 @@
     return &run_and_print_cmd($cmd, $fh);
 }
-#ERROR (36096) running bash -c "export GSDLHOME=&& source setup.bash && buildcol.pl Small-HTML -removeold": Illegal seek
 
 
@@ -475,9 +527,54 @@
 }
 
-
-# TO DO:
 # Sending emails with perl: http://learn.perl.org/examples/email.html
 # Sending email attachments with perl: http://www.perlmonks.org/?node_id=19430
-
+# Sadly none of the packages are installed by default and use of MIME::Lite is discouraged
+sub mail_with_report_attached 
+{
+    # email out with report attached, if the tests failed                                                      
+    print STDOUT "Checking if successful... \n";
+    my $cmd = "java org.apache.xalan.xslt.Process -IN $xmlout -XSL $ENV{'TASK_HOME'}/xsl/passed-or-not.xsl";
+    #my $result = system($cmd);
+    my $result = `$cmd`;
+
+    print STDOUT "result: $result\n";
+
+    if($result ne "yes") {
+	my $msg = "$gsdl regression test for $dateid failed";
+	my $subject = "Regression Test Failed"; #"$gsdl regression test for $dateid failed\n";
+	my $attach_file = &filename_concat($ENV{'DATA_DIR'}, "report-$dateid.html");
+
+	if($isWin) {	    
+	    # http://stackoverflow.com/questions/709635/sending-mail-from-batch-file
+	    #blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body"
+
+	    $cmd = "blat -to $ENV{'MONITOR_EMAIL'} -server $ENV{'GSDL_SMTP'} -f $attach_file -subject $subject -body $msg"; # need to install blat on windows
+
+	} else {
+	    my $status = system("command -v mutt > /dev/null 2>&1;"); #better way of doing "which mutt"
+	    
+	    if($status != 0) { # mutt doesn't exist, can't send attachments, so send simple email
+		$cmd="echo '$gsdl regression test for $dateid failed.' | mail -s 'Regression Test Failed' $ENV{'MONITOR_EMAIL'}";
+
+		print STDERR "********************************************\n";
+		print STDERR "No mutt installed, unable to mail attachment\n";
+		print STDERR "Inspect report at: $attach_file\n";
+		print STDERR "********************************************\n";
+	    } else {
+		#$cmd = "bash -c \"echo '$gsdl regression test for $dateid failed' | mutt -a $attach_file -s 'Regression Test Failed' -- $ENV{'MONITOR_EMAIL'}\"";
+		$cmd = "echo '$gsdl regression test for $dateid failed' | mutt -a $attach_file -s 'Regression Test Failed' -- $ENV{'MONITOR_EMAIL'}";
+	    }
+	}
+
+	# run the mail command
+	system($cmd); #	&run_and_print_cmd($cmd);
+
+	print STDOUT "Sent mail with report attached.\n";
+    } else {
+	print STDERR "********************************************\n";
+	print STDOUT "Tests were successful. Not sending mail.\n";
+	print STDERR "********************************************\n";
+    }
+}
 
 # The old version of this program contained the following, consisting of 1 line of active code:
