#!/bin/bash

PROGRAM_NAME=$(basename "$0")
ABRA_CONFIG=abra.yml

if [ -z "$COMPOSE_FILE" ]; then
	COMPOSE_FILE="compose.yml"
fi

yml_pattern_exists() {
	PATTERN=$1

	if ! type yq > /dev/null 2>&1; then
		echo "$(tput setaf 1)ERROR: yq program is not installed$(tput sgr0)"
		exit
	fi

	if [ -f $ABRA_CONFIG ]; then
			RESULT=$(yq read $ABRA_CONFIG "$PATTERN")

			if [ "$RESULT" != 0 ]; then
				return 0
			fi
	fi

	return 1
}

if [ "$1" == "-a" ]; then
	STACK_NAME=$2
	shift 2
fi

if [ -f abra.yml ]; then
	if yml_pattern_exists stack_name; then
		STACK_NAME=$(yq read abra.yml stack_name)
	fi
fi

if [ -z "$STACK_NAME" ]; then
	echo "$(tput setaf 1)ERROR: \$STACK_NAME must be set (e.g. export STACK_NAME=my_cool_app)$(tput sgr0 )"
	exit
fi

if type direnv > /dev/null 2>&1 && ! direnv status | grep -q 'Found RC allowed true'; then
	echo "$(tput setaf 1)ERROR: direnv is blocked, run direnv allow$(tput sgr0)"
	exit
fi

if [ -f abra-commands.sh ]; then
	# shellcheck disable=SC1091
	. abra-commands.sh
fi

sub_help() {
	echo "Usage: $PROGRAM_NAME [-a STACK_NAME] <subcommand> [options]"
	echo ""
	echo "Subcommands:"
	echo "    cp SRC_PATH SERVICE:DEST_PATH         copy files to a container"
	echo "    deploy                                let 'em rip"
	echo "    logs SERVICE [ARGS]                   tail logs from a deployed service"
	echo "    run SERVICE CMD                       run a command in the specified service's container"
	echo "    run_args SERVICE ARGS CMD             run, passing extra args to docker exec"
	echo "    secret_generate SECRET VERSION [CMD]  generate a secret, store it in pass & as a Docker secret"
	echo "    ... (custom commands)"
	echo ""
	echo "Make sure \$STACK_NAME is set using direnv or -a"
	echo ""
	echo "Runs compose.yml by default, set e.g. COMPOSE_FILE=\"compose.yml:compose2.yml\" to override"
}

sub_secret_help() {
	echo "Usage: $PROGRAM_NAME [-a STACK_NAME] secret <subcommand> [options]"
	echo ""
	echo "Subcommands:"
	echo "    generate [PW]                         generate & store secret"
}

sub_secret_generate(){
	SECRET=$1
	VERSION=$2
	PW=${3:-pwqgen}

	if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
		echo "Usage: $PROGRAM_NAME secret_generate SECRET VERSION"
		exit
	fi

	$PW | tee \
		>(docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" -) \
		>(pass insert "hosts/autonomic-swarm/${STACK_NAME}/${SECRET}" -m)
}

sub_secret() {
	SUBCOMMAND=$1
	shift
	# shellcheck disable=SC2068
	parse_subcommand "$SUBCOMMAND" "secret" $@
}

sub_run_args(){
	SERVICE=$1
	DOCKER_ARGS=$2

	shift 2

	if [ -z "$SERVICE" ]; then
		echo "Usage: $PROGRAM_NAME run SERVICE [CMD]"
		exit
	fi

	CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
		| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)

	if [ -z "$CONTAINER" ]; then
		echo "Container not found! 🚨"
		exit
	fi

	# shellcheck disable=SC2086
	docker exec $DOCKER_ARGS -it "$CONTAINER" "$@"

	return
}

sub_run(){
	SERVICE=$1

	shift

	sub_run_args "$SERVICE" "" "$@"
}

