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

if [ ! -x "/opt/zimbra/common/sbin/opendkim" ]; then
  exit 0
fi

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

PID=""
PIDFILE=${zimbra_log_directory}/opendkim.pid
odk=/opt/zimbra/common/sbin/opendkim
config=/opt/zimbra/conf/opendkim.conf

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

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 opendkim >/dev/null 2>&1; then
                        RUNNING=1
                else
                        PID=""
                        RUNNING=0
                fi
        fi
}

start()
{
        checkrunning
        if [ $RUNNING = 0 ]; then
          if [ x$1 = "x" ]; then
            rewriteconfig
          fi
          $odk -x $config -u zimbra
          if [ $? != 0 ]; then
            echo "Failed to start opendkim: $?"
            exit 1
          fi
          sleep 2
          getpid
          echo "Started opendkim: pid $PID"
        else
          echo "zmopendkimctl already running: pid $PID"
          exit 0
        fi
}

stop()
{
        checkrunning
        if [ $RUNNING = 0 ]; then
          echo "zmopendkimctl not running"
          exit 0
        else
          echo -n "Stopping opendkim..."
          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
}

status()
{
        checkrunning
        echo -n "zmopendkimctl is "
        if [ $RUNNING = 0 ]; then
                echo "not running."
                exit 1
        else
                echo "running with pid: $PID"
                exit 0
        fi
}

case "$1" in
        reload|restart)
                $0 stop
                $0 start $2
                ;;
        start)
                start $2
                ;;
        stop)
                stop
                ;;
        status)
                status
                ;;
        *)
                echo "Usage: $0 start|stop|restart|reload|status"
                exit 1
                ;;
esac
