#!/bin/bash
#
# This script can be used to facilitate supervisor development. Its core feature is allowing
# faster development iterations by bind-mounting the local './dist' directly into the running
# supervisor container.
#
# Setting the '--mount-nm' flag in either 'run' or 'buildrun' action will bind-mount
# './node_modules/' into the running supervisor. In this case, it's up to the developer
# to make sure that the correct dependencies are installed.
#
# Usage: dindctl action [options]
#
# Actions:
#   build                   build local supervisor image. By default it will be balena/amd64-supervisor:master, you can override the tag with --tag.
#   run [options]           build dind host container, run it (with name balena_supervisor_1), which will include the specified supervisor image and run it.
#   buildrun [options]      run 'build' and then immediately 'run' the built container.
#   refresh                 recompile sources in './src' and restart supervisor container on dind host - requires --mount-dist in order to work properly.
#   logs [-f]               print out supervisor log files - use '-f' to follow instead, or any other arguments you'd send to journalctl.
#   stop                    stop dind supervisor host container.
# Options:
#   --arch | -a [arch]      architecture of the supervisor to build (default: amd64 )
#   --image | -i [image]    image name for supervisor image to build/use ( default: balena/$ARCH-supervisor:master )
#   --dind-image [image]    image to use for the resinos-in-container host (default: resin/resinos:2.12.5_rev1-intel-nuc)
#   --dind-container [name] container name suffix for the dind host container ( default: "supervisor", which will produce a container named resinos-in-container-supervisor)
#   --mount-dist            bind-mount './dist/' (where webpack stores the built js) from local development environment into supervisor container.
#   --mount-nm              bind-mount './node_modules/' from local development environment into supervisor container.
#   --mount-backup          bind-mount './tools/dind/backup.tgz' to simulate a migration backup.
#   --preload | -p          use tools/dind/apps.json to preload an application image into the dind host.
#   --config | -c [file]    path to config.json, relative to tools/dind ( default: config.json )
#   --tag | -t [tag]        for the "build" action, specify the tag to build (default: master)
#   --no-clean              for the "stop" action, skip removing the data, boot and state volumes
#
# See README.md for examples.
#
# The script requires make and docker.
#

THIS_FILE=$0

set -o errexit
set -o pipefail

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
SUPERVISOR_BASE_DIR="${DIR}"

ARCH="amd64"
SUPERVISOR_IMAGE="balena/${ARCH}-supervisor:master"
DIND_IMAGE="resin/resinos:2.27.0_rev1-intel-nuc"
MOUNT_DIST="false"
MOUNT_NODE_MODULES="false"
CONTAINER_NAME="supervisor"
PRELOADED_IMAGE=""
OPTIMIZE="true"
CONFIG_FILENAME="config.json"
TAG="master"
CLEAN_VOLUMES="true"
MOUNT_BACKUP="false"

function showHelp {
	cat $THIS_FILE | awk '{if(/^#/)print;else exit}' | tail -n +2 | sed 's/\#//' | sed 's|dindctl|'$THIS_FILE'|'
}

function parseOptions {
	while [[ $# -ge 1 ]]
	do
		case $1 in
			--mount-dist)
				MOUNT_DIST="true"
				;;
			--mount-nm)
				MOUNT_NODE_MODULES="true"
				;;
			--mount-backup)
				MOUNT_BACKUP="true"
				;;
			-p|--preload)
				PRELOADED_IMAGE="true"
				;;
			-i|--image)
				SUPERVISOR_IMAGE="$2"
				shift || { echo "--image provided not specified" && exit 1; }
				;;
			-c|--config)
				CONFIG_FILENAME="$2"
				shift || { echo "--config provided not specified" && exit 1; }
				;;
			--dind-image)
				DIND_IMAGE="$2"
				shift || { echo "--dind-image provided not specified" && exit 1; }
				;;
			--dind-container)
				CONTAINER_NAME="$2"
				shift || { echo "--dind-container provided not specified" && exit 1; }
				;;
			-a|--arch)
				ARCH="$2"
				shift || { echo "--arch provided not specified" && exit 1; }
				;;
			-t|--tag)
				TAG="$2"
				shift || { echo "--tag provided not specified" && exit 1; }
				;;
			-n|--no-optimize)
				OPTIMIZE="false"
				;;
			--no-clean)
				CLEAN_VOLUMES="false"
				;;
			*)
				echo "Warning: unknown argument: $arg"
				;;
		esac
		shift
	done
}

