#! /usr/bin/perl
#
# Perl script to generate PBS submission script 
#
# Peter Willendrup, 20161031

use POSIX;
use File::Which;
use File::Basename;
use Cwd 'abs_path';

my $nodes = "1";
my $coresprnode = "8";
my $queue = "express";
my $runtime = 1;;
my $name = "McSub_" . $ENV{'USER'} . "_" . POSIX::strftime("%Y%m%d_%H%M%S", localtime);
my $mailrcpt = "none";
my @cmdline;

for($i = 0; $i < @ARGV; $i++) {
    if($ARGV[$i] =~ /--help|-h$/) {
        $show_help=1;
    } elsif($ARGV[$i] =~ /^--nodes=([0-9\-]+)$/) {
	$nodes = $1;
    } elsif($ARGV[$i] =~ /^--cpn=([0-9\-]+)$/) {
	$coresprnode = $1;
    } elsif($ARGV[$i] =~ /^--name=([a-zA-Z0-9_]+)$/) {
	$name = $1;
    } elsif(($ARGV[$i] =~ /^-q([a-zA-Z0-9_]+)$/) ||
            ($ARGV[$i] =~ /^--queue=([a-zA-Z0-9_]+)$/)) {
        $queue = $1;
    } elsif(($ARGV[$i] =~ /^-r([0-9]+)$/) ||
            ($ARGV[$i] =~ /^--runtime=([0-9]+)$/)) {
        $runtime = $1;
    } elsif(($ARGV[$i] =~ /^-e([0-9a-zA-Z_\.\@]+)$/) ||
            ($ARGV[$i] =~ /^--email=([0-9a-zA-Z_\.\@]+)$/)) {
        $mailrcpt = $1;
    } elsif($ARGV[$i] =~ /mxrun/) {
	# strip any reference to mxrun
     } else {
        push @cmdline, $ARGV[$i]; }
}

# Figure out which mcstas/mcxtrace is currently loaded
my $mccode_path = which('mcxtrace');
my $mccode_version =  basename(dirname(dirname(abs_path$mccode_path)));

if (@cmdline == 0) { $show_help=1; }
if ($show_help) {
die "$0 generate PBS submission script

Usage: $0 [options] [mxrun params] 
 -h        --help            Show this help
 -rN       --runtime=N       Specify maximum runtime (hours) [default $runtime]
 -qQNAME   --queue=QNAME     Specify wanted SLURM queue [default '$queue']
 -e<mail>  --email=<mail>    Specify address to notify in reg. sim status [default $mailrcpt]
           --nodes=NUM       Specify wanted number of nodes [default $nodes]
           --coresprnode=NUM Specify wanted number of nodes [default $coresprnode]
           --name=NAME       Specify openPBS job name [default \"McSub_<USERNAME>_<TIMESTAMP>\"]
\n\nAfter running $0 NAME.batch is ready for submission using the sbatch command\n";
} else {
    # Write PBS script to file
    open($OUT,"> $name.batch") || die "Could not write to $name.batch!\n";
    open($OUT,"> $name.batch") || die "Could not write to $name.batch!\n";
    print $OUT "#!/bin/sh\n#\n# PBS queue script generated by $0\n\n";
    print $OUT "### Job name\n";
    print $OUT "#PBS -N $name\n";
    print $OUT "### Number of cpus\n";
    print $OUT "#PBS -lncpus=".$nodes*coresprnode."\n";
    print $OUT "### Maximum walltime\n";
    print $OUT "#PBS -l walltime=$runtime:00:00\n";
    print $OUT "### Output\n";
    print $OUT "#PBS -o $name.OUT\n";
    print $OUT "#PBS -e $name.ERR\n";
    print $OUT "### Queue name\n";
    print $OUT "#PBS -q $queue\n";
    print $OUT "\n\n";
    print $OUT "module load mcxtrace/$mccode_version\n";    
    print $OUT "\n";
    print $OUT "### Set up mxrun line\n";
    print $OUT "cd \$PBS_O_WORKDIR\n";
    print $OUT "mxrun --mpi=".$nodes*$coresprnode." --machines=\$PBS_NODEFILE ";
    print $OUT join(' ',@cmdline);
    if (!($mailrcpt eq "none")) {
      print $OUT "### Send mail on b)egin e)xit a)bort\n";
      print $OUT "#PBS -m abe\n";
      print $OUT "#PBS -M $mailrcpt\n";

    }
    print $OUT "\n### END of McSub script\n";
    close($OUT);
    
    print "Your batchfile \n\n--> $name.batch\n\n is now ready for submission using sbatch!\n";
    print " \n";
    print "Resources:\n";
    print "  $nodes nodes using all their cores on queue $queue, expected duration $runtime hours \n";
    print " \n";
    print "mxrun Command to be submitted: \n";
    print "  mxrun --mpi=".$nodes*coresprnode;
    print join(' ',@cmdline);    print " \n";
    print " \n";
    print "Output/error messages will go to: \n";
    print "  $name.stdout and $name.stderr\n";
    print " \n";
    print "\nNOTE: Please compile your instrumentfile using\n";
    print "        mxrun --mpi -c -n0 instrument.instr\n";
    print "      before submitting!\n";
    print " \n";
}



