#!/usr/bin/env bash

# DON'T MODIFY THIS FILE DIRECTLY!
# It was added automatically to this project via `botanist init`.
# If you want to make a change or fix broken behavior in this file,
# you should do so upstream in https://github.com/burninggarden/botanist.

set -e

if [[ $1 == '--verify' ]]; then
	if [ ! -f dist/.last-compile-time ]; then
		exit 1
	fi

	typeset -i LAST_MODIFICATION_TIME=$(find src test -type f -print0 |
		xargs -0 stat --format "%X %N" |
		sort -rn |
		head -1 |
		awk '{ print $1 }')

	typeset -i LAST_COMPILE_TIME=$(cat dist/.last-compile-time)

	if (( $LAST_MODIFICATION_TIME > $LAST_COMPILE_TIME )); then
		exit 1
	fi

	exit 0
fi

if [[ -f ./dist/.last-publish-time ]]; then
	exit 0
fi

echo "Recompiling..."
rm -rf dist/src
rm -rf dist/test
./node_modules/.bin/tsc --declaration
./bin/remap-paths
echo $(date +%s) > dist/.last-compile-time
