#!/bin/sh
deploy_from="version1"

# Get location of database
if [ $DBHOST = "127.0.0.1" ]
then
        echo "local database"
        PSQL="psql"
else
        echo "remote database"
        PSQL="PGPASSWORD=keycar08 psql -h $DBHOST -U postgres"
fi

# Set flag for ubuntu version
u2204=`lsb_release -r | grep -c "22\.04"`
u2404=`lsb_release -r | grep -c "24\.04"`
u2604=`lsb_release -r | grep -c "26\.04"`

if [ $u2604 -eq 0 ] && [ $u2404 -eq 0 ] && [ $u2204 -eq 0 ]; then
    echo "ERROR: Unsupported Ubuntu version. This version of Smap requires Ubuntu 22.04 or later."
    echo "Your server has not been changed."
    exit 1
fi

# Detect installed Tomcat by checking for actual service/directory
if [ -d /var/lib/tomcat10 ] || systemctl is-enabled tomcat10 2>/dev/null | grep -q "enabled"; then
    TOMCAT_VERSION=tomcat10
    TOMCAT_USER=tomcat
elif [ -d /var/lib/tomcat9 ] || systemctl is-enabled tomcat9 2>/dev/null | grep -q "enabled"; then
    TOMCAT_VERSION=tomcat9
    TOMCAT_USER=tomcat
    NEEDS_TOMCAT_UPGRADE=true
else
    echo "ERROR: No supported Tomcat installation found (expected tomcat9 or tomcat10)"
    exit 1
fi

# Migrate Tomcat 9 -> 10 if needed (runs once; subsequent deploys skip this block)
if [ "$NEEDS_TOMCAT_UPGRADE" = "true" ]; then
    echo "=== Migrating Tomcat 9 -> Tomcat 10 ==="

    # Save existing context.xml (JNDI datasource config including passwords)
    if [ -f /etc/tomcat9/context.xml ]; then
        OLD_CONTEXT=/etc/tomcat9/context.xml         # 22.04 apt install
    else
        OLD_CONTEXT=/var/lib/tomcat9/conf/context.xml # 24.04 manual install
    fi
    cp $OLD_CONTEXT /tmp/context.xml.bak

    systemctl stop tomcat9
    systemctl disable tomcat9

    TC10_VER=10.1.40
    TC10_URL=https://archive.apache.org/dist/tomcat/tomcat-10/v${TC10_VER}/bin/apache-tomcat-${TC10_VER}.tar.gz
    wget -q $TC10_URL -O /tmp/tomcat10.tar.gz
    mkdir -p /var/lib/tomcat10
    tar -xzf /tmp/tomcat10.tar.gz -C /var/lib/tomcat10 --strip-components=1
    rm /tmp/tomcat10.tar.gz

    cp ../install/config_files/server.xml.tomcat10 /var/lib/tomcat10/conf/server.xml
    cp /tmp/context.xml.bak /var/lib/tomcat10/conf/context.xml

    mkdir -p /var/log/tomcat10
    rm -rf /var/lib/tomcat10/logs
    ln -s /var/log/tomcat10 /var/lib/tomcat10/logs
    chown -R tomcat /var/lib/tomcat10 /var/log/tomcat10
    chgrp -R tomcat /var/lib/tomcat10 /var/log/tomcat10

    # Point the tomcat user's home at the new CATALINA_HOME. systemd derives
    # the runtime $HOME from this passwd entry, and the AWS SDK reads
    # $HOME/.aws/credentials. The old tomcat9 home (e.g. /var/lib/tomcat9) may
    # no longer exist, so realign it now.
    usermod -d /var/lib/tomcat10 tomcat

    mkdir -p /etc/systemd/system/tomcat10.service.d
    cp ../install/config_files/tomcat10.service /etc/systemd/system/tomcat10.service
    JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
    JH="Environment=\"JAVA_HOME=$JAVA_HOME\""
    sed -i "/JAVA_HOME/c$JH" /etc/systemd/system/tomcat10.service
    cp ../install/config_files/override.conf /etc/systemd/system/tomcat10.service.d/override.conf
    systemctl daemon-reload
    systemctl enable tomcat10

    a2enmod proxy
    a2enmod proxy_ajp

    TOMCAT_VERSION=tomcat10
    TOMCAT_USER=tomcat
    echo "=== Tomcat 10 migration complete ==="
