#!/bin/sh
#
#   yamm, Yet Another Micro Monitor
#   FindNcurses
#   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=/usr/bin/find
WC=/usr/bin/wc
AWK=/usr/bin/awk
INC=ncurses.h
INCTMP=ncurses.h.tmp
RM=/bin/rm

trap "$RM -f $INC $INCTMP; exit_error \"interrupted\"" 1 2 3 15

exit_error() {
	$RM -f $INCTMP $INC
	echo
	echo -n "FindNcurses aborted: "
	echo $1
	exit -1
}

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 FindNcurses 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."
echo "Remember to edit the Makefile and set the NCURSESD to $DIRH"
