#!/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
   python2  ./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
elif [ "$CLUSTER" == "CS" ]; then
  echo "Resource: avx2 -> Filename: cs_all_avx2"
  ml --show-hidden -t av 2>&1 | awk '{print $1 ",2"}' | grep -v -e '/,\|:' | sort -u > cs_all_avx2.csv
  ./modules.py > cs_all_avx2.md

  for resource in "/apps/aarch64/modules,aarch64" "/apps/avx512/modules,avx512" "/apps/ppc64le/modules,ppc64le" "/apps/el9/aarch64/modules,el9_aarch64"
  do
    MODULEBASE=${resource%,*}
    MODULEPATH=""
    FILENAME=${resource#*,}

    echo "Resource: $MODULEBASE -> Filename: cs_$FILENAME"

    echo "MODULEBASE: $MODULEBASE"

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

    export MODULEPATH

    echo $MODULEPATH

    ml --show-hidden -t av 2>&1 | awk '{print $1 ",4"}' | grep -v -e '/,\|:' | sort -u > "cs_$FILENAME".csv
    ./modules.py > "cs_$FILENAME".md
  done
fi

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

git log --since=2024-01-01 -p -- karolina.csv | grep "^+\|^-\|Date" | grep -v "@\|karolina" | sed 's|^+|   * add |g' | sed 's|^-|   * remove |g' | sed 's|Date:   |\n## |g' | sed 's|+0200|\n|g' > karolina-changelog.md
git log --since=2024-01-01 -p -- barbora.csv | grep "^+\|^-\|Date" | grep -v "@\|barbora" | sed 's|^+|   * add |g' | sed 's|^-|   * remove |g' | sed 's|Date:   |\n## |g' | sed 's|+0200|\n|g' > barbora-changelog.md
git log --since=2024-01-01 -p -- dgx.csv | grep "^+\|^-\|Date" | grep -v "@\|dgx" | sed 's|^+|   * add |g' | sed 's|^-|   * remove |g' | sed 's|Date:   |\n## |g' | sed 's|+0200|\n|g' > dgx-changelog.md

git add karolina-changelog.md barbora-changelog.md dgx-changelog.md
git commit -am "$DATE Changelog"
git push origin master
