#!/bin/bash

set -e # exit immediately if a simple command exits with a non-zero status set -u # report the usage of uninitialized variables

# Setup env vars and folders for the webapp_ctl script source /var/vcap/jobs/<%= job_name %>/bin/ctl_setup.sh

# Override ctl_setup.sh defaults: redirect_output 'nginx' PIDFILE=$RUN_DIR/nginx.pid

case $1 in

start)
  pid_guard $PIDFILE "nginx for $JOB_NAME"

  echo $$ > $PIDFILE

  for dir in $UPLOADS_DIR $STAGED_UPLOADS_DIR
  do
    mkdir -p ${dir}
  done

  exec /var/vcap/packages/nginx_next/sbin/nginx \
       -c $JOB_DIR/config/nginx.conf \
       >>$LOG_DIR/nginx.log 2>&1
  ;;

stop)
  echo "Stopping nginx"
  kill_and_wait $PIDFILE
  ;;

*)
  echo "Usage: nginx_ctl {start|stop}"

  ;;

esac