#!/bin/sh

# a ridiculous Configure utility for sysline - bjdouma
# for one thing it doesn't check if what you specify
# exists...

#set -x

rm -f Makefile config.h

ask_LIBPATH()
{
	LDFLAGS=-L$2
	echo -n "Directory of lib$1.a? [$2] "
	read choice
	echo
	case $choice in
		"")	;;
		*)	LDFLAGS=-L$choice ;;
	esac
}

ask_INCPATH()
{
	CFLAGS=-I$2
	echo -n "Directory for $1 include files? [$2] "
	read choice
	echo
	case $choice in
		"")	;;
		*)	CFLAGS=-I$choice ;;
	esac
}

ask_WHICH_CURSES()
{
	echo  "Which curses package should I use? [1]"
	echo "1) standard curses"
	echo "2) ncurses"
	read choice
	case $choice in
		2)
			echo -e "Using ncurses\n"
			CURSESLIB=-lncurses
			ask_LIBPATH "ncurses" "/usr/lib"
			ask_INCPATH "ncurses" "/usr/include/ncurses"
			;;
		*)	
			echo -e "Using standard curses\n"
			CURSESLIB=-lcurses
			ask_LIBPATH "curses" "/usr/lib"
			ask_INCPATH "curses" "/usr/include"
			;;
	esac
}

ask_WHICH_TERMDATABASE()
{
	echo
	echo "What terminal database do you want sysline to use? [1]"
	echo "1) termcap"
	echo "2) terminfo"
	read choice
	case $choice in
		2)
			echo -e "Using ncurses\n"
			CURSESLIB=-lncurses
			ask_LIBPATH "ncurses" "/usr/lib"
			ask_INCPATH "ncurses" "/usr/include/ncurses"
			;;
		*) 	
			echo -e "Using termcap\n"
			TERMLIB=-ltermcap
			ask_WHICH_CURSES
			;;
	esac
}

ask_WHICH_TERMDATABASE

# make config.h
cp config.h.in config.h
echo "making config.h"
if [ ! "$TERMLIB" = "-ltermcap" ]
then
	cat <<- EOT >>config.h
	/* terminfo */
	#define TERMINFO
	#include <term.h>
	EOT
fi

# make Makefile
if [ "$CURSESLIB" = "-lcurses" ]
then
	cat <<- EOT >>config.h
	/* curses */
	#include <curses.h>
	EOT
else
	cat <<- EOT >>config.h
	/* ncurses */
	#include <ncurses.h>
	#include <unctrl.h>
	EOT
fi

LIBS="$TERMLIB $CURSESLIB"

echo "making Makefile"
cat Makefile.in | sed "s|@CFLAGS@|$CFLAGS|" | sed "s|@LDFLAGS@|$LDFLAGS|" | sed "s|@LIBS@|$LIBS|" >Makefile

echo -e "\nedit defines in config.h and Makefile to your discretion,\nthen run make.\n"
