#!/bin/sh

# Copyright 2015-2024 Rivoreo

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


set -f

src_dir="${0%/*}"
if [ "$src_dir" = "$0" ]; then
	src_dir=
else
	src_dir="$src_dir/"
fi

output_file=ping
while getopts Bao: c
do case $c in
	B|a)
		;;
	o)
		output_file="$OPTARG"
		;;
esac done

[ -z "$CC" ] && CC=gcc
have_optimizing_flag=
for flag in $CFLAGS; do case "$flag" in
	-O*)
		have_optimizing_flag=1
		;;
esac done
CFLAGS="$CFLAGS -Wall"
[ -z "$have_optimizing_flag" ] && CFLAGS="$CFLAGS -Os"

rm -f test || exit
if ! echo "int main() { return -1; }" | $CC $CFLAGS $LDFLAGS -x c - -o test; then
	printf "Compiler '%s' doesn't work\\n" "$CC" 1>&2
	exit 1
fi
if printf '#ifndef __ANDROID__\n#error "Not Android"\n=fail\n#endif\n' | $CC $CFLAGS -c -x c - -o test > /dev/null 2>&1; then
	rm -f test
	exec $CC $CFLAGS -I "${src_dir}linux-sysctl/android-include" $LDFLAGS "${src_dir}ping.c" "${src_dir}linux-sysctl/sysctl.c" -l m $LIBS -o "$output_file"
fi
if ! printf '#include <features.h>\n#if !defined __GLIBC__ || !defined __linux__\n#error "Not GNU/Linux"\n=fail\n#endif\n' | $CC $CFLAGS -c -x c - -o test > /dev/null 2>&1; then
	exec $CC $CFLAGS $LDFLAGS "${src_dir}ping.c" -l m $LIBS -o "$output_file"
fi
rm -f test
if msg1="`$CC $CFLAGS -c \"${src_dir}ping.c\" -o ping.o 2>&1`"; then
	[ -n "$msg1" ] && printf %s\\n "$msg1" 1>&2
else
	$CC $CFLAGS -I "${src_dir}linux-sysctl/gnu-include" -c "${src_dir}ping.c" -o ping.o || exit
fi
trap "rm -f ping.o sysctl.o" EXIT
if msg1="`$CC $LDFLAGS ping.o -l m $LIBS -o \"$output_file\" 2>&1`"; then
	[ -n "$msg1" ] && printf %s\\n "$msg1" 1>&2
	true
elif msg2="`$CC $CFLAGS -c \"${src_dir}linux-sysctl/sysctl.c\" -o sysctl.o 2>&1`"; then
	[ -n "$msg2" ] && printf %s\\n "$msg2" 1>&2
	$CC $LDFLAGS ping.o sysctl.o -l m $LIBS -o "$output_file"
else
	#SYSCTL_SYM_SONAME=libc.so.6
	SYSCTL_SYM_SONAME=librt.so.1

	set -e
	rm -f libc_local.so
	printf '.text\n.global sysctl\n.type sysctl, @function\nsysctl:\n.skip 4\n' | $CC -w -fPIC --shared -Wl,--soname,$SYSCTL_SYM_SONAME -nostdlib -x assembler - -o libc_local.so
	r=0
	if ! msg3="`$CC $LDFLAGS ping.o -l m $LIBS libc_local.so -o \"$output_file\" 2>&1`"; then
		printf '%s\n%s\n' "$msg1" "$msg2" 1>&2
		r=1
	fi
	set +e
	[ -n "$msg3" ] && printf %s\\n "$msg3" 1>&2
	rm -f libc_local.so
	exit $r
fi
