#!/bin/bash

project_name="tomcat"
project_port_1="8080"
project_port_2="8501"

_version="9.0.55"
tomcat_version="apache-tomcat-$_version"

dir="$(cd "$(dirname "$0")" && pwd)"
cd "$dir" || exit
echo "-> 当前文件目录：[$dir]"

if [ ! -f "$tomcat_version.tar.gz" ]; then
  # wget https://archive.apache.org/dist/tomcat/tomcat-9/v$_version/bin/apache-tomcat-$_version.tar.gz
  wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v$_version/bin/$tomcat_version.tar.gz -q
fi

tar -zxvf $tomcat_version.tar.gz
mv $tomcat_version $project_name

sed -i 's/<Server port="8005" shutdown="SHUTDOWN">/<Server port="'$project_port_2'" shutdown="SHUTDOWN">/g' $project_name/conf/server.xml
sed -i 's/<Connector port="8080" protocol="HTTP\/1.1"/<Connector port="'$project_port_1'" protocol="HTTP\/1.1" URIEncoding="UTF-8" /g' $project_name/conf/server.xml
sed -i 's/connectionTimeout="20000"/connectionTimeout="8000" acceptCount="100" disableUploadTimeout="true" enableLookups="false" maxHttpHeaderSize="8192" /g' $project_name/conf/server.xml
sed -i 's/redirectPort="8443"/maxThreads="150" minSpareThreads="25" relaxedPathChars="|{}[],%" relaxedQueryChars="|{}[],%" redirectPort="8443"/g' $project_name/conf/server.xml
sed -i 's/autoDeploy="true"/autoDeploy="false"/g' $project_name/conf/server.xml

rm -rf $project_name/webapps/docs
rm -rf $project_name/webapps/examples
rm -rf $project_name/webapps/host-manager
rm -rf $project_name/webapps/manager
rm -rf $project_name/webapps/ROOT/*

echo "" >$project_name/webapps/ROOT/index.html
echo "\
#!/bin/sh
export LANG='zh_CN.UTF-8'
export LC_ALL='zh_CN.UTF-8'
export JAVA_OPTS='-server -Xms1024m -Xmx2048m -XX:NewSize=64m -XX:MaxNewSize=128m -XX:+UseParallelGC -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true'
" >$project_name/bin/setenv.sh

echo "\
handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = \${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 180
1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = \${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 180
2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = UTF-8

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler
" >$project_name/conf/logging.properties

chmod 755 $project_name
chmod 755 $project_name/bin/*.sh
echo "-> finish"
rm -rf $tomcat_version.tar.gz

# sed -i 's/<Server port="8005" shutdown="SHUTDOWN">/<Server port="8501" shutdown="SHUTDOWN">/g' conf/server.xml
# sed -i 's/<Connector port="8080" protocol="HTTP\/1.1"/<Connector port="80" protocol="HTTP\/1.1" URIEncoding="UTF-8"/g' conf/server.xml
# sed -i 's/connectionTimeout="20000"/acceptCount="100" connectionTimeout="8000" disableUploadTimeout="true" enableLookups="false" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25"/g' conf/server.xml
# rm -rf webapps
