#!/bin/bash
# vim: ai ts=2 sw=2 et sts=2 ft=sh

PWD="/home/easybuild/git/it4i-modules"
cd $PWD || exit 1

STATE=$(git fetch 2>&1 | grep master)
if [ ! -z "$STATE" ]; then
  git pull
  ./get_modules.sh
  exit 0
fi

CLUSTER="$CLUSTERNAME"
if [ -z "$CLUSTER" ]; then
   echo "CLUSTERNAME not defined"
   exit 1
fi

if [ "$CLUSTER" == "DGX" ]; then
   ml --show-hidden -t av 2>&1 | awk '{print $1 ",4"}' | grep -v -e '/,\|:' | sort -u > dgx.csv
  ./modules.py > dgx.md
elif [ "$CLUSTER" == "BARBORA" ]; then
   ml --show-hidden -t av 2>&1 | awk '{print $1 ",2"}' | grep -v -e '/,\|:' | sort -u > barbora.csv
     ./modules.py > barbora.md
elif [ "$CLUSTER" == "KAROLINA" ]; then
   ml --show-hidden -t av 2>&1 | awk '{print $1 ",1"}' | grep -v -e '/,\|:' | sort -u > karolina.csv
     ./modules.py | sed "s|(N/A)|()|g" > karolina.md
fi

DATE=$(date -R)
git diff --exit-code || git commit -am "$DATE"
git push origin master
