#!/usr/bin/env bash

SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P )

IMAGE_NAME="yoursystemcz/node-interpretter"
IMAGE_VERSION="latest"

SERVICE_NAME="node-interpretter"


source ${SCRIPT_PATH}/../common.shinc

#
# Project specific variables
#

MOUNT_PATHS=()
RESTART=0

source ${SCRIPT_PATH}/shared.env.shinc 2> /dev/null
source ${SCRIPT_PATH}/env.shinc 2> /dev/null


__createLink() {
	local continue
	local hostPath="${1}"
	local binaryLink="${2}"

	if [[ -f "${hostPath}" || -L "${hostPath}" ]]; then
		__warn "There is already a ${hostPath}, are you sure you want to replace it? [y/(n)]:"
		read continue
		if [[ "${continue}" == "y" || "${continue}" == "Y" ]]; then
			__msg "Linking node binary at ${hostPath}"
			sudo rm -f "${hostPath}"
			sudo ln -s "${SCRIPT_PATH}/bin/${binaryLink}" "${hostPath}"
			[[ $? -ne 0 ]] && __err "Could not link ${binaryLink} binary at ${hostPath}" && return 137
		fi
	else
		__msg "Linking node binary at ${hostPath}"
		sudo ln -s "${SCRIPT_PATH}/bin/${binaryLink}" "${hostPath}"
	fi
}

__deleteLink() {
	local hostPath="${1}"
	local binaryLink="${2}"

	if [[ -L "${hostPath}" ]]; then
		local linkName=$(readlink -f "${hostPath}")
		if [[ $? -eq 0 && "${linkName}" == "${SCRIPT_PATH}/bin/${binaryLink}" ]]; then
			__warn "Removing link at ${hostPath}"
			sudo rm -f ${hostPath}
		fi
	fi
}

__createLinks() {
	__createLink "${NODE_HOST_PATH}" node
	__createLink "${NODE_HOST_PATH}js" node
	__createLink "${NPM_HOST_PATH}" npm
	__createLink "${NPX_HOST_PATH}" npx
	__createLink "${YARN_HOST_PATH}" yarn
	__createLink "${YARNPKG_HOST_PATH}" yarnpkg
}

__deleteLinks() {
	__deleteLink "${NODE_HOST_PATH}" node
	__deleteLink "${NODE_HOST_PATH}js" node
	__deleteLink "${NPM_HOST_PATH}" npm
	__deleteLink "${NPX_HOST_PATH}" npx
	__deleteLink "${YARN_HOST_PATH}" yarn
	__deleteLink "${YARNPKG_HOST_PATH}" yarnpkg
}

init() {
	__createLinks

	local mountPaths=""
        [[ ${#MOUNT_PATHS[@]} -eq 0 ]] && __err "No MOUNT_PATHS were specified. Please provide these in an .env.shinc file at ${SCRIPT_PATH}/.env.shinc" && return 137
        for mountPath in ${MOUNT_PATHS[@]}; do
                mountPaths="${mountPaths} -v ${mountPath}"
        done

	args="${mountPaths} -v ${SCRIPT_PATH}/configs/${SERVICE_NAME/plex_/}:/config"

        if [[ ${RESTART} -eq 1 ]]; then
                args="${args} --restart=always"
        fi

	docker create \
		--name ${SERVICE_NAME} \
		--user $(id -u) \
		-v ${SSH_KEY}:/home/node/.ssh/${SSH_KEY_NAME} \
		-v ${KNOWN_HOSTS}:/home/node/.ssh/known_hosts \
		-v npm-cache:/home/node/.npm \
		-e TZ=Europe/Prague \
		${args} \
		--net host \
		--entrypoint /cmd \
		${IMAGE_NAME}:${IMAGE_VERSION}

	[[ $? -ne 0 ]] && __err "Could not create the docker container" && return 137

	docker start ${SERVICE_NAME} && \
	docker exec --user 0 ${SERVICE_NAME} mkdir -p /usr/local/lib/node_modules && \
	docker exec --user 0 ${SERVICE_NAME} chown $(id -u) /usr/local/lib/node_modules && \
	docker exec --user 0 ${SERVICE_NAME} chown $(id -u) /usr/local/bin
}

remove() {
	stop

	__msg "Removing container... " 0 no
	STATUS=$(docker rm ${SERVICE_NAME} 2>&1)
	if [[ $? -ne 0 ]]; then
		__err "${STATUS}"
		return 1
	else
		__success "[ok]"
	fi

	__deleteLinks

	return 0
}

"$@"

exit $?
