#!/bin/sh
# vim:set filetype=sh:

# Tiny Cloud

set -e

: "${PREFIX:=/usr}"
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR/tiny-cloud/common"

usage() {
	cat <<-EOF
		Usage: ${0##*/} [-h | --help] { boot | early | main | final | --bootstrap {complete|incomplete|status} | --enable | --disable }
	EOF
}

bootstrap_complete() {
	touch "$TINY_CLOUD_VAR/.bootstrap-complete"
}
bootstrap_incomplete() {
	rm -f "$TINY_CLOUD_VAR/.bootstrap-complete"
}
is_bootstrap_complete() {
	[ -f "$TINY_CLOUD_VAR/.bootstrap-complete" ]
}

args=$(getopt -o hEDb: --long help,enable,disable,bootstrap: -n ${0##*/} -- "$@") || {  usage >&2; exit 1; }
if [ $# -eq 0 ]; then
	usage >&2
	exit 1
fi
eval set -- "$args"
while true; do
	case "$1" in
		-h|--help) usage; exit 0;;
		-b|--bootstrap) shift
			case "$1" in
				complete)	# indicate bootstrap is done
					bootstrap_complete
					log -i notice 'bootstrap marked complete';;
				incomplete)	# indicate bootstrap isn't done
					bootstrap_incomplete
					log -i warning 'bootstrap marked incomplete';;
				status) is_bootstrap_complete && echo 'complete' || echo 'incomplete' ;;
				*) usage >&2; exit 1;;
			esac
			exit 0;;
		-[ED]|--enable|--disable)	# just openrc for now
			: "${ROOT:=}"	# for mounted volumes
			# always start with a clean slate
			rm -f "$ETC"/runlevels/*/tiny-cloud*
			log -i info "- tiny-cloud* services removed from all runlevels"
			if [ "$1" = '-D' ] || [ "$1" = '--disable' ]; then
				exit 0
			fi
			ln -s /etc/init.d/tiny-cloud-boot "$ETC"/runlevels/boot
			log -i info "+ tiny-cloud-boot service added to boot runlevel"
			for p in early main final; do
				ln -s "/etc/init.d/tiny-cloud-$p" "$ETC"/runlevels/default
				log -i info "+ tiny-cloud-$p service added to default runlevel"
			done
			exit 0;;
		--) shift; break;;
		*) usage >&2; exit 1;;
	esac
	shift
done

phase="$1"
shift

case "$phase" in
	boot|early|main|final) ;;
	*) usage >&2; exit 1;;
esac

if [ -e "$ETC"/tiny-cloud.disabled ]; then
	log -i -t "$phase" info "tiny-cloud disabled"
	exit 0
fi

# is initial bootstrap already done?
if is_bootstrap_complete; then
	log -i -t "$phase" info "already bootstrapped"
	exit 0;
fi

# load init functions
. "$LIBDIR/tiny-cloud/init"

### non-overrideable stuff

# should we skip this action?
skip_action() {
	local action="$1"
	for i in $SKIP_INIT_ACTIONS; do
		[ "$i" = "$action" ] && return 0
	done
	return 1
}

# mandatory final action...
init__bootstrap_complete() {
	bootstrap_complete
}
INIT_ACTIONS_FINAL="${INIT_ACTIONS_FINAL} bootstrap_complete"

### let's do stuff!

case "$phase" in
	boot)	INIT_ACTIONS="$INIT_ACTIONS_BOOT";;
	early)	INIT_ACTIONS="$INIT_ACTIONS_EARLY";;
	main)	INIT_ACTIONS="$INIT_ACTIONS_MAIN";;
	final)	INIT_ACTIONS="$INIT_ACTIONS_FINAL";;
	*)		usage >&2; exit 1;;
esac

printf '\n' >&2
for ACTION in $INIT_ACTIONS; do
	if skip_action "$ACTION"; then
		printf '   -- ' >&2
		log -i -t "$phase" notice "$ACTION: skipped"
		continue
	fi
	printf '   ++ ' >&2
	log -i -t "$phase" info "$ACTION: starting"
	RESULT="unknown"
	LEVEL="err"
	if type "init__$ACTION" | grep -q -w "function"; then
		if "init__$ACTION" "$@"; then
			RESULT="done"
			LEVEL="info"
		else
			RESULT="failed"
		fi
	fi
	printf '   ++ ' >&2
	log -i -t "$phase" "$LEVEL" "$ACTION: $RESULT"
	if  [ "$RESULT" = "failed" ]; then
		exit 1
	fi
done
