#!/usr/bin/perl -w

#######################################################################
# A program to submit jobs to the queue
# Creates submission script for various platforms.
#
# Last Updated: 19 Dec 2005 
# was 7 Dec 2005 Was 2 Dec 2005 Was 10 Oct 2005
#
# Another fine program by Michael Sullivan
# michael@ihpc.a-star.edu.sg or mike@sullivan.sg
# http://www.sullivan.sg/
#
# Use .inp for CPMD
# Use .com for Gaussian
# Use INCAR or .icar or .kpts or pcar (POSCAR) or potc (POTCAR) for VASP
# Use .rundat for CASTEP
# Use .gin for GULP
# Use .adf for ADF
# Use .run for ADF run file (from adfinput)
#
# General switches:
# -h for help
# -time 1:00:00 to run for an hour
# -proc 4 to run on 4 procs using MPI
# -mp 1 to spawn openMP processes on each MPI or Linda job
# -linda 4 to run Gaussian on 4 procs using Linda
# -pplib . to use the basis set from the current directory for CPMD
# -pplib /Home2/nus/nusmbs/basis/PPLIB to use the ones in my home directory
# -r no if you don't want it to run but just make the queue file
# -mem set memory (only works on Turing)
# -link link Gaussian jobs together meaning that the sequence of jobs runs
#       using the previous checkpoint file. Just copies old checkpoint for new job.
########################################################################

use strict;
use File::Basename;
use File::Path;

my $currdir=$ENV{'PWD'};
my $user=$ENV{'USER'};
my $mach=0;
my $hostname=0;
#my $q="cpq";
#my $q="linux";
my $q="ll";

if(defined $ENV{'MACHTYPE'}){
    $mach=$ENV{'MACHTYPE'};
}
if(defined $ENV{'HOSTNAME'}){
    $hostname=$ENV{'HOSTNAME'};
#    $mach="undefined";
    if ($hostname=~/darwin/){
	$q="default";
    }
}
#my $mach="linux";
#print("$mach\n");
my $homedir=$ENV{'HOME'};
my $proc=1;
my $openmp=1;
my $linda=1;
my $mem="500mb";
my $ver;
#my $cmd="g03m";
my $cmd="g03";

