#!/usr/bin/env bash

# Parameters are passed to stdin, we need to use read to get them. Only ref matters for what we do.
read oldrev newrev ref
# By default, ref contains refs/heads/branch-name. We use bash substitution mechanism to remove "refs/heads/".
branch_name=${ref/refs\/heads\//}

# Once again, force a checkout to the branch.
( unset GIT_DIR && cd .. && git checkout -f $branch_name )

# You can add additionnal steps to complete checkout on the current branch (install deps, build JS/CSS files, …)
# For instance:
( cd .. && source .venv/bin/activate && pip install -r requirements.txt )

# We move to the folder that contains our code.
# We extract the domain (and guincorn instance name) from the folder
# by keeping only the name of the current directory of the full path.
# Finally, we restart the proper instance of gunicorn.
( cd ..; unit_name=$(pwd); unit_name=${unit_name##*/}; sudo systemctl restart "gunicorn@${unit_name}" )
# You could simply this line by using the $USER variable and creating BASE_DOMAIN="bl.test" into ``sudo systemcl restart "gunicorn@${USER}.${BASE_DOMAIN}"``
