#!/bin/sh
#
#   yamm, Yet Another Micro Monitor
#   Configure v. 0.1
#   Copyright (C) 1994  Riccardo Facchetti
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

FIND=find
WC=wc
AWK=/usr/bin/awk
INC=ncurses.h
INCTMP=ncurses.h.tmp
RM=/bin/rm
MTMP=Makefile

trap "exit_error" 1 2 3 15

exit_error() {
	$RM -f $INCTMP $INC $CTMP $AOUT
	echo
	echo "Configuration aborted: "
	msg=$1
	if [ "$msg"x = "x" ]
	then
		msg=interrupted
	fi
	echo $msg
	exit -1
}

FindOS() {
	OS=`uname -a|awk '{print $1}'`
	if [ "$OS" = "Linux" ]
	then
		OS=linux
	else
		OS=
		if [ -f /hp-ux ]
		then
			OS=HPUX
		fi
	fi
	if [ "$OS"x = "x" ]
	then
		exit_error "can not guess OS."
	fi
}

FindCompiler() {
	comp=$1
	CTMP=tmp$$.c
	AOUT=a.out
	echo "Searching C compiler..."
	echo "main(){int i;i=0;}" > $CTMP
	$comp $CTMP -o $AOUT 2> /dev/null
	if [ $? -ne 0 ]
	then
		comp=cc
		$comp $CTMP -o $AOUT 2> /dev/null
		if [ $? -ne 0 ]
		then
			exit_error "seems you have not a C compiler"
		fi
	fi
	echo "Found $comp compiler..."
	echo
	echo "Testing C preprocessor..."
	stringify=
	echo "#define _S(p)	printf(#p)" > $CTMP
	echo "main() { _S(prova); }" >> $CTMP
	$comp $CTMP -o $AOUT 2> /dev/null
	if [ $? -eq 0 ]
	then
		echo "C preprocessor works fine."
		echo
		stringify="-DSTRFY"
	else
		echo "C preprocessor don't support stringify"
		echo "It will compile, but the names of signals in signal.c"
		echo "will be set up to UNKNOWN. Check signals.c"
		echo
	fi
	$RM -f $CTMP $AOUT
}

FindNcurses() {
	echo "Searching for ncurses.h..."
	$FIND /usr/include -name $INC -print > $INCTMP
	$FIND /usr/local/include -name $INC -print >> $INCTMP
	NUMN=`$WC $INCTMP|$AWK '{print $1}'`
	if [ $NUMN != 1 ]
	then
		echo
		echo "Error: you have a probably broken installation of ncurses."
		echo "I have found zero or more than one ncurses.h header file"
		echo "in your system include directory. Follows a list of"
		echo "ncurses headers found:"
		echo
		if [ "`cat $INCTMP`"x = x ]
		then
			echo
			echo "No ncurses.h file found in system include directories."
			echo "Please be sure you have the ncurses library installed"
			echo "on your system before trying to use it"
			echo
			exit_error "ncurses.h not found."
		else
			cat $INCTMP
			echo
			echo "Find the right ncurses.h file and delete or rename the"
			echo "bad one. This may be due to an incorrect upgrade of the"
			echo "ncurses package."
			echo
			exit_error "too many ncurses.h."
		fi
	fi
	NCDIRH=`cat $INCTMP | sed s/ncurses\.h//g`
	echo "/* header file generated by 'Configure' of yamm 2.5.2 */" > $INC
	echo "/* do not touch */" >> $INC
	grep local $INCTMP
	if [ $? -eq 0 ]
	then
		offset=20
	else
		offset=14
	fi
	HDR=`cat $INCTMP | cut -b $offset-`
	echo "#include <$HDR>" >> $INC
	$RM -f $INCTMP
	echo "ncurses.h found and included"
}

readline() {
	line=
	def=$2
	if [ "$OS" = "linux" ]
	then
		echo -n "$1 [$2]: "
	else
		echo "$1 [$2]: "
	fi
	read line
	if [ "$line"x = "x" ]
	then
		line=$def
	fi
}
	
# Main section
echo "Searching OS..."
FindOS
echo OS="$OS"
#
FindCompiler "gcc"
# readline "Choose the compiler" "$comp"
# comp=$line
#
readline "Choose where the binary file will go" "/usr/local/bin"
bindir=$line
#
readline "Choose where the manual page will go" "/usr/local/man/man1"
mandir=$line
#
readline "Which kind of curses do you want ? (curses/ncurses/no)" "ncurses"
curs=$line
case $curs in
	ncurses)
		FindNcurses
		curlib=-l$curs
		NCFILE=ncurses.h;;
	curses)
		NCDIRH=.
		curlib=-l$curs
		NCFILE=;;
	no)
		NCDIRH=.
		curlib=
		NCFILE=;;
