#!/usr/bin/env bash

###### Global help

DOC="
The cooperative cloud utility belt 🎩🐇

Usage: abra [-c|--config=<path>] [-e|--env=<path>] [-h|--help]
            [-s|--stack=<path>] [-v|--version] <command> [<args>...]

Options:
  -c, --config=<path>  Stack configuration to use
  -e, --env=<path>     Environment variables to load
  -h, --help           Show this message and exit
  -s, --stack=<path>   Name of the target stack
  -v, --version        Show program version

These are common Abra commands used in various situations:

Manage an application lifecycle:
  deploy      let 'em rip
  upgrade     upgrade to the latest version

View logs from running applications:
  logs        tail logs from a deployed service
  multilogs   tail logs from a whole stack

Interact with running containers:
  cp          copy files to a container
  run         run a command in the specified service's container

Manage servers:
  server add  <host> [user] [port]  add docker context
  server rm   <host>                remove docker context
  server use  <host>                activate docker context
  server init <host>                activate docker swarm mode

Manage secrets:
  secret generate <secret> <version> [pwgen]     generate & store secret
  secret insert   <secret> <version> <password>  save password in docker & pass

See 'abra help <command>' to read about a specific subcommand.
"

# docopt parser below, refresh this parser with `docopt.sh abra`
# shellcheck disable=2016,1075,2154
docopt() { parse() { if ${DOCOPT_DOC_CHECK:-true}; then local doc_hash
if doc_hash=$(printf "%s" "$DOC" | (sha256sum 2>/dev/null || shasum -a 256)); then
if [[ ${doc_hash:0:5} != "$digest" ]]; then
stderr "The current usage doc (${doc_hash:0:5}) does not match \
what the parser was generated with (${digest})
Run \`docopt.sh\` to refresh the parser."; _return 70; fi; fi; fi
local root_idx=$1; shift; argv=("$@"); parsed_params=(); parsed_values=()
left=(); testdepth=0; local arg; while [[ ${#argv[@]} -gt 0 ]]; do
if [[ ${argv[0]} = "--" ]]; then for arg in "${argv[@]}"; do
parsed_params+=('a'); parsed_values+=("$arg"); done; break
elif [[ ${argv[0]} = --* ]]; then parse_long
elif [[ ${argv[0]} = -* && ${argv[0]} != "-" ]]; then parse_shorts
elif ${DOCOPT_OPTIONS_FIRST:-false}; then for arg in "${argv[@]}"; do
parsed_params+=('a'); parsed_values+=("$arg"); done; break; else
parsed_params+=('a'); parsed_values+=("${argv[0]}"); argv=("${argv[@]:1}"); fi
done; local idx; if ${DOCOPT_ADD_HELP:-true}; then
for idx in "${parsed_params[@]}"; do [[ $idx = 'a' ]] && continue
if [[ ${shorts[$idx]} = "-h" || ${longs[$idx]} = "--help" ]]; then
stdout "$trimmed_doc"; _return 0; fi; done; fi
if [[ ${DOCOPT_PROGRAM_VERSION:-false} != 'false' ]]; then
for idx in "${parsed_params[@]}"; do [[ $idx = 'a' ]] && continue
if [[ ${longs[$idx]} = "--version" ]]; then stdout "$DOCOPT_PROGRAM_VERSION"
_return 0; fi; done; fi; local i=0; while [[ $i -lt ${#parsed_params[@]} ]]; do
left+=("$i"); ((i++)) || true; done
if ! required "$root_idx" || [ ${#left[@]} -gt 0 ]; then error; fi; return 0; }
parse_shorts() { local token=${argv[0]}; local value; argv=("${argv[@]:1}")
[[ $token = -* && $token != --* ]] || _return 88; local remaining=${token#-}
while [[ -n $remaining ]]; do local short="-${remaining:0:1}"
remaining="${remaining:1}"; local i=0; local similar=(); local match=false
for o in "${shorts[@]}"; do if [[ $o = "$short" ]]; then similar+=("$short")
[[ $match = false ]] && match=$i; fi; ((i++)) || true; done
if [[ ${#similar[@]} -gt 1 ]]; then
error "${short} is specified ambiguously ${#similar[@]} times"
elif [[ ${#similar[@]} -lt 1 ]]; then match=${#shorts[@]}; value=true
shorts+=("$short"); longs+=(''); argcounts+=(0); else value=false
if [[ ${argcounts[$match]} -ne 0 ]]; then if [[ $remaining = '' ]]; then
if [[ ${#argv[@]} -eq 0 || ${argv[0]} = '--' ]]; then
error "${short} requires argument"; fi; value=${argv[0]}; argv=("${argv[@]:1}")
else value=$remaining; remaining=''; fi; fi; if [[ $value = false ]]; then
value=true; fi; fi; parsed_params+=("$match"); parsed_values+=("$value"); done
}; parse_long() { local token=${argv[0]}; local long=${token%%=*}
local value=${token#*=}; local argcount; argv=("${argv[@]:1}")
[[ $token = --* ]] || _return 88; if [[ $token = *=* ]]; then eq='='; else eq=''
value=false; fi; local i=0; local similar=(); local match=false
for o in "${longs[@]}"; do if [[ $o = "$long" ]]; then similar+=("$long")
[[ $match = false ]] && match=$i; fi; ((i++)) || true; done
if [[ $match = false ]]; then i=0; for o in "${longs[@]}"; do
if [[ $o = $long* ]]; then similar+=("$long"); [[ $match = false ]] && match=$i
fi; ((i++)) || true; done; fi; if [[ ${#similar[@]} -gt 1 ]]; then
error "${long} is not a unique prefix: ${similar[*]}?"
elif [[ ${#similar[@]} -lt 1 ]]; then
[[ $eq = '=' ]] && argcount=1 || argcount=0; match=${#shorts[@]}
[[ $argcount -eq 0 ]] && value=true; shorts+=(''); longs+=("$long")
argcounts+=("$argcount"); else if [[ ${argcounts[$match]} -eq 0 ]]; then
if [[ $value != false ]]; then
error "${longs[$match]} must not have an argument"; fi
elif [[ $value = false ]]; then
if [[ ${#argv[@]} -eq 0 || ${argv[0]} = '--' ]]; then
error "${long} requires argument"; fi; value=${argv[0]}; argv=("${argv[@]:1}")
fi; if [[ $value = false ]]; then value=true; fi; fi; parsed_params+=("$match")
parsed_values+=("$value"); }; required() { local initial_left=("${left[@]}")
local node_idx; ((testdepth++)) || true; for node_idx in "$@"; do
if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true
return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then
left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi
return 0; }; either() { local initial_left=("${left[@]}"); local best_match_idx
local match_count; local node_idx; ((testdepth++)) || true
for node_idx in "$@"; do if "node_$node_idx"; then
if [[ -z $match_count || ${#left[@]} -lt $match_count ]]; then
best_match_idx=$node_idx; match_count=${#left[@]}; fi; fi
left=("${initial_left[@]}"); done; ((testdepth--)) || true
if [[ -n $best_match_idx ]]; then "node_$best_match_idx"; return 0; fi
left=("${initial_left[@]}"); return 1; }; optional() { local node_idx
for node_idx in "$@"; do "node_$node_idx"; done; return 0; }; oneormore() {
local i=0; local prev=${#left[@]}; while "node_$1"; do ((i++)) || true
[[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; switch() { local i
for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
eval "((var_$1++))" || true; else eval "var_$1=true"; fi; return 0; fi; done
return 1; }; value() { local i; for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; local value
value=$(printf -- "%q" "${parsed_values[$l]}"); if [[ $3 = true ]]; then
eval "var_$1+=($value)"; else eval "var_$1=$value"; fi; return 0; fi; done
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1345}
usage=${DOC:40:131}; digest=f2937; shorts=(-c -e -h -s -v)
longs=(--config --env --help --stack --version); argcounts=(1 1 0 1 0)
node_0(){ value __config 0 true; }; node_1(){ value __env 1 true; }; node_2(){
switch __help 2; }; node_3(){ value __stack 3 true; }; node_4(){
switch __version 4; }; node_5(){ value _command_ a; }; node_6(){
value _args_ a true; }; node_7(){ optional 0 0; }; node_8(){ optional 1 1; }
node_9(){ either 2 2; }; node_10(){ optional 9; }; node_11(){ optional 3 3; }
node_12(){ either 4 4; }; node_13(){ optional 12; }; node_14(){ oneormore 6; }
node_15(){ optional 14; }; node_16(){ required 7 8 10 11 13 5 15; }; node_17(){
required 16; }; cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
printf "%s\n" "${DOC:40:131}" >&2; exit 1; }'; unset var___config var___env \
var___help var___stack var___version var__command_ var__args_; parse 17 "$@"
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__config" "${prefix}__env" \
"${prefix}__help" "${prefix}__stack" "${prefix}__version" "${prefix}_command_" \
"${prefix}_args_"; if declare -p var___config >/dev/null 2>&1; then
eval "${prefix}"'__config=("${var___config[@]}")'; else
eval "${prefix}"'__config=()'; fi; if declare -p var___env >/dev/null 2>&1; then
eval "${prefix}"'__env=("${var___env[@]}")'; else eval "${prefix}"'__env=()'; fi
eval "${prefix}"'__help=${var___help:-false}'
if declare -p var___stack >/dev/null 2>&1; then
eval "${prefix}"'__stack=("${var___stack[@]}")'; else
eval "${prefix}"'__stack=()'; fi
eval "${prefix}"'__version=${var___version:-false}'
eval "${prefix}"'_command_=${var__command_:-}'
if declare -p var__args_ >/dev/null 2>&1; then
eval "${prefix}"'_args_=("${var__args_[@]}")'; else eval "${prefix}"'_args_=()'
fi; local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2
for ((;docopt_i>0;docopt_i--)); do declare -p "${prefix}__config" \
"${prefix}__env" "${prefix}__help" "${prefix}__stack" "${prefix}__version" \
"${prefix}_command_" "${prefix}_args_"; done; }
# docopt parser above, complete command for generating this parser is `docopt.sh abra`

PROGRAM_NAME=$(basename "$0")

###### Utility functions

yml_pattern_exists() {
	PATTERN=$1

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

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

	return 1
}

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
}

error() {
	echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
	exit 1
}

warning() {
	echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
}

success() {
	echo "$(tput setaf 2)$*$(tput sgr0)"
}

###### Top-level arguments

ABRA_CONFIG=abra.yml
if [ "$1" == "-c" ]; then
	ABRA_CONFIG=$2
	shift 2
fi

if [ "$1" == "-e" ]; then
	ABRA_ENV=$2
	shift 2
fi

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

###### Load config

if [ -f "$ABRA_CONFIG" ]; then
	require_yq

	if yml_pattern_exists stack_name; then
		STACK_NAME=$(yq read "$ABRA_CONFIG" stack_name)
	fi
	# FIXME load other variables somehow
fi

if [ -n "$ABRA_ENV" ]; then
	# shellcheck disable=SC1090
	source "$ABRA_ENV" || error "Unable to load env from '$ABRA_ENV'"
fi

###### Default settings

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

if [ -z "$ABRA_STACK_DIR" ]; then
	ABRA_STACK_DIR="stacks/$SERVICE"
fi

load_context() {
	if [ -z "$DOCKER_CONTEXT" ]; then
		warning "\$DOCKER_CONTEXT not set, (slowly) looking it up"
		# shellcheck disable=SC2063
		DOCKER_CONTEXT=$(docker context ls | grep '*' | cut -d' ' -f1)
		# FIXME 3wc: make sure grep doesn't parse this, we're want a literal '*'
	fi
}

###### Safety checks

require_yq() {
	if ! type yq > /dev/null 2>&1; then
		error "yq program is not installed"
	fi
}

require_multitail() {
	if ! type multitail > /dev/null 2>&1; then
		error "multitail program is not installed"
	fi
}

require_stack() {
	if [ -z "$STACK_NAME" ]; then
		error "no stack_name, export \$STACK_NAME=my_cool_app or add it to abra.yml"
	fi
}

require_stack_dir() {
	if [ -z "$ABRA_STACK_DIR" ] || [ ! -d "$ABRA_STACK_DIR" ]; then
		error "can't find \$ABRA_STACK_DIR '$ABRA_STACK_DIR'"
	fi
}

if [ -z "$ABRA_ENV" ] && [ -f .envrc ] && type direnv > /dev/null 2>&1 && ! direnv status | grep -q 'Found RC allowed true'; then
	error "direnv is blocked, run direnv allow"
fi

###### Custom commands

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

if [ -f "$ABRA_STACK_DIR/abra-commands.sh" ]; then
	# shellcheck disable=SC1090
	source "$ABRA_STACK_DIR/abra-commands.sh"
fi

###### Subcommand `secret`

sub_secret_insert() {
	require_stack
	load_context

	SECRET=$1
	VERSION=$2
	PW=$3

	if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
		error "Required arguments missing"
		echo
		echo "Usage: $PROGRAM_NAME secret insert SECRET VERSION PW"
		exit
	fi

	echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
	echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
}

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

	if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
		error "Required arguments missing"
		echo
		echo "Usage: $PROGRAM_NAME secret generate SECRET VERSION [PW]"
		exit
	fi

	PW=$($PWGEN)

	success "Password: $PW"

	sub_secret_insert "$SECRET" "$VERSION" "$PW"
}

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

###### Subcommand `run`

sub_run(){
	require_stack

	if [ "$1" == "-u" ]; then
		RUN_USER="-u $2"
		shift 2
	fi

	SERVICE=$1

	shift

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

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

	if [ -z "$CONTAINER" ]; then
		error "Can't find a container for ${STACK_NAME}_${SERVICE}"
		exit
	fi

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

	return
}

###### Subcommand `deploy`

sub_deploy (){
	require_stack
	require_stack_dir
	load_context

	echo "About to deploy:"
	echo "  Context: $(tput setaf 4)${DOCKER_CONTEXT}$(tput sgr0)"
	if [ "${COMPOSE_FILE/:/}" == "${COMPOSE_FILE}" ]; then
		echo "  Compose: $(tput setaf 3)${ABRA_STACK_DIR}/${COMPOSE_FILE}$(tput sgr0)"
	else
		echo "  Compose: $(tput setaf 3)${ABRA_STACK_DIR}/"
		IFS=':' read -ra COMPOSE_FILES <<< "$COMPOSE_FILE"
		for COMPOSE in "${COMPOSE_FILES[@]}"; do
			echo "   - ${COMPOSE}"
		done
		tput sgr0
	fi
	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

	(
		cd "$ABRA_STACK_DIR" || error "\$ABRA_STACK_DIR '$ABRA_STACK_DIR' not found"
		# shellcheck disable=SC2086
		if docker stack deploy -c ${COMPOSE_FILE/:/ -c } "$STACK_NAME"; then
			if [ -n "$DOMAIN" ]; then
				success "Yay! App should be available at https://${DOMAIN}"
			else
				success "Yay! That worked. No \$DOMAIN defined, check logs."
			fi
		else
			error "Oh no! Something went wrong 😕 Check errors above"
		fi
	)
}

###### Subcommand `logs`

# Inspired by https://github.com/moby/moby/issues/31458#issuecomment-475411564
sub_multilogs() {
	require_stack
	require_multitail

	# Get a list of the service names
	SERVICES=$(docker stack services --format "{{.Name}}" "${STACK_NAME}")
	# Sort the service names
	SERVICES=$(echo "${SERVICES}" | sort)
	# Create the command to run
	COMMAND='multitail --mergeall'
	for SERVICE in ${SERVICES}; do
			COMMAND="${COMMAND} -L 'docker service logs --tail 20 -f ${SERVICE}'"
	done
	# Run the command
	bash -c "${COMMAND}"
}

sub_logs (){
	require_stack

	SERVICE=$1

	if [ -z "$SERVICE" ]; then
		warning "No \$SERVICE provided, running multilogs"
		sub_multilogs
	fi

	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
}

###### Subcommand `cp`

sub_cp() {
	require_stack

	SOURCE=$1
	DEST=$2

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

	if [ -z "$SERVICE" ]; then
		echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
		echo "       $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
		echo ""
		error "Can't find SERVICE in either SRC or DEST"
	fi

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

	if [ -z "$CONTAINER" ]; then
		error "Can't find a container for ${STACK_NAME}_${SERVICE}"
		exit
	fi

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

	# shellcheck disable=SC2086
	docker cp ${CP_ARGS}
}

###### Subcommand `context`

sub_server_init() {
	load_context

	docker swarm init || true
	docker network create --driver=overlay proxy --scope swarm || true
}

sub_server_add() {
	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_server_rm() {
	HOST="$1"
	docker context rm "$HOST"
}

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

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


###### Subcommand `upgrade`

sub_upgrade() {
	curl -fsSL https://install.abra.autonomic.zone | bash
}

###### Docker alias subcommands

sub_stack() {
	# shellcheck disable=SC2068
	docker stack $@
}

sub_volume() {
	# shellcheck disable=SC2068
	docker volume $@
}

###### Main

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

abra() {
  eval "$(docopt "$@")"
}

abra "$@"
