#!/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 ctl script
source /var/vcap/jobs/proxy/helpers/ctl_setup.sh 'proxy' 'nginx'

export LANG=en_US.UTF-8

case $1 in
  start)
    pid_guard $PIDFILE $JOB_NAME
    exec $JOB_DIR/packages/nginx/sbin/nginx \
      -c $JOB_DIR/config/nginx.conf \
      >>$LOG_DIR/nginx.log 2>&1
    ;;
  stop)
    kill -QUIT $(head -1 $PIDFILE)
    sleep 5
    kill_and_wait $PIDFILE
    ;;
  reload)
    kill -HUP $(head -1 $PIDFILE)
    ;;
  *)
    echo "Usage: ctl {start|stop|reload}"
    ;;
esac
exit 0