esac
#
cat conf/Makefile.start.in | sed -e s/MYCC/$comp/g -e s/MYSYS/$OS/g > $MTMP
echo "# Here the ncurses specific defs" >> $MTMP
echo "NCURSESH = $NCFILE" >> $MTMP
echo "NCURSESD = $NCDIRH" >> $MTMP
echo >> $MTMP
echo "# and here should be the target directories" >> $MTMP
echo "BINDIR = $bindir" >> $MTMP
echo "MANDIR = $mandir" >> $MTMP
#

mod=
sysdef=
drvyammdir=

optimize=

readline "Code generation flags: (optimize/debug)" "optimize"
optimize=$line

case $OS in
	linux)
		case $optimize in
			optimize|OPTIMIZE|Optimize)
				debug="-O2 -N -fomit-frame-pointer -m486"
				linker="-s";;
			debug|DEBUG|Debug)
				debug="-g"
				linker="-g";;
		esac
		;;
	HPUX)
		case $optimize in
			optimize|OPTIMIZE|Optimize)
				if [ "$comp" = "gcc" ]
				then
					debug="-O2 -N -fomit-frame-pointer"
				else
					debug="+O3"
				fi
				linker="-s"
				;;
			debug|DEBUG|Debug)
				debug="-g"
				linker="-g"
				curlib=/lib/libcurses.sl
				;;
		esac
		;;
esac

echo "  If you want to change the default flags, do it now:"
readline "    Compiler flags" "$debug"
debug=$line
readline "    Linker flags" "$linker"
linker=$line

echo
echo "Setup of $OS specific section:"
echo

if [ "$OS" = "linux" ]
then
	readline "Do you want to use the module drv_yamm ? (yes/no)" "yes"
	if [ "$line" = yes ]
	then
		mod=drv_yamm.o
		sysdef="-DMOD"
		readline "Choose where the drv_yamm will go" "/boot/yamm"
		drvyammdir=$line
		echo
		echo "Remember to edit the /etc/rc.local and add this line:"
		echo "insmod $drvyammdir/drv_yamm.o"
		echo
	fi
	case $curs in
		ncurses)
			defines="-DFGC=COLOR_GREEN -DBGC=COLOR_BLACK -DOWN_WINCH -DNCURSES";;
		curses)
			defines="-DOWN_WINCH";;
		no)
			defines="-DNO_CURSES";;
	esac
	cat conf/Makefile.linux.in >> $MTMP
	echo "DEFINES = $defines" >> $MTMP
	echo "LDFLAGS = $linker" >> $MTMP
	echo "DEBUG = $debug" >> $MTMP
	echo "MODULE = $mod" >> $MTMP
	echo "SYSDEFINES = $sysdef $stringify" >> $MTMP
	echo "LLIBS = $curlib" >> $MTMP
	echo "DRVYAMMDIR = $drvyammdir" >> $MTMP
	echo "#-------------------------------------------------------------------------------" >> $MTMP
	echo >> $MTMP
	cat conf/Makefile.end.in >> $MTMP
fi
if [ "$OS" = "HPUX" ]
then
	case $curs in
		ncurses)
			defines="-DFGC=COLOR_GREEN -DBGC=COLOR_BLACK -DNCURSES";;
		curses)
			defines=;;
		no)
			defines="-DNO_CURSES";;
	esac
	cat conf/Makefile.HPUX.in >> $MTMP
	echo "DEFINES = $defines" >> $MTMP
	echo "LDFLAGS = $linker" >> $MTMP
	echo "DEBUG = $debug" >> $MTMP
	echo "MODULE = $mod" >> $MTMP
	echo "SYSDEFINES = $sysdef $stringify" >> $MTMP
	echo "LLIBS = $curlib" >> $MTMP
	echo "DRVYAMMDIR = $drvyammdir" >> $MTMP
	echo "#-------------------------------------------------------------------------------" >> $MTMP
	echo >> $MTMP
	cat conf/Makefile.end.in >> $MTMP
fi

echo
echo "Remember:"
echo "make all		to make all the quid"
echo "make install	to install all the things in the right place"
echo "make clean	to clean up the objects and unwanted things"
echo "make cleanall	to clean up all the whole things, included configuration"
echo
echo "Now edit the Makefile and double check it :)"
