#!/bin/bash

source ./allSetParams.sh

if [[ $LoadManager == "SLURM" ]]; then

#########################################
########## 1. SLURM Prepare submitPrecip.sh
cat > submitPrecip.sh  << 'EOF'
#!/bin/bash
#SBATCH --account=cpc-s2s
#SBATCH --qos=batch
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH -J obsEAF
#SBATCH --time=01:00:00         # Each task has the full 8 hours
#SBATCH -o zobsprec.out
#SBATCH -e zobsprec.err
#SBATCH --mail-user=zewdu.segele@noaa.gov
#SBATCH --mail-type=FAIL

module purge
module load contrib pyncl

workingDir=`pwd`

ncl $workingDir/obsPrecip.ncl

#ncl $workingDir/precDownloadStdz.ncl

#ncl $workingDir/sstDownloadStdz.ncl
EOF


#########################################
########## 2. SLURM Prepare submitFcst.sh
cat > submitFcst.sh  << 'EOF'
#!/bin/bash
#SBATCH --account=cpc-s2s
#SBATCH --qos=batch
#SBATCH -J zseason
#SBATCH --nodes=1               # Each task needs exactly 1 node
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=40      # Use all 40 cores on the node
#SBATCH --time=08:00:00         # Each task has the full 8 hours
#SBATCH -o zFcstRuns.out
#SBATCH -e zFcstRuns.err
#SBATCH --exclusive
#SBATCH --mail-user=zewdu.segele@noaa.gov
#SBATCH --mail-type=FAIL

module purge
module load spack-managed-x86-64_v3 r/4.4.0
module load contrib pyncl
module load netcdf-c/4.9.2
module load udunits/2.2.28

workingDir=`pwd`
Rscript $workingDir/multiNominalRegression.R
EOF



#########################################
########## 3. SLURM Prepare submitBestModel.sh
cat > submitBestModel.sh  << 'EOF'
#!/bin/bash
#SBATCH --account=cpc-s2s
#SBATCH --qos=batch
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1  # Only 1 core needed for aggregation
#SBATCH --mem=32G          # Ensure you have enough RAM to hold the big arrays
#SBATCH -J bestmod
#SBATCH --time=01:00:00         # Each task has the full 8 hours
#SBATCH -o zbestmodel.out
#SBATCH -e zbestmodel.err
#SBATCH --exclusive
#SBATCH --mail-user=zewdu.segele@noaa.gov
##SBATCH --mail-type=BEGIN,END,FAIL
#SBATCH --mail-type=FAIL

module purge
module load spack-managed-x86-64_v3 r/4.4.0
module load contrib pyncl
module load netcdf-c/4.9.2
module load udunits/2.2.28

workingDir=`pwd`
Rscript $workingDir/templateBestModelFreq.R
EOF

#########################################
########## 4. SLURM Prepare submitPlot.sh
cat > mySlurmBatch  << 'EOF'
#!/bin/bash
#SBATCH --account=cpc-s2s
#SBATCH --qos=batch
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH -J finalPlots
#SBATCH --time=01:00:00         # Each task has the full 8 hours
#SBATCH -o zallplots.out
#SBATCH -e zallplots.err
##SBATCH --exclusive
#SBATCH --mail-user=zewdu.segele@noaa.gov
##SBATCH --mail-type=BEGIN,END,FAIL
#SBATCH --mail-type=FAIL

module purge
module load contrib pyncl
EOF

cat mySlurmBatch allSetParams.sh allFinalPlots.sh > submitPlots.sh

elif [[ $LoadManager == "QSUB" ]]; then
  echo "$LoadManager"
elif [[ $LoadManager == "LINUX" ]]; then
  echo "Starting preparation for  $LoadManager"

#########################################
########## 1. LINUX Prepare submitPrecip.sh

cat >  submitPrecip.sh << 'EOF'
#!/bin/bash

CONDA_BASE="/cpc/data/software/miniconda"
source "$CONDA_BASE/bin/activate"
conda activate renv

workingDir=$(pwd)
echo $workingDir
ncl "${workingDir}"/obsPrecip.ncl
ncl "${workingDir}"/precDownloadStdz.ncl
ncl "${workingDir}"/sstDownloadStdz.ncl
EOF

#########################################
########## 2. LINUX Prepare submitFcst.sh
cat > submitFcst.sh  << 'EOF'
#!/bin/bash

CONDA_BASE="/cpc/data/software/miniconda"
source "$CONDA_BASE/bin/activate"
conda activate renv

workingDir=$(pwd)
Rscript $workingDir/multiNominalRegression.R
EOF

#########################################
########## 3. LINUX Prepare submitPlots.sh
cat > myLinuxBatch  << 'EOF'
#!/bin/bash

CONDA_BASE="/cpc/data/software/miniconda"
source "$CONDA_BASE/bin/activate"
conda activate renv

workingDir=`pwd`
EOF

cat myLinuxBatch allSetParams.sh allFinalPlots.sh > submitPlots.sh
####
## Done
echo "DONE: "$LoadManager
fi


