#!/bin/bash
# Used in CI workflow: install hugo binary from GitHub
# Copyright © 2021 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -euo pipefail

readonly PROJECT_ROOT=$(pwd)
readonly TARBALL=hugo.tar.gz
readonly SOURCE="https://github.com/gohugoio/hugo/releases/download/v0.110.0/hugo_0.110.0_linux-amd64.tar.gz"

readonly BIN_PATH=tmp/bin
readonly BIN=$BIN_PATH/hugo

readonly DIST=public

source $(pwd)/scripts/lib.sh

help() {
	cat << EOF
hugo.sh: hugo build script
USAGE:
    hugo.sh <options>
OPTIONS:
  b   build     build website
  c   clean     clean dependencies and build artifacts
  d   deploy    deploy build to branch
  h   help      print this help menu
  i   install   install build dependencies
  u   url       make urls relative
  z   hugo      invoke hugo
EOF
}

download() {
	if [ ! -e $BIN_PATH ];
	then
		mkdir -p $BIN_PATH
		cd $BIN_PATH
		echo "[*] Downloading Hugo"
		wget --quiet --output-document=$TARBALL $SOURCE
		tar -xvzf $TARBALL > /dev/null
		rm $TARBALL
		echo "[*] Downloaded hugo into $BIN"
		cd $PROJECT_ROOT
	fi
}

init() {
	if [ ! -d $BIN_PATH ]
	then
		mkdir $BIN_PATH
	fi

	if [ ! -f $BIN ]
	then
		cd $BIN_PATH
		download
	fi
}

run() {
	$BIN "${@:1}"
}

build() {
	run
}

no_absolute_url() {
	sed -i 's/https:\/\/mcaptcha.org//g' $(find public -type f | grep html)
}

clean() {
	rm -rf $BIN_PATH || true
	rm -rf $DIST || true
	echo "[*] Workspace cleaned"
}

# $1: branch name
# $2: directory containing build assets
# $3: Author in <author-name author@example.com> format
deploy() {
	cd $PROJECT_ROOT
	original_branch=$(git branch --show-current)
	tmp_dir=$(mktemp -d)
	cp -r $2/* $tmp_dir

	if [[ -z $(git ls-remote --heads origin ${1}) ]]
	then
		echo "[*] Creating deployment branch $1"
		git checkout --orphan $1
	else
		echo "[*] Deployment branch $1 exists, pulling changes from remote"
		git fetch origin $1
		git switch $1
	fi

	git rm -rf .
	/bin/rm -rf *
	cp -r $tmp_dir/* .
	git add --all
	if [ $(git status --porcelain | xargs | sed '/^$/d' | wc -l) -gt 0 ];
	then
		echo "[*] Repository has changed, committing changes"
		git commit \
			--author="$3" \
			--message="new deploy: $(date --iso-8601=seconds)"
	fi
	git checkout $original_branch
}

check_arg $1
download

if match_arg $1 'c' 'clean'
then
	clean
elif match_arg $1 'i' 'install'
then
	init
elif match_arg $1 'd' 'deploy'
then
	check_arg $2
	check_arg $3
	check_arg $4
	deploy $2 $3 $4
elif match_arg $1 'b' 'build'
then
	build
elif match_arg $1 'h' 'help'
then
	help
elif match_arg $1 'z' 'hugo'
then
	hugo "${@:3}"
else
	echo "Error: $1 is not an option"
	help
	exit 1
fi

exit 0
