#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
country=`curl -sS --connect-timeout 10 -m 60 http://ip.vpser.net/country`
public_file=/www/server/panel/install/public.sh
if [ ! -f $public_file ];then
	wget -O $public_file https://download.bt.cn/install/public.sh -T 5;
fi
. $public_file

download_Url=$NODE_URL
#准备基础数据
Root_Path=`cat /var/bt_setupPath.conf`
run_path='/root'
Is_64bit=`getconf LONG_BIT`
tomcat7='7.0.108'
tomcat8='8.5.78'
tomcat9='9.0.62'


#安装tomcat
Install_Tomcat()
{
	#前置准备
	Uninstall_Tomcat
	Install_JavaJdk
	pkill -9 yum 
	yum install rng-tools -y
	service rngd start
	systemctl start rngd
	useradd www
	chown -R www.www $Setup_Path
	#下载并安装主程序
	filename=apache-tomcat-$tomcatVersion.tar.gz
	wget -c -O $filename  $download_Url/install/src/$filename -T 20
	tar xvf $filename
	useradd www
	
	mv -f apache-tomcat-$tomcatVersion $Setup_Path
	rm -f $filename
	if [ "$tomcatVersion" == "$tomcat9" ];then
		tomcatVersion="9.0.0";
	fi
	echo "$tomcatVersion" > $Setup_Path/version.pl

	#替换默认配置
	sxml=$Setup_Path/conf/server.xml
	sed -i "s#TOMCAT_USER=tomcat#TOMCAT_USER=www#" $Setup_Path/bin/daemon.sh
	chown -R www.www $Setup_Path
}
#安装java-jdk
Install_JavaJdk()
{
	if [ ! -d "$jdk_path" ];then
		if [ ! -f "/tmp/java-jdk.rpm" ];then
			wget -c -O /tmp/java-jdk.rpm $download_Url/install/src/jdk-$jdk-linux-x$Is_64bit.rpm -T 20
		fi
		rpm -ivh /tmp/java-jdk.rpm --force --nodeps
	fi
	rm -f /tmp/java-jdk.rpm
}

Install_Jsvs(){
	cd $Setup_Path/bin
	tar -zxf commons-daemon-native.tar.gz
    if [ "$version" == "8" ] || [ "$version" == "9" ];then
        cd commons-daemon-1.3.0-native-src/unix
    else
        cd commons-daemon-1.2.4-native-src/unix
    fi
	./configure --with-java=$jdk_path
	make
	\cp jsvc $Setup_Path/bin
	sed -i "/Set JAVA_HOME/a\JAVA_HOME=$jdk_path" $Setup_Path/bin/daemon.sh
	if [ -f "$start_tomcat" ]; then
		rm -f $start_tomcat
	fi

cat>>$start_tomcat<<EOF
#!/bin/bash
# chkconfig: 2345 55 25
# description: tomcat Service
### BEGIN INIT INFO
# Provides:          tomcat
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts tomcat
# Description:       starts the tomcat
### END INIT INFO
path=$Setup_Path/bin
cd \$path
bash daemon.sh \$1
EOF

if [ "$version" == "7" ];then
if [ -f "$Setup_Path/conf/server.xml" ]; then
		rm -f $Setup_Path/conf/server.xml
fi
cat>>$Setup_Path/conf/server.xml<<EOF
	<Server port="8005" shutdown="SHUTDOWN">
	  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
	  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener" />
	  <Listener className="org.apache.catalina.core.JasperListener" />
	  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
	  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
	  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
	  <GlobalNamingResources>
	    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase" />
	  </GlobalNamingResources>
	  <Service name="Catalina">
	    <Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443" />
	    <!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />-->
	    <Engine defaultHost="localhost" name="Catalina">
	      <Realm className="org.apache.catalina.realm.LockOutRealm">
	        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
	      </Realm>
	      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
	        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt" />
	      </Host>
	    </Engine>
	  </Service>
	</Server>
EOF
fi

if [ "$version" == "8" ];then
if [ -f "$Setup_Path/conf/server.xml" ]; then
		rm -f $Setup_Path/conf/server.xml
fi
cat>>$Setup_Path/conf/server.xml<<EOF
<Server port="8098" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector connectionTimeout="20000" port="8082" protocol="HTTP/1.1" redirectPort="8490" />
    <!--<Connector port="8008" protocol="AJP/1.3" redirectPort="8490" />-->
    <Engine defaultHost="localhost" name="Catalina">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
      </Realm>
      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt" />
      </Host>
    </Engine>
  </Service>
</Server>
EOF
fi
if [ "$version" == "9" ];then
if [ -f "$Setup_Path/conf/server.xml" ]; then
		rm -f $Setup_Path/conf/server.xml
fi
cat>>$Setup_Path/conf/server.xml<<EOF
<Server port="8098" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector connectionTimeout="20000" port="8083" protocol="HTTP/1.1" redirectPort="8490" />
    <!--<Connector port="8008" protocol="AJP/1.3" redirectPort="8490" />-->
    <Engine defaultHost="localhost" name="Catalina">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
      </Realm>
      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt" />
      </Host>
    </Engine>
  </Service>
</Server>
EOF
fi


	chmod +x $start_tomcat
	chkconfig --add $tomcat_version
	chkconfig --level 2345 $tomcat_version on
	$start_tomcat start
	echo "安装完成" >>/tmp/tomcat.json
}

