#!/bin/bash
set -e
here=$(cd $(dirname $0); pwd)
source "${here}/../lib/ddlib.sh"

function cleanup() {
    rm -f "${conf}"
}

pid=$(get_pid)
if [ ! -z ${pid} ]; then
    echo >&2 "demodock already running (PID ${pid})"
    exit 1
fi

root=.
port=80
while getopts r:p: opt; do
    case "${opt}" in
        r) root="${OPTARG}";;
        p) port="${OPTARG}";;
        \?)
            echo >&2 "usage: demodock-up [-r <web root path>] [-p <port number>]"
            exit 1;;
    esac
done

root=$(cd ${root}; pwd)

nginx="$(get_nginx_binary)"
config=$(get_config_dir)
conf=`${here}/make-conf "${root}" "${port}"`

echo "root: ${root}"

trap cleanup ERR

${nginx} -p ${config}/nginx -c ${conf}
echo ${conf} >${config}/conf-file
