#!/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 obsRAIV
#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 --job-name=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 zout/zFcstRuns.o%j
#SBATCH -e zFcstRuns.err
#SBATCH --exclusive
#SBATCH --mail-user=zewdu.segele@noaa.gov
#SBATCH --mail-type=FAIL,TIME_LIMIT_90

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"
#########################################
########## 1. SLURM Prepare submitPrecip.sh
cat > submitPrecip.sh  << 'EOF'
#!/bin/bash
#PBS -q batch
#PBS -N obsDat
#PBS -l select=1:mpiprocs=1
#PBS -l walltime=01:00:00
#PBS -o zobsPrec.out
#PBS -e zobsPrec.err
#PBS -l place=excl
#PBS -M zewdu.segele@noaa.gov
#PBS -m a

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
#PBS -q batch
#PBS -N zseason
#PBS -l select=1:ncpus=40:mpiprocs=1
#PBS -l walltime=08:00:00
#PBS -o zout/zFcstRuns.oJ
#PBS -e zFcstRuns.err
#PBS -l place=excl
#PBS -M zewdu.segele@noaa.gov
#PBS -m a

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
#PBS -q batch
#PBS -l select=1:ncpus=1:mpiprocs=1:mem=32gb
#PBS -N bestmod
#PBS -l walltime=01:00:00
#PBS -o zbestmodel.out
#PBS -e zbestmodel.err
#PBS -l place=excl
#PBS -M zewdu.segele@noaa.gov
#PBS -m a

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
#PBS -q batch
#PBS -l select=1:ncpus=1:mpiprocs=1
#PBS -N finalPlots
#PBS -l walltime=01:00:00
#PBS -o zallplots.out
#PBS -e zallplots.err
#PBS -M zewdu.segele@noaa.gov
#PBS -m a

module purge
module load contrib pyncl
EOF

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


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

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

cat >  tmp1 << 'EOF'
#!/bin/bash

# 1. Point explicitly to the micromamba binary in your home folder
export MAMBA_EXE="${HOME}/.local/bin/micromamba"
export MAMBA_ROOT_PREFIX="${HOME}/micromamba"

# 2. Generate the shell hook using the explicit binary path
eval "$($MAMBA_EXE shell hook --shell bash)"

# 3. Now activate will work flawlessly
micromamba activate cpc_rcca_env


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

EOF


if [[ $DownloadDat == "True" ]]; then

cat >  tmp2 << 'EOF'
ncl "${workingDir}"/precDownloadStdz.ncl
ncl "${workingDir}"/sstDownloadStdz.ncl
EOF

else
cat >  tmp2 << 'EOF'
#ncl "${workingDir}"/precDownloadStdz.ncl
#ncl "${workingDir}"/sstDownloadStdz.ncl
EOF
fi

cat tmp1 tmp2  >  submitPrecip.sh

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

# 1. Point explicitly to the micromamba binary in your home folder
export MAMBA_EXE="${HOME}/.local/bin/micromamba"
export MAMBA_ROOT_PREFIX="${HOME}/micromamba"

# 2. Generate the shell hook using the explicit binary path
eval "$($MAMBA_EXE shell hook --shell bash)"

# 3. Now activate will work flawlessly
micromamba activate cpc_rcca_env

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

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

# 1. Point explicitly to the micromamba binary in your home folder
export MAMBA_EXE="${HOME}/.local/bin/micromamba"
export MAMBA_ROOT_PREFIX="${HOME}/micromamba"

# 2. Generate the shell hook using the explicit binary path
eval "$($MAMBA_EXE shell hook --shell bash)"

# 3. Now activate will work flawlessly
micromamba activate cpc_rcca_env

workingDir=`pwd`
EOF

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