sub_deploy (){
	echo "About to deploy:"
	echo "  Compose: $(tput setaf 3)${PWD}/${COMPOSE_FILE}$(tput sgr0)"
	if [ -n "$DOMAIN" ]; then
		echo "  Domain: $(tput setaf 2)${DOMAIN}$(tput sgr0)"
	fi
	echo "  Stack: $(tput setaf 1)${STACK_NAME}$(tput sgr0)"

	read -rp "Continue? (y/[n])? " choice

	case "$choice" in
		y|Y ) ;;
		n|N ) return;;
		* ) return;;
	esac

	# shellcheck disable=SC2086
	if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
		if [ -n "$DOMAIN" ]; then
			echo "$(tput setaf 2)Yay! App should be available at https://${DOMAIN}$(tput sgr0)"
		else
			echo "$(tput setaf 2)Yay! That worked. No \$DOMAIN defined, check logs.(tput sgr0)"
		fi
	else
		echo "$(tput setaf 1)Oh no! Something went wrong 😕 Check errors above$(tput sgr0)"
	fi
}

sub_logs (){
	SERVICE=$1

	shift

	if [ $# -eq 0 ]; then
		LOGS_ARGS="\
		--follow \
		--no-trunc \
		--details \
		--timestamps"
	else
		# shellcheck disable=SC2124
		LOGS_ARGS=$@
	fi

	# shellcheck disable=SC2086
	docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
}

sub_cp() {
	SOURCE=$1
	DEST=$2

	SERVICE=$(echo "$SOURCE" | grep -o '^[^:]\+:' || echo "$DEST" | grep -o '^[^:]\+:')
	SERVICE=$(echo "$SERVICE" | tr -d ':')

	if [ -z "$SERVICE" ]; then
		echo "$(tput setaf 1)ERROR: Can't find SERVICE in either SRC or DEST$(tput sgr0)"
		echo ""
		echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
		echo "       $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
		exit
	fi

	CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
		| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)

	if [ -z "$CONTAINER" ]; then
		echo "$(tput setaf 1)ERROR: Can't find a ${STACK_NAME}_${SERVICE}$(tput sgr0)"
		exit
	fi

	CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE/$CONTAINER/")

	# shellcheck disable=SC2086
	docker cp $CP_ARGS
}

sub_context_help() {
	echo "Usage: $PROGRAM_NAME [-a STACK_NAME] context <subcommand> [options]"
	echo ""
	echo "Subcommands:"
	echo "    init HOST [USER] [PORT]               set up remote Docker context"
	echo "    use                                   activate remote Docker context"
}

sub_context_init() {
	HOST="$1"
	USERNAME="$2"
	PORT="$3"

	if [ -n "$PORT" ]; then
		PORT=":$PORT"
	fi

	if [ -n "$USERNAME" ]; then
		USERNAME="$USERNAME@"
	fi

	docker context create "$HOST" \
		--docker "host=ssh://$USERNAME$HOST$PORT"
}

sub_context_use() {
	docker context use "$1"
}

sub_context() {
	SUBCOMMAND2=$1
	shift
	# shellcheck disable=SC2068
	parse_subcommand "$SUBCOMMAND2" "context" $@
}

parse_subcommand() {
	SUBCOMMAND="$1"
	PREFIX=$2

	if [ -n "$PREFIX" ]; then
		PPREFIX="_$2"
		SPREFIX="$2 "
		SSPREFIX=" $2"
	fi

	case $SUBCOMMAND in
		"" | "-h" | "--help")
			"sub${PPREFIX}_help"
			;;
		*)
			shift 2
			"sub${PPREFIX}_${SUBCOMMAND}" "$@"
			if [ $? = 127 ]; then
				echo "Error: '$SPREFIX$SUBCOMMAND' is not a known subcommand." >&2
				echo "       Run '$PROGRAM_NAME$SSPREFIX --help' for a list of known subcommands." >&2
				exit 1
			fi
			;;
	esac
}

SUBCOMMAND=$1
shift

# shellcheck disable=SC2086,SC2068
parse_subcommand $SUBCOMMAND "" $@