my $base;
my $ext;
my $infile;
my $ofile;
my $chkfile;
my $efile;
my $outfile;
#logmy $logfile;
my $runfile;
my @job_chk;
my @job_in;
my @job_out;
my $run="y";
my $molecule;
my $num_jobs=0;
my $link="n";
my $g98=0;
my $g03=0;
my $castep=0;
my $vasp=0;
my $icar=0;
my $cpmd=0;
my $gulp=0;
my $adf=0;
my $adfrun=0;
my $pplib;
my $pplibdefault="$homedir/basis/PPLIB";
#my $pplib="/Home2/nus/nusmbs/basis/PPLIB";
#my $pplib="/Home2/nus/nusmbs/basis/PPLIB";
my $cputime;
my $epochsubtime=time;
my $scratchdir;
# Go thru options
while (@ARGV){
    my $option=shift;
    if ($option !~ /^\-/){
	$infile=$option;
	#Break up basename
	#    $base=basename($infile,".com");
# run vasp job
	if ($infile =~/INCAR/){
	    $vasp=1;
	    $base=updirname($currdir);
	    $cmd="vasp";
	}
	else{
	    my @splits=split(/\.\w+$/,$infile);
	    $base=$splits[0];
	    #   my @splits2=split(/$base\./,$infile);
	    #   $ext=$splits2[0];
	    my $splitter=$infile;
	    $splitter=~s/$base\.(\w+)/$1/;
	    $ext=$splitter;
	    #    print("split0=$splits[0] base=$base ext=$ext\n");
	    unless (-e "$infile"){
		print("Can't find $infile\n");
		help();
	    }
	    if ($ext=~/com/){
		$g03=1;
	    }
	    elsif ($ext=~/rundat/){
		$castep=1;
		$cpmd="castep";
	    }
	    elsif ($ext=~/inp/){
		$cpmd=1;
		$g03=0;
		$g98=0;
		$cmd="cpmd";
	    }
	    elsif ($ext=~/icar/ ||$ext=~/kpts/ ||$ext=~/pcar/||$ext=~/potc/){
		$cpmd=0;
		$g03=0;
		$g98=0;
		$vasp=1;
		$cmd="vasp";
		$icar=1;
	    }
	    elsif ($ext=~/gin/){
		$cpmd=0;
		$g03=0;
		$g98=0;
#		$cmd="/apps/cc/gulp1.3/Src/gulp";
		$cmd="gulp";
		$gulp=1;
	    }
	    elsif ($ext=~/adf/){
		$cpmd=0;
		$g03=0;
		$g98=0;
#		$cmd="/apps/acad/adf2004.01/bin/adf";
		$cmd="\$ADFBIN/adf";
		$adf=1;
	    }
	    elsif ($ext=~/run/){
		$cpmd=0;
		$g03=0;
		$g98=0;
#		$cmd="/apps/acad/adf2004.01/bin/adf";
		$cmd="./$infile";
		$adfrun=1;
	    }
	    else{
		print("Learn the usage. I don't know that extension\n");
	    }
	}
	$ofile="$base.o";
	$efile="$base.e";
	if ($cmd=~/g98/){
	    $g98=1;
	    $g03=0;
	}
	elsif ($cmd=~/g03/){
	    $g98=0;
	    $g03=1;
	}
	if ($g98==1 || $g03==1){
#	print("g98=$g98 g03=$g03\n");
	    $outfile="$base.log";
	    $chkfile="$base.chk";
	    $runfile="$base.q";
	    if (-d "/Scratch"){
		$scratchdir="/Scratch/$user.$epochsubtime.$base";
	    }
	    else{
		$scratchdir="/tmp/$user.$epochsubtime.$base";
	    }
	    
	    my @filearray=split(/-/, $base);
	    $molecule=$filearray[0];
#      unless (-e $outfile){
	    $job_in[$num_jobs]=$infile;
	    $job_out[$num_jobs]=$outfile;
	    $job_chk[$num_jobs]=$chkfile;
	    $num_jobs++;
	    #C    print("$num_jobs ");
#      }
	    #C    print("base=$base\n");
	}
	elsif ($castep==1){
	    $runfile="$base.q";
	    my @filearray=split(/-/, $base);
	    $molecule=$filearray[0];
	    $outfile="$base.out";
	    # Set default Q to SGI
	    $q="sgi";
	    unless (-e $outfile){
		$job_in[$num_jobs]=$base;
		$job_out[$num_jobs]=$outfile;
		$job_chk[$num_jobs]="placeholder";
		$num_jobs++;
		#C    print("$num_jobs ");
	    }
	}
	# run vasp
	elsif ($vasp==1){
	    $runfile="$base.q";
	    my @filearray=split(/-/, $base);
	    $molecule=$filearray[0];
	    if ($icar == 1){
		$outfile="$base.vout";
	    }
	    else{
		$outfile="OUTCAR";
	    }
#      unless (-e $outfile){
#	$job_in[$num_jobs]=$base;
	    $job_in[$num_jobs]=$infile;
	    $job_out[$num_jobs]=$outfile;
	    $job_chk[$num_jobs]="placeholder";
	    $num_jobs++;
	    #C    print("$num_jobs ");
#      }
	    unless ((-e "POSCAR" or -e "$base.pcar") and (-e "POTCAR" or -e "$base.potc") and (-e "INCAR" or -e "$base.icar") and (-e "KPOINTS" or -e "$base.kpts")){
		die("All the VASP files aren't there...\n");
	    }
	}
	# Run cpmd job  
	elsif ($cpmd==1){
	    $runfile="$base.q";
	    my @filearray=split(/-/, $base);
	    $molecule=$filearray[0];
	    $outfile="$base.cout";
	    unless (-e $outfile){
		$job_in[$num_jobs]=$infile;
		$job_out[$num_jobs]=$outfile;
		$job_chk[$num_jobs]="placeholder";
		$num_jobs++;
		#C    print("$num_jobs ");
	    }
	}
	# Run GULP job  
	elsif ($gulp==1){
	    $runfile="$base.q";
	    my @filearray=split(/-/, $base);
	    $molecule=$filearray[0];
	    $outfile="$base.gout";
	    unless (-e $outfile){
		$job_in[$num_jobs]=$infile;
		$job_out[$num_jobs]=$outfile;
		$job_chk[$num_jobs]="placeholder";
		$num_jobs++;
		#C    print("$num_jobs ");
	    }
	}
	# Run ADF job  
	elsif ($adf==1 || $adfrun==1){
	    $runfile="$base.q";
	    my @filearray=split(/-/, $base);
	    $molecule=$filearray[0];
	    $outfile="$base.aout";
#log	    $logfile="$base.alog";
	    
	    if (-d "/Scratch"){
		$scratchdir="/Scratch/$user.$epochsubtime.$base";
	    }
	    else{
		$scratchdir="/tmp/$user.$epochsubtime.$base";
	    }
	    unless (-e $outfile){
		$job_in[$num_jobs]=$infile;
		$job_out[$num_jobs]=$outfile;
		$job_chk[$num_jobs]="placeholder";
		$num_jobs++;
		#C    print("$num_jobs ");
	    }
	}
# Close if not an option
    }
    #Go through Options
    else {
	if ($option =~ /-q/){
	    $q=shift;
	}
	elsif ($option =~ /-h|-help|--help/){
#      print("Learn the usage\n");
	    help();
	}
	elsif ($option =~ /-ver/){
	    $ver=shift;
	}
	elsif ($option =~ /-cmd/){
	    $cmd=shift;
	}
	elsif ($option =~ /-mem/){
	    $mem=shift;
	}
	elsif ($option =~ /-time/){
	    $cputime=shift;
	}
	elsif ($option =~ /-mp/){
	    $openmp=shift;
	}
	elsif ($option =~ /-pplib/){
	    $pplib=shift;
	}
	elsif ($option =~ /-link/){
	    $link="y";
	}
	elsif ($option =~ /-r/){
	    my $r=shift;
	    if ($r =~ /^y/i){
		$run="y";
	    }
	    elsif ($r =~ /^n/i){
		$run="n";
	    }
	    else {
		print ("Do you want to run or not! I give up!\n");
		help();
		exit;
	    }
	}
	elsif ($option =~ /-proc/){
	    $proc=shift;
	}
	elsif ($option =~ /-linda/){
	    $linda=shift;
	    $proc=$linda;
	}
	else {
	    die("You're not doing the right thing.\n");
	}
    }
}

#print ("mach=$mach hostname=$hostname\n");

# Open run file
#C print("Opening run file\n");
open (RUN,">$runfile") || die("Can't create $runfile: $!");
print RUN ("#!/bin/sh\n");
print RUN ("\n");

# If I'm on IHPC neumann6 (IBM Regatta running LoadLeveler)

if($mach=~/rs6000/){
#	  print RUN ("#!/bin/tcsh\n");
#	  print RUN ("\n");
    
    if ($cpmd==1 || $vasp==1 || $adf==1 || $adfrun==1 || $linda>1 ||$proc>1){
	print RUN ("#\@ job_type=parallel\n");
#	print RUN ("#\@ network.MPI=css0,not_shared,US\n");
#  print RUN ("# For now, don't run OpenMP change later if you want\n");
	if ($cmd=~/g\d\d/ && $linda<2){
	    print RUN ("#\@ blocking=$proc\n");
	}
	else {
	    print RUN ("#\@ blocking=unlimited\n");
	}
#  print RUN ("#\@ blocking=$proc\n");
	print RUN ("#\@ total_tasks=$proc\n");
    }
    else{
	print RUN ("#\@ job_type=serial\n");
    }
    print RUN ("#\@ job_name=$base\n");
    print RUN ("#\@ output = $ofile\n");
    print RUN ("#\@ error  = $efile\n");
    if (defined $cputime){
	print RUN ("#\@ wall_clock_limit=$cputime\n");
    }
    print RUN ("#\@ restart=no\n");
    print RUN ("#\@ queue\n");
    if ($g98==1 || $cmd=~/g98/ || $g03==1 || $cmd=~/g03/){
	print RUN ("# Let's set some other variables we might use\n");
#	      print RUN ("setenv GAUSS_SCRDIR /Scratch/$user\n");
	print RUN ("export GAUSS_SCRDIR=$scratchdir\n");
	
	if ($g98==1 || $cmd=~/g98/){
##    print RUN ("setenv LD_LIBRARY_PATH \"\$\{LD_LIBRARY_PATH\}:/usr/local/g98a7/g98\"\n");
	    print RUN ("export GAUSS_EXEDIR=/apps/cc/g98\n");
	}
	elsif ($g03==1 || $cmd=~/g03/){
#		  print RUN ("# Let's set some other variables we might use\n");
	    print RUN ("export g03root=/apps/cc\n");
	    print RUN ("export GAUSS_EXEDIR=/apps/cc/g03:/apps/cc/g03/bsd\n");
#	      print RUN ("ls -l /apps/cc/g03/bsd/g03.profile\n");
	    print RUN ("/apps/cc/g03/bsd/g03.profile\n");
	}
	print RUN ("export PATH=\$PATH:\$GAUSS_EXEDIR\n");
#	      print RUN ("set path=(~/bin \$GAUSS_EXEDIR)\n");
	print RUN ("\n# Make the scratch dir\n");
	print RUN ("mkdir \$GAUSS_SCRDIR\n");
    }
    elsif ($adf==1 || $adfrun==1){
	print RUN ("# Let's set some variables for ADF\n");
	print RUN ("export ADFHOME=/apps/acad/adf2004.01\n");
	print RUN ("export ADFBIN=\$ADFHOME/bin\n");
	print RUN ("export ADFRESOURCES=\$ADFHOME/atomicdata\n");
	print RUN ("export SCMLICENSE=\$ADFHOME/license\n");
	print RUN ("export SCM_TMPDIR=$scratchdir\n");
	print RUN ("mkdir \$SCM_TMPDIR\n");
	print RUN ("export NSCM=$proc\n");
	print RUN ("export PATH=\$PATH:\$ADFBIN\n");
	print RUN ("export SCM_RESULTDIR=$currdir\n");
	print RUN ("\n\n");
    }
}

