#! /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 "sl15_adcirc1.pl <system> <path> <directory name> <hurricane name> <start time(yy mm dd) <decimal days> <asci/binary> <timestep (in seconds)> \n";    
   exit 1;
}

my $system=shift @ARGV;
my $path=shift @ARGV;
my $dirname= shift @ARGV;
my $hurricane_name=shift @ARGV;
my $year_month_date=shift @ARGV;
my $days=shift @ARGV;
my $asc_bin=shift @ARGV;
my $seconds=shift @ARGV;

# Creating a new directory 
mkdir $dirname,0755 or die "Cannot create the directory";
chdir("$dirname")or die "Cannot enter the directory";

# Creating sub-directories inside the new directory
# "exe" directory has all the executables: adcirc, adcprep, adcpost, padcirc
mkdir "exe", 0755 or die "Cannot create directory";

# "par" directory consists of input files needed: fort.13, fort.14, fort.15, fort.22, test.pbs
mkdir "par", 0755 or die "Cannot create directory";

# "run" directory has all the files needed for an adcirc run (executables and input files)
mkdir "run", 0755 or die "Cannot create directory";


# Location of source files
my $source13="$path/adcirc_files_sl15/fort.13";
my $source14="$path/adcirc_files_sl15/fort.14";
my $source15="$path/adcirc_files_sl15/fort.15";
my $source22="$path/adcirc_files_sl15/fort.22";
my $source_adcirc="$path/adcirc_files_sl15/adcirc";
my $source_adcpost="$path/adcirc_files_sl15/adcpost";
my $source_adcprep="$path/adcirc_files_sl15/adcprep";
my $source_padcirc="$path/adcirc_files_sl15/padcirc";
my $source_pbs="$path/adcirc_files_sl15/adcirc.pbs";
my $source_pbs_ranger="$path/adcirc_files_sl15/adcirc1.pbs";

# Location of the destination files to be copied to 
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";


# Copying files from source to destination
copy($source13,$par_13) or die "File cannot be copied";
copy($source14,$par_14) or die "File cannot be copied";
copy($source15,$par_15) or die "File cannot be copied";
copy($source22,$par_22) or die "File cannot be copied";
#copy($source_pbs,$run_pbs) or die "File cannot be copied";
copy($source_adcirc,$exe_adcirc) or die "File cannot be copied";
copy($source_adcpost,$exe_adcpost) or die "File cannot be copied";
copy($source_adcprep,$exe_adcprep) or die "File cannot be copied";
copy($source_padcirc,$exe_padcirc) or die "File cannot be copied";
if($system eq "ranger")
{
  copy($source_pbs_ranger,$run_pbs) or die "File cannot be copied";
}
else
{
  copy($source_pbs,$run_pbs) or die "File cannot be copied";
}

# Change permissions for the copied files
chmod(0777,$par_15) or die "Cannot change permissions";

# Modifications to be done to the fort.15 file with the given input
$hurricane_name="$hurricane_name ! 32 CHARACTER ALPHANUMERIC RUN DESCRIPTION\n";

$year_month_date="$year_month_date 1 0.9\n";

my $asc_bin_366="$asc_bin 0.0 $days $seconds ! NOUTE,TOUTSE,TOUTFE,NSPOOLE:ELEV STATION OUTPUT INFO (UNIT  61)\n";
my $asc_bin_373="$asc_bin 0.0 $days $seconds ! NOUTV,TOUTSV,TOUTFV,NSPOOLV:VEL STATION OUTPUT INFO (UNIT  62)\n";
my $asc_bin_378="$asc_bin 0.0 $days $seconds ! NOUTM,TOUTSM,TOUTFM,NSPOOLM: MET STATION OUTPUT INFO (UNIT  71,72)\n";
my $asc_bin_385="$asc_bin 0.0 $days $seconds ! NOUTGE,TOUTSGE,TOUTFGE,NSPOOLGE : GLOBAL ELEVATION OUTPUT INFO (UNIT  63)\n";
my $asc_bin_386="$asc_bin 0.0 $days $seconds ! NOUTGV,TOUTSGV,TOUTFGV,NSPOOLGV : GLOBAL VELOCITY  OUTPUT INFO (UNIT  64)\n";
my $asc_bin_387="$asc_bin 0.0 $days $seconds ! NOUTGM,TOUTSGM,TOUTFGM,NSPOOLGM : GLOBAL MET  OUTPUT INFO (UNIT  73,74)\n";

$days="$days !0.115740741 ! 10000 timesteps ! 7.5 ! RNDAY - TOTAL LENGTH OF SIMULATION (IN DAYS)\n";
# Modifications done completely to the fort.15 file

# Read fort.15 file
open FILE15, "<", $par_15 or die "Cannot open fort.15 file: $!";
my @lines = <FILE15>;
close FILE15;

# Modify lines in fort.15 file
$lines[0] = $hurricane_name;
$lines[28] = $year_month_date;
$lines[29] = $days;
$lines[365] = $asc_bin_366;
$lines[372] = $asc_bin_373;
$lines[377] = $asc_bin_378;
$lines[384] = $asc_bin_385;
$lines[385] = $asc_bin_386;
$lines[386] = $asc_bin_387;


# Write into fort.15 file
open FILE15, ">", $par_15 or die "Cannot open fort.15 file: $!";
print FILE15 join('', @lines);
close FILE15;