fi

CATALINA_HOME=/var/lib/$TOMCAT_VERSION

# Copy postgres driver
cp -r $deploy_from/jdbc/* $CATALINA_HOME/lib/

# version 26.06 - ensure the JDBC connection pool validates connections so a
# connection dropped by the database during an idle period is detected and
# replaced instead of being handed to the application (caused intermittent 500s
# after a page was left idle for a long time). Patched in place so the existing
# datasource url/username/password are preserved.
CONTEXT_FILE=$CATALINA_HOME/conf/context.xml
if [ -f "$CONTEXT_FILE" ] && ! grep -q "testWhileIdle" "$CONTEXT_FILE"; then
    echo "Adding JDBC connection pool validation to $CONTEXT_FILE"
    sudo cp "$CONTEXT_FILE" "$CONTEXT_FILE.bak.`date +%Y%m%d%H%M%S`"
    cat > /tmp/patch_context.py <<'PYEOF'
import re, sys
f = sys.argv[1]
s = open(f).read()
attrs = ('          testOnBorrow="true"\n'
         '          testWhileIdle="true"\n'
         '          validationQuery="SELECT 1"\n'
         '          validationInterval="30000"\n'
         '          timeBetweenEvictionRunsMillis="30000"\n'
         '          minEvictableIdleTimeMillis="60000"')
def fix(m):
    block = m.group(0)
    # drop any pre-existing pool-validation attributes so a consistent set is applied
    block = re.sub(r'\n\s*(testOnBorrow|testWhileIdle|validationQuery|validationInterval|'
                   r'timeBetweenEvictionRunsMillis|minEvictableIdleTimeMillis)="[^"]*"', '', block)
    # insert the validation attributes just before the closing />
    block = re.sub(r'\s*/>\s*$', '\n' + attrs + '/>', block)
    return block
# each jdbc datasource Resource element (from <Resource ... name="jdbc/... up to />)
s = re.sub(r'<Resource\b[^>]*?name="jdbc/[^>]*?/>', fix, s, flags=re.S)
open(f, 'w').write(s)
PYEOF
    sudo python3 /tmp/patch_context.py "$CONTEXT_FILE"
    rm -f /tmp/patch_context.py
    echo "JDBC pool validation added - Tomcat restart required to take effect"
fi

echo '##### 0. check configuration'
# Set flag if this is apache2.4
a24=`sudo apachectl -version | grep -c "2\.4"`
a_config_dir="/etc/apache2/sites-available"
if [ $a24 -eq 0 ]; then	
	echo "%%%%%% Warning: Apache configuration files for Apache 2.4 will be installed. "
	echo "PLease install Apache 2.4 prior to upgrading"
	exit 1;
fi

version=2606
if [ -e /smap_bin/smap_version ]; then
        version=`sudo cat /smap_bin/smap_version`
elif [ -e ~/smap_version ]; then
        version=`sudo cat ~/smap_version`
        cp ~/smap_version /smap_bin
fi

echo "Current Smap Version is $version"

# Apply database patches

if [ $version -lt "1908" ]
then
echo "applying pre 1909 patches to survey_definitions"
cat ./sd_pre_1908.sql | sudo -i -u postgres $PSQL -q -d survey_definitions 2>&1 | grep -v "already exists" | grep -v "duplicate key" | grep -vi "addgeometrycolumn" | grep -v "implicit index" | grep -v "skipping" | grep -v "is duplicated" | grep -v "create unique index" | grep -v CONTEXT
fi

