# !/bin/bash

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

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

if [ ! -z `hostname -f | grep "bullx"` ]; then
   ml --show-hidden -t av 2>&1 > /dev/null| awk '{print $1 ",1"}' | grep -v -e '/,\|:' > anselm.csv
   ./modules.py > anselm.md
else
   ml --show-hidden -t av 2>&1 > /dev/null| awk '{print $1 ",2"}' | grep -v -e '/,\|:' > salomon.csv
   ./modules.py > salomon.md 

   MODULEBASE="/appsuv/modules"
   MODULEPATH=""

    for dir in `ls $MODULEBASE`
    do
        # Exclude following directories
        if [[ ($dir == "all") ]]; then
            continue
        fi
        # In case that it's directory
        if [ -d $MODULEBASE/$dir ]; then
            if [ -z $MODULEPATH ]; then
                MODULEPATH="$MODULEBASE/$dir"
            else
                MODULEPATH="$MODULEPATH:$MODULEBASE/$dir"
            fi
        fi
    done

   export MODULEPATH
   
   ml --show-hidden -t av 2>&1 > /dev/null| awk '{print $1 ",3"}' | grep -v -e '/,\|:' > uv2000.csv
   ./modules.py > uv2000.md

fi

DATE=`date -R`
git diff --quiet --exit-code || git commit -am "$DATE" --quiet
git push