elsif($hostname=~/turing/){
#	  print RUN ("#!/bin/sh\n");
#	  print RUN ("\n");
#	  if ($proc>1){
    print RUN ("#PBS -N $base\n");
    
    print RUN ("#PBS -e $efile\n#PBS -o $ofile\n");
    if (defined $cputime){
	print RUN ("#PBS -lwalltime=$cputime\n");
    }
    if ($proc>1){
	print RUN ("#PBS -l nodes=1:ppn=$proc:IA64\n");
    }
    print RUN ("#PBS -l ncpus=$proc\n");
#      print RUN ("#PBS -V\n");
    print RUN ("\n");
    
#    print RUN ("### The following command computes the number of processors requested\n");
#    print RUN ("### from the file containing the list of nodes assigned to the job\n");
#    print RUN ("export NPROCS=\`wc -l \$PBS_NODEFILE \|gawk \'\/\/{print \$1\}\'\`\n\n");
#	  }
    print RUN ("cd $currdir\n\n");
    
    if ($adf==1 || $adfrun==1){
	print RUN ("# Let's set some variables for ADF\n");
	print RUN ("export ADFHOME=/apps/adf/adf2004.01\n");
	print RUN ("export ADFBIN=\$ADFHOME/bin\n");
	print RUN ("export ADFRESOURCES=\$ADFHOME/atomicdata\n");
	print RUN ("export SCMLICENSE=\$ADFHOME/license.txt\n");
	print RUN ("export SCM_TMPDIR=$scratchdir\n");
	print RUN ("mkdir \$SCM_TMPDIR\n");
	print RUN ("export NSCM=$proc\n");
	print RUN ("export PATH=\$PATH:\$ADFBIN\n");
	print RUN ("export SCM_RESULTDIR=$currdir\n");
	print RUN ("\n\n");
    }
}
elsif($hostname=~/darwin/){
#	  print RUN ("#!/bin/sh\n");
#	  print RUN ("\n");
#	  if ($proc>1){
    print RUN ("#PBS -N $base\n");
    
    print RUN ("#PBS -e $efile\n#PBS -o $ofile\n");
    if (defined $cputime){
	print RUN ("#PBS -lwalltime=$cputime\n");
    }
    if ($proc>1){
	print RUN ("#PBS -l nodes=$proc:ppn=1\n");
    }
    print RUN ("#PBS -l ncpus=$proc\n");
#      print RUN ("#PBS -V\n");
    print RUN ("\n");
    
#    print RUN ("### The following command computes the number of processors requested\n");
#    print RUN ("### from the file containing the list of nodes assigned to the job\n");
#    print RUN ("export NPROCS=\`wc -l \$PBS_NODEFILE \|gawk \'\/\/{print \$1\}\'\`\n\n");
#	  }
    print RUN ("cd $currdir\n\n");
    
}
# For Maxwell
elsif($mach=~/sparc/){
    print RUN ("\# Set up for Sparc with No Queue\n");
    if ($g98==1 || $cmd=~/g98/ || $g03==1 || $cmd=~/g03/){
	print RUN ("# Let's set some other variables we might use\n");
#	      print RUN ("setenv GAUSS_SCRDIR /Scratch/$user\n");
	print RUN ("GAUSS_SCRDIR=$scratchdir\n");
	print RUN ("export GAUSS_SCRDIR\n");
	
	if ($g98==1 || $cmd=~/g98/){
	    print RUN ("GAUSS_EXEDIR=/apps/g98\n");
	    print RUN ("export GAUSS_EXEDIR\n");
	}
	elsif ($g03==1 || $cmd=~/g03/){
#		  print RUN ("# Let's set some other variables we might use\n");
	    print RUN ("g03root=/apps/\n");
	    print RUN ("GAUSS_EXEDIR=/apps/g03:/apps/g03/bsd\n");
	    print RUN ("export g03root GAUSS_EXEDIR\n");
#	      print RUN ("ls -l /apps/g03/bsd/g03.profile\n");
	    print RUN ("/apps/g03/bsd/g03.profile\n");
	}
	print RUN ("PATH=\$PATH:\$GAUSS_EXEDIR\n");
	print RUN ("export PATH\n");
#	      print RUN ("set path=(~/bin \$GAUSS_EXEDIR)\n");
	print RUN ("\n# Make the scratch dir\n");
	print RUN ("mkdir \$GAUSS_SCRDIR\n");
    }
    
}

