#!/bin/sh

echo
echo "Welcome to ngxproxy utility."
echo "We're about to create a new virtual host (AKA server block)."
echo

while [ "$NAME" == "" ]; do
    read -p "Name: " NAME
done

if [ -f "/sites-enabled/$NAME.conf" ]; then
    echo "ERROR: /sites-enabled/$NAME.conf already exists."
    exit 1
fi

while [ "$DOMAIN" == "" ]; do
    read -p "Domain: " DOMAIN
done

if [ "$(dig +short $DOMAIN)" == "" ]; then
    echo "WARNING: $DOMAIN couldn't be resolved: it may not work!"
    echo "HINT: Is this domain correct? Did you update your DNS zone?"
fi

read -p "Webroot (default is /): " WEBROOT

if [ "$WEBROOT" == "" ]; then
    WEBROOT="/"
elif [ "$WEBROOT" != "/" ]; then
    echo "WARNING: You might have to add a proxy header to get your custom webroot working."

    while [[ "$CONFIGURE_WEBROOT" !=  "y" && "$CONFIGURE_WEBROOT" != "n" ]]; do
        read -p "Is it required (by the app) to configure it? [y/n]: " CONFIGURE_WEBROOT
    done

    if [ "$CONFIGURE_WEBROOT" == "y" ]; then
        while [ "$WEBROOT_HEADER" == "" ]; do
            read -p "Type the required proxy_set_header (like X-Script-Name): " WEBROOT_HEADER
        done
    fi
fi

while [ "$CONTAINER" == "" ]; do
    read -p "Container: " CONTAINER
done

ping -c 1 $CONTAINER >/dev/null 2>&1

if [ "$?" != "0" ]; then
    echo "WARNING: $CONTAINER seems to be unavailable. It may not work!"
    echo "HINT: Did you correctly link the container?"
fi

read -p "Port (default is 80): " PORT

if [ "$PORT" == "" ]; then
    PORT="80"
elif ! [ "$PORT" -eq "$PORT" ] 2>/dev/null; then
    echo "ERROR: an integer value was expected."
    exit 1
elif [ "$PORT" -gt "65535" ]; then
    echo "ERROR: $PORT exceeds the maximum TCP port which is 65535"
    exit 1
fi

while [[ "$HTTPS" !=  "y" && "$HTTPS" != "n" ]]; do
    read -p "HTTPS [y/n]: " HTTPS
done

if [ "$HTTPS" == "y" ]; then
    while [ ! -f "$CERTIFICATE_PATH" ]; do
        read -p "Certificate path: " CERTIFICATE_PATH
    done

    while [ ! -f "$KEY_PATH" ]; do
        read -p "Certificate key path: " KEY_PATH
    done

    cp -f /etc/nginx/conf/vhost_https.conf /tmp/${NAME}.conf

    sed -i \
        -e "s|<CERTIFICATE_PATH>|$CERTIFICATE_PATH|g" \
        -e "s|<KEY_PATH>|$KEY_PATH|g" \
        /tmp/$NAME.conf

    while [[ "$HEADERS" !=  "y" && "$HEADERS" != "n" ]]; do
        read -p "Secure headers [y/n]: " HEADERS
    done

    if [ "$HEADERS" == "y" ]; then
        sed -i 's|#include /etc/nginx/conf/headers_params|include /etc/nginx/conf/headers_params|g' /tmp/$NAME.conf
    fi

    while [[ "$HSTS" !=  "y" && "$HSTS" != "n" ]]; do
        read -p "Enable HSTS header ? [y/n]: " HSTS
    done

    if [ "$HSTS" == "y" ]; then

        read -p "Max-age in seconds (default is 31536000): " HSTS_MAX_AGE

        if [ "$HSTS_MAX_AGE" == "" ]; then
            HSTS_MAX_AGE="31536000"
        elif ! [ "$HSTS_MAX_AGE" -eq "$HSTS_MAX_AGE" ] 2>/dev/null; then
            echo "ERROR: an integer value was expected."
            exit 1
        fi

        while [[ "$HSTS_SUBDOMAINS" !=  "y" && "$HSTS_SUBDOMAINS" != "n" ]]; do
            read -p "Include subdomains ? [y/n]: " HSTS_SUBDOMAINS
        done

        if [ "$HSTS_SUBDOMAINS" == "y" ]; then
            HSTS_SUBDOMAINS="includeSubDomains;"
        else
            HSTS_SUBDOMAINS=""
        fi

        while [[ "$HSTS_PRELOAD" !=  "y" && "$HSTS_PRELOAD" != "n" ]]; do
            read -p "Enable preload list mechanism ? [y/n]: " HSTS_PRELOAD
        done

        if [ "$HSTS_PRELOAD" == "y" ]; then
            HSTS_PRELOAD="preload"
        else
            HSTS_PRELOAD=""
        fi

        sed -i \
            -e 's/#add_header/add_header/g' \
            -e "s/<HSTS_MAX_AGE>/$HSTS_MAX_AGE/g" \
            -e "s/<HSTS_SUBDOMAINS>/$HSTS_SUBDOMAINS/g" \
            -e "s/<HSTS_PRELOAD>/$HSTS_PRELOAD/g" /tmp/$NAME.conf
    fi
else
    cp -f /etc/nginx/conf/vhost_http.conf /tmp/${NAME}.conf
fi

while [ "$MAX_BODY_SIZE" == "" ]; do
    read -p "Max body size in MB (integer/null): " MAX_BODY_SIZE
done

if ! [ "$MAX_BODY_SIZE" -eq "$MAX_BODY_SIZE" ] 2>/dev/null && [ "$MAX_BODY_SIZE" != "null" ]; then
    echo "ERROR: Incorrect value."
    exit 1
fi

if [ "$MAX_BODY_SIZE" != "null" ]; then
    sed -i "s|#client_max_body_size <MAX_BODY_SIZE>|client_max_body_size $MAX_BODY_SIZE|g" /tmp/$NAME.conf
fi

if [ "$CONFIGURE_WEBROOT" == "y" ]; then
    sed -i "/proxy_pass/a \ \ \ \ proxy_set_header $WEBROOT_HEADER $WEBROOT;" /tmp/$NAME.conf
fi

sed -i \
    -e "s|<DOMAIN>|$DOMAIN|g" \
    -e "s|<CONTAINER>|$CONTAINER|g" \
    -e "s|<PORT>|$PORT|g" \
    -e "s|<WEBROOT>|$WEBROOT|g" \
    /tmp/$NAME.conf

mv /tmp/$NAME.conf /sites-enabled/

echo
echo "Done! $NAME.conf has been generated."

while [[ "$RELOAD" !=  "y" && "$RELOAD" != "n" ]]; do
    read -p "Reload nginx now? [y/n]: " RELOAD
done

if [ "$RELOAD" == "y" ]; then
    su-exec $UID:$GID nginx -s reload
    echo "nginx successfully reloaded."
else
    echo "Restart manually nginx to enable this new vhost."
fi

echo
exit 0