#!/bin/bash

# Define Variables
TOMCAT_VER='7.0.65'
TOMCAT_URL="http://down.vqiu.cn/package/tarball/tomcat/apache-tomcat-${TOMCAT_VER}.tar.gz"

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

}

function Yum_Depend() {
	yum -y install java-1.8.0-openjdk*
}

function Install_Tomcat() {
	wget -c 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-7.0.65 /opt/tomcat7
	chown -R tomcat.tomcat /opt

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

function Add_Systemd() {
	cat > /usr/lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Apache Tomcat 7
After=network.target
[Service]
Type=oneshot
ExecStart=/opt/tomcat7/bin/startup.sh
ExecStop=/opt/tomcat7/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
Yum_Depend
Install_Tomcat
Add_Systemd
