#!/bin/bash
set -e -u

while read oldrev newrev ref
do
  if [[ $ref =~ .*/main$ ]]; then
    echo "main ref received"

		echo "moving into temporary work tree"; {
      gitdir=$PWD
      cd ~/jonathan.is
    }

    echo "checking out latest source"; {
      git --work-tree=$PWD --git-dir=$gitdir checkout --force
    }

    echo "generating static site"; {
      podman-compose build
    }

    echo "restarting server"; {
      podman-compose up --detach
    }

    echo "updating post-receive hook for future runs"; {
      cp hooks/post-receive $0
    }
  fi
done
