#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2009, 2010, 2011, 2012, 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

pidfile=${cbpolicyd_pid_file:=${zimbra_log_directory}/cbpolicyd.pid}
dbfile=${cbpolicyd_db_file:=/opt/zimbra/data/cbpolicyd/db/cbpolicyd.sqlitedb}


rewriteconfig() {
  /opt/zimbra/libexec/configrewrite cbpolicyd > /dev/null 2>&1
}

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

checkrunning() {
  getpid
  if [ "x$PID" = "x" ]; then
    RUNNING=0
    if [ -f $pidfile ]; then
      rm -f $pidfile
    fi
  else
    kill -0 $PID 2> /dev/null
    if [ $? != 0 ]; then
      PID=""
      RUNNING=0
    else
      RUNNING=1
    fi
  fi
}


#
# Main
#
case "$1" in
  'start')
    if [ ! -x /opt/zimbra/common/bin/cbpolicyd ]; then
      echo "cbpolicyd not installed, skipping."
      exit 0
    fi
    checkrunning
  
    echo -n "Starting policyd..."
    if [ $RUNNING = 1 ]; then
      echo "policyd is already running."
      exit 0
    else
      if [ ! -d /opt/zimbra/data/cbpolicyd/db ]; then
        mkdir /opt/zimbra/data/cbpolicyd/db
      fi
      if [ ! -f ${dbfile} ]; then
        /opt/zimbra/libexec/zmcbpolicydinit
        if [ $? != 0 ]; then
          echo "Unable to initialize cbpolicyd database." 
          exit 1
        fi
      fi
      if [ x$2 = "x" ]; then
        rewriteconfig
      fi
      /opt/zimbra/common/bin/cbpolicyd --config /opt/zimbra/conf/cbpolicyd.conf 2> /dev/null
      for ((i=0; i < 30; i++)); do
        sleep 1
        checkrunning
        if [ $RUNNING = 1 ]; then 
          break
        fi  
      done
      if [ "x$PID" = "x" ]; then
        echo "failed."
        exit 1
      else 
        echo "done."
      fi 
    fi
  ;;

  'kill')
    $0 stop
  ;;

  'stop')
    checkrunning
    echo -n "Stopping policyd..."
    if [ $RUNNING = 0 ]; then
      echo "policyd is not running."
      exit 0
    else 
      kill -TERM $PID 2> /dev/null
      for ((i=0; i < 300; i++)); do
        sleep 5
        kill -0 $PID 2> /dev/null
        if [ $? != 0 ]; then
          echo " done."
          exit 0
        fi
      done
      kill -TERM $PID 2> /dev/null
      if [ $? = 0 ]; then
        echo " failed to stop $PID"
        exit 1
      else 
        echo " done."
      fi
    fi
    exit 0
  ;;
    
  'restart'|'reload')
    $0 stop
    $0 start $2
  ;;
  
  'status')
    checkrunning
    echo -n "policyd is "
    if [ $RUNNING = 0 ]; then
      echo "not running."
      exit 1
    else
      echo "running."
      exit 0
    fi
  ;;
    
  *)
    echo "Usage: $0 start|stop|kill|reload|restart|status"
    exit 1
  ;;
esac
