#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 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/unbound" ]; then
  exit 0
fi

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

PID=""
unbound=/opt/zimbra/libexec/zmunbound
config=/opt/zimbra/conf/unbound.conf
platform=`/opt/zimbra/libexec/get_plat_tag.sh`;

RESOLVCONF=false
if [ -x /sbin/resolvconf ]; then
  RESOLVCONF=true
fi

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

getpid()
{
	PID=`pgrep -f /opt/zimbra/common/sbin/unbound`
}


checkrunning()
{
        getpid
        if [ "x$PID" = "x" ]; then
                RUNNING=0
        else
                kill -0 $PID
                if [ $? != 0 ]; then
                        PID=""
                        RUNNING=0
                else
                        RUNNING=1
                fi
        fi
}

start()
{
  checkrunning
  if [ $RUNNING = 0 ]; then
    if [ x$1 = "x" ]; then
      rewriteconfig
    fi
    if [ -f /opt/zimbra/common/sbin/unbound-anchor ]; then
         if [ ! -f /opt/zimbra/conf/root.key ]; then
              /opt/zimbra/common/sbin/unbound-anchor -a "/opt/zimbra/conf/root.key"
         fi
    fi
    if [ x"$RESOLVCONF" = "xtrue" ]; then
      /opt/zimbra/common/sbin/unbound-checkconf -o interface | (
        while read interface; do
          if [ "x$interface" = x127.0.0.1 ]; then
            echo "nameserver 127.0.0.1"
          elif [ "x$interface" = x::1 ]; then
            echo "nameserver ::1"
          fi
        done
      ) | sudo /sbin/resolvconf -a lo.zimbra-unbound
    elif [[ $platform == "RHEL"*"64" || $platform == "SLES"*"64" || $platform == "CentOS"*"64" ]]; then
      sudo /opt/zimbra/libexec/zmdnscachealign add
    fi
    sudo $unbound
    if [ $? != 0 ]; then
      echo "Failed to start dnscache: $?"
      exit 1
    fi
    sleep 2
    getpid
    echo "Started dnscache: pid $PID"
  else
    echo "dnscache already running: pid $PID"
    exit 0
  fi
}

stop()
{
  checkrunning
  if [ x"$RESOLVCONF" = "xtrue" ]; then
    sudo /sbin/resolvconf -d lo.zimbra-unbound
  elif [[ $platform == "RHEL"*"64" || $platform == "SLES"*"64" || $platform == "CentOS"*"64" ]]; then
    sudo /opt/zimbra/libexec/zmdnscachealign remove
  fi
  if [ $RUNNING = 0 ]; then
    echo "dnscache not running"
    exit 0
  else
    echo -n "Stopping dnscache..."
    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 "dnscache 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
