#!/bin/sh

# csh and tcsh do not like this file
# ash produces incorrect quoting

if [ "`/bin/ls -la /bin/sh |grep '\-> ash'`" != "" ]; then
	echo -e "\nash shell is not supported\n"
	exit
fi

##############################################################################

CC="cc"
LD="cc"
CFLAGS="-O2 -Wall"
LFLAGS=""

PROG="bfe2"
OWNER="root"
GROUP="root"

DOCFILES="BUGS CHANGELOG COMMANDS COPYRIGHT INSTALL README.bferc TODO bferc-sample"

# install directories
PREFIX="/usr/local"
BIN="bin"
SYSCONF="etc"
DOC="share/doc/bfe2"

BOCHSPATH="bochs"

##############################################################################
# Platform configurations

OPTIONS=""
GTK="1.2"
BOCHS="2.0"

PLATFORM=`uname`
if [ "$PLATFORM" = "FreeBSD" ]; then
	CC=%%CC%%
	LD=%%CC%%
	CFLAGS=%%CFLAGS%%
	GROUP="wheel"
	PREFIX=%%PREFIX%%
fi

##############################################################################
# Parse command line options

DEBUG="NO"
while [ $1 ]; do
	if [ "$1" = "--help" ]; then
		echo ""
		echo -e "\t--prefix     [path]\tSet prefix path [default: $PREFIX]"
		echo -e "\t--bindir     [path]\tSet binary path [default: \$PREFIX/$BIN]"
		echo -e "\t--sysconfdir [path]\tSet config path [default: \$PREFIX/$SYSCONF]"
		echo -e "\t--docdir     [path]\tSet help files path [default: \$PREFIX/$DOC]"
		echo -e "\t--bochs-path [path]\tSet path to Bochs binary [default: $BOCHSPATH]"
		echo -e "\t--bochs      [ver]\tSet Bochs version: 2.0 [default: $BOCHS]"
		echo -e "\t--gtk        [ver]\tSet GTK version: 1.2 or 2.0 [default: $GTK]"
		echo -e "\t--quiet\t\t\tEnable fewer configure.sh messages"
		echo -e "\t--debug\t\t\tEnable debugging"
		echo -e "\t--confirm-quit\t\tEnable quit confirmation"
		echo ""
		exit
	elif [ "$1" = "--confirm-quit" ]; then
		OPTIONS="$OPTIONS -D__CONFIRM_QUIT__"
		shift
	elif [ "$1" = "--debug" ]; then
		OPTIONS="$OPTIONS -D__DEBUG__"
		DEBUG="YES"
		CFLAGS="$CFLAGS -g"
		shift
	elif [ "$1" = "--quiet" ]; then
		QUIET=1
		shift
	elif [ "$1" = "--gtk" ]; then
		GTK="$2"
		shift
		shift
	elif [ "$1" = "--bochs" ]; then
		BOCHS="$2"
		shift
		shift
	elif [ "$1" = "--bochs-path" ]; then
		BOCHSPATH="$2"
		shift
		shift
	elif [ "$1" = "--prefix" ]; then
		PREFIX="$2"
		shift
		shift
	elif [ "$1" = "--bindir" ]; then
		BINDIR="$2"
		shift
		shift
	elif [ "$1" = "--sysconfdir" ]; then
		SYSCONFDIR="$2"
		shift
		shift
	elif [ "$1" = "--docdir" ]; then
		DOCDIR="$2"
		shift
		shift
	else
		echo "Invalid option ($1)"
		exit
	fi
done

if [ "$PREFIX" = "/" ]; then
	PREFIX=""
fi
if [ "$BINDIR" = "" ]; then
	BINDIR="$PREFIX/$BIN"
fi
if [ "$SYSCONFDIR" = "" ]; then
	SYSCONFDIR="$PREFIX/$SYSCONF"
fi
if [ "$DOCDIR" = "" ]; then
	DOCDIR="$PREFIX/$DOC"
fi

if [ "$GTK" = "1.2" ]; then
	LIBS="\`gtk-config --libs\`"
	INCS="\`gtk-config --cflags\`"
