#!/bin/bash
#
#	FarSync WAN/OEM driver for Linux
#
#	Installation script
#       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:	install
#	Author:	R.J.Dunlop	<bob.dunlop@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
DOWNLD_DIR=$COMPNY_DIR/downloads
MODULE_DIR=$COMPNY_DIR/modules
HEADER_DIR=$COMPNY_DIR/include

#
# Even though the driver type is derived from the command line parameter
# We may have to over-ride it under the following circumstances:
# 2.2 Kernel is syncppp only
# 2.4 Kernel is syncppp for pre-2.4.13
# 2.4 Kernel post 2.4.13 is as specified in the parameter
# 2.6 Kernel is as specified in the parameter
#
cat <<EOF

       FarSync WAN/OEM driver for Linux installation

       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 [ "$1"  == "wan" ]
then
    echo "Installing WAN Network drivers"
    install_type="wan"
else
    if [ "$1"  == "oem" ]
    then
        echo "Installing OEM Development drivers"
        install_type="oem"
    else
        echo "Parameter must be oem or wan"
        exit 1
    fi
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
# Double check. Prior to 2.2.15 not supported
if [ "${KV}" = "2.2" -a ${KERN_PAT} -lt 15 ]
then
	echo "Sorry 2.2 kernel versions prior to 2.2.15 are not supported by"
	echo "this package."
	echo
	echo "Please upgrade you kernel to 2.2.15 or better before installing"
	echo "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
case ${KV} in
	2.2)
		DRIVER_DIR=${KERNEL_DIR}/drivers/net
		;;
	2.4)
		DRIVER_DIR=${KERNEL_DIR}/drivers/net/wan
		;;
	2.6)
		DRIVER_DIR=${KERNEL_DIR}/drivers/net/wan
		;;
esac

# SyncPPP patch (should be for 2.2.15 kernels only)
if [ -f ${KSRC_DIR}/syncppp.patch-${KV}.${KERN_PAT} ]
then
	PFILE=${KSRC_DIR}/syncppp.patch-${KV}.${KERN_PAT}

	if grep sppp_of ${KERNEL_DIR}/drivers/net/syncppp.h >/dev/null
	then
		echo
		echo It would appear that the SyncPPP module is already patched
		echo for multiple device operation.
		echo SyncPPP patch will not be applied.
		sleep 1
	else
		echo
		echo Applying SyncPPP module patch

		patch -b -p0 -N -d /usr/src < ${PFILE}
	fi
fi
# SyncPPP one line patch (if_down) for 2.4.x kernels
if [ -f ${KSRC_DIR}/syncppp.patch-${KV}.x ]
then
	PFILE=${KSRC_DIR}/syncppp.patch-${KV}.x

	if grep ')dev)->sppp;' ${KERNEL_DIR}/drivers/net/wan/syncppp.c >/dev/null
	then
		echo
		echo Applying SyncPPP if_down correction patch

		patch -b -p0 -N -d /usr/src < ${PFILE}
	else
		echo
		echo It would appear that the SyncPPP module is already patched
		echo for the if_down bug.
		echo SyncPPP patch will not be applied.
		sleep 1
	fi
fi

# Kernel patch
# i.e. for Kernels before farsync was included
#
if [ -f ${KSRC_DIR}/farsync.patch-${KV}.${KERN_PAT} ]
then
	PFILE=${KSRC_DIR}/farsync.patch-${KV}.${KERN_PAT}
else
	PFILE=${KSRC_DIR}/farsync.patch
fi

# Red Hat likes to tweak kernels.
# Select RH specific patch if we have one and look like we are on a RH system
if [ -f /usr/src/linux/include/linux/rhconfig.h -a -f ${PFILE}-rh ]
then
	PFILE=${PFILE}-rh
fi

if [ -r ${PFILE} ]
then
	if grep CONFIG_FARSYNC ${KERNEL_DIR}/Documentation/Configure.help >/dev/null
	then
		echo
		echo It would appear that FarSync configuration is already in
		echo the kernel source tree.
		echo Configuration support patch will not be applied.
		sleep 1
	else
		echo
		echo Applying kernel configuration patch

		patch -b -p0 -N -d /usr/src < ${PFILE}
	fi
