#!/bin/sh

set -e

if [ $# -eq 0 ]; then
	cat << EOF
Syntax:
  $(basename $0) OPT1 [OPT2...]"

Example:
  $(basename $0) [prepend|append] -fsanitize=address -fno-common -U_FORTIFY_SOURCE
  By default flags are appending.
EOF
	exit 1
fi

find --help > /dev/null || (echo >&2 "find utility malfunction, please check your environment" && exit 1)

if [ $(find $(dirname $0) -maxdepth 30 -name \*-real | wc -l) -gt 0 ]; then
	echo >&2 "$(basename $0): directory was already processed, aborting"
	exit 1
fi

FLAGS=""
LD_FLAGS=""

function divide_flags {
	NEED_LIB_NAME="N"
	for f in "$@"; do
		case $f in
		-l)
			NEED_LIB_NAME="Y"
			;;

		-l*)
			LDFLAGS="$LDFLAGS $f"
			;;

		-Wl,*)
			LDFLAGS="$LDFLAGS ${f:4}"
			;;

		*)
			if [ "$NEED_LIB_NAME" = "Y" ]; then
				LDFLAGS="$LDFLAGS -l$f"
				NEED_LIB_NAME="N"
			else
				FLAGS="$FLAGS $f"
			fi
		esac
	done
}

case "$1" in
prepend)
	shift
	divide_flags $@
	PREFLAGS=$FLAGS
	POSTFLAGS=
	LD_PREFLAGS=$LDFLAGS
	LD_POSTFLAGS=
	;;
append)
	shift
	divide_flags $@
	PREFLAGS=
	POSTFLAGS=$FLAGS
	LD_PREFLAGS=
	LD_POSTFLAGS=$LDFLAGS
	;;
*)
	divide_flags $@
	PREFLAGS=
	POSTFLAGS=$FLAGS
	LD_PREFLAGS=
	LD_POSTFLAGS=$LDFLAGS
	;;
esac

TMP=$(pwd)/tmp.$$
cat > $TMP << EOF
#!/bin/sh
if echo "$PREFLAGS "\$@" $POSTFLAGS" | grep -q -- "-fsanitize=undefined" && echo "\$@" | grep -q "\.gch\>"; then
	# UBSan doesn't support precompiled headers. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66343
	echo "Precompiled headers currently not supported by UBSan" >&2
	# Don't instrument kernel modules
	# Don't instrument with "-nostdlib" linking
elif ! echo "\$@" | grep -q -e __KERNEL__ -e \-nostdlib; then
	# Use readlink in order to follow symlinks if any
	\$(readlink -f \$0)-real $PREFLAGS "\$@" $POSTFLAGS
else
	# -Wl,--tizen-no-force-options is used to tell collect2 to not add force
	# options.  It will be removed in collect2.
	\$(readlink -f \$0)-real "\$@" -Wl,--tizen-no-force-options
fi
EOF
chmod +x $TMP

# Match gcc|g++|c++ with optional cross- preffix and -version ([0-9] and .) # suffix
GCC_REG='\b(gcc|g\+\+|c\+\+)([-\d\.]?)+'

find -L $(dirname $0) -maxdepth 30 -type f -a -perm -a=x | grep -E "($GCC_REG)$" | while read tool; do
	mv $tool $tool-real
	cp $TMP $tool
done

LD_TMP=$(pwd)/ld_tmp.$$
cat > $LD_TMP << EOF
#!/bin/sh
if ! echo "\$@" | grep -q -e \-\-tizen\-no\-force\-options; then
	# Use readlink in order to follow symlinks if any
	\$(readlink -f \$0)-real $LD_PREFLAGS "\$@" $LD_POSTFLAGS
else
	# Remove --tizen-no-force-options from the argument list
	FLAGS=\$(echo \$@ | sed -e 's/--tizen-no-force-options//g')
	\$(readlink -f \$0)-real \$FLAGS
fi
EOF
chmod +x $LD_TMP

GCC_REG_EXTRA='collect2'

find -L /usr/lib/gcc/armv7l-tizen-linux-gnueabi/14.2.0 -maxdepth 30 -type f -a -perm -a=x -name "$GCC_REG_EXTRA" | while read tool; do
	mv $tool $tool-real
	cp $LD_TMP $tool
done

if [ -d /emul ]; then
	find -L /emul -maxdepth 30 -type f -a -perm -a=x | grep -E "($GCC_REG|$GCC_REG_EXTRA)$" | while read tool; do
		ln -sf $(basename $tool) $tool-real
	done
fi

rm $TMP
rm $LD_TMP
