#!/bin/bash
# Copyright: (c) 2022 Claris International Inc. All rights reserved.
#

Linux_EXTRA_DIR="/opt/FileMaker/FileMaker Server/HTTPServer/conf/extra"
Mac_EXTRA_DIR="/Library/FileMaker Server/HTTPServer/conf/extra"
PATCH_CHANGE="# For Upload to Host and Insert into container. \n# Note: Compatible with Apache 2.4.40 and later\n<IfVersion > 2.4.48>\n    Proxy100Continue Off\n</IfVersion>\n\n</VirtualHost>"

if [[ "$OSTYPE" == "linux-gnu"* ]];
then
    if grep -q 'Proxy100Continue' "$Linux_EXTRA_DIR/httpd-ssl.conf";
    then
		echo "Patch already updated on Linux."
    else
        sed -i '/<\/VirtualHost>/d' "$Linux_EXTRA_DIR/httpd-ssl.conf"
        echo -e $PATCH_CHANGE >> "$Linux_EXTRA_DIR/httpd-ssl.conf"
        fmsadmin restart -y httpserver        
    fi
elif [[ "$OSTYPE" == "darwin"* ]];
then
	if grep -q 'Proxy100Continue' "$Mac_EXTRA_DIR/httpd-ssl.conf.2.4";
    then
		echo "Patch already updated on Mac."
    else
        sed -i '' '/<\/VirtualHost>/d' "$Mac_EXTRA_DIR/httpd-ssl.conf.2.4"
        echo -e $PATCH_CHANGE >> "$Mac_EXTRA_DIR/httpd-ssl.conf.2.4"
        rm "$Mac_EXTRA_DIR/httpd-ssl.conf"
        ln "$Mac_EXTRA_DIR/httpd-ssl.conf.2.4" "$Mac_EXTRA_DIR/httpd-ssl.conf"
        fmsadmin restart -y httpserver
    fi
else
	echo "Mac or Linux OS not found."
fi