if [ $version -lt "2306" ]
then
echo "applying pre 2306 patches to survey_definitions"
cat ./sd_pre_2306.sql | sudo -i -u postgres $PSQL -q -d survey_definitions 2>&1 | grep -v "already exists" | grep -v "duplicate key" | grep -vi "addgeometrycolumn" | grep -v "implicit index" | grep -v "skipping" | grep -v "is duplicated" | grep -v "create unique index" | grep -v CONTEXT
fi

echo "applying new patches to survey_definitions"
cat ./sd.sql | sudo -i -u postgres $PSQL -q -d survey_definitions 2>&1 | grep -v "already exists" | grep -v "duplicate key" | grep -vi "addgeometrycolumn" | grep -v "implicit index" | grep -v "skipping" | grep -v "is duplicated" | grep -v "create unique index" | grep -v CONTEXT | grep -v "does not exist"

echo "applying patches to results"
cat ./results.sql | sudo -i -u postgres $PSQL -q -d results 2>&1 | grep -v "already exists"

echo "Archiving records in survey_definitions"
cat ./archive.sql | sudo -i -u postgres $PSQL -q -d survey_definitions 2>&1 

# Version 14.02
if [ $version -lt "1402" ]
then
	echo "Applying patches for version 14.02"
	sudo apt-get install gdal-bin
fi

# version 14.03
if [ $version -lt "1403" ]
then
	echo "Applying patches for version 14.03"
	echo "set up forwarding - assumes uploaded files are under /smap"
        sudo cp -v  ../install/config_files/subscribers_fwd.conf /etc/init
        sudo cp -v  ../install/subscribers.sh /smap_bin
	sudo sed -i "s#{your_files}#/smap#g" /etc/init/subscribers_fwd.conf
	echo "Modifying URLs of attachments to remove hostname, also moving uploaded files to facilitate forwarding of old surveys"
	java -jar version1/patch.jar apply survey_definitions results
	sudo chown -R $TOMCAT_version /smap/uploadedSurveys
fi

