#!/bin/sh

os=$(cat "$__global/explorer/os")

case "$os" in
    devuan)
        CONF_DIR=/etc/nginx/sites-enabled
    ;;
    alpine)
	CONF_DIR=/etc/nginx/conf.d
    ;;
    *)
        echo "OS $os currently not supported" >&2
        exit 1
    ;;
esac

DOMAIN_NAME="$__object_id"
WEB_ROOT=/var/www/html/${DOMAIN_NAME}

__directory $WEB_ROOT --parents --mode 0644

require="__package/nginx" __file "${CONF_DIR}/${DOMAIN_NAME}.conf" \
           --mode 0644 --source - << EOF
server {
    listen *:80;
    listen [::]:80;

    server_name $DOMAIN_NAME;
    root $WEB_ROOT;

    location /.well-known/acme-challenge/ {
        root $WEB_ROOT;
    }

    # Everything else -> ssl
    location / {
        return 301 https://\$host\$request_uri;
    }
}
EOF
