#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2007, 2008, 2009, 2010, 2013, 2014, 2015, 2016 Synacor, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
# 

if [ x`whoami` != xzimbra ]; then
    echo "Error: must be run as zimbra user"
  exit 1
fi

source `dirname $0`/zmshutil || exit 1
zmsetvars

servicename=proxy
configfile=/opt/zimbra/conf/nginx.conf
pidfile=${zimbra_log_directory}/nginx.pid
pid=""

getpid()
{
  if [ -f ${pidfile} ]; then
    pid=$(cat ${pidfile})
  fi
}

checkrunning()
{
  getpid
  if [ "x$pid" = "x" ]; then
    running=0
  else
    if ps --no-headers -p $pid -o cmd 2>/dev/null | grep nginx >/dev/null 2>&1; then
      running=1
    else
      pid=""
      running=0
    fi
  fi
}

case "$1" in 
  start)
    if [ ! -x /opt/zimbra/common/sbin/nginx ]; then
      echo "Error: nginx not installed"
      exit 1
    fi

    checkrunning
    echo -n "Starting ${servicename}..."
    if [ $running = 1 ]; then
      echo "${servicename} is already running."
      exit 0
    fi
    if [ "x$2" = "x" ]; then
      /opt/zimbra/libexec/configrewrite proxy > /dev/null 2>&1
    fi

    if [ ! -f ${configfile} ]; then
      echo "failed.  ${configfile} is missing."
      exit 1
    fi

    # read the last line of nginx.conf which indicates the conf gen result
    res=`tail -n 1 ${configfile}`
    warn=''

    if ! [[ $res =~ __SUCCESS__ ]]; then
      msg=`echo $res | awk -F ':' '{print $2}'`
      if [ x"$msg" = "x" ]; then msg="unknown"; fi
      if ! [[ $res =~ "No available nginx lookup handlers could be contacted" ]]; then
        echo "failed."
        echo "nginx start failed. reason: $msg"
        exit 1
      else
        warn=$msg
      fi
    fi

    /opt/zimbra/common/sbin/nginx -c ${configfile}
    for ((i=0; i < 30; i++)); do
      checkrunning
      if [ $running = 1 ]; then
        break
      fi
      sleep 1
    done
    if [ "x$pid" != "x" ]; then
      echo "done."
      if [ x"$warn" != "x" ]; then
        echo "Warning: $warn"
      fi
      exit 0
    else
      echo "failed."
      exit 1
    fi
  ;;
  stop)
    checkrunning
    echo -n "Stopping ${servicename}..."
    if [ $running = 0 ]; then
      echo "${servicename} is not running."
      exit 0
    else
      /opt/zimbra/common/sbin/nginx -c /opt/zimbra/conf/nginx.conf -s stop
      sleep 1
    fi
    if [ -s ${pidfile} ]; then
      echo "failed."
      exit 1
    else
      echo "done."
    fi
    exit 0
  ;;
  restart)
    $0 stop
    $0 start $2
  ;;
  reload)
    checkrunning
    if [ $running = 1 -a "x$pid" != "x" ]; then
      echo -n "Reloading ${servicename}..."
      /opt/zimbra/common/sbin/nginx -c /opt/zimbra/conf/nginx.conf -s reload
      echo "done."
    fi
    
  ;;
  status)
    echo -n "${servicename} is "
    checkrunning
    if [ $running = 0 ]; then
      echo "not running."
      exit 1
    else
      echo "running."
      exit 0
    fi
  ;;
  *)
    echo "$0 start|stop|restart|reload|status"
    exit 1
  ;;
esac
