#!/bin/sh

IFS='
'
# This patch will try to update a file in the image directory, korrelation.html. 
# There was a small update to this file after it had been released to customer. 
# If the file exsist, we check if it is the old version. If so, wi simply overwrites it
# If the file does not exist, or if it is not exactly the same as the old one, if is left 
# unchanged

# Directory of this script
SCRIPT_DIR=$(dirname "$0")

# What to do: check if this patch is needed or actually applying it...
ACTION=$1

# Directory that contains the file to be updated 
IMAGES_DIR="/usr/local/www/default/files/images"

# Name of the html-file
HTML_FILE_NAME=korrelation.html
HTML_FILE_NAME_OLD=korrelation_old.html

# Check if html-file exist
if [ -e $IMAGES_DIR/$HTML_FILE_NAME ]; 
then
	# Go to folder where the html-file is located
	cd $IMAGES_DIR

	# Run diff to determine if the file needs to be updated. Ignore whitespaces...
	diff -w $SCRIPT_DIR/$HTML_FILE_NAME_OLD $IMAGES_DIR/$HTML_FILE_NAME > /dev/null
	
	# Check if the file needs to be updated
	if [ $? -eq 0 ];
	then
		if [ "$ACTION" == "check" ]; 
		then
			# Inform caller that this patch is needed
			exit 1
		else
			logger "$HTML_FILE_NAME needs to be replaced"
			cp $SCRIPT_DIR/$HTML_FILE_NAME $IMAGES_DIR/$HTML_FILE_NAME
		fi	
	else
		# html-file does not need to be updated, do not execute this script
		exit 0
	fi

else
	# html-file does not exist, do not execute this script
	exit 0
fi

exit 0