#Now to run on SVU Machines
else{
#    print RUN ("#!/bin/tcsh\n");
#    print RUN ("\n");
    #print RUN ("#BSUB -o $ofile\n");
    print RUN ("#BSUB -C 0\n");
    #print RUN ("#BSUB -J $base\n");
    #print RUN ("#BSUB -e $efile\n");
    #print RUN ("#BSUB -q $q\n");
    print RUN ("#BSUB -n $proc\n");
    print RUN ("#BSUB -m $mem\n");
    # Program Specifics for the run file
    # Can't mix G98 and Castep
    if ($castep==1){
	print RUN ("#BSUB -R 'rusage[castep_lic=1]'\n");
    }
    elsif ($g98==1){
	print RUN ("# Let's set some other variables we might use\n");
#    print RUN ("setenv LD_LIBRARY_PATH \"\$\{LD_LIBRARY_PATH\}:/usr/local/g98a7/g98\"\n");
	print RUN ("export LD_LIBRARY_PATH=\"\$\{LD_LIBRARY_PATH\}:/usr/local/g98a7/g98\"\n");
    }
    else{
	print("Don't know what's happening. Suppose to be g98 or castep...\n");
    }
}
if ($cpmd==1){
    print RUN ("# Running CPMD\n");
    if (defined $pplib){
	print RUN ("export PP_LIBRARY_PATH=$pplib\n");
#	print RUN ("setenv PP_LIBRARY_PATH $pplib\n");
    }
    elsif (defined $ENV{'PP_LIBRARY_PATH'}){
	print RUN ("export PP_LIBRARY_PATH=$ENV{'PP_LIBRARY_PATH'}\n");
#	print RUN ("setenv PP_LIBRARY_PATH $ENV{'PP_LIBRARY_PATH'}\n");
    }
    else {
	print RUN ("export PP_LIBRARY_PATH=$pplibdefault\n");
#	print RUN ("setenv PP_LIBRARY_PATH $pplibdefault\n");
    }
    print RUN ("export OMP_NUM_THREADS=$openmp\n\n");
#  print RUN ("setenv OMP_NUM_THREADS $openmp\n\n");
}
if($mach=~/rs6000/){
print RUN ("\n");
print RUN ("# Ensures that when you run your code, the program will use all \n# the memory on that node instead of across the nodes\n");
print RUN ("export MP_SHARED_MEM=yes\n");
}print RUN ("# print the time in standard format for timing use\n");
print RUN ("perl -e \'\$start=time\;print \"Epoch time started is: \$start\\n\"\'\n");
print RUN ("echo 'Epoch time submitted is: $epochsubtime\\n'");
print RUN ("\n");

