#!/bin/bash

export build_dir=xios_test_suite/RUN_TEST_SUITE/build_${arch}_${mode}
export svnR=$(svn info --show-item revision ../../)
if [[ -v enable_mem_track ]]; then
  echo "enable_mem_track is set to '$enable_mem_track'"
fi

echo "Start Building XIOS ... "

if [[ ${xios_machine_name} == "jeanzay" ]]; then

  rm -f compile.sh
  echo "#!/bin/bash" >> compile.sh 
  echo "#SBATCH --ntasks=1 ">> compile.sh
  echo "#SBATCH --cpus-per-task=16 ">> compile.sh
  echo "#SBATCH --hint=nomultithread ">> compile.sh
  echo "#SBATCH -t 00:30:00  " >> compile.sh
  echo "#SBATCH -o compile.out ">> compile.sh
  echo "#SBATCH -e compile.err ">> compile.sh
  echo "#SBATCH --account="$user_account  >> compile.sh
  echo "#SBATCH --job-name=XIOS_rev"$svnR >> compile.sh
  echo "#SBATCH --exclusive " >> compile.sh
  echo "cd \${SLURM_SUBMIT_DIR}">> compile.sh
  echo "ulimit -c 0">> compile.sh
  echo "cd ../.. ">> compile.sh
  echo "./make_xios --arch_path `pwd`/../ARCH --arch ${arch} --${mode} --use_tv --build_dir ${build_dir} --job 16" >> compile.sh
  cmd=$(sbatch compile.sh)
  jobid="${cmd//[!0-9]/}"
  output=$(squeue -u uim55ri | grep ${jobid})
  while [ ! -z "$output" ]
  do
    echo "compiling job " $jobid "pending/running for about" ${i} seconds
    sleep 30
    ((i+=30))
    output=$(squeue -u uim55ri | grep ${jobid})
  done

fi

if [[ ${xios_machine_name} == "irene" ]]; then

  rm -f compile.sh
  echo "#!/bin/bash" >> compile.sh 
  echo "#MSUB -o compile.out" >> compile.sh
  echo "#MSUB -e compile.err" >> compile.sh
  echo "#MSUB -eo" >> compile.sh
  echo "#MSUB -c 16" >> compile.sh
  echo "#MSUB -n 1" >> compile.sh
  echo "#MSUB -X" >> compile.sh
  echo "#MSUB -x" >> compile.sh
  echo "#MSUB -T 1800" >> compile.sh
  echo "#MSUB -q skylake" >> compile.sh
  echo "#MSUB -A "$user_account >> compile.sh
  echo "#MSUB -Q test" >> compile.sh
  echo "#MSUB -r XIOS_rev"$svnR >> compile.sh
  echo "#MSUB -m work,scratch" >> compile.sh
  echo "cd ../.. ">> compile.sh
  echo "./make_xios --arch_path `pwd`/../ARCH --arch ${arch} --${mode} --use_tv ${enable_mem_track} --build_dir ${build_dir} --job 16" >> compile.sh
  cmd=$(ccc_msub compile.sh)
  jobid="${cmd//[!0-9]/}"
  output=$(ccc_mpp | grep ${jobid})
  while [ ! -z "$output" ]
  do
    echo "compiling job " $jobid "pending/running for about" ${i} seconds
    sleep 30
    ((i+=30))
    output=$(ccc_mpp | grep ${jobid})
  done

fi

build_info=$(sacct -j $jobid -o State)
if [[ $build_info == *"COMPLETED"* ]]; then
  build_ok=true
else
  build_ok=false
fi


if [ "$build_ok" = true ]
then
  echo "XIOS Build Finished. Start Unit Tests"
  bash ./my_run.sh
  
  if [ "$COPY_TO_SERVER" = true ]
  then
    echo "with files copy"
    rundir=${xios_test_suite_repository}/RUN
    mkdir -p $rundir ; CHMOD  $rundir
    mkdir -p ${rundir}/test_${xios_machine_name} ; CHMOD ${rundir}/test_${xios_machine_name}

    cp report_${svnR}_${arch}_${mode}.txt ${rundir}/test_${xios_machine_name}/test_${svnR}_${xios_machine_name}_${arch}_${mode}.txt

    CHMOD ${rundir}/test_${xios_machine_name}/test_${svnR}_${xios_machine_name}_${arch}_${mode}.txt

    mkdir -p ${rundir}/def_files ;  CHMOD ${rundir}/def_files
    mkdir -p ${rundir}/def_files/${svnR} ;  CHMOD ${rundir}/def_files/${svnR}

    for i in $(ls -d test_*/)
    do
      mkdir -p ${rundir}/def_files/${svnR}/${i%%} ; CHMOD ${rundir}/def_files/${svnR}/${i%%}
      cp ${i%%}/user_param_*.json ${rundir}/def_files/${svnR}/${i%%}
      for j in $(ls -d ${i%%/}/CONFIG_*)
      do
        mkdir -p ${rundir}/def_files/${svnR}/${j%%} ; CHMOD ${rundir}/def_files/${svnR}/${j%%}
        cp ${j%%}/all_param.def ${rundir}/def_files/${svnR}/${j%%}
      done
    done
  else
    echo "without files copy"
  fi

else
  echo "XIOS Build Failed. Skip Unit Tests"
fi



