#!/bin/bash
set -euo pipefail

source "${HOME}/bin/script_framework.sh"

function usage() {
	echo "Usage: $0 [-h] [-k key] [-p prefix] <port>" >&2
}

args=(-p 31088 master.k8s.kserver.biz)
host=localhost
key=loki
remote="80"

# Parse options
while getopts ":hi:p:v" o; do
	case "${o}" in
	h)
		usage
		exit 0
		;;
	k)
		key="${OPTARG}"
		;;
	p)
		remote="${OPTARG}:80"
		;;
	*)
		usage
		fatal "Invalid option -- ${OPTARG}"
		;;
	esac
done
shift $((OPTIND - 1))

# Add port
port="${1:-}"
[[ -n $port ]] || fatal "Missing local port as first argument"

step "Starting port-forward..."
vault-sshadd ${key}
exec /usr/bin/ssh -o ControlPath=/tmp/shareport-${port} -R "${remote}:${host}:${port}" "${args[@]}"