#!/usr/bin/env bash


if [ -z ${2} ]; then
	COOLDOWN="60 minutes"
else
	COOLDOWN=${2}
fi

case ${1} in
	"delay")
		atq | cut -f 1 | xargs atrm || true
		cd /root && echo '/usr/local/bin/seppuku' | at now + 2 hours
		;;
	"cooldown")
		atq | cut -f 1 | xargs atrm
		cd /root && echo '/usr/local/bin/seppuku' | at now + ${COOLDOWN}
		;;
	*)
		ID=`curl -s http://169.254.169.254/hetzner/v1/metadata | grep 'instance-id' | awk '{print $2}'`
		HETZNER_API_TOKEN=`cat /home/git/.hetzner`

		echo "PERFORMING SEPPUKU. Goodbye."

		sleep 2

		curl -s \
			-X DELETE \
			-H "Authorization: Bearer $HETZNER_API_TOKEN" \
			"https://api.hetzner.cloud/v1/servers/${ID}"
		;;
esac