fi

# Add our config and storage directories
if [ ! -d ${DOWNLD_DIR} ]
then
	install -d -m 755 ${DOWNLD_DIR}
fi
if [ ! -d ${COMPNY_DIR} ]
then
	install -d -m 755 ${COMPNY_DIR}
fi
if [ ! -d ${MODULE_DIR} ]
then
	install -d -m 755 ${MODULE_DIR}
fi
if [ ! -d ${HEADER_DIR} ]
then
	install -d -m 755 ${HEADER_DIR}
fi

# Driver install
echo
echo Installing the driver sources
#
# All Kernels use the farsync.h file
#
	install -b -m 644 includes/farsync.h ${DRIVER_DIR}/farsync.h
        install -b -m 644 includes/*.h ${HEADER_DIR}/

if [ "$install_type" == "oem" ]
then
        echo
        echo Installing the OEM driver module
        install -b -m 644 ${KSRC_DIR}-oem/farsync.c ${DRIVER_DIR}/farsync.c
        if [ "${KV}" = "2.6" ]
        then
            if [ ${KERN_PAT} -lt 9 ]
            then
                sed "s/ fsflex.o//g" ${KSRC_DIR}/Makefile > ${KSRC_DIR}-oem/Makefile1
                sed "s/install fsflex.ko/#install fsflex.ko/g" ${KSRC_DIR}-oem/Makefile1 > ${KSRC_DIR}-oem/Makefile
                sed "s/ fsflex.o//g" ${KSRC_DIR}/Makefile-with-syncppp > ${KSRC_DIR}-oem/Makefile-with-syncppp1
                sed "s/install fsflex.ko/#install fsflex.ko/g" ${KSRC_DIR}-oem/Makefile-with-syncppp1 > ${KSRC_DIR}-oem/Makefile-with-syncppp
            fi
            cd ${KSRC_DIR}-oem
            make
            make install
            cd ../
        fi
else
        echo
        echo Installing the WAN driver module
#
# Now it gets more complicated, depending on Kernel Version

        if [ "${KV}" = "2.4" ]
        then
#	    : 2.4.X drivers
            sleep 1
#
# But there are two choices of driver for 2.4
#
            if [ ${KERN_PAT} -lt 13 ]
            then
                install -b -m 644 ${KSRC_DIR}/farsync.c ${DRIVER_DIR}/farsync.c
            else
#
# Has the generic hdlc patch been installed
#
                if [ -a /usr/src/linux/drivers/net/wan/hdlc_fr.c ]
                then
                    USING_HDLC="Y"
                    install -b -m 644 ${KSRC_DIR}-hdlc/farsync.c ${DRIVER_DIR}/farsync.c
		    if grep RS485 /usr/src/linux/include/linux/if.h >/dev/null
		    then
                        echo "if.h patch already installed"
		    else
			if [ ${KERN_PAT} -ge 23 ]
			then
			    if [ ${KERN_PAT} -le 26 ]
			    then
				patch < patch/x21d23.patch -p0
			    else
				patch < patch/shdsl.patch -p0
			    fi
			else
			    patch < patch/x21d.patch -p0
			fi
		    fi
                else
                    echo
                    echo "WARNING:  SyncPPP version of the driver has been Installed"
                    echo "          If you wish to use generic hdlc module please install generic hdlc patch."
                    echo "          See install documentation for more details."
                    sleep 3
                    install -b -m 644 ${KSRC_DIR}/farsync.c ${DRIVER_DIR}/farsync.c
                fi
            fi
        else
            sleep 1
            USING_HDLC="Y"
            install -b -m 644 ${KSRC_DIR}-hdlc/farsync.c ${DRIVER_DIR}/farsync.c
            if [ "${KV}" = "2.6" ]
            then
                if [ ${KERN_PAT} -lt 9 ]
                then
                    sed "s/ fsflex.o//g" ${KSRC_DIR}/Makefile > ${KSRC_DIR}-hdlc/Makefile1
                    sed "s/install fsflex.ko/#install fsflex.ko/g" ${KSRC_DIR}-hdlc/Makefile1 > ${KSRC_DIR}-oem/Makefile

                    sed "s/ fsflex.o//g"  ${KSRC_DIR}/Makefile-with-hdlc > ${KSRC_DIR}-hdlc/Makefile-with_hdlc1
                    sed "s/install fsflex.ko /#install fsflex.ko/g" ${KSRC_DIR}-hdlc/Makefile-with-hdlc1 > ${KSRC_DIR}-oem/Makefile-with-hdlc

                fi
                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
            fi
            cd ${KSRC_DIR}-hdlc
            make
            make install
            cd ../
    fi
    
fi

# Script install
echo
echo Installing support utilities and scripts
sleep 1

# Divide distros into Red Hat like and non-RH (Debian)
if [ -f /etc/sysconfig/network-scripts/ifup ]
then
	DS=rh
	echo
	echo Network configuration is Red Hat like
	sleep 1

	if [ ! -L $CONFIG_DIR ]
	then
		ln -s /etc/sysconfig/network-scripts $CONFIG_DIR
	fi
        if [ "${USING_HDLC}" = "Y" ]
	then
	    #Install our own ifup-hdlc, ifup-pvc and ifdown-pvc commands
	    install -m 700 kernel$KERN_VERS.$KERN_REL/ifup-hdlc-rh $CONFIG_DIR/ifup-hdlc
	    install -m 700 kernel$KERN_VERS.$KERN_REL/ifup-pvc-rh $CONFIG_DIR/ifup-pvc
	    install -m 700 kernel$KERN_VERS.$KERN_REL/ifdown-pvc-rh $CONFIG_DIR/ifdown-pvc
	fi

else
	DS=db
	echo
	echo "Network configuration is non-Red Hat like (possibly Debian like)"
	sleep 1

	if [ ! -d $CONFIG_DIR ]
	then
		install -d -m 755 $CONFIG_DIR
	fi

	#Install our own ifup ifdown equivalents
	install -m 700 common/farifup   /sbin/farifup
	install -m 700 common/farifdown /sbin/farifdown

        if [ "${USING_HDLC}" = "Y" ]
	then
	    install -m 700 kernel$KERN_VERS.$KERN_REL/ifup-hdlc-db $CONFIG_DIR/ifup-hdlc
	    install -m 700 kernel$KERN_VERS.$KERN_REL/ifup-pvc-db $CONFIG_DIR/ifup-pvc
	    install -m 700 kernel$KERN_VERS.$KERN_REL/ifdown-pvc-db $CONFIG_DIR/ifdown-pvc
	fi

fi

#
# Which farutil command do we need
#
echo "Installing farutil"
if [ "$USING_HDLC" = "Y" ]
then
# Install download, gen hdlc util and the firmware images
    cd common ; make clean; make farhdlc; make fardsl; make farmon; cd ..
    install -m 700 common/farhdlc /sbin/farutil
    install -m 700 common/fardsl /sbin/fardsl
    install -m 700 common/farmon /sbin/farmon
    echo "Installing Man pages"
    if [ -d /usr/share/man/man8 ]
    then
	    install -m 444 common/farhdlc.8 /usr/share/man/man8/farutil.8
    elif [ -d /usr/man/man8 ]
    then
	    install -m 444 common/farhdlc.8 /usr/man/man8/farutil.8
    fi
else
# Install download syncppp util and the firmware images
    cd common ; make clean; make farutil; make fardsl; make farmon; cd ..
    install -m 700 common/farutil /sbin/farutil
    install -m 700 common/fardsl /sbin/fardsl
    install -m 700 common/farmon /sbin/farmon
    echo "Installing Man pages"
    if [ -d /usr/share/man/man8 ]
    then
	    install -m 444 common/farutil.8 /usr/share/man/man8/farutil.8
    elif [ -d /usr/man/man8 ]
    then
	    install -m 444 common/farutil.8 /usr/man/man8/farutil.8
    fi
fi

install -m 444 common/downloads/trtbios.cde  ${DOWNLD_DIR}/trtbios.cde
install -m 444 common/downloads/farsync2.cde ${DOWNLD_DIR}/farsync2.cde
install -m 444 common/downloads/farsync4.cde ${DOWNLD_DIR}/farsync4.cde
install -m 444 common/downloads/farsynu1.cde ${DOWNLD_DIR}/farsynu1.cde
install -m 444 common/downloads/farsynu2.cde ${DOWNLD_DIR}/farsynu2.cde
install -m 444 common/downloads/farsynu4.cde ${DOWNLD_DIR}/farsynu4.cde
install -m 444 common/downloads/fsyncte1.cde ${DOWNLD_DIR}/fsyncte1.cde
install -m 444 common/downloads/fsyncdsl.cde ${DOWNLD_DIR}/fsyncdsl.cde

# System runtime configuration files
echo "Checking for System V directory layout"
if [ -d /etc/rc.d/init.d ] || [ -f /etc/slackware-version ]
then
	# Argh SuSE 7.2 has a good looking directory structure but still has
	# a broken chkconfig
	if [ -r /sbin/chkconfig ]
        then
	    if [ `wc -l < /sbin/chkconfig` -lt 10 ]
	    then
		# Dummy SuSE script
		INITPRE=/etc
		INITPRE2=/etc/init.d
		SUSE=y
		STIME=04
	    else
		INITPRE=/etc/rc.d
		INITPRE2=/etc/rc.d
		SUSE=n
		STIME=09
	    fi
	else
	    INITPRE=/etc/rc.d
	    INITPRE2=/etc/rc.d
	    SUSE=n
	    STIME=09
	fi
else
	INITPRE=/etc
	if [ -d /etc/rc0.d ]
	then
		INITPRE2=/etc
		SUSE=n
		STIME=09
	else
		INITPRE2=/etc/init.d
		SUSE=y
		STIME=04
	fi
fi
#
# Might have to make the System V directories
#
if [ -d ${INITPRE}/init.d ]
then
    echo ${INITPRE}/init.d exists
else
    mkdir ${INITPRE}/init.d
fi

for i in 0 1 2 3 4 5 6
do
    if [ -d  ${INITPRE}/rc$i.d ]
    then
	echo Info: ${INITPRE}/rc$i.d] exists
    else
	mkdir ${INITPRE}/rc$i.d
    fi
done
install -m 755 common/farsync.init-${DS} ${INITPRE}/init.d/farsync

if [ -x /sbin/chkconfig -a "$SUSE" = "n" ]
then
        echo "last minute check for debian/ubuntu"
        if [ -f /etc/debian_version ]
        then
            update-rc.d farsync defaults
        else
            chkconfig --add farsync
        fi

elif [ -x /sbin/insserv ]
then
	insserv ${INITPRE}/init.d/farsync
else
	# Do it the hard way
	rm -f ${INITPRE2}/rc?.d/K91farsync ${INITPRE2}/rc?.d/S0?farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc0.d/K91farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc1.d/K91farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc2.d/S${STIME}farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc3.d/S${STIME}farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc4.d/S${STIME}farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc5.d/S${STIME}farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc6.d/K91farsync
fi

# Has any of our attempts to install the init scripts worked?
init_scripts=`find ${INITPRE2}/rc3.d/. |grep farsync`
if [ ${#init_scripts} = "0" ]
then
        echo "Installing init scripts manually"
	rm -f ${INITPRE2}/rc?.d/K91farsync ${INITPRE2}/rc?.d/S0?farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc0.d/K91farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc1.d/K91farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc2.d/S${STIME}farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc3.d/S${STIME}farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc4.d/S${STIME}farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc5.d/S${STIME}farsync
	ln -s ${INITPRE}/init.d/farsync ${INITPRE2}/rc6.d/K91farsync
fi

# Install our base up and down scripts
for i in ifup-sync ifdown-sync
do
	if [ -f ${KSRC_DIR}/${i}-${DS} ]
	then
		install -m 755 ${KSRC_DIR}/${i}-${DS} ${CONFIG_DIR}/${i}
	fi
done

# If we don't have an existing config script create some
if [ "$USING_HDLC" = "Y" ]
then
    if [ ! -f ${CONFIG_DIR}/ifcfg-hdlc0 ]
    then
	install -m 644 common/ifcfg-hdlc0 ${CONFIG_DIR}/ifcfg-hdlc0
	install -m 644 common/ifcfg-hdlc1 ${CONFIG_DIR}/ifcfg-hdlc1
        install -m 644 common/ifcfg-hdlc-fr ${CONFIG_DIR}/ifcfg-hdlc-fr
        install -m 644 common/ifcfg-pvc0 ${CONFIG_DIR}/ifcfg-pvc0
    fi
else
    if [ ! -f ${CONFIG_DIR}/ifcfg-sync0 ]
    then
        install -m 644 common/ifcfg-sync0 ${CONFIG_DIR}/ifcfg-sync0
	install -m 644 common/ifcfg-sync1 ${CONFIG_DIR}/ifcfg-sync1
    fi
fi

echo
echo
if [ ! -d /etc/ppp ]
then
    echo "It doesn't look as though ppp is installed"
else
    echo "Copying ppp scripts"
    install -m 755 common/ppp-on /etc/ppp/ppp-on
    install -m 755 common/ppp-off /etc/ppp/ppp-off
    if [ -f ${CONFIG_DIR}/ifcfg-hdlc0 ]
    then
        install -m 644 common/ppp-options.tty_hdlc /etc/ppp/options.tty_hdlc0
        install -m 644 common/ppp-options.tty_hdlc /etc/ppp/options.tty_hdlc1
    else
        install -m 644 common/ppp-options.tty_hdlc /etc/ppp/options.tty_sync0
        install -m 644 common/ppp-options.tty_hdlc /etc/ppp/options.tty_sync1
    fi
fi

if [ "${KV}" == "2.4" ]
then
#
# For 2.4 Kernels, tell the installer that he has to rebuild the Kernel
# as the bext step
#
cat << EOF

                        Installation complete
			---------------------

You now need to reconfigure your kernel to include the FarSync WAN driver.
See the "Wan interfaces" menu under "Network device support", and then build
and install the new kernel.

EOF
else
#
# Does the installer need to build the WAN modules as well?
#
  echo
  echo "Checking to see what the next actions should be..."
  echo
  if [ -f /lib/modules/`uname -r`/kernel/drivers/net/wan/syncppp.ko -o $install_type = "oem" ] 
  then
    echo "... Start the system"
#
# No I think we can just start the system next
#
    echo
    echo "                    Installation complete"
    echo "                    ---------------------"
    echo
    echo "As far as I can tell the install is now complete and you should be able to"
    echo "start the farsync driver with the following command"
    echo
    echo "   /etc/init.d/farsync start"
    echo
    echo "You may need to load the required WAN modules first."
    echo "See the Post Install Instructions in the install document to"
    echo "find out how to load and unload them with the farsync driver."
    echo 
    echo "If this does not result in the driver being loaded then please"
    echo "consult the Trouble Shooting Guide."
    echo
    depmod -a
  else
#
# Yes, needs to get the WAN modules source etc...
#
    echo "... Build the WAN Modules"
    if [ "$1"  == "wan" ]
    then
      echo "    syncppp and the Generic HDLC modules"
    else
      echo "    syncppp only"
    fi
    echo
    echo "                        Installation complete "
    echo "                        ---------------------"
    echo
    echo "As far as I can tell you need to build the WAN drivers."
    echo "Please refer to the Post Install instructions in the install documentation."
    echo
  fi
fi
echo "Finally you will need to configure the new synchronous network interfaces."
echo "See the Install documentation for more details."
echo
exit 0
