#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.zimbra.com/license
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is: Zimbra Collaboration Suite Server.
# 
# The Initial Developer of the Original Code is Zimbra, Inc.
# Portions created by Zimbra are Copyright (C) 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

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

#
# Expected by catalina scripts
#
export JAVA_HOME=${tomcat_java_home}
if [ ! -d ${tomcat_directory} ]; then
	exit 0
fi
zmassert -d ${tomcat_java_home}
zmassert -d ${tomcat_directory}

#
# Memory for use by JVM
#
memkb=$(zmsysmemkb)
jm=`expr $memkb '*' ${tomcat_java_heap_memory_percent} / 100 / 1024`

#
# Expand war files.
#
mk_download_dir() {
	if [ -d "${tomcat_directory}/webapps/zimbra/downloads" ]; then
		cat >${tomcat_directory}/webapps/zimbra/downloads/index.html<<EOF
<HTML><BODY>
EOF
		for i in ${tomcat_directory}/webapps/zimbra/downloads/*; do
			filename=`basename $i`
			if [ x$filename = "xindex.html" ]; then
				continue
			fi
			cat >>${tomcat_directory}/webapps/zimbra/downloads/index.html<<EOF
<BR>
<A HREF="/downloads/${filename}">${filename}</A>
EOF
		done
		cat >>${tomcat_directory}/webapps/zimbra/downloads/index.html<<EOF
</BODY></HTML>
EOF
	fi
}

rewriteconfig() {
    /opt/zimbra/libexec/zmmtaconfig mailbox > /dev/null 2>&1
}

rewriteWebXml() {
    /opt/zimbra/libexec/zmmtaconfig webxml > /dev/null 2>&1
}

#
# Main
#
case "$1" in
    'start')
        mk_download_dir
		if [ x$2 = "x" ]; then
			rewriteconfig
			rewriteWebXml
		fi
		/opt/zimbra/bin/zmtlsctl
		sudo ${zimbra_home}/libexec/zmtomcatmgr status
        if [ $? = 0 ]; then
			echo "Error: tomcat already running"
			exit 0
        fi
        # Create the work directories so they're owned by zimbra
        mkdir -p ${tomcat_directory}/work/Catalina/localhost/_/org/apache/jsp/public_
        mkdir -p ${tomcat_directory}/work/Catalina/localhost/zimbra/org/apache/jsp/public_
        mkdir -p ${tomcat_directory}/work/Catalina/localhost/service/org/apache/jsp/public_
        mkdir -p ${tomcat_directory}/work/Catalina/localhost/zimbraAdmin/org/apache/jsp/public_
        mkdir -p ${tomcat_directory}/work/Catalina/localhost/manager
        mkdir -p ${tomcat_directory}/work/Catalina/localhost/host-manager
                
        sudo ${zimbra_home}/libexec/zmtomcatmgr start \
	       -Xms${jm}m -Xmx${jm}m ${tomcat_java_options}
	exit $?
        ;;

    'kill')
		lines=`wc -l ${tomcat_directory}/logs/catalina.out | awk '{print $1}'`
		sudo ${zimbra_home}/libexec/zmtomcatmgr threaddump
		sleep 2
		newlines=`wc -l ${tomcat_directory}/logs/catalina.out | awk '{print $1}'`
		difflines=`expr $newlines - $lines`
		tail -${difflines} ${tomcat_directory}/logs/catalina.out > ${tomcat_directory}/logs/stacktrace.$$

        sudo ${zimbra_home}/libexec/zmtomcatmgr stop 
		exit 0
        ;;

    'stop')

		lines=`wc -l ${tomcat_directory}/logs/catalina.out | awk '{print $1}'`
		sudo ${zimbra_home}/libexec/zmtomcatmgr threaddump
		sleep 2
		newlines=`wc -l ${tomcat_directory}/logs/catalina.out | awk '{print $1}'`
		difflines=`expr $newlines - $lines`
		tail -${difflines} ${tomcat_directory}/logs/catalina.out > ${tomcat_directory}/logs/stacktrace.$$

		sudo ${zimbra_home}/libexec/zmtomcatmgr stop
		exit 0
        ;;
    
    'restart')
        $0 stop
        $0 start
        ;;
    
    'status')
		sudo ${zimbra_home}/libexec/zmtomcatmgr status
        exit $?
        ;;

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