# This script will be called just before starting build process for W800
# It allows you to make changes to the SDK, for example..
# For example, you can use changed files in the SDK for the automated build during the checks for a PR without changing the SDK itself:
# So your PR needs a modified define in the SDK, for example ? This script can make this change directly before the build.
#
#
# As an example you will find a script below which will copy all content of the "override"
# directory to the corresponding location in the SDK
#
#DIRNAME=$(dirname $0);
#echo "PREBUILD script! Executed from $DIRNAME!"
# allow whitespace in file or path, so take only newline as seperator
#OFS=$IFS
#IFS='
#'
#for X in $(find platforms/W800/override/ -type f);do
#	# script is executed from main app directory, so take found file and path as source
#	S=${X};
#	# destination is path stripped from path to override
#	# so inside "override" we have the full path to the file
#	# starting with "sdk/OpenW800/..."
#	D=${X#platforms/W800/override/};
#	# if file is present, we replace it, otherwise file is added ...
#	[ -e $D ] && echo "PREBUILD: replacing file\n\t$D\n\twith file\n\t$S" || echo "PREBUILD: adding file\n\t$S\n\tas\n\t$D"
#	cp $S $D;
#done
## restore IFS to whatever it was before ...
#IFS=$OFS
# you can also use all other commands to change files, like
# sed -i "s/#define FOO bar/#define FOO baz/" sdk/OpenW800/platform/drivers/gpio/file_to_change.c
# or, let's assume you made a local change to your SDK
# and make a diff from that change (inside sdk/OpenW800/)
# git diff > ../../platforms/W800/my_change.diff
# ( or make the diff and copy this file to platforms/W800)
#
# and then in pre_build.sh you apply this patch with:
#
# patch -p 1 -d sdk/OpenW800 < platforms/W800/my_change.diff

LWIPOPTS="sdk/OpenW800/src/network/lwip2.0.3/include/lwip/lwipopts.h"

if [ -f "$LWIPOPTS" ]; then
	echo "PREBUILD W800: enabling lwIP mDNS options"

	if grep -q '^[[:space:]]*#define LWIP_MDNS_RESPONDER' "$LWIPOPTS"; then
		sed -i 's/^[[:space:]]*#define LWIP_MDNS_RESPONDER.*/#define LWIP_MDNS_RESPONDER             1/' "$LWIPOPTS"
	else
		sed -i '/^[[:space:]]*#define LWIP_IGMP[[:space:]]\+/a #define LWIP_MDNS_RESPONDER             1' "$LWIPOPTS"
	fi

	if grep -q '^[[:space:]]*#define LWIP_NUM_NETIF_CLIENT_DATA' "$LWIPOPTS"; then
		sed -i 's/^[[:space:]]*#define LWIP_NUM_NETIF_CLIENT_DATA.*/#define LWIP_NUM_NETIF_CLIENT_DATA      1/' "$LWIPOPTS"
	else
		sed -i '/^[[:space:]]*#define LWIP_IGMP[[:space:]]\+/a #define LWIP_NUM_NETIF_CLIENT_DATA      1' "$LWIPOPTS"
	fi

	if grep -q '^[[:space:]]*#define MEMP_NUM_UDP_PCB' "$LWIPOPTS"; then
		sed -i 's/^[[:space:]]*#define MEMP_NUM_UDP_PCB.*/#define MEMP_NUM_UDP_PCB                5/' "$LWIPOPTS"
	else
		sed -i '/^[[:space:]]*#define LWIP_IGMP[[:space:]]\+/a #define MEMP_NUM_UDP_PCB                5' "$LWIPOPTS"
	fi
else
	echo "PREBUILD W800: WARN lwipopts.h not found: $LWIPOPTS"
fi
