#!/bin/bash
# Filename: Tomcat_Install.sh
# Function: Onekey Install Tomcat Server
# Author: shuhui @ 2016

### Define Variables Start

JDK_VER='8u66'                        # JDK Version
JDK_DIR='/usr/local/jdk'              # JDK Directory

TOMCAT_VER='8.0.32'                   # Tomcat Version

### Define Variables End

function Add_User() {
	getent group tomcat || groupadd -r tomcat
	getent  passwd  tomcat || useradd -r -g tomcat -d /opt -s /sbin/nologin tomcat

}

function Install_JDK() {
    curl -L -O http://down.vqiu.cn/package/tarball/java/jdk-${JDK_VER}-linux-x64.tar.gz
	tar axvf jdk-${JDK_VER}-linux-x64.tar.gz -C /usr/local
	ln -sv /usr/local/jdk1.8.0_66 /usr/local/jdk

	for i in /etc/profile.d/jdk.sh
	do
		echo "# JDK Environment" >$i
		echo "JAVA_HOME=${JDK_DIR}" >>$i
		echo 'JRE_HOME=${JAVA_HOME}/jre' >>$i
		echo 'JAVA_BIN=${JAVA_HOME}/bin' >> $i
		echo 'CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib' >>$i
		source $i
	done
}

function Install_Tomcat() {
	curl -L -O http://down.vqiu.cn/package/tarball/tomcat/apache-tomcat-${TOMCAT_VER}.tar.gz
	tar axvf apache-tomcat-${TOMCAT_VER}.tar.gz -C /opt
	ln -sv /opt/apache-tomcat-${TOMCAT_VER} /opt/tomcat
	chown -R tomcat.tomcat /opt

	# Turn off AJP
	#sed  -i.bak  '/8009/d'  /opt/tomcat/conf/server.xml
	#sed  -i 's#HTTP/1.1#org.apache.coyote.http11.Http11NioProtocol#' /opt/tomcat/conf/server.xml
}

function Add_Systemd() {
	cat > /usr/lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Apache Tomcat 
After=network.target
[Service]
Type=oneshot
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
RemainAfterExit=yes
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
EOF
	systemctl enable tomcat.service
	systemctl start tomcat.service

}


Add_User
Install_JDK
Install_Tomcat
Add_Systemd
