#!/bin/sh
# A script for compiling the kernel using ifconfig
# Written by the life. All rights reversed.
# Send bug reports to /dev/lamparna and apply patches yourself.
# Run in a directory with properly configured kernel sources.
# Shake well before using. Don't run on ENIAC. Warranty void unless int.

set -e
export LANG=C

echo -n "Looking for kernel sources... "
if [ ! -f kernel/sched.c ] ; then
	echo EEEK!
	echo >&2 "Use the source, Luke!"
	exit 1
fi
echo Oook.

PRIV=
PUB=
echo -n "Looking for IP address (running ifconfig)... "
for ip in `ifconfig | sed '/\<inet \(addr:\)\?/!d;s/.*inet [^0-9]*\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/'` ; do
	case $ip in
		127.*)	;;
		10.*|192.168.*|172.1[6-9].*|172.2[0-9].*|172.3[01].*)
			PRIV=$ip
			;;
		*)	PUB=$ip
			;;
	esac
done
IP=${PUB:-$PRIV}
if [ -z "$IP" ] ; then
	echo FAILED.
	echo >&2 "Your machine doesn't have a reasonable IP address. This is unlikely and unsupported."
	echo >&2 "... or it has an alien ifconfig command. This is awkward and also unsupported."
	exit 1
fi
echo $IP

TD=`mktemp -dt compile-kernel.XXXXXX`
cat >$TD/hostname <<EOF
#!/bin/sh
echo $IP
EOF
chmod +x $TD/hostname
PATH="$TD:$PATH"

RC=1
echo "Running make clean..."
if make clean ; then
	echo "Running full make..."
	make && echo "Congratulations, JXP would be proud of you!" && RC=0
fi

rm $TD/hostname
rmdir $TD
exit $RC