#卸载tomcat
Uninstall_Tomcat()
{
	if [ -d "$Setup_Path" ];then
		$Setup_Path/bin/daemon.sh stop
		rm -rf $Setup_Path
	fi
	if [ -f "$start_tomcat" ];then
		chkconfig --del $tomcat_version
		rm -f $start_tomcat
		rm -f $init_tomcat
	fi
}

actionType=$1
version=$2

if [ "$actionType" == 'install' ];then
	Setup_Path=$Root_Path/server/tomcat7
	start_tomcat=/etc/init.d/tomcat7
	init_tomcat=/etc/rc.d/init.d/tomcat7
	tomcat_version=tomcat7
	tomcatVersion=$tomcat7
	jdk='7u80'
	jdk_path='/usr/java/jdk1.7.0_80'
	if [ "$version" == "8" ];then
		Setup_Path=$Root_Path/server/tomcat8
		start_tomcat=/etc/init.d/tomcat8
		init_tomcat=/etc/rc.d/init.d/tomcat8
		tomcat_version=tomcat8
		tomcatVersion=$tomcat8
		jdk='8u121'
		jdk_path='/usr/java/jdk1.8.0_121'
	elif [ "$version" == "9" ];then
		Setup_Path=$Root_Path/server/tomcat9
		tomcat_version=tomcat9
		start_tomcat=/etc/init.d/tomcat9
		init_tomcat=/etc/rc.d/init.d/tomcat9
		tomcatVersion=$tomcat9
		jdk='8u121'
		jdk_path='/usr/java/jdk1.8.0_121'
	fi
	Install_Tomcat
	Install_Jsvs

elif [ "$actionType" == 'uninstall' ];then
	Setup_Path=$Root_Path/server/tomcat7
	start_tomcat=/etc/init.d/tomcat7
	init_tomcat=/etc/rc.d/init.d/tomcat7
	tomcat_version=tomcat7
	tomcatVersion=$tomcat7
	if [ "$version" == "8" ];then
		Setup_Path=$Root_Path/server/tomcat8
		start_tomcat=/etc/init.d/tomcat8
		init_tomcat=/etc/rc.d/init.d/tomcat8
		tomcat_version=tomcat8
		tomcatVersion=$tomcat8
	elif [ "$version" == "9" ];then
		Setup_Path=$Root_Path/server/tomcat9
		tomcat_version=tomcat9
		start_tomcat=/etc/init.d/tomcat9
		init_tomcat=/etc/rc.d/init.d/tomcat9
		tomcatVersion=$tomcat9
	fi
	Uninstall_Tomcat
fi
