#!/bin/bash

# install - 05/05/2000 release - (C) 1998-2000 Cyclades Corporation
# Author: Ivan Passos <ivan@cyclades.com>
# Distribution permitted under the terms of the GNU "copyleft".

LINUX_DIR=/usr/src/linux
EXEC_DIR=`pwd`
CYC_K_SERIES=

check_drv_ver()
{
VER1=$1
VER2=$2
if [ $VER1 -lt $VER2 ]; then
	INSTALL_NEW_DRV=1
elif [ $VER1 = $VER2 ]; then
	INSTALL_NEW_DRV=2
else
	INSTALL_NEW_DRV=0
fi
}


while true
do
	clear

	echo -e "\n"
	echo "             --------------------------------------------------"
	echo "                 Cyclades Linux Driver Installation Script"
	echo "               Copyright (C) Cyclades Corporation, 1998-2001"
	echo "             --------------------------------------------------"
	echo -e "\n\n\n"
	echo "Please choose the board types you wish to support:"
	echo ""
	echo "1.	Cyclom-Y"
	echo "2.	Cyclades-Z"
	echo "3.	Both"
	echo -e "4.	Quit\n\n"
	echo -e "Option => \c"
	read OPTION

	case $OPTION in

		1)
			CYY=1;
			break;
			;;
		2)
			CYZ=1;
			break;
			;;
		3)
			CYY=1; CYZ=1;
			break;
			;;
		4)
			exit
			;;
		*)
			;;
	esac
done

clear

# Determine the kernel src tree version
if [ -f $LINUX_DIR/Makefile ]; then
	REL1=$(grep ^VERSION $LINUX_DIR/Makefile | cut -d' ' -f3)
	REL2=$(grep ^PATCHLEVEL $LINUX_DIR/Makefile | cut -d' ' -f3)
	REL3=$(grep ^SUBLEVEL $LINUX_DIR/Makefile | cut -d' ' -f3)
	RELEASE="$REL1.$REL2.$REL3"
else
	echo "WARNING: the install script cannot find a valid kernel src tree in $LINUX_DIR."
	echo "Aborting installation ..."
	exit 1
fi

CYC_K_SERIES="$REL1.$REL2"

if [ -d $EXEC_DIR/v$CYC_K_SERIES ]; then
	cd $EXEC_DIR/v$CYC_K_SERIES
else
	echo "WARNING: this kernel series ($CYC_K_SERIES) is not supported by this driver package."
	echo "For support availability, please check our FTP site at:"
	echo -e "\n   ftp://ftp.cyclades.com/pub/cyclades/async/linux/\n"
	echo "Aborting installation ..."
	exit 2
fi

case $CYC_K_SERIES in
	2.0)
		CYC_PROC_FLAG="-DOLD_PROC"
			;;
	2.2|2.4)
		CYC_PROC_FLAG=""
			;;
esac

# Determine the driver version

DRV_RELEASE=$(grep \$Revision: ./cyclades.c | cut -d' ' -f2)
let DRV_REL1=${DRV_RELEASE%%.*}
DRV_REL234=${DRV_RELEASE#*.}
let DRV_REL2=${DRV_REL234%%.*}
DRV_REL34=${DRV_REL234#*.}
let DRV_REL3=${DRV_REL34%.*}
let DRV_REL4=${DRV_REL34#*.}

if [ -f $LINUX_DIR/drivers/char/cyclades.c ]; then
	K_DRV_RELEASE=$(grep \$Revision: $LINUX_DIR/drivers/char/cyclades.c | cut -d' ' -f2)
	let K_DRV_REL1=${K_DRV_RELEASE%%.*}
	K_DRV_REL234=${K_DRV_RELEASE#*.}
	let K_DRV_REL2=${K_DRV_REL234%%.*}
	K_DRV_REL34=${K_DRV_REL234#*.}
	let K_DRV_REL3=${K_DRV_REL34%.*}
	let K_DRV_REL4=${K_DRV_REL34#*.}

	#Kernel driver files installation
	# INSTALL_NEW_DRV: 0 --> Do not install new drv
	#                  1 --> Install new drv
	#                  2 --> Check next version number
	INSTALL_NEW_DRV=0
	check_drv_ver $K_DRV_REL1 $DRV_REL1
	if [ $INSTALL_NEW_DRV = 2 ]; then
		check_drv_ver $K_DRV_REL2 $DRV_REL2
		if [ $INSTALL_NEW_DRV = 2 ]; then
			check_drv_ver $K_DRV_REL3 $DRV_REL3
			if [ $INSTALL_NEW_DRV = 2 ]; then
				check_drv_ver $K_DRV_REL4 $DRV_REL4
			fi
		fi
	fi

else
	INSTALL_NEW_DRV=1
fi

if [ $INSTALL_NEW_DRV = 1 ]; then
	echo "Replacing Cyclades driver in kernel $REL1.$REL2.$REL3:"
	echo "   Old version: $K_DRV_REL1.$K_DRV_REL2.$K_DRV_REL3.$K_DRV_REL4"
	echo "   New version: $DRV_REL1.$DRV_REL2.$DRV_REL3.$DRV_REL4"
	if [ -f $LINUX_DIR/include/linux/cyclades.h ]; then
		cp -p $LINUX_DIR/include/linux/cyclades.h $LINUX_DIR/include/linux/cyclades.h.$K_DRV_REL1.$K_DRV_REL2.$K_DRV_REL3.$K_DRV_REL4
	fi
	install -m 644 cyclades.h $LINUX_DIR/include/linux
	if [ -f $LINUX_DIR/drivers/char/cyclades.c ]; then
		cp -p $LINUX_DIR/drivers/char/cyclades.c $LINUX_DIR/drivers/char/cyclades.c.$K_DRV_REL1.$K_DRV_REL2.$K_DRV_REL3.$K_DRV_REL4
	fi
	install -m 644 cyclades.c $LINUX_DIR/drivers/char
	for PATCHFILE in `ls cy.pa*`
	do
		patch -p0 -N -d /usr/src < $PATCHFILE >/dev/null
	done
	echo
else
	echo "This kernel version ($RELEASE) already has the latest Cyclades "
	echo "driver files ($DRV_REL1.$DRV_REL2.$DRV_REL3.$DRV_REL4)."
	echo "Skipping driver files installation ..."
	echo
fi

#Compilation correction for kernels < 2.2.15
if [ "$CYC_K_SERIES" = "2.2" -a "$REL3" -lt "15" ]; then
	patch -p0 -N -d /usr/src < cy.rmv >/dev/null
fi

#Loader and Utilities compilation / installation
echo	"----------------------------------------------------"
echo	"Generic loader and utilities compilation/instalation"
echo

export LINUX_DIR CYC_PROC_FLAG RELEASE

cd $LINUX_DIR
make symlinks
cd $EXEC_DIR/common
make clean util

if [ "$CYY" = "1" ]; then
	echo
	echo	"-----------------------------"
	echo 	"Installing Cyclom-Y utilities"
	cd $EXEC_DIR/common/cyclom-y
	make clean util
fi

if [ "$CYZ" = "1" ]; then
	echo 
	echo	"-------------------------------"
	echo 	"Installing Cyclades-Z utilities"
	echo 

	cd $EXEC_DIR/common/cyclades-z
	make clean lib install util
fi

# Devices creation
echo
cd $EXEC_DIR ; ./mkcyc

# Module instalation
if [ $REL2 -eq 4 ]; then
	sh module_install;
fi

echo
echo 	"----------------------------------------------------------"
echo -e "               Installation complete."
echo -e	"----------------------------------------------------------"
