#!/bin/sh
#
#   yamm, Yet Another Micro Monitor
#   Configure v. 1.0
#   Copyright (C) 1994, 1995  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
INCOLD=ncurses.h.old
INC=ncurses.h
INCTMP=ncurses.h.tmp
RM=/bin/rm
MV=/bin/mv
MTMP=Makefile.tmp
MFILE=Makefile

trap "exit_error" 1 2 3 15

exit_error() {
    $RM -f $INCTMP $INC $CTMP $AOUT $MTMP
    if [ -f $INCOLD ]
    then
        $MV $INCOLD $INC
    fi
    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 "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..."
    HDR=
    if [ -d /usr/include/ncurses ]
    then
        NCDIRH=/usr/include/ncurses
        if [ -f /usr/include/ncurses/ncurses.h ]
        then
            HDR=ncurses/ncurses.h
            echo "found standard ncurses in $HDR"
        else
            if [ -f /usr/include/ncurses/curses.h ]
            then
                HDR=ncurses/curses.h
                echo "found standard ncurses in $HDR"
            fi
        fi
    fi
    if [ "$HDR"x = "x" ]
    then
        echo "Searching /usr/include..."
        $FIND /usr/include -name $INC -print > $INCTMP
        echo "Searching /usr/local/include..."
        $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 standard system include"
                echo "directories. Please be sure you have the ncurses library"
                echo "installed 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`
        grep local $INCTMP
        if [ $? -eq 0 ]
        then
            offset=20
        else
            offset=14
        fi
        HDR=`cat $INCTMP | cut -b $offset-`
    fi
    echo "/* header file generated by 'Configure' of yamm */" > $INC
    echo "/* do not modify by hands! */" >> $INC
    echo "#include <$HDR>" >> $INC
    $RM -f $INCTMP
    echo "ncurses package found and included."
}

readline() {
    line=
    def=$2
    if [ "$OS" = "linux" ]
    then
        echo -n "$1 [$2]: "
    else
        echo "$1 [$2]: \c"
    fi
    read line
    if [ "$line"x = "x" ]
    then
        line=$def
    fi
}
    
# Main section
if [ -f $INC ]
then
    $MV $INC $INCOLD
fi
echo "Searching OS..."
FindOS
echo OS="$OS"
#
FindCompiler "gcc"
#
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
        echo
        echo "yamm have now a new resizing code, for use in Xwindows"
        echo "terminals. This code is very ncurses-version dependent"
        echo "and it work with ncurses-1.8.6"
        echo "Warning: I have never tryed resizing code with ncurses"
        echo "different from 1.8.6, so if you want to try it, do it"
        echo "at your own risk (of core dumping of course :)"
        echo
        readline "Would you like to try new resizing code ?" "n"
        if [ "$line" = "y" ]
        then
            echo "#define NCURSES_186" >> $INC
        else
            echo "#undef  NCURSES_186" >> $INC
        fi
        echo
        echo Using ncurses package, you can use yamm with character colors.
        echo You can choose the foreground and background colors now.
        echo The avaliable colors are:
        echo
        echo "COLOR_BLACK    COLOR_RED        COLOR_GREEN    COLOR_YELLOW"
        echo "COLOR_BLUE     COLOR_MAGENTA    COLOR_CYAN     COLOR_WHITE"
        echo
        readline "Choose the foreground color" "COLOR_GREEN"
        fgc=$line
        readline "Choose the background color" "COLOR_BLACK"
        echo
        bgc=$line
        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"
                arch=`uname -m`
                if [ "$arch" = "i486" ]
                then
                    debug="$debug -m486"
                fi
                if [ "$arch" = "pentium" ]
                then
                    debug = "$debug -mpentium"
                fi
                linker="-s";;
            debug|DEBUG|Debug)
                debug="-g -O"
                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

#
# Add ansi directives if compiler is HP's one:
if [ "$comp" = "gcc" ]
then
    debug="$debug"
else
    debug="-Aa -D_HPUX_SOURCE $debug"
fi

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="-DMODULE"
        readline "Choose where the drv_yamm will go" "/lib/modules/default/misc"
        drvyammdir=$line
        echo
        echo "Remember to edit the /etc/rc.local and add this line:"
        echo
        echo "insmod $drvyammdir/drv_yamm.o"
        echo
    fi
    readline "Where is your Linux kernel source directory ?" "/usr/src/linux"
    linuxdir=$line
    case $curs in
        ncurses)
            defines="-DFGC=$fgc -DBGC=$bgc -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 "LINUXDIR = $linuxdir" >> $MTMP
    echo "#-------------------------------------------------------------------------------" >> $MTMP
    echo >> $MTMP
    cat conf/Makefile.end.in >> $MTMP
fi
if [ "$OS" = "HPUX" ]
then
    mod=dummy_module
    case $curs in
        ncurses)
            defines="-DFGC=$fgc -DBGC=$bgc -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

#
# Cleanup
#
$MV $MTMP $MFILE
$RM -f $INCOLD

echo
echo "The targets of Makefile are:"
echo
echo "make config         to configure the package for compilation"
echo "make dep            to make the dependancies"
echo "make all            to make all the quid"
echo "make module         to make the drv_yamm.o module"
echo "make install        to install all the things in the right place"
echo "make install_module to install drv_yamm.o in the right place"
echo "make install_man    to install the man page in the right place"
echo "make clean          to clean up the objects and unwanted things"
echo "make distclean      to clean up all the whole things, included configuration"
echo
echo "If you want a better control of compilation, cd src/ and use the Makefile"
echo "contained in it."
echo
echo "Now edit the src/Makefile and double check it :)"
