#!/bin/bash -e

cd $(dirname $(readlink -e "$0"))
. ./libinstall.sh

# Explicitly set umask
umask 0022

# Restart the upgrade
if [ -t 1 ]; then
	$0 | tee -a "upgrade.log"
	exit 0
fi

path='nagioslogserver'
proddir="/var/www/html/nagioslogserver"
backenddir="/usr/local/nagioslogserver"

do_upgrade_check

#source the old version number
. "$proddir/lsversion"
oldversion=$VERSION

#source the new number
. "$path/basedir/html/lsversion"
newversion=$VERSION

# Initialize upgrade.log
echo "Nagios Log Server Upgrade Log
==========================
DATE: $(date)

DISTRO INFO:
$distro
$version
$architecture

Old Version: $oldversion
New Version: $newversion
"

ELASTICSEARCH_UPDATED=0
LOGSTASH_UPDATED=0
NCPA_UPDATED=0
INTERACTIVE="True"

# Make sure php-dev/php-devel is added
if [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then
    if [ "$distro" == "Ubuntu" ] && [ -f /etc/apt/sources.list.d/debian.list ]; then
        rm /etc/apt/sources.list.d/debian.list
    fi

    apt-get -y install php-dev

    if [ "$dist" == "ubuntu24" ]; then
        apt-get install -y tzdata-legacy
    fi
else
    if [ "$dist" != "el7" ]; then
        # This package is only unavailable on rhel/oracle 7
        yum install -y php-devel
    fi
fi

# Install sourceguardian and restart Apache/php-fpm
install_sourceguardian
if [ `command -v systemctl` ]; then
    systemctl restart $httpd

    # systemctl status returns 0 if php-fpm exists, 4 if php-fpm is not a service
    set +e
    systemctl status php-fpm >/dev/null 2>&1
    has_php_fpm=$?
    set -e
    if [ "x$has_php_fpm" = "x0" ]; then
        systemctl restart php-fpm
    fi
else
    service $httpd restart
    # Note: As of March 2023, php-fpm is only installed on systemd-based distros
    # Init-based systems are also using mod_php, so just restarting apache should be sufficient.
fi

# Verify server has needed prereqs
if [ "$oldversion" -ge 200 ]; then

    /usr/bin/php $proddir/www/index.php install/verify_prereqs

fi

# Set Elasticsearch clustering defaults
elasticsearch_allow_reallocate

#### VERSION SPECIFIC UPDATES ##########


if [ "$oldversion" -le 1 ]; then

    # Update elasticsearch to latest
    if [ $ELASTICSEARCH_UPDATED -eq 0 ]; then
        (
            cd ./subcomponents/elasticsearch
            ./upgrade
        )
        ELASTICSEARCH_UPDATED=1
    fi

fi


if [ "$oldversion" -le 2 ]; then

    pip install setuptools --upgrade

fi


if [ "$oldversion" -le 100 ]; then

    cp -r nagioslogserver/basedir/html/application/config/config.local.php "$proddir/application/config/config.local.php"

fi


if [ "$oldversion" -le 103 ]; then

    sed -i 's/cluser_id/cluster_id/g' "$proddir/application/config/config.local.php"

fi


if [ "$oldversion" -le 104 ]; then
    # Update MAX_LOCKED_MEMORY to unlimited so we can use the mlockall feature in ES
    sed -i 's/^#MAX_LOCKED_MEMORY=$/MAX_LOCKED_MEMORY=unlimited/g' /etc/sysconfig/elasticsearch
    # Force a 1g heap if not already changed
    mem='$(expr $(free -m|awk '\''/^Mem:/{print $2}'\'') / 2 )m'
    sed -i 's~^#ES_HEAP_SIZE=2g$~# Nagios Log Server Default to 0.5 physical Memory\nES_HEAP_SIZE='"$mem"'~g' /etc/sysconfig/elasticsearch

fi


if [ "$oldversion" -le 105 ]; then
    yum install php-ldap -y
    
    # Update Elasticsearch
    if [ $ELASTICSEARCH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/elasticsearch
            ./upgrade
        )
        ELASTICSEARCH_UPDATED=1
    fi

    # Update Logstash
    if [ $LOGSTASH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/logstash
            ./upgrade
        )
        LOGSTASH_UPDATED=1
    fi

