#!/bin/sh

set -eu

IFS=','

sitezone="$1"; shift;
cfg_script="$0"
cfg_srcdir=$(cd -- "${cfg_script%/*}/" ; pwd -P)

# header
cat << _EOF
server {
    listen                [::]:80;
    server_name           ${sitezone};
    return                301 https://\$server_name\$request_uri;
}

server {
    listen                [::]:443;
    server_name           ${sitezone};

_EOF

# indexed locations
for sitedir in $@; do

cat << _EOF
    location /${sitedir} {
        root              /srv/www/htdocs/\$host;
        fancyindex        on;
        ssi               on;
    }

_EOF

done

# root directory
cat << _EOF
    location / {
        root              /srv/www/htdocs/\$host;
        index             index.xhtml index.html index.htm;
        ssi               on;
        ssi_types         application/xhtml+xml;
    }

_EOF

# ssl and footer
cat << _EOF
    ssl_certificate           /srv/webroot/${sitezone}/ssl/fullchain.pem;
    ssl_certificate_key       /srv/webroot/${sitezone}/ssl/privkey.pem;
    ssl_trusted_certificate   /srv/webroot/${sitezone}/ssl/chain.pem;

    include conf.d/ssl_params;
}
_EOF