#print RUN ("\$EPOCH=\$(perl -e'print time')\n");
#print RUN ("Epoch time started is: \$EPOCH\n")
#print ("g98=$g98 g03=$g03 cmd=$cmd job_in=@job_in\n");
#dprint ("job_in=@job_in\n");
my $i=0;
for my $infile (@job_in){
#d    print ("infile = $infile\n");
    my $outfile=shift @job_out;
    if (($g98==1)||($g03==1)||($cmd=~/g98/)||($cmd=~/g03/)){
	if ($linda>1){
	    if($cmd!~/g98/){
		print("\nERROR: You can only use Linda with G98\n\n");
		exit;
	    }
	    if ($cmd=~/(g\d\d)/){
		$cmd=$1;
	    }
	    $cmd=("$cmd"."l");
#	  print("New cmd=$cmd\n");
	}
	chkmv($outfile);
	print RUN ("time $cmd < $infile >> $outfile\n");
	print RUN ("echo \"Finished $infile: \"\n");
	if ($link=~/y/ && $i <($num_jobs-1)){
	    my $oldchk=$job_chk[$i];
	    $i++;
	    my $newchk=$job_chk[$i];
	    unless (-e $newchk){
		print RUN ("cp $oldchk $newchk\n");
	    }
	}
    }
    elsif ($castep==1){
	print RUN ("time castep.test $infile\n");
    }
    elsif ($vasp==1){
#    print RUN ("time poe /apps/cc/vasp/vasp.4.4/vasp\n\n");
#C      print ("outfile=$outfile base=$base\n");
#C      print ("icar=$icar\n");
	my @vaspext=(".icar",".kpts",".pcar",".potc");
	if ($icar==1){
	    my $vaspbase=basename($infile,@vaspext);
#C	  print("vaspbase=$vaspbase\n");
	    if (-e "$vaspbase.icar"){
		print RUN ("cp $vaspbase.icar INCAR\n");
	    }
	    if (-e "$vaspbase.pcar"){
		print RUN ("cp $vaspbase.pcar POSCAR\n");
	    }
	    if (-e "$vaspbase.potc"){
		print RUN ("cp $vaspbase.potc POTCAR\n");
	    }
	    if (-e "$vaspbase.kpts"){
		print RUN ("cp $vaspbase.kpts KPOINTS\n");
	    }
	    if ($link=~/y/ && (($i>0))){
		print RUN ("cp CONTCAR POSCAR\n");
	    }
	    #print "i=$i num_jobs=$num_jobs\n";
	    $i++
#	  }
		
	    }
	if ($mach=~/rs6000/){
	    print RUN ("time poe /apps/cc/vasp46/vasp_opt\n");
	}
	elsif ($hostname=~/turing/){
	  print RUN ("time mpirun -np $proc /apps/Vasp/46/vasp_mpi\n"); # 4.6.21
#	    print RUN ("time mpirun -np $proc /apps/Vasp/46/vasp_MPI\n"); # 4.6.27
	}
	if ($icar==1){
	    my $vaspbase=basename($infile,@vaspext);
	    print RUN ("cp OUTCAR $vaspbase.vout\n");
	    print RUN ("cp POTCAR $vaspbase.potc\n");
	    print RUN ("cp POSCAR $vaspbase.pcar\n");
	    print RUN ("cp CONTCAR $vaspbase.ccar\n");
	    print RUN ("cp KPOINTS $vaspbase.kpts\n");
	}
	print RUN ("\n");
	
    }
    elsif ($gulp==1){
	if ($mach=~/rs6000/){
	    print RUN ("time poe /apps/cc/gulp1.3/Src/$cmd < $infile >> $outfile\n\n");
	}
	elsif ($hostname=~/turing/){
#	    print ("proc=$proc\n");
	    if ($proc > 1){
		print RUN ("time mpirun -np $proc /apps/Gulp/13/gulpmpi < $infile >>$outfile \n\n");
	    }
	    else{
		print RUN ("time /apps/Gulp/13/gulp < $infile >>$outfile \n\n");
	    }
	}
	elsif ($hostname=~/darwin/){
#	    print ("proc=$proc\n");
	    if ($proc > 1){
		print RUN ("time mpirun -np $proc -machinefile \$PBS_NODEFILE /usr/local/gulp1.3.2/gulp.5 < $infile >>$outfile \n\n");
	    }
	    else{
		print RUN ("time /usr/local/gulp1.3.2/gulp < $infile >>$outfile \n\n");
	    }
	}
    }
    elsif ($adf==1){
	print RUN ("time $cmd < $infile >> $outfile\n\n");
	print RUN ("/bin/rm -f TAPE\*\n");
#log	print RUN ("mv logfile\* $logfile\n");
    }
    elsif ($adfrun==1){
	print RUN ("time ./$infile \n\n");
#	print RUN ("/bin/rm -f TAPE\*\n");
#log	print RUN ("mv logfile\* $logfile\n");
    }
    elsif ($cpmd==1){
	if ($mach=~/rs6000/){
	    if ($proc > 1){
#		print RUN ("time poe /apps/cc/CPMD391/cpmd.x  $infile >> $outfile\n");
#	print RUN ("time poe /apps/cc/CPMD391/cpmd-smp.x  $infile >> $outfile\n");
#	print RUN ("time poe /apps/cc/CPMD/cpmd.x  $infile >> $outfile\n");
#	print RUN ("time poe /apps/cc/CPMD/cpmd_smp_64.x  $infile >> $outfile\n");
		print RUN ("time poe /apps/cpmd/CPMD-3.9.2/cpmd.x  $infile >> $outfile\n");
#		print RUN ("time poe /Scratch/cc0404/CPMD-3.9.2/CPMD-3.9.2/IBM-SP4/cpmd.x  $infile >> $outfile\n");
	    }
	    else{
#		print RUN ("time poe /apps/cc/CPMD391/cpmd.x  $infile >> $outfile\n");
	print RUN ("/apps/cc/CPMD/cpmd.x  $infile >> $outfile\n");
#		print RUN ("time poe /apps/cpmd/CPMD-3.9.2/cpmd.x  $infile >> $outfile\n");

	    }
	}
	elsif ($hostname=~/darwin/){
	    print RUN ("time mpirun -np \$NPROCS -machinefile \$PBS_NODEFILE /usr/local/cpmd/cpmd.x $infile > $outfile\n");
	}
	elsif ($hostname=~/turing/){
#	    print RUN ("time mpirun -np $proc /apps/acad/Cpmd/392/cpmd_mpi.x $infile > $outfile\n");
	    print RUN ("time mpirun -np $proc /apps/Cpmd/392/cpmd_MPI2.x $infile > $outfile\n");
	}
	else{
	    print RUN ("time mpijob-gm mpirun -np $proc /home/smambs/bin/cpmd-mpi.x $infile >> $outfile\n");
	}
    }
    else{
	print("Don't know what's happening. Suppose to be g98 or castep...\n");
    }
}

