#!/bin/sh -e

os="$(cat "${__global}/explorer/os")"
case "${os}" in
	devuan|debian)
	;;
	*)
		echo "Your OS '${os}' is currently not supported." > /dev/stderr
		exit 1
	;;
esac

DOMAIN="${__object_id}"
ADMIN_EMAIL="$(cat "${__object}/parameter/admin-email")"
CHANNEL_LAST_N="$(cat "${__object}/parameter/channel-last-n")"
DEFAULT_LANGUAGE="$(cat "${__object}/parameter/default-language")"
NOTICE_MESSAGE="$(cat "${__object}/parameter/notice-message")"
START_VIDEO_MUTED="$(cat "${__object}/parameter/start-video-muted")"
TURN_SERVER="$(cat "${__object}/parameter/turn-server")"
VIDEO_CONSTRAINTS="$(cat "${__object}/parameter/video-constraints")"
ANALYTICS_SETTINGS="$(cat "${__object}/parameter/analytics-settings")"
BRANDING_APP_NAME="$(cat "${__object}/parameter/branding-app-name")"
BRANDING_INDEX="$(cat "${__object}/parameter/branding-index")"
BRANDING_JSON="$(cat "${__object}/parameter/branding-json")"
BRANDING_WATERMARK="$(cat "${__object}/parameter/branding-watermark")"
STATE="$(cat "${__object}/parameter/state")"

if [ "${BRANDING_INDEX}" = "-" ]; then
	BRANDING_INDEX="${__object}/stdin"
fi

if [ -f "${__object}/parameter/enable-third-party-requests" ]; then
	ENABLE_THIRD_PARTY_REQUESTS="YES"
fi
if [ -f "${__object}/parameter/disable-audio-levels" ]; then
	DISABLE_AUDIO_LEVELS="YES"
fi
if [ -f "${__object}/parameter/secured-domains" ]; then
	SECURED_DOMAINS="YES"
fi

if [ -z "${TURN_SERVER}" ]; then
	TURN_SERVER="${__target_host}"
fi
if [ -z "${JITSI_HOST}" ]; then
	JITSI_HOST="${__target_host}"
fi

if [ -n "${BRANDING_JSON}" ]; then
	DYNAMIC_BRANDING_URL="/branding.json"
fi

case "${STATE}" in
	present)
		# When adding the domain, Let's Encrypt must come before nginx
		le_require=""
		nginx_require="__letsencrypt_cert/${DOMAIN}"
		;;
	absent)
		# When removing, nginx must come before Let's Encrypt
		le_require="__file/etc/nginx/sites-enabled/${DOMAIN}.conf"
		nginx_require=""
		;;
	*)
		cat >> /dev/stderr <<-EOM
		Unsupported state '${STATE}', must be 'present' or 'absent'.
		EOM
		exit 1
		;;
esac

#
# Deal with certbot
#
# use object id as domain
require="${le_require}" __letsencrypt_cert "${DOMAIN}" \
	--state "${STATE}" \
	--admin-email "${ADMIN_EMAIL}" \
	--deploy-hook "service nginx reload" \
	--webroot /usr/share/jitsi-meet

# Create virtualhost for nginx
# shellcheck source=type/__jitsi_meet_domain/files/nginx.sh
. "${__type}/files/nginx.sh"  # This defines JITSI_NGINX_CONFIG
require="${nginx_require}" __file \
	"/etc/nginx/sites-enabled/${DOMAIN}.conf" \
	--state "${STATE}" \
	--mode 0644 --source "-" <<EOF
${JITSI_NGINX_CONFIG}
EOF

# Setup jitsi config
# shellcheck source=type/__jitsi_meet_domain/files/config.js.sh
. "${__type}/files/config.js.sh"  # This defines JITSI_CONFIG_JS
__file "/etc/jitsi/meet/${DOMAIN}-config.js" \
	--state "${STATE}" \
	--mode 0644 --source "-" <<EOF
${JITSI_CONFIG_JS}
EOF

# Setup jitsi interface config
# shellcheck source=type/__jitsi_meet_domain/files/interface_config.js.sh
. "${__type}/files/interface_config.js.sh"  # This defines JITSI_CONFIG_JS
__file "/etc/jitsi/meet/${DOMAIN}-interface_config.js" \
	--state "${STATE}" \
	--mode 0644 --source "-" <<EOF
${JITSI_INTERFACE_CONFIG_JS}
EOF


#
# Deal with branding as requested
#
# Helper function to manage the state of the target branding file
_var_state() {
	if [ "${STATE}" = "present" ] && [ -n "${1}" ]; then
		echo "present"
	else
		echo "absent"
	fi
}

__file "/usr/share/jitsi-meet/index-${DOMAIN}.html" \
	--mode 0644 \
	--state "$(_var_state "${BRANDING_INDEX}")" \
	--source "${BRANDING_INDEX}"
__file "/etc/jitsi/meet/${DOMAIN}-branding.json" \
	--mode 0644 \
	--state "$(_var_state "${BRANDING_JSON}")" \
	--source "${BRANDING_JSON}"
__file "/usr/share/jitsi-meet/images/watermark-${DOMAIN}.png" \
	--mode 0644 \
	--state "$(_var_state "${BRANDING_WATERMARK}")" \
	--source "${BRANDING_WATERMARK}"
# Simple body customisation
__file "/usr/share/jitsi-meet/body-${DOMAIN}.html" \
	--mode 0644 \
	--state "$(_var_state "${STATE}")" \
	--source "${__object}/parameter/branding-extra-body"

#
# Take care of prosody settings for the domain
#
JITSI_DOMAIN="${DOMAIN}"
# Prosody settings for common components (jvb, focus, ...)
# shellcheck source=type/__jitsi_meet_domain/files/prosody.cfg.lua.sh
. "${__type}/files/prosody.cfg.lua.sh"  # This defines PROSODY_CONFIG
__file "/etc/prosody/conf.avail/${DOMAIN}.cfg.lua" \
	--group prosody \
	--mode 0440 \
	--state "${STATE}" \
	--source '-' <<EOF
${PROSODY_CONFIG}
EOF
__link "/etc/prosody/conf.d/${DOMAIN}.cfg.lua" \
	--source "/etc/prosody/conf.avail/${DOMAIN}.cfg.lua" \
	--state "${STATE}" \
	--type symbolic

if [ "${STATE}" = "present" ]; then
	export require="${require} __file/etc/prosody/conf.avail/${DOMAIN}.cfg.lua __link/etc/prosody/conf.d/${DOMAIN}.cfg.lua"
	__check_messages "prosody/${DOMAIN}" \
		--pattern '^(__file|__link)/etc/prosody/conf[.](avail|d)/' \
		--execute "$(cat <<EOF
if [ ! -f "/var/lib/prosody/${DOMAIN}.crt" ]; then
	echo | prosodyctl cert generate '${DOMAIN}';
	ln -sf '/var/lib/prosody/${DOMAIN}.key' '/etc/prosody/certs/${DOMAIN}.key'
	ln -sf '/var/lib/prosody/${DOMAIN}.crt' '/etc/prosody/certs/${DOMAIN}.crt'
fi
# Surprisingly, a reload is not enough
service prosody restart
EOF
)"
fi
