#!/bin/bash
#
#	FarSync WAN/OEM driver for Linux
#
#	Update script
#       Run this after you have updated the Kernel to rebuild the
#       drivers.  2.6 Kernel only
#       A paameter is always required and must be either oem or wan.
#
#	Copyright (C) 2001-2008 FarSite Communications Ltd.
#	www.farsite.co.uk
#
#	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.
#
#	File:	update
#	Author:	Kevin Curtis	<kevin.curtis@farsite.co.uk>
#
#	Revision history at end of file
#

PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin

KERNEL_DIR=/usr/src/linux
COMPNY_DIR=/etc/farsite
CONFIG_DIR=$COMPNY_DIR/farsync
MODULE_DIR=$COMPNY_DIR/modules

cat <<EOF

       FarSync WAN/OEM driver for Linux Update
       Rebuild drivers after a Kernel update
       Make sure the symbolic link /usr/src/linux is pointing
       to the Kernel source or headers for the Updated Kernel

       Copyright (C) 2001-2008 FarSite Communications Ltd.

EOF

if [ ! "`id -u`" = "0" ]
then
	echo Sorry you must be root to run this script
	exit 1
fi
#
# Is the /usr/src/linux symbolic link in place?
#
if [ ! -d ${KERNEL_DIR} ]
then
	echo
	echo Cannot find ${KERNEL_DIR}
	echo Please make a symbolic link called ${KERNEL_DIR} to where your
	echo Kernel Source tree or Kernel Header files are located
	echo
	echo
	exit 1
fi

sleep 3

if [ -a ${CONFIG_DIR}/ifcfg-hdlc0 ]
then
    echo "Updating WAN Network drivers"
    install_type="wan"
elif [ -a ${CONFIG_DIR}/ifcfg-sync0 ]
then
    echo "Updating OEM Development drivers"
    install_type="oem"
else
    echo "I cannot find the config directory ${CONFIG_DIR}."
    echo "Are the FarSync Base Drivers installed?"
    echo
    exit 1
fi

# First check we have a kernel source tree and the version
if [ ! -f ${KERNEL_DIR}/Makefile ]
then
	echo
	echo Unable to find kernel source tree.
	echo
	echo ${KERNEL_DIR}/Makefile is missing.
	echo
	echo Please install the kernel development set from your distribution
	echo media before re-running this script.
	exit 1
fi
# Crude parse of kernel makefile to find the version
while read var equals val junk
do
	if [ "X${var}" = "XVERSION" -a "X${equals}" = "X=" ]
	then
		eval KERN_VERS=${val}
	elif [ "X${var}" = "XPATCHLEVEL" -a "X${equals}" = "X=" ]
	then
		eval KERN_REL=${val}
	elif [ "X${var}" = "XSUBLEVEL" -a "X${equals}" = "X=" ]
	then
		eval KERN_PAT=${val}
	fi
done < ${KERNEL_DIR}/Makefile

echo Kernel source identified as version ${KERN_VERS}.${KERN_REL}.${KERN_PAT}
echo
sleep 1
KV=${KERN_VERS}.${KERN_REL}

# Check it's one we support. If it is we'll have a kernel dir for it
if [ -d kernel${KV} ]
then
	KSRC_DIR=kernel${KV}
else
	echo "Sorry can't find a kernel support directory for version ${KV}"
	echo
	echo "Please check http://www.farsite.co.uk/ or email linux@farsite.co.uk"
	echo "for support availability for this kernel version."
	echo
	echo "Alternativly try upgrading your kernel to 2.2.15 or later before"
	echo "installing this driver package."
	exit 1
fi

# Check we have patch installed
if [ ! -x /usr/bin/patch -a ! -x /bin/patch ]
then
	echo "The patch command is required for driver installation."
	echo
	echo "Please install the patch command (from distribution RPM probably)"
	echo "before attempting this install again."
	exit 1
fi

# Check we have make installed
if [ ! -x /usr/bin/make -a ! -x /bin/make ]
then
	echo "The make command is required for driver installation."
	echo
	echo "Please install the required package (from distribution RPM probably)"
	echo "before attempting this install again."
	echo
	exit 1
fi

# Check we have gcc installed
if [ ! -x /usr/bin/gcc -a ! -x /bin/gcc ]
then
	echo "The gcc command is required for driver installation."
	echo
	echo "Please install the required package (from distribution RPM probably)"
	echo "before attempting this install again."
	echo
	exit 1
fi

# Check we have c header files installed
if [ ! -s /usr/include/stdio.h ]
then
	echo "The C Language standard header files are required for installation."
	echo
	echo "Please install the required package (from distribution RPM probably)"
	echo "before attempting this install again."
	echo
	exit 1
fi

# Now work out which directories and any other options we need based on kernel
# version
DRIVER_DIR=${KERNEL_DIR}/drivers/net/wan

# Driver update
if [ "$install_type" == "oem" ]
then
        echo
        echo Updating the OEM driver module
	cd ${KSRC_DIR}-oem
	make
	make install
	cd ../
else
        echo
        echo Updating the WAN driver module
#
# Now it gets more complicated, depending on Kernel Version

	if grep RS485 /usr/src/linux/include/linux/if.h >/dev/null
	    then
	    echo "if.h patch already installed"
	else
	    if [ ${KERN_PAT} -le 6 ]
		then
		patch < patch/x21d23.patch -p0
	    else
		patch < patch/shdsl.patch -p0
	    fi
	fi
	cd ${KSRC_DIR}-hdlc
	make
	make install
	cd ../
fi

cat << EOF

                        Update complete
			---------------

EOF
depmod -a
exit 0
