#!/bin/sh

if [ ! -d lib ]
then
    echo "This script should be run from the top level directory of"
    echo "the ax25-utils source code tree. Please change directory and"
    echo "run 'make config' again".
    echo
    exit 1
fi

echo "---------------------------------------------------------------------"
echo "This script is designed to determine the layout of your Linux system"
echo "and to build a configuration header file to suit it."
echo
echo "Once it has finished running, if you have doubts about the decisions"
echo "that it has made, check the contents of the file lib/config.h and the"
echo "Makefiles in the sub-directories and edit them to match your system."
echo "---------------------------------------------------------------------"
echo

rm -f lib/config.h

echo -n "Checking kernel header files are ok ... "

if [ ! -f /usr/include/linux/ax25.h ]; then
    echo "not found."
    echo
    echo "You don't appear to have any Linux kernel source on line."
    echo "The ax25 utilities need the header files that are included"
    echo "in the linux kernel source. Unpack the Linux kernel source"
    echo "into the /usr/src/linux directory and make sure your symbolic"
    echo "links from the /usr/include directory are correct before trying"
    echo "again."
    echo
    echo "Terminating configuration."
    exit 1
else
    echo "found, good."
fi

echo -n "Checking kernel has been configured ... "

if [ ! -f /usr/include/linux/autoconf.h ]; then
    echo "nope."
    echo
    echo "You don't appear to have configured your kernel yet."
    echo "The ax25 utilities need a header file that is created"
    echo "after you run 'make config' in the /usr/src/linux directory"
    echo "Configure your kernel and then rerun this script."
    echo
    echo "Terminating configuration."
    exit 1
else
    echo "yes, good."
fi

echo -n "Checking /usr/include symlinks ... "

if [ ! -L /usr/include/linux -o ! -L /usr/include/asm ]; then
	echo "hmm..."
	echo
	echo "Your /usr/include/linux and /usr/include/asm should be"
	echo "symbolic links into the linux kernel source. They appear"
	echo "not to be. Please read: /usr/src/linux/README and make"
	echo "the appropriate changes before trying again."
	echo
	echo "Terminating configuration."
	exit 1
else
	echo "ok."
fi

echo -n "Checking versions ... "

UTILVER=`head -1 VERSION | sed -e 's/^.*-//; s/[a-z]*$//'`
KERNVER=`grep UTS /usr/include/linux/version.h | sed -e 's/^.* //' | tr -d '"'`
UTILINT=`echo $UTILVER | tr -d "."`
KERNINT=`echo $KERNVER | tr -d "."`

if [ $UTILINT -gt $KERNINT -a ! -f /usr/include/linux/rose.h ]
then
    echo "nope, bad version."
    echo
    echo "This version of AX25 utilities is for kernel version linux-$UTILVER"
    echo "or newer. Please find an older version of the AX25 utilities. The"
    echo "version numbers of the AX25 utilities specify which is the oldest"
    echo "version of kernel that they will work with. You need a version of"
    echo "the ax25 utilities called: 'ax25-utils-$KERNVER.tar.gz' or older."
    echo
    echo "Terminating configuration."
    exit 1
else
    echo "versions ok, good"
fi

echo -n "Checking for the existence of ncurses... "

FOUND=0
if [ -f /usr/include/curses.h -o -d /usr/include/ncurses ]; then
    if [ ! -d /usr/include/ncurses ]; then
        grep __NCURSES_H /usr/include/curses.h > /dev/null
        if [ $? = 0 ]; then
            echo "found, good."
            FOUND=1
        fi
    else
        echo "found, good."
        FOUND=1
    fi
fi

if [ $FOUND = 0 ]; then
    echo "not found"
    echo
    echo "Your system has not got ncurses installed. To compile some of the"
    echo "programs in this package you need ncurses installed. You will need"
    echo "to locate and install the ncurses package and then re-run configure."
    echo
    echo "Terminating configuration"
    exit 1
fi

echo -n "Checking for the existence of X11... "

X11DIR=""
for x11dir in /usr/X11 /usr/X386 /usr/X11R5 /usr/X11R6
do
    if [ -f $x11dir/include/X11/X.h ]
    then
        X11DIR=$x11dir
    fi
done
if [ -z "$X11DIR" ]
then
    echo "not found."
    echo "            Without X11 some tools will not compile."
    echo "            See INSTALL for more information."
    echo
else
    echo $X11DIR
fi


#
# If we have X11, then search for the XForms library.
#
# XForms version required ...
XFVERR=0.84

if [ ! -z "$X11DIR" ]
then

    echo -n "Checking for the existence of XForms libraries... "

    XFLIB=""
    for xflib in /usr/X11 /usr/X386 /usr/X11R5 /usr/X11R6 /usr/local
    do
        if [ -f $xflib/lib/libforms.a ]
        then
            XFLIB=$xflib
        fi
    done
    if [ -z "$XFLIB" ]
    then
        echo "not found."
    else
        echo $XFLIB/lib
    fi

    if [ ! -z "$XFLIB" ]
    then

        echo -n "Checking that XForms is version $XFVERR ... "
        XFVERI=`ls $XFLIB/lib/libforms.so.[0-9].* |\
            sed "s/.*libforms.so.//"`;

        if [ "$XFVERI" != "$XFVERR" ];
        then
            echo "nope, version $XFVERI."
            XFLIB=""
        else
            echo "yes."
        fi

        XFINC=""
        if [ ! -z "$XFLIB" ]
        then
            echo -n "Checking for the existence of XForms includes... "

            for xfinc in /usr/X11 /usr/X386 /usr/X11R5 /usr/X11R6 /usr/local
            do
                if [ -f $xfinc/include/X11/forms.h ]
                then
                    XFINC=$xfinc
                fi
            done
            if [ -z "$XFINC" ]
            then
                echo "not found."
            else
                echo $XFINC/include/X11
            fi
        fi
    fi
