#! /usr/bin/perl -w

use File::Copy;
use strict;
use warnings;

# Storing command line arguments in an array
print "The following arguments were passed to this script:\n";
print "@ARGV\n";

if (! @ARGV) {
    print "No arguments given.\n";
    print "Call adcirc.pl as:\n";
    print "adcirc2.pl <system> <path> <directory name> <output directory> <email id> <processors> <walltime> <job name> <alloca
tion name> \n";    
    exit 1;
}

my $system=shift @ARGV;
my $path=shift @ARGV;
my $dirname= shift @ARGV;
my $output=shift @ARGV;
my $email_id=shift @ARGV;
my $nprocs=shift @ARGV;
my $walltime=shift @ARGV;
my $job_name=shift @ARGV;
my $allocation_name=shift @ARGV;

#Creating "output" sub directory
chdir "$path/$dirname/" or die "Cannot move to the directory";
mkdir $output,0755 or die "Cannot create the directory";

#Location of source files
my $par_13="$path/$dirname/par/fort.13";
my $par_14="$path/$dirname/par/fort.14";
my $par_15="$path/$dirname/par/fort.15";
my $par_22="$path/$dirname/par/fort.22";
my $run_pbs="$path/$dirname/run/adcirc.pbs";
my $exe_adcirc="$path/$dirname/exe/adcirc";
my $exe_adcpost="$path/$dirname/exe/adcpost";
my $exe_adcprep="$path/$dirname/exe/adcprep";
my $exe_padcirc="$path/$dirname/exe/padcirc";

#Location of destination files
my $output_pbs="$path/$dirname/$output/adcirc.pbs";
my $output_13="$path/$dirname/$output/fort.13";
my $output_14="$path/$dirname/$output/fort.14";
my $output_15="$path/$dirname/$output/fort.15";
my $output_22="$path/$dirname/$output/fort.22";
my $output_adcirc="$path/$dirname/$output/adcirc";
my $output_adcpost="$path/$dirname/$output/adcpost";
my $output_adcprep="$path/$dirname/$output/adcprep";
my $output_padcirc="$path/$dirname/$output/padcirc";

# Copying files from source to destination
copy($par_13,$output_13) or die "File cannot be copied";
copy($par_14,$output_14) or die "File cannot be copied";
copy($par_15,$output_15) or die "File cannot be copied";
copy($par_22,$output_22) or die "File cannot be copied";
copy($run_pbs,$output_pbs) or die "File cannot be copied";
copy($exe_adcirc,$output_adcirc) or die "File cannot be copied";
copy($exe_adcpost,$output_adcpost) or die "File cannot be copied";
copy($exe_adcprep,$output_adcprep) or die "File cannot be copied";
copy($exe_padcirc,$output_padcirc) or die "File cannot be copied";

# Change permissions for the copied files
chmod(0777,$output_adcprep) or die "Cannot change permissions";
chmod(0777,$output_padcirc) or die "Cannot change permissions";
chmod(0777,$output_adcpost) or die "Cannot change permissions";
chmod(0777,$output_adcirc) or die "Cannot change permissions";
chmod(0777,$output_13) or die "Cannot change permissions";
chmod(0777,$output_14) or die "Cannot change permissions";
chmod(0777,$output_15) or die "Cannot change permissions";
chmod(0777,$output_22) or die "Cannot change permissions";
chmod(0777,$output_pbs) or die "Cannot change permissions";

# Modifications to be done in the PBS script
my $nodes= 0;
my $nprocs1=0;
my $work_dir=' ';
my $out_file=' ';
my $error_file=' ';
my $dollar=' ';
my $ppn = 4;
if($system ne "ranger")
{
  $dollar='PBS';
  $email_id="#$dollar -M $email_id\n";
  
#  my $ppn = 4;
  if($system eq "queenbee")
  {
   $ppn = 8;
  }
  
  #$nprocs = $nodes * $ppn;
  $nodes=$nprocs/$ppn;
  $nprocs1="#$dollar -l nodes=$nodes:ppn=$ppn\n";

  $walltime="#$dollar -l walltime=$walltime\n";

  $job_name="#$dollar -N $job_name\n";

  $allocation_name="#$dollar -A $allocation_name\n";

  $work_dir="WORK_DIR=$path/$dirname/$output";
} else {
  $dollar='$';

  $email_id="#$dollar -M $email_id\n";
  
  $nprocs1="#$dollar -pe 16way $nprocs\n"; 
  
  $walltime="#$dollar -l h_rt=$walltime\n";

  $out_file="#$dollar -o $path/$dirname/$output/sl15stdout32.out\n";

  $error_file="#$dollar -e $path/$dirname/$output/sl15stderr32.err\n";

  $job_name="#$dollar -N $job_name\n";

  $allocation_name="#$dollar -A $allocation_name\n";

  $work_dir="WORK_DIR=$path/$dirname/$output"; 
 
  $nprocs= "NPROCS=$nprocs\n";
}
#Modifications done to the PBS script

# Read PBS script
open FILEpbs, "<", $run_pbs or die "Cannot open the PBS script: $!";
my @lines_pbs = <FILEpbs>;
close FILEpbs;

# Modify lines in PBS script
if($system eq "ranger")
{
  $lines_pbs[3] = $email_id;
  $lines_pbs[4] = $nprocs1;
  $lines_pbs[5] = $walltime;
  $lines_pbs[10] = $out_file;
  $lines_pbs[11] = $error_file;
  $lines_pbs[14] = $job_name;
  $lines_pbs[17] = $allocation_name;
  $lines_pbs[24] = $work_dir;
  $lines_pbs[26] = $nprocs;
} else {
  $lines_pbs[3] = $email_id;
  $lines_pbs[4] = $nprocs;
  $lines_pbs[5] = $walltime;
  $lines_pbs[14] = $job_name;
  $lines_pbs[17] = $allocation_name;
  $lines_pbs[23] = $work_dir;
}

# Write into PBS script
open FILEpbs, ">", $output_pbs or die "Cannot open the PBS script: $!";
print FILEpbs join('',@lines_pbs);
close FILEpbs;

# Moving into the new directory created
chdir "$path/$dirname/$output/" or die "Cannot move to the output directory";

# Submittiing the PBS script
system "qsub adcirc.pbs";