# Change permissions on executable
chmod (0755, "$runfile");
if($mach=~/rs6000/){
    if ($run =~ /y/){
	print ("llsubmit $runfile\n");
	system ("llsubmit $runfile");
    }
    else {
	print ("To submit, run this: ");
	print ("llsubmit $runfile\n");
    }
    if (($g98==1)||($g03==1)||($cmd=~/g98/)||($cmd=~/g03/)||($adf==1)||($adfrun==1)){
	print RUN ("\n# Clean Up...\n/bin/rm -rf $scratchdir\n");
    }
    print RUN ("# Create this file when done\n");
    print RUN ("touch $base.done\n\n");
    print RUN ("# To submit, run this: \n");
    print RUN ("# llsubmit $runfile\n");
}
elsif($hostname=~/darwin/ || $hostname=~/turing/){
#     my $qcmd="qsub -e $efile -o $ofile";
    my $qcmd="qsub";
#      print RUN ("-e $efile -o $ofile");
#      if (defined $cputime){
##	  $qcmd="$qcmd"." -l walltime=$cputime";
#	  print RUN ("-lwalltime=$cputime");
#      }
#      if ($proc>1){
#	  if ($hostname=~/turing/){
#	  $qcmd="$qcmd"." -l nodes=1:ppn=$proc:IA64";
#	      print RUN ("#PBS -l nodes=1:ppn=$proc:IA64\n");
#	  }
#	  elsif($hostname=~/darwin/){
#	  $qcmd="$qcmd"." -l nodes=$proc:ppn=$proc";
#	      print RUN ("#PBS -l nodes=1:ppn=$proc\n");
#	  }
#      }
    if ($run =~ /y/){
	print ("$qcmd $runfile\n");
	system ("$qcmd $runfile");
    }
    else {
	print ("To submit, run this: ");
	print ("$qcmd $runfile\n");
    }
    
    if (($g98==1)||($g03==1)||($cmd=~/g98/)||($cmd=~/g03/)||($adf==1)||($adfrun==1)){
	print RUN ("\n# Clean Up...\n/bin/rm -rf $scratchdir\n");
    }
    print RUN ("\n# Create this file when done\n");
    print RUN ("touch $base.done\n\n");
    print RUN ("# To submit, run this: \n");
    print RUN ("# $qcmd $runfile\n");
}
elsif ($mach=~/sparc/){
    if($run=~/y/){
	print ("Running Interactively...\n");
	system("./$runfile > $ofile 2> $efile")
	}
    else{
	print("To run, do this: \n");
	print("./$runfile \>\& $efile\n");
    }
    if (($g98==1)||($g03==1)||($cmd=~/g98/)||($cmd=~/g03/)){
	print RUN ("\n# Clean Up...\n/bin/rm -rf $scratchdir\n");
    }
    print RUN ("# Create this file when done\n");
    print RUN ("touch $base.done\n\n");
}
else{
    if ($run =~ /y/){
	print ("bsub -q $q -o $ofile -J $molecule $runfile\n");
	system ("bsub -q $q -o $ofile -J $molecule $runfile");
    }
    else {
	print ("To submit, run this: ");
	print ("bsub -q $q -o $ofile -J $molecule $runfile\n");
    }
    print RUN ("# To submit, run this: ");
    print RUN ("# bsub -q $q -o $ofile -J $molecule $runfile\n");
}
#close RUN;