fi
if [ -z "$XFLIB" -o -z "$XFINC" ]
then
    echo "            Without the correct XForms library some tools will not compile."
    echo "            See INSTALL for more information."
    echo
fi

echo -n "Checking for the existence of /usr/local/etc/axports... "

if [ -f /usr/local/etc/axports ]; then
    echo "found"
    echo
    echo "The existence of a previous version of the AX.25 Utilities has been"
    echo "found. This release uses the FSSTND layout for its files and as such"
    echo "uses completely different locations for everything. Doing a make and"
    echo "install and an installconf will not overwrite existing files, the new"
    echo "locations go like this:"
    echo
    echo "    configuration files  -> /etc/ax25"
    echo "    binary files         -> /usr/bin"
    echo "    system binary files  -> /usr/sbin"
    echo "    manual pages         -> /usr/man/..."
    echo "    support files        -> /usr/lib/ax25/..."
    echo "    variable data files  -> /var/ax25/..."
    echo
    echo "Please move existing files to the new locations, using the newly"
    echo "installed configuration files as a guide."
    echo
else
    echo "not found, ok."
    echo -n "Checking for the existence of /etc/ax25/axports... "
    if [ -f /etc/ax25/axports ]; then
        echo "found config, good."
    else
        echo "not found, ok."
        echo
        echo "  I could not find any existing AX25 configuration files so I am"
        echo "  assuming this is a new installation."
        echo
        echo "  Will use FSSTND locations:"
        echo "      configuration files  -> /etc/ax25"
        echo "      binary files         -> /usr/bin"
        echo "      system binary files  -> /usr/sbin"
        echo "      manual pages         -> /usr/man/..."
        echo "      support files        -> /usr/lib/ax25/..."
        echo "      variable data files  -> /var/ax25/..."
        echo
        echo "  'make installconf' will install a default configuration"
        echo "  which you can modify if you wish."
        echo
    fi
fi


echo -n "Checking for location for serial lock files... "

LOCKDIR=""
for lockdir in /var/lock /var/spool/locks /usr/spool/uucp
do
    if [ -d $lockdir ]; then
        LOCKDIR=$lockdir
        break
    fi
done
if [ -z "$LOCKDIR" ]; then
    echo "not found"
    echo
    echo "Your system does not appear to have a directory to store your serial"
    echo "lock files in. Not sure how to proceed."
    echo
    echo "Terminating configuration"
    exit 1
else
    echo $LOCKDIR
fi


echo -n "Checking for location of sendmail executable... "

SM_DIR=""
for smpath in /usr/sbin /sbin /usr/local/sbin
do
    if [ -d $smpath ]; then
        SM_DIR=$smpath
        break
    fi
done
if [ -z "$SM_DIR" ]; then
    echo "not found"
    echo
    echo "Your system does not appear to have the sendmail daemon installed."
    echo "I'm going to use /usr/sbin, but the pms will not be able to deliver"
    echo "messages properly. You may edit the lib/config.h file and include"
    echo "another delivery agent if you have one installed."
    echo
    SM_DIR=/usr/sbin
else
    echo $SM_DIR
fi

echo

# Global protocol definition symbols for programmers that want to conditionally
# compile
	HAVEAX25="#undef"
	HAVENETROM="#undef"
	HAVEROSE="#undef"

	echo -n "Include support for the AX.25 protocol ? [y/n]: "; read answer
	if [ "$answer" = "Y" -o $answer = "y" ]
	then
		HAVEAX25="#define"
	fi
	#
	echo -n "Include support for the NetRom protocol ? [y/n]: "; read answer
	if [ "$answer" = "Y" -o "$answer" = "y" ]
	then
		HAVEAX25="#define"
		HAVENETROM="#define"
	fi
	#
	echo -n "Include support for the Rose protocol ? [y/n]: "; read answer
	if [ "$answer" = "Y" -o "$answer" = "y" ]
	then
		HAVEAX25="#define"
		HAVEROSE="#define"
	fi

echo "Updating Makefile"

sed -e "s~@X11DIR@~$X11DIR~g; \
	s~@XFINC@~$XFINC~g; \
	s~@XFLIB@~$XFLIB~g" <Makefile.in >Makefile

echo "Creating config.h"

sed -e	"s~@LOCKDIR@~$LOCKDIR~g; \
	s~@ETC_DIR@~$ETC_DIR~g; \
	s~@LIB_DIR@~$LIB_DIR~g; \
	s~@SM_DIR@~$SM_DIR~g; \
	s~@HAVEAX25@~$HAVEAX25~g; \
	s~@HAVENETROM@~$HAVENETROM~g; \
	s~@HAVEROSE@~$HAVEROSE~g" < lib/config.h.in > lib/config.h

echo

echo "Configuration successful"

exit 0

