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

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

if [ ! -d ${mailboxd_directory} ]; then
  exit 0
fi

if [ ! -x /opt/zimbra/common/bin/mysql ]; then
  exit 0
fi

if [ ! -d ${zimbra_java_home} ]; then
  exit 0
fi

NC=`which nc 2>/dev/null`; NC=${NC:-`which netcat 2>/dev/null`}
platform=`/opt/zimbra/libexec/get_plat_tag.sh`
#
# Memory for use by JVM.
#
javaXmx=${mailboxd_java_heap_size:=512}
javaXms=${javaXmx}
mailboxd_java_heap_new_size_percent=${mailboxd_java_heap_new_size_percent:=25}
javaXmn=$(expr ${javaXmx} '*' ${mailboxd_java_heap_new_size_percent} / 100)

#
# Spnego - the spnego_options_file is written by zmconfigd 
#          to avoid a zmprov call here.
#
spnego_options_file=/opt/zimbra/conf/spnego_java_options
if [ -e ${spnego_options_file} ]; then
  spnegoJavaOptions=$(cat $spnego_options_file)
fi

#
# Expand war files.
#
mk_download_dir() {
  if [ -d "${mailboxd_directory}/webapps/zimbra/downloads" ]; then
    /opt/zimbra/libexec/zmupdatedownload
  fi
}

#
# Main
#
case "$1" in
    'start')
      mk_download_dir
	  if [ "x$2" = "x" ]; then
		  /opt/zimbra/bin/zmtlsctl > /dev/null 2>&1
	  fi
      sudo /opt/zimbra/libexec/zmmailboxdmgr status
      if [ $? = 0 ]; then
        echo "mailboxd already running."
        exit 0
      fi

      mkdir -p ${mailboxd_directory}/work/service/jsp
      mkdir -p ${mailboxd_directory}/work/zimbra/jsp
      mkdir -p ${mailboxd_directory}/work/zimbraAdmin/jsp
      if [ ! -d ${mailboxd_directory}/webapps/zimlet/WEB-INF ]; then
        [ -f /opt/zimbra/bin/zmacl ] && /opt/zimbra/bin/zmacl disable &> /dev/null
        mkdir -p ${mailboxd_directory}/webapps/zimlet/WEB-INF
        [ -f /opt/zimbra/bin/zmacl ] && /opt/zimbra/bin/zmacl enable &> /dev/null
      fi

      mailboxd_thread_stack_size=${mailboxd_thread_stack_size:=256k}
      if [ -z "`echo ${mailboxd_java_options} | grep Xss`" ]; then
        mailboxd_java_options="${mailboxd_java_options} -Xss${mailboxd_thread_stack_size}"
      fi

      networkaddress_cache_ttl=${networkaddress_cache_ttl:=60}
      if [ -z "`echo ${mailboxd_java_options} | grep sun.net.inetaddr.ttl`" ]; then
        mailboxd_java_options="${mailboxd_java_options} -Dsun.net.inetaddr.ttl=${networkaddress_cache_ttl}"
      fi
      echo -n "Starting mailboxd..."
      sudo /opt/zimbra/libexec/zmmailboxdmgr start \
        -Dfile.encoding=UTF-8 ${mailboxd_java_options} ${spnegoJavaOptions} -Xms${javaXms}m \
        -Xmx${javaXmx}m < /dev/null > /dev/null 2>&1
      status=$?
      if [ $status != 0 ]; then
        echo "failed."
        exit $status
      fi
      status=1
      MPORT=`/opt/zimbra/bin/zmprov -l gs ${zimbra_server_hostname} zimbraMailPort | grep zimbraMailPort: | awk '{print $2}'`
      if [ x$platform = "xRHEL7_64" ]; then
        ncOpt="--send-only --recv-only"
      else
        ncOpt="-z"
      fi
      for ((i=0; i < 12; i++)); do
        $NC $ncOpt localhost ${MPORT} >/dev/null 2>&1
        if [ $? = 0 ]; then
          status=0
          break
        fi
        sleep 5
      done
      if [ $status = 0 ]; then
        echo "done."
      else
        echo "failed."
      fi
      exit $status 
    ;;

    'kill'|'stop')
      echo -n "Stopping mailboxd..."
      sudo /opt/zimbra/libexec/zmmailboxdmgr status
      if [ $? != 0 ]; then
        echo "mailboxd is not running."
        exit 0
      fi
      /opt/zimbra/bin/zmthrdump -i -o /opt/zimbra/log/stacktrace.$$.$(date +%Y%m%d%H%M%S) 2> /dev/null
      sudo /opt/zimbra/libexec/zmmailboxdmgr stop 
      if [ $? = 0 ]; then
        echo "done."
      else 
        echo "failed."
      fi 
      exit 0
    ;;

    
    'restart'|'reload')
        $0 stop
        $0 start $2
    ;;
    
    'status')
      echo -n "mailboxd is "
      sudo /opt/zimbra/libexec/zmmailboxdmgr status
      if [ $? = 0 ]; then
        echo "running."
        exit 0
      else 
        echo "not running."
        exit 1
      fi
    ;;

	'update')
		mk_download_dir
	;;

    *)
      echo "Usage: $0 start|stop|kill|restart|reload|status|update"
      exit 1
    ;;
esac