fi


# Version 1.4.0
if [ "$oldversion" -le 140 ]; then

    yum install openssl pyOpenSSL -y
    
    # Update elasticsearch curator to latest
    pipextra=""
    if [ "$dist" == "el6" ]; then
        pipextra="urllib3==1.22"
    fi
    pip install --upgrade elasticsearch-curator==3.4.0 $pipextra

    # Re-install apache config - first check if the readwrite strings have been previously added
    apacheregex=$(sed -n '/RewriteEngine/{:start /\[L,QSA\]/!{N;b start};/.*/p}' "$httpdconfdir/nagioslogserver.conf")

    if [ -z "$apacheregex" ]; then
        echo >> "$httpdconfdir/nagioslogserver.conf" # newline
        grep "^Rewrite" nagioslogserver/httpd.nagioslogserver.conf >> "$httpdconfdir/nagioslogserver.conf" # append readwrite conditions
    fi
fi


# Version 1.4.2
# - Minor patches for older versions
if [ "$oldversion" -lt 142 ]; then

    # Generate a random key value for encryption_key
    key=$(openssl rand -hex 32)
    sed -i "/\$config\['encryption_key'\]/c\$config['encryption_key'] = '$key';" $proddir/application/config/config.local.php

fi


# Version 2.0.0
# - Updates to LS and ES versions
if [ "$oldversion" -lt 200 ]; then

    if [ "$INTERACTIVE" = "True" ]; then
    # CentOS, RedHat, Ubuntu, Debian, openSUSE, or SUSE Enterprise
    fmt -s -w $(tput cols) <<-EOF

===========================
Nagios Log Server 2 Upgrade
===========================

Upgrading to version 2 requires active maintenance on your license. Please double check before upgrading.

Once upgraded, the Log Server interface will not be usable until you have activated. You can activate do this directly in the interface after upgrade.

You will need the following information to activate:

- Log Server license key
- Account ID or Token

EOF
        read -p "Do you want to continue? [Y/n] " res

        case "$res" in
            Y | y | "")
                echo "Proceeding with upgrade..."
                ;;
            *)
                echo "Upgrade cancelled"
                exit 0
        esac
    fi

    # Update Elasticsearch -> 1.7.6
    if [ $ELASTICSEARCH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/elasticsearch
            ./upgrade "$distro" "$dist" "$apacheuser" "$apachegroup" "$nagiosuser" "$nagiosgroup"
        )
        ELASTICSEARCH_UPDATED=1
    fi

    # Update Logstash -> 2.4.1
    if [ $LOGSTASH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/logstash
            ./upgrade "$distro" "$dist"
        )
        LOGSTASH_UPDATED=1
    fi

fi


# Version 2.0.3
# - Update LS tcp input to not have tcp connection leak and install Java 8
if [ "$oldversion" -lt 2003 ]; then

    # Install Java 8
    yum install -y java-1.8.0-openjdk

    # Update tcp input plugin
    (
        cd subcomponents/logstash/mods/plugins
        $backenddir/logstash/bin/logstash-plugin install --version 4.2.4 logstash-input-tcp.4.2.4-java.gem || true   
    )

    # Restart logstash
    if [ `command -v systemctl` ]; then
        systemctl restart logstash
    else
        service logstash restart
    fi

fi


# Version 2.0.8
# - Update the LS filter geoip to use the latest geoip2 dbs
if [ "$oldversion" -lt 2008 ]; then

    # Set the version and update the filter for geoip2
    (
        cd subcomponents/logstash/mods/plugins
        $backenddir/logstash/bin/logstash-plugin install --version 4.0.4 logstash-filter-geoip.4.0.4-java.gem || true
    )

    # Restart logstash
    if [ `command -v systemctl` ]; then
        systemctl restart logstash
    else
        service logstash restart
    fi
    
    # re-copy init script for Ubuntu and Debian required to allow more than 1GB memory
    if [ "$distro" == "Debian" ] || [ "$distro" == "Ubuntu" ]; then
        mv /etc/init.d/elasticsearch /etc/init.d/elasticsearch.old
        cp ./subcomponents/elasticsearch/mods/etc/init.d/elasticsearch.deb.init /etc/init.d/elasticsearch
        chmod +x /etc/init.d/elasticsearch
        if [ `command -v systemctl` ]; then
            systemctl daemon-reload
        fi
    fi
