#!/usr/bin/env zsh
#####################################################################

use cloud/aws/cli
use cloud/aws/zshparse/overrides

DEPENDENCIES+=(jq mount sort sudo)
REQUIRED_ENV+=(AWS__EFS__LOCAL_MOUNT_POINT)

#####################################################################

USAGE__description='
	interactively mount an AWS EFS volume to the local filesystem
'

#####################################################################

MAIN() {
	local PARSERS=(cloud.aws.zshparse.overrides)
	eval "$(utils.parse.autosetup)"
	utils.io.getsudo || return 1
	##########################################

	{
		mkdir -p -- "${AWS__EFS__LOCAL_MOUNT_POINT}" \
			|| sudo mkdir -p -- "${AWS__EFS__LOCAL_MOUNT_POINT}"
	} &>/dev/null

	[ -d "${AWS__EFS__LOCAL_MOUNT_POINT}" ] \
		|| echo.error "unable to create local mount point '${AWS__EFS__LOCAL_MOUNT_POINT}'" \
		|| return

	local FS_ID=$(\
		$AWS efs describe-file-systems \
			| jq -r '.[] | .[] | .FileSystemId' \
			| utils.fzf 'select a filesystem to mount' \
	)
	[ ! ${FS_ID} ] && utils.abort

	local MOUNT_POINT="${AWS__EFS__LOCAL_MOUNT_POINT}/${FS_ID}"
	[ -d "${MOUNT_POINT}" ] && sudo rmdir "${MOUNT_POINT}" &>/dev/null
	[ -d "${MOUNT_POINT}" ] && {
		echo.status "${FS_ID} is already mounted"
		return 0
	}

	local MOUNT_TARGETS=$($AWS efs describe-mount-targets --file-system-id ${FS_ID})
	local ZONE=$(\
		echo ${MOUNT_TARGETS} \
			| jq -r '.[] | .[] | .AvailabilityZoneName' \
			| sort -u | utils.fzf 'select availability zone'\
	)
	[ ! "${ZONE}" ] && utils.abort

	local MOUNT_IP=$(\
		echo ${MOUNT_TARGETS} \
			| jq -r ".[] | .[] | select (.AvailabilityZoneName == \"${ZONE}\") | .IpAddress" \
			| head -n1 \
	)

	echo.success  'ready to mount!'
	echo.status "
		file system id    : ${FS_ID}
		availability zone : ${ZONE}
		file system ip    : ${MOUNT_IP}
		local mount point : ${MOUNT_POINT}
	 "
	echo.reminder 'for private file-systems, you must be connected to the appropriate VPN'
	Yn 'proceed?' || utils.abort

	sudo mkdir -- "${MOUNT_POINT}" \
		&& sudo mount \
			-t nfs4 \
			-o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport \
			"${MOUNT_IP}:/" \
			"${MOUNT_POINT}" \
		&& echo.success "mounted at '${MOUNT_POINT}'" \
		|| {
			sudo rmdir -- "${MOUNT_POINT}" &>/dev/null
			echo.error "unable to mount '${FS_ID}'"
		}
}
