#!/bin/bash
set -e

echo Backing up tomcat...
cp -r /usr/local/tomcat9 /usr/local/tomcat9-backup

echo Stopping service...
systemctl stop tomcat.service

echo Downloading new tomcat version...
cd /tmp
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz

echo Verifying download...
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz.sha512
sha512sum -c apache-tomcat-9.0.65.tar.gz.sha512

echo Updating tomcat version...
tar -xf apache-tomcat-9.0.65.tar.gz
yes | cp -rf apache-tomcat-9.0.65/bin apache-tomcat-9.0.65/lib /usr/local/tomcat9/
yes | cp -rf /usr/local/tomcat9-backup/bin/setenv.sh /usr/local/tomcat9-backup/bin/liblmxjava.so /usr/local/tomcat9/bin/
chown illuminitserver:illuminitserver /usr/local/tomcat9/bin/* /usr/local/tomcat9/lib/*

echo Starting service...
systemctl start tomcat.service

echo Cleaning up...
rm -rf /usr/local/tomcat9-backup
rm -rf /tmp/apache-tomcat-9.0.65
rm -f /tmp/apache-tomcat-9.0.65.tar.gz
rm -f /tmp/apache-tomcat-9.0.65.tar.gz.sha512

