#!/bin/bash

# exit this script if any error occurs
set -e

# get the directory where this script is in
SCRIPTDIR="$(realpath -s "$(dirname "$0")")"

# define sample solution
SAMPLE_SOLUTION="$(scontrol show hostnames $(scontrol show partition small | grep -e '^ *Nodes=' | sed 's/ *Nodes=//') | wc -l)
$(scontrol show hostnames $(scontrol show partition small_fat | grep -e '^ *Nodes=' | sed 's/ *Nodes=//') | wc -l)
$(scontrol show hostnames $(scontrol show partition small_gpu | grep -e '^ *Nodes=' | sed 's/ *Nodes=//') | wc -l)
$(scontrol show nodes --all | sed -rn 's/^NodeName=([^ ]+).+$/\1/p' | grep -v "login" | sort | uniq | wc -l)
$(scontrol show partition dev | sed -rn 's/.+MaxTime=([[:digit:]]+).*/\1/p' | sed 's/^0*//')
$(( $(scontrol show partition dev | sed -rn 's/.+mem=([[:digit:]]+)M.*/\1/p') / $(scontrol show partition dev | sed -rn 's/.+node=([[:digit:]]+).*/\1/p') / 1000 ))"

# check if the solution file exists and get solution
SOLUTION_FILE="${SCRIPTDIR}/my_solution.txt"
if [ ! -f "${SOLUTION_FILE}" ]; then
  echo "ERROR: No solution file not found!"
  exit 1
fi
SOLUTION="$(cut -d ' ' -f 1 "$SOLUTION_FILE" | sed '/^[[:space:]]*$/d')"

# check if there is a different number of lines
if [ $(echo -e "$SAMPLE_SOLUTION" | wc -l) -ne $(echo -e "$SOLUTION" | wc -l) ]; then
  echo "ERROR: Number of results do not match!"
  exit 1
fi

# check the results
if diff <(echo -e "$SAMPLE_SOLUTION") <(echo -e "$SOLUTION") > /dev/null; then
  echo "Congratulations! Task solved correctly."
else
  echo "ERROR: The solution file does not contain the expected output!"
  ARR_A=($(echo -e $SAMPLE_SOLUTION))
  ARR_B=($(echo -e $SOLUTION))
  for i in "${!ARR_A[@]}"; do
    [ "${ARR_A[i]}" != "${ARR_B[i]}" ] && echo "       Line $((i + 1)) is wrong!"
  done
fi

# exit the script with zero exit code
exit 0