# version 14.08
if [ $version -lt "1408" ]
then
echo "Applying patches for version 14.08"
echo "installing ffmpeg"
if [ $(cat /etc/*-release | grep "DISTRIB_CODENAME=" | cut -d "=" -f2) == 'trusty' ];
then  
sudo add-apt-repository 'deb  http://ppa.launchpad.net/jon-severinsson/ffmpeg/ubuntu trusty main'  && sudo add-apt-repository 'deb  http://ppa.launchpad.net/jon-severinsson/ffmpeg/ubuntu saucy main'  && sudo apt-get update
fi
sudo apt-get update -y
sudo apt-get install ffmpeg -y
fi

# version 14.10

if [ $version -lt "1410" ]
then
echo "Applying patches for version 14.10"
sudo chown -R $TOMCAT_version /var/log/subscribers
fi

# version 14.11
# Yes 14.11 patches being reapplied as last release was actually 14.10.02
if [ $version -lt "1411" ]
then
echo "Applying patches for version 14.11"
sudo chown -R $TOMCAT_version /smap/attachments
fi


# version 15.01
if [ $version -lt "1501" ]
then
echo "Applying patches for version 15.01"
sudo mkdir /smap/media/organisation
sudo chown -R $TOMCAT_version /smap/media
fi

# version 15.02
if [ $version -lt "1502" ]
then
echo "Applying patches for version 15.02"
sudo rm /var/lib/$TOMCAT_VERSION/webapps/fieldManager.war
fi

# version 15.03
if [ $version -lt "1503" ]
then
sudo mkdir /smap_bin/resources
sudo mkdir /smap_bin/resources/css
fi

# version 15.09
if [ $version -lt "1509" ]
then

# Patch the database
java -jar version1/patch1505.jar apply survey_definitions results

cd ../install
# Set up new apache configuration structure

if [ ! -f "$a_config_dir/smap.conf.bu" ]; then
    sudo cp  $a_config_dir/smap.conf $a_config_dir/smap.conf.bu
fi
sudo cp config_files/a24-smap.conf $a_config_dir/smap.conf

if [ ! -f "$a_config_dir/smap-ssl.conf.bu" ]; then
    sudo cp $a_config_dir/smap-ssl.conf $a_config_dir/smap-ssl.conf.bu
fi
sudo cp config_files/a24-smap-ssl.conf $a_config_dir/smap-ssl.conf

sudo a2ensite  smap.conf
sudo a2ensite  smap-ssl.conf

# Disable default sites - TODO find some way of smap coexistign with other sites on the same apache server automatically
sudo a2dissite 000-default
sudo a2dissite default-ssl

cd ../deploy

# Create miscelaneous directory
sudo mkdir /smap/misc
sudo chown $TOMCAT_version /smap/misc

fi

# version 15.11
if [ $version -lt "1511" ]
then
java -jar version1/patch.jar apply survey_definitions results
fi

# version 16.01
if [ $version -lt "1601" ]
then
	java -jar version1/patchcomplete.jar apply survey_definitions results
fi

# version 16.02
if [ $version -lt "1602" ]
then
	echo "no patches for 16.02"
fi

# version 16.03
if [ $version -lt "1603" ]
then
	echo "no patches for 16.03"
fi

# version 16.12
if [ $version -lt "1612" ]
then
echo "Applying patches for version 16.12"
sudo mkdir /smap_bin
sudo mkdir /smap_bin/resources
sudo mkdir /smap_bin/resources/css

sudo apt-get install python-dev -y
sudo apt-get install libxml2-dev -y
sudo apt-get install libxslt-dev
sudo apt-get install libxslt1-dev -y
sudo apt-get install git -y
sudo apt-get install python-setuptools -y
sudo easy_install pip
sudo pip install setuptools --no-use-wheel --upgrade
sudo pip install xlrd
sudo rm -rf src/pyxform
sudo pip install -e git+https://github.com/UW-ICTD/pyxform.git@master#egg=pyxform
sudo rm -rf /smap_bin/pyxform
sudo cp -r src/pyxform/pyxform/ /smap_bin
sudo a2enmod headers

sudo chown -R $TOMCAT_version /smap_bin

fi

# version 16.12
if [ $version -lt "1612" ]
then
	echo '# copy subscriber service files'
	service_dir="/etc/systemd/system"
	sudo cp ../install/config_files/subscribers.service $service_dir/subscribers.service
	sudo chmod 664 $service_dir/subscribers.service
	sudo cp ../install/config_files/subscribers_fwd.service $service_dir/subscribers_fwd.service
	sudo chmod 664 $service_dir/subscribers_fwd.service
fi

# Version 24.04
if [ $version -lt "2410" ]; then
	sudo a2enmod session
	sudo a2enmod request
	sudo a2enmod auth_form
	sudo a2enmod session_cookie
	sudo a2enmod session_crypto
fi

if [ ! -d "/smap/settings" ]
then
    mkdir /smap/settings
    cp ~ubuntu/region /smap/settings
    cp ~ubuntu/hostname /smap/settings
    cp ~ubuntu/bucket /smap/settings
    cp ~ubuntu/subscriber /smap/settings
fi

#####################################################################################
# All versions
# Copy the new apache configuration files and tomcat directory access
# Copy aws credentials

if [ -f $deploy_from/resources/properties/credentials ]
then
    # Resolve the tomcat user's home directory. systemd sets $HOME for the
    # tomcat service from this passwd entry, and the AWS SDK reads
    # $HOME/.aws/credentials. On servers migrated from tomcat9 the passwd home
    # can be stale (e.g. /var/lib/tomcat9, which no longer exists), so realign
    # it with the live CATALINA_HOME before copying. This self-heals servers
    # that were migrated before this fix and never re-run the migration block.
    TOMCAT_HOME=$(getent passwd $TOMCAT_USER | cut -d: -f6)
    NEEDS_HOME_FIX=false
    if [ -z "$TOMCAT_HOME" ] || [ ! -d "$TOMCAT_HOME" ] || [ "$TOMCAT_HOME" != "$CATALINA_HOME" ]; then
        NEEDS_HOME_FIX=true
    fi
    if [ "$NEEDS_HOME_FIX" = "true" ]; then
        echo "Tomcat user home '$TOMCAT_HOME' missing or stale; setting to $CATALINA_HOME"
        # usermod cannot change the home of a user that owns running processes.
        # patchdb.sh otherwise leaves them up, so stop every service that runs
        # as $TOMCAT_USER (tomcat plus the subscriber processes) for the change.
        sudo systemctl stop $TOMCAT_VERSION subscribers subscribers_fwd
        sudo usermod -d $CATALINA_HOME $TOMCAT_USER
        TOMCAT_HOME=$CATALINA_HOME
    fi
    # Clear a stale .aws that is a plain file or broken symlink, not a dir;
    # otherwise mkdir -p errors "File exists" and the cp fails "Not a directory".
    [ -d "$TOMCAT_HOME/.aws" ] || sudo rm -f "$TOMCAT_HOME/.aws"
    sudo mkdir -p $TOMCAT_HOME/.aws
    sudo cp $deploy_from/resources/properties/credentials $TOMCAT_HOME/.aws/credentials
    sudo chown -R $TOMCAT_USER $TOMCAT_HOME/.aws
    # Restart with the corrected home and credentials already in place
    if [ "$NEEDS_HOME_FIX" = "true" ]; then
        sudo systemctl start $TOMCAT_VERSION subscribers subscribers_fwd
    fi
    # update any other existing credential files
    for f in `locate .aws/credentials 2>/dev/null`
    do
        echo "processing $f"
        cp $deploy_from/resources/properties/credentials $f
    done
fi
sudo cp  $deploy_from/resources/properties/setcredentials.sh /smap_bin
if [ -f /var/lib/$TOMCAT_VERSION/bin/setenv.sh ]; then
	envset=`cat /var/lib/$TOMCAT_VERSION/bin/setenv.sh | grep -c "setcredentials"`
	if [ $envset -eq 0 ]; then
		echo ". /smap_bin/setcredentials.sh" | sudo tee -a /var/lib/$TOMCAT_VERSION/bin/setenv.sh 
	fi
fi


cd ../install
chmod +x apacheConfig.sh
./apacheConfig.sh

mkdir -p /etc/systemd/system/tomcat10.service.d
cp config_files/override.conf /etc/systemd/system/tomcat10.service.d/override.conf

# Make sure there is a logging properties file in the settings directory
if [ ! -f "/smap/settings/smap-logging.properties" ]; then
    echo "Default logging properties file added"
    sudo cp config_files/smap-logging.properties /smap/settings
fi

# Make sure exiftool is installed (used by processAttachment.sh to preserve exif data)
if ! command -v exiftool >/dev/null 2>&1; then
    echo "installing exiftool"
    sudo apt-get install libimage-exiftool-perl -y
fi

systemctl daemon-reload
cd ../deploy
 

# Get the AWS language codes
cp language_codes.csv /smap_bin
echo "truncate language_codes" | sudo -i -u postgres $PSQL -d survey_definitions
echo "\COPY language_codes (code, aws_translate, aws_transcribe, transcribe_default, transcribe_medical) FROM '/smap_bin/language_codes.csv' DELIMITER ',' CSV HEADER;" | sudo -i -u postgres $PSQL -d survey_definitions

# Set the full version in the database for the about page
echo "Setting version for about page"
full_version=`cat ./full_version`
echo "update server set version = '$full_version'" | sudo -i -u postgres $PSQL -q -d survey_definitions 2>&1

# update version reference
echo "2606" > /smap_bin/smap_version