fi


# Version 2.1.0
# Need to install some options for AD/LDAP certificate management
if [ "$oldversion" -lt 2100 ]; then

    # Make sure wkhtmltox dependencies are installed
    if [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then
        apt-get -y install xfonts-base xfonts-75dpi

        # Set up languages for Debian and Ubuntu
        (
            cd nagioslogserver/basedir/html/application/language
            for lang in *; do
                if [ -d "$lang" ]; then
                    if [ "$dist" == "debian8" ] || [ "$dist" == "debian9" ]; then
                        sed -i "/#[[:blank:]]*$lang/s/#//" /etc/locale.gen
                    else
                        locale-gen "$lang" &> /dev/null
                        locale-gen "$lang.UTF-8" &> /dev/null
                    fi
                fi
            done
            echo "Configuring locale... this might take a minute..."
            if [ `command -v locale-update` ]; then
                locale-update &> /dev/null
            else
                locale-gen &> /dev/null
            fi
        )

    fi

    # This is the same as configure_ldap() in the fullinstall script
    # php-ldap already installed in prereqs
    if [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then
        ldap_config="/etc/ldap/ldap.conf"
        ldap_dir="/etc/ldap"
        cacerts_dir="/etc/ldap/cacerts"
    else
        ldap_config="/etc/openldap/ldap.conf"
        ldap_dir="/etc/openldap"
        cacerts_dir="/etc/openldap/cacerts"
    fi

    # Set the permissions of the openldap configuration files/folders
    mkdir -p $cacerts_dir
    mkdir -p $ldap_dir/certs
    chown $apacheuser.$nagiosgroup $ldap_dir $ldap_dir/certs $cacerts_dir $ldap_config
    chmod 664 $ldap_config
    chmod 775 $ldap_dir $ldap_dir/certs $cacerts_dir

    echo "\$config['ldap_dir'] = '$ldap_dir';" >> $proddir/application/config/config.local.php
    echo "\$config['ldap_cacerts_dir'] = '$cacerts_dir';" >> $proddir/application/config/config.local.php

    # Edit line in ldap config
    sed -i 's/TLS_CACERTDIR/#TLS_CACERTDIR/g' $ldap_config
    echo "TLS_CACERTDIR $cacerts_dir" >> $ldap_config

    touch $backenddir/var/auditlog.log

    touch "$backenddir/var/chromium_report.log"
	chown $nagiosuser:$nagiosgroup "$backenddir/var/chromium_report.log"
	chmod 0664 "$backenddir/var/chromium_report.log"

    # Install NCPA
    (
        cd subcomponents/ncpa
        ./install "$distro" "$dist" "$ver"
    )

    # Update permissions on logstash
    (
        cd subcomponents/logstash
        ./upgrade "$distro" "$dist"
    )
fi


# Version 2.1.2
# Need to install JP fonts for wkhtmltopdf
if [ "$oldversion" -lt 2102 ]; then

    if [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then
        apt-get install -y fonts-ipafont-mincho fonts-ipafont-gothic
    else
        yum install -y ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmincho-fonts
    fi

fi

# Version 2.1.11
# Reinstall elasticsearch and logstash to remove log4j dependencies
if [ "$oldversion" -lt 2111 ]; then

    # Recompile core for added XSS patches
    if [ $ELASTICSEARCH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/elasticsearch
            ./upgrade "$distro" "$dist"
        )
        ELASTICSEARCH_UPDATED=1
    fi

    if [ $LOGSTASH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/logstash
            ./upgrade "$distro" "$dist"
        )
        LOGSTASH_UPDATED=1
    fi
fi

# Version 2.1.12
# Reinstall logstash again due to an incorrect path in 2.1.11
if [ "$oldversion" -lt 2112 ]; then

    if [ $LOGSTASH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/logstash
            ./upgrade "$distro" "$dist"
        )
        LOGSTASH_UPDATED=1
    fi

    if [ $NCPA_UPDATED -eq 0 ]; then
        (
            cd subcomponents/ncpa
            ./upgrade "$distro" "$dist" "$ver"
        )
        NCPA_UPDATED=1
    fi
fi

# Version 2.1.13
# Reinstall elasticsearch and logstash to add reload4j in places where log4j was.
if [ "$oldversion" -lt 2113 ]; then

    if [ $ELASTICSEARCH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/elasticsearch
            ./upgrade "$distro" "$dist"
        )
        ELASTICSEARCH_UPDATED=1
    fi

    if [ $LOGSTASH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/logstash
            ./upgrade "$distro" "$dist"
        )
        LOGSTASH_UPDATED=1
    fi
fi

# Version 2.1.14
if [ "$oldversion" -lt 2114 ]; then
    # Update logstash (automatically delete minitar 0.6.1)
    if [ $LOGSTASH_UPDATED -eq 0 ]; then
        (
            cd subcomponents/logstash
            ./upgrade "$distro" "$dist"
        )
        LOGSTASH_UPDATED=1
    fi

    # Update NCPA
    if [ $NCPA_UPDATED -eq 0 ]; then
        (
            cd subcomponents/ncpa
            ./upgrade "$distro" "$dist" "$ver"
        )
        NCPA_UPDATED=1
    fi
fi

# Version 2.1.15
if [ "$oldversion" -lt 2115 ]; then
    # Ensure prerequisite mibs are installed.
    if [ "$distro" == "Ubuntu" ]; then
        apt-get install -y cron snmp-mibs-downloader
    fi

    # On CentOS 8/9, ensure other locales are installed
    if [ "$dist" == "el8" ] || [ "$dist" == "el9" ]; then
        dnf install -y glibc-langpack-bg glibc-langpack-cs glibc-langpack-de glibc-langpack-es glibc-langpack-fr glibc-langpack-it glibc-langpack-ja glibc-langpack-ko glibc-langpack-pl glibc-langpack-pt glibc-langpack-ru glibc-langpack-zh
    fi
fi

# Version 3.0.0, This refers to 2024R1
if [ "$oldversion" -lt 3000 ]; then
    (
        cd subcomponents/chromium
        ./install "$distro" "$dist"
    )

    if [ $(command -v python3.9) ]; then
        # do nothing
        :
    else
        wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
        tar -xzf Python-3.9.0.tgz
        # Using a subshell to temporarily change directory
        (
            cd Python-3.9.0
            if [ "$distro" != "Ubuntu" ] && [ "$distro" != "Debian" ]; then
                yum install -y openssl-devel openssl
            elif [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then
                apt-get install -y libssl-dev
            fi
            ./configure --prefix=/opt/python3.9 
            make && make install
        )

        ln -s /opt/python3.9/bin/python3.9 /usr/local/bin/python3.9
    fi

    python3.9 -m pip install --upgrade pip
    python3.9 -m pip install "openai<1.0.0"
    python3.9 -m pip install "urllib3<2.0"

    systemctl restart sendmail
    systemctl enable sendmail
fi

if [ $oldversion -lt 3304 ]; then
    get_ip

    systemctl stop elasticsearch

    # Convert elasticserch service to a systemd based service
    if [ -d "/usr/lib/systemd" ] ; then
        cp -prf subcomponents/elasticsearch/mods/etc/systemd/* /usr/lib/systemd
        cp -prf subcomponents/elasticsearch/mods/etc/systemd/* /usr/lib/systemd/system
    fi

    cp -p subcomponents/elasticsearch/mods/elasticsearch/elasticsearch.in.sh /usr/local/nagioslogserver/elasticsearch/bin

    systemctl daemon-reload
    systemctl enable elasticsearch
    systemctl start elasticsearch

    # Edit line in rsyslog config
    sed -i "s/Target=\"localhost\"/Target=\"$ip\"/g" /etc/rsyslog.d/nagioslogserver.conf
	if [ `command -v firewalld` ]; then
		systemctl restart rsyslog
	else
		service rsyslog restart
	fi

    if [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then
		ldap_config="/etc/ldap/ldap.conf"
		ldap_dir="/etc/ldap"
		cacerts_dir="/etc/ldap/cacerts"
	else
		ldap_config="/etc/openldap/ldap.conf"
		ldap_dir="/etc/openldap"
		cacerts_dir="/etc/openldap/cacerts"
	fi

	# Edit line in ldap config
	sed -i 's/^TLS_CACERTDIR/#TLS_CACERTDIR/g' $ldap_config
	if [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then
		echo "TLS_CACERTDIR $ldap_dir/cacerts" >> $ldap_config
	fi

fi

if [ $oldversion -lt 3305 ]; then
    DATA_DIR='"/usr/local/nagioslogserver/elasticsearch/data"'
    # Determine existing ES dta directory
    if [ -f /etc/sysconfig/elasticsearch ]; then
        DATA_DIR=`grep DATA_DIR /etc/sysconfig/elasticsearch | sed "s/DATA_DIR=//"`
    elif [ -f /etc/default/elasticsearch ]; then
        DATA_DIR=`grep DATA_DIR /etc/default/elasticsearch | sed "s/DATA_DIR=//"`
    fi

    sed -i "s#^DATA_DIR=.*#DATA_DIR=$DATA_DIR#g" /usr/local/nagioslogserver/elasticsearch/bin/elasticsearch.in.sh

    systemctl restart elasticsearch
fi

##### ALL VERSION UPDATES ##############


# ---------------------------------
# Update sudoers if it needs it
# ---------------------------------
sudoers

# update cron
sed -i "s/@APACHE@/$apacheuser/g" nagioslogserver/nagioslogserver.cron.d
install -m 644 nagioslogserver/nagioslogserver.cron.d /etc/cron.d/nagioslogserver 

# Copy source directory to the system
/bin/cp -f "$proddir/application/config/config.local.php" /tmp
/bin/rm -rf "$proddir/system"
/bin/rm -rf "$proddir/application/controllers"
/bin/rm -rf "$proddir/application/models"
/bin/rm -rf "$proddir/application/views"
/bin/rm -rf "$proddir/application/libraries"
/bin/cp -rf nagioslogserver/basedir/html/* "$proddir"

/bin/mv -f /tmp/config.local.php "$proddir/application/config/"

# Copy new data files to log server
/bin/cp -rf nagioslogserver/basedir/etc/* "$backenddir/etc"

# Copy backend directory scripts and replace permissions
/bin/cp -rf nagioslogserver/basedir/scripts/* "$backenddir/scripts"
chown -R nagios:nagios "$backenddir"
chmod -R 0775 "$backenddir"
chmod 0554 $backenddir/scripts/*
for script in change_timezone.sh reconfigure_ncpa.sh get_logstash_ports.sh profile.sh reconfigure_ncpa.sh; do
    chown root:root $backenddir/scripts/$script
done

# Update log rotation file
cp -r ./nagioslogserver/logrotate.nagioslogserver /etc/logrotate.d/nagioslogserver

# Update Kibana
(
    cd subcomponents/kibana
    ./upgrade
)

# Set up permissions
for dir in application/cache application/config application/logs application/dashboards www/media; do
    mkdir -p "$proddir/$dir"
    chgrp -R $apachegroup "$proddir/$dir"
    chmod -R g+rwx  "$proddir/$dir"
done

if [ $oldversion -lt 3306 ]; then
	chmod -R og-w "$backenddir"
	chmod -R 0664 "$backenddir/var"
	chmod 775 "$backenddir/var"
	chmod 775 "$backenddir/tmp"
	chmod 0554 $backenddir/scripts/*
fi

# Special Logstash changes
if [ $LOGSTASH_UPDATED -eq 1 ]; then

    echo "Re-applying logstash configuration..."
    /usr/bin/php $proddir/www/index.php configure/write_configs_for_node

    # Restart logstash
    if [ `command -v systemctl` ]; then
        systemctl restart logstash
    else
        service logstash restart
    fi

fi

# Version 2.1.0
# This goes after source files are updated but before elasticsearch is restarted.
if [ "$oldversion" -lt 2100 ]; then

    # Add Audit Log to global config
    # Depends on new sources
    auditlog_path=$(pwd)/nagioslogserver/basedir/etc/configs/xi_auditlog_config
    auditlog_path=$(echo $auditlog_path | sed 's#/#%2F#g')
    php "$proddir/www/index.php" cli CLI_AddToConfig add config_filters 'XI Audit Log' "$auditlog_path"

fi

# Version 2.1.3
# - Update existing users to have a username_lower property
if [ "$oldversion" -lt 2103 ]; then
    /usr/bin/php $proddir/www/index.php install/update_usernames
fi

# Version 2.1.4
# - Real-time Alerts need to have their logstash configurations regenerated
if [ "$oldversion" -lt 2104 ]; then
    /usr/bin/php $proddir/www/index.php install/update_realtimealert_configs
    /usr/bin/php $proddir/www/index.php configure/apply_to_instances
fi

if [ "$oldversion" -lt 2115 ]; then
    # Ensure dead snapshots from 2.1.14 are deleted
    if [ "$distro" == "Ubuntu" ]; then
        /usr/bin/php $proddir/www/index.php install/upgrade_2115_clean_snaps
    fi
fi

if [ "$oldversion" -lt 3001 ]; then
    /usr/bin/php $proddir/www/index.php install/create_default_reports
fi

if [ "$oldversion" -lt 3100 ]; then
    /usr/bin/php $proddir/www/index.php install/update_ai_api_key_settings
    python3.9 -m pip install --upgrade "openai<1.23.6"
	python3.9 -m pip install "anthropic<0.29.0"
	python3.9 -m pip install "mistralai<0.4.1"
    /usr/bin/php $proddir/www/index.php install/add_audit_and_alert_retention
fi

# Apply elasticsearch template
mkdir -p $backenddir/elasticsearch/config/templates
cp -rf subcomponents/elasticsearch/mods/elasticsearch/config/templates/* $backenddir/elasticsearch/config/templates
if [ `command -v systemctl` ]; then
    systemctl restart elasticsearch
else
    service elasticsearch restart
fi

# Wait for elasticsearch to listen:
echo "Waiting 30 seconds for Elasticsearch to start..."
ES_COUNTDOWN=30
ES_RUNNING=0
while [ $ES_COUNTDOWN -gt 0 ]; do
    if (command -v nc >/dev/null 2>&1 && nc -z -w 1 localhost 9200 2>/dev/null) \
       || timeout 1 bash -c "</dev/tcp/localhost/9200" 2>/dev/null; then
        ES_RUNNING=1
        break
    fi
    ES_COUNTDOWN=$((ES_COUNTDOWN - 1))
    sleep 1
done

if [ $ES_RUNNING -eq 0 ]; then
    echo "Elasticsearch failed to start. Please check /var/log/elasticsearch/elasticsearch.log"
    exit 1
fi

sleep 2

# Run the upgrade script to import new system dashboards
/usr/bin/php $proddir/www/index.php install/upgrade/$oldversion


if [ "$distro" != "Ubuntu" ] && [ "$distro" != "Debian" ]; then
	# Opens ElasticSearch Port for future migration.
	open_tcp_ports 9200:9400
fi

# Set php.ini defaults
echo "Checking php.ini defaults..."
for file in $(find /etc -name "php.ini"); do
	# Check memory_limit
    echo "Checking memory_limit for PHP in $file..."
    memory_limit=$(grep "memory_limit" $file | awk '{print $3}')
    # Extract the integer value from the memory limit
    int_memory_limit=$(echo $memory_limit | sed 's/[^0-9]*//g')
    # Compare the extracted integer value with another value
    if [ -n "$int_memory_limit" ]; then
        if [ "$int_memory_limit" -lt "1024" ]; then
            echo "memory_limit is less than 1024M in $file, setting to 1024M..."
            sed -i 's/memory_limit = .*/memory_limit = 1024M/g' $file
        fi
    fi
done

#### After Upgrade Final Changes ####


# Version 2.0.0
# - Reset the backend commands
if [ "$oldversion" -lt 200 ]; then
    /usr/bin/php $proddir/www/index.php install/reset_commands
fi

#### Complete Upgrade ####


echo
echo "Nagios Log Server Upgrade Complete!"
echo

if [ -n "$path" ]; then
    get_ip
    
    echo "You can access the Nagios Log Server web interface by visiting:"
    echo "    http://$ip/$path/"
    echo
fi