function buildSupervisor {
	echo "Building supervisor image for architecture $ARCH and tagging as $TAG"
	ARCH="$ARCH" TAG="$TAG" bash automation/build.sh
}

function buildSupervisorSrc {
	if [ "$OPTIMIZE" = "true" ]; then
		echo "Rebuilding supervisor source"
		( cd "$SUPERVISOR_BASE_DIR" && npm install && npm run build )
	else
		echo "Rebuilding supervisor source without optimizations"
		( cd "$SUPERVISOR_BASE_DIR" && npm install && npm run build -- --env.noOptimize )
	fi
}

function refreshSupervisorSrc {
	buildSupervisorSrc
	echo "Restarting the supervisor container"
	docker exec -ti resinos-in-container-$CONTAINER_NAME systemctl restart resin-supervisor
}

function runDind {
	if [ ! -f "tools/dind/resinos-in-container/resinos-in-container.sh" ]; then
		git submodule update --init
	fi
	if [ "$MOUNT_DIST" = "true" ]; then
		buildSupervisorSrc
		echo "Running with mounted dist folder"
	fi
	if [ "$PRELOADED_IMAGE" = "true" ]; then
		echo "Running with preloaded apps"
	fi
	if ! ( docker inspect $SUPERVISOR_IMAGE &> /dev/null ); then
		echo "$SUPERVISOR_IMAGE not available locally, pulling"
		docker pull $SUPERVISOR_IMAGE
	fi
	echo "Starting dind supervisor"
	make -C "$SUPERVISOR_BASE_DIR" \
		ARCH="$ARCH" \
		SUPERVISOR_IMAGE="$SUPERVISOR_IMAGE" \
		MOUNT_DIST="$MOUNT_DIST" \
		MOUNT_NODE_MODULES="$MOUNT_NODE_MODULES" \
		MOUNT_BACKUP="$MOUNT_BACKUP" \
		PRELOADED_IMAGE="$PRELOADED_IMAGE" \
		CONTAINER_NAME="$CONTAINER_NAME" \
		CONFIG_FILENAME="$CONFIG_FILENAME" \
		DIND_IMAGE="$DIND_IMAGE" \
		run-supervisor
}

function stopDind {
	echo "Stopping dind supervisor"
	make -C "$SUPERVISOR_BASE_DIR" CONTAINER_NAME="$CONTAINER_NAME" stop-supervisor
	if [ "$CLEAN_VOLUMES" = "true" ]; then
		cleanDind
	fi
}

function cleanDind {
	echo "Cleaning dind supervisor volumes"
	docker volume rm "resin-boot-$CONTAINER_NAME" "resin-state-$CONTAINER_NAME" "resin-data-$CONTAINER_NAME" &> /dev/null || true
}

function logs {
	docker exec -ti resinos-in-container-$CONTAINER_NAME journalctl $@
}

action="$1"
shift || true

if [ "$action" = "logs" ]; then
	logs "$@"
else
	parseOptions "$@"
	case $action in
		build)
			buildSupervisor
			;;
		run)
			stopDind
			runDind
			;;
		buildrun)
			buildSupervisor && runDind
			;;
		refresh)
			refreshSupervisorSrc
			;;
		stop)
			stopDind
			;;
		*)
			showHelp
			;;
	esac
fi