#!/usr/bin/bash

git clone git@gitlab.com:kit-scc-scs-course/ci-course.git
cd                                          ci-course
git switch -c doc
cat > Readme.md <<EOF
# Hallo World

Hello World program implemented in C
EOF
git add Readme.md
git commit -m "Added initial version of Readme.md"
git push --set-upstream origin doc

git switch main

git switch -c src

cat >helloWorld.c <<EOF
int main(int* argc, char *argv[]) {
  printf("Hello World\n");
}
EOF
git add helloWorld.c
git commit -m "Added initial version of helloWorld.c"
git push --set-upstream origin src

cat >helloWorld.c <<EOF
#include <stdio.h>

int main(int* argc, char *argv[]) {
  printf("Hello World\n");
}
EOF
git commit -m "Added missing include directive to helloWorld.c" helloWorld.c
git push

cat > .gitignore <<EOF
helloWorld
EOF
git add .gitignore
git commit -m "Add helloWorld binary to .gitignore"
git push

git switch -c ci
cat >.gitlab-ci.yml <<'EOF'
stages:
- build
- test

build-hello-world-job:
  stage: build
  script: cc helloWorld.c -o helloWorld
  artifacts:
    paths:
    - helloWorld
    expire_in: 1 day

test-hello-world-job:
  stage: test
  script: if [[ $(helloWorld) != "Hello World" ]]; then exit 1; fi
EOF
git add .gitlab-ci.yml
git commit -m "Add gitlab CI configuration"
git push --set-upstream origin ci

git switch src
git switch -c ci-srun
cat >.gitlab-ci.yml <<'EOF'
stages:
- build
- test

build-hello-world-job:
  stage: build
  script: cc helloWorld.c -o helloWorld
  artifacts:
    paths:
    - helloWorld
    expire_in: 1 day

test-hello-world-job:
  stage: test
  script: 
  - salloc -p dev_cpuonly     -t 20 -N 1 -n 76              mpirun helloWorld | uniq -c | tee out_dev_cpuonly
  - if [[ ! "$(< out_dev_cpuonly)" =~ "76 Hello World" ]]; then exit 1; fi
  - salloc -p dev_accelerated -t 20 -N 1 -n 76 --gres=gpu:4 mpirun helloWorld | uniq -c | tee out_dev_accelerated
  - if [[ ! "$(< out_dev_accelerated)" =~ "76 Hello World" ]]; then exit 1; fi
EOF
git add .gitlab-ci.yml
git commit -m "Add gitlab CI srun configuration"
git push --set-upstream origin ci-srun
