#!/bin/bash

# Takes care of updating the variable definitions for the web server
update_httpd_defs() {
    local conf=/etc/apache2/conf.d/01-spxmanage-defs.conf
    local srppass=/etc/spxmanage/srpsecrets/passwd.srpv
    local defs="NOSSL_LOCALHOST_ONLY NOSSL_DAV_PORT_ALLOW_REMOTE SSL_LOCALHOST_ONLY NOSSL_REDIRECT SSL_DISABLE_SRP SSL_SUBJECT_NAME"
    local d
    local c
    
    # If the config file was replaced by a new default configuration file we
    # need to transfer any uncommented defines to the new configuration file
    if [ -f "$conf".rpmsave ]; then
        for d in $defs; do
            c="$(egrep -m 1 '^[[:space:]]*Define[[:space:]]+'"$d"'([[:space:]]+|$)' "$conf".rpmsave)"
            if [ -n "$c" ]; then
                sed -i -e '/^#\?\s*Define\s\+'"$d"'\(\s\+\|$\)/c\' -e "$c" "$conf"
            fi
        done
        rm -f "$conf".rpmsave
        echo "Integrated $conf.rpmsave"
    fi
    
    # If there is no SRP verifier file it means that we are updating from a firmware
    # which did not have support for it, so we need to enable plain HTTP for the
    # WebDAV port of the web server to keep backwards compatibility
    if [ ! -f "$srppass" ]; then
        d="NOSSL_DAV_PORT_ALLOW_REMOTE\|SSL_DISABLE_SRP"
        sed -i -e '/^#\s*Define\s\+\('"$d"'\)\(\s\+\|$\)/s/^#\s*//' "$conf"
        echo "Enabling plain http WebDAV for backwards compatibility"
        echo "Disabling TLS-SRP for backwards compatibility"
    fi
}

update_httpd_defs