sub chkmv {
    my $file=$_[0];
    my @splits=split(/\.\w+$/,$file);
    my $base=$splits[0];
    #d    print("base=$base\n");
    my $chk="$base.chk";
    my $xfr="$base.xfr";
    my $fchk="$base.fchk";
    my $chkgz="$base.chk.gz";
    my $xfrgz="$base.xfr.gz";
    my $fchkgz="$base.fchk.gz";
    unless (-e $chk){    
	if(-e $xfrgz){
	    print RUN ("gunzip $xfrgz\n");
	    print RUN ("chkmove u $xfr $chk\n");
	}
	elsif(-e $xfr){
	    print RUN ("chkmove u $xfr $chk\n");
	}
	elsif(-e $chkgz){
	    print RUN ("gunzip $chkgz\n");
	}
	elsif(-e $fchk){
	    print RUN ("formchk $fchk $chk\n");
	}
	elsif(-e $fchkgz){
	    print RUN ("gunzip $fchkgz\n");
	    print RUN ("formchk $fchk $chk\n");
	}
    }
}

#Finds the name of the directory
sub updirname{
    my $dir=$_[0];
    $dir=~s/.*\/(\w+)/$1/;
#  print (" dir=$dir\n");
    return($dir);
}
sub help{
    print <<EOH;
_______________________________________________________________________________
                        gorun help.
_______________________________________________________________________________

A program to submit jobs to the queue
Creates submission script for various platforms.

Another fine program by Michael Sullivan
michael\@ihpc.a-star.edu.sg
http://homepage.mac.com/mbsullivan/home.html

Use .inp for CPMD
Use .com for Gaussian
Use INCAR for VASP
Use .rundat for CASTEP

General switches:
-time 1:00:00 to run for an hour
-proc 4 to run on 4 procs using MPI
-mp 1 to spawn openMP processes on each MPI or Linda job (only on loadleveler)
-linda 4 to run Gaussian on 4 procs using Linda
-pplib . to use the basis set from the current directory for CPMD
-pplib /Home2/nus/nusmbs/basis/PPLIB to use the ones in my home directory
-r no if you don't want it to run but just make the queue file
-mem set memory (only works on SVU)
-link link Gaussian jobs together meaning that the sequence of jobs runs
      using the previous checkpoint file. Just copies old checkpoint for new job.

EOH
exit;
}
