#!/bin/sh

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 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 ]
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 /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... "

if [ -d /var/lock ]; then
    LOCKDIR=/var/lock
else
    if [ -d /var/spool/locks ]; then
        LOCKDIR=/var/spool/locks
    else
        if [ -d /usr/spool/uucp ]; then
            LOCKDIR=/usr/spool/uucp
        else
            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
        fi
    fi
fi

echo $LOCKDIR

echo

echo "Creating config.h"

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

echo

echo "Configuration successful"

exit 0
