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

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

MYSQL="/opt/zimbra/bin/mysql -u zimbra --password=${zimbra_mysql_password}"

START_ORDER="mysql.server zmmailboxdctl"
STOP_ORDER="zmmailboxdctl mysql.server"

STATUS=0

flushDirtyPages() {
  # make sure mysql is running
  /opt/zimbra/bin/mysql.server status > /dev/null 2>&1
  if [ $? != 0 ]; then
    return
  fi

  # make sure innodb is enabled
  local innodb_status=$(echo "show engines;" | ${MYSQL} | grep InnoDB | cut -f2)
  if [ "$innodb_status" = "DISABLED" ]; then
    return
  fi

  # set max_dirty_pages=0 so mysql starts flushing dirty pages to disk.
  ${MYSQL} -e "set global innodb_max_dirty_pages_pct=0;"
  if [ $? != 0 ]; then
    return
  fi

  # wait for 600 seconds or until there are no more dirty pages
  local i=0
  while [ $i -lt 600 ]; do
    local pages=$(${MYSQL} -e "show engine innodb status\G" | grep '^Modified db pages' | grep -Eo '[0-9]+$')
    local total_pages=$(echo $pages | sed 's/ / + /g')
    total_pages=$(( $total_pages ))

    if [ "$total_pages" = "0" ]; then
      break
    fi
    #echo -ne "$pages\r"
    i=$((i+1))
    sleep 1
  done
}

case "$1" in 
  start)
    if [ x$2 = "x" ]; then
	  # Call tlsctl to get all the mailbox config files
      /opt/zimbra/bin/zmtlsctl >/dev/null 2>&1
    fi
    for i in $START_ORDER; do
      /opt/zimbra/bin/$i start norewrite >/dev/null 2>&1
      R=$?
      if [ $R -ne "0" ]; then
        STATUS=$R
      fi
    done
    exit $STATUS
  ;;
  stop)
    for i in $STOP_ORDER; do
      if [ "$i" = "mysql.server" ]; then
        flushDirtyPages
      fi
      /opt/zimbra/bin/$i stop
      R=$?
      if [ $R -ne "0" ]; then
        STATUS=$R
      fi
    done
    exit $STATUS
  ;;
  restart|reload)
    for i in $STOP_ORDER; do
      if [ "$i" = "mysql.server" ]; then
        flushDirtyPages
      fi
      /opt/zimbra/bin/$i stop
      R=$?
      if [ $R -ne "0" ]; then
        STATUS=$R
      fi
    done
    if [ x$2 = "x" ]; then
	  # Call tlsctl to get all the mailbox config files
      /opt/zimbra/bin/zmtlsctl >/dev/null 2>&1
    fi
    for i in $START_ORDER; do
      /opt/zimbra/bin/$i start norewrite
      R=$?
      if [ $R -ne "0" ]; then
        STATUS=$R
      fi
    done
    exit $STATUS
  ;;
  status)
    for i in $START_ORDER; do
      if [ $i = "mysql.server" ]; then
        /opt/zimbra/bin/mysqladmin status > /dev/null 2>&1
      else
        /opt/zimbra/bin/$i status > /dev/null 2>&1
      fi
      R=$?
      if [ $R -ne "0" ]; then
        echo "$i is not running."
        if [ x"$i" != "xzmconfigdctl" ]; then
          STATUS=$R
        fi
      fi
    done
    exit $STATUS
  ;;
  *)
    echo "$0 start|stop|restart|reload|status"
    exit 1
  ;;
esac