elif [ "$GTK" = "2.0" ]; then
	LIBS="-L/usr/X11R6/lib \`pkg-config --libs gtk+-2.0\`"
	INCS="\`pkg-config --cflags gtk+-2.0\`"
else
	echo -e "\tInvalid GTK version [$GTK]"
	exit
fi

if [ "$BOCHS" = "2.0" ]; then
	BOCHSPATH="$BOCHSPATH -q"
else
	echo -e "\tInvalid Bochs version [$BOCHS]"
	exit
fi

echo ""
echo -e "\tConfig:     bindir = $BINDIR"
echo -e "\tConfig: sysconfdir = $SYSCONFDIR"
echo -e "\tConfig:     docdir = $DOCDIR"
echo -e "\tConfig:    options = $OPTIONS"
echo -e "\tConfig:      debug = $DEBUG"
echo -e "\tConfig: bochs-path = $BOCHSPATH"
echo -e "\tConfig:      bochs = $BOCHS"
echo -e "\tConfig:        gtk = $GTK"
echo ""


##############################################################################
# Final messages

if [ "$QUIET" != "1" ]; then
	echo -e "\tType 'make' to compile"
	if [ "$DEBUG" = "NO" ]; then
		echo -e "\tType 'make install' to install"
	fi
	echo -e "\tType 'make clean' to clean up"
	echo -e "\tType 'make distclean' to clean up and remove Makefile"
	echo ""

	if [ "$DEBUG" = "NO" ]; then
		echo -e "\t*******************************************"
		echo -e "\t*                                         *"
		echo -e "\t*  Did you read the above messages?       *"
		echo -e "\t*  Did you read \`./configure.sh --help\`?  *"
		echo -e "\t*                                         *"
		echo -e "\t*******************************************"
		echo ""
	fi

	echo -e "\tBochs MUST be configured with --enable-debugger,"
	echo -e "\t--enable-disasm and --disable-readline for BFE to work."
	echo -e "\tBinary packages of Bochs will not work with BFE."
	echo ""
fi


##############################################################################
# create Makefile

rm -f Makefile

cp -f .gtkrc-bfe gtkrc-bfe-sample
cp -f .bferc bferc-sample

echo "# Generated by configure.sh" >> Makefile
OBJS=""
for file in *.c; do
	OBJS="$OBJS ${file%%.c}.o"
done
echo -e "
all: $PROG

distclean: clean
	rm -f Makefile gtkrc-bfe-sample bferc-sample

clean:
	rm -f $PROG *.o core __*__

$PROG: $OBJS
	$LD $LFLAGS -o $PROG $OBJS $LIBS

.c.o:
	$CC $CFLAGS -o \$@ -c \$< $OPTIONS $INCS -DBOCHS_PATH=\"\\\"$BOCHSPATH\\\"\" -DCFGDIR=\"\\\"$SYSCONFDIR\\\"\"
" >> Makefile
for file in *.c; do
	echo "${file%%.c}.o: $file common.h functions.h Makefile" >> Makefile
done

if [ "$DEBUG" = "NO" ]; then
	echo -e "\ninstall: $PROG\n\tstrip $PROG" >> Makefile
	echo -e "\t(umask 022 ; /bin/mkdir -m 0755 -p $BINDIR)" >> Makefile
	echo -e "\tinstall -o $OWNER -g $GROUP -m 0755 $PROG $BINDIR" >> Makefile

	echo -e "\t(umask 022 ; /bin/mkdir -m 0755 -p $SYSCONFDIR)" >> Makefile
	echo -e "\tinstall -o $OWNER -g $GROUP -m 0644 .gtkrc-bfe $SYSCONFDIR/gtkrc-bfe" >> Makefile

	echo -e "\t(umask 022 ; /bin/mkdir -m 0755 -p $DOCDIR)" >> Makefile
	echo -e "\tinstall -o $OWNER -g $GROUP -m 0644 $DOCFILES $DOCDIR" >> Makefile
fi
