#!/bin/sh
#
#       Equinox SST Loadable Module installation script
#
#       Copyright (C) 1989-2002 Equinox Systems Inc.     

#
#	install / remove / get status from eqnx package
#
#	1) eqnx-cfg [-i] [-v][-o <parms>][-d <rpmdir>] <source RPM name>
#		installs the eqnx product from <source RPM name>
#	2) eqnx-cfg -U [-v][-o <parms>][-d <rpmdir>] <source RPM name>
#		updates the eqnx product from <source RPM name>
#	3) eqnx-cfg -r [-v][-o <parms>][-d <rpmdir>] 
#		removes installed eqnx RPM and optionally all RPM
#		components
#	4) eqnx-cfg -s
#		get status of eqnx RPM
#
#	-v 	: verbose option, additional inforamation is displayed
#	-o <parms> : additional options/parameters to pass to rpm
#	-d <RPM dir> : RPM directory (such as /usr/src/redhat)

#
#
# do_install name
#	install eqnx from src RPM
#
do_install() {

	echo "Equinox SST (eqnx) Installation (from source RPM)"

	rm -f ${LOG}		   > /dev/null 2>&1
	rm -rf /var/tmp/eqnx-build > /dev/null 2>&1

	#
	# install the source RPM
	#
	echo " "
	echo "1) validating build environment ..."

	# Kernel Search Path
	# These are all the places where kernel source might exist
	KSP=none
	if [ -e /usr/src/linux-`uname -r`/include/linux ]
	then
		KSP=/usr/src/linux-`uname -r`
        elif [ -e /usr/src/linux-`uname -r | sed 's/-.*//'`/include/linux ]
	then
        	KSP=/usr/src/linux-`uname -r | sed 's/-.*//'`
        elif [ -e /usr/src/linux-`uname -r | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/'`/include/linux ]
	then
        	KSP=/usr/src/linux-`uname -r | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/'`
	elif [ -e /usr/src/linux/include/linux ]
	then
		KSP=/usr/src/linux
	fi

	if [ ${KSP} = none ]
	then
		echo "The Linux kernel source could not be found."
		echo "Please install the kernel source before proceeding."
		exit 1
	fi

	if [ ${VERBOSE} -eq 1 ]
	then
		echo "     Linux kernel source = ${KSP}"
	fi
	KINC=${KSP}/include
   
	# Check for version.h, config.h and modversions.h files
	VERSFILE=${KINC}/linux/version.h
	CONFFILE=${KINC}/linux/config.h
	MODVFILE=${KINC}/linux/modversions.h
     
	if [ ! -f ${VERSFILE} ]
	then
		echo "WARNING: could not find ${VERSFILE}."
		echo "This may cause the eqnx driver to not build correctly."
	fi

	if [ ! -f ${CONFFILE} ]
	then
		echo "WARNING: could not find ${CONFFILE}."
		echo "This may cause the eqnx driver to not build correctly."
	fi

	if [ ! -f ${MODVFILE} ]
	then
		echo "WARNING: could not find ${MODVFILE}."
		echo "This may cause the eqnx driver to not build correctly."
	fi
	 
	# get the kernel version 
	KVER=`cc -E -dM -I ${KINC} ${VERSFILE} | grep UTS_RELEASE | \
	awk '{ print $3 }' | sed 's/\"//g'`

	KVER2=`uname -r`

	if [ ${KVER} != ${KVER2} ]
	then
		echo "WARNING: kernel source version, ${KVER}"
		echo "does not match running kernel, ${KVER2}"
		echo "Its likely that the eqnx driver will not build successfully."
	fi

	if [ $VERBOSE -eq 1 ]
	then
		echo "     Linux kernel version = ${KVER}"
	fi
		   
	# check the SMP flag - first from config.h
	SMP=`cc -E -dM -I ${KINC} ${CONFFILE} | \
	grep CONFIG_SMP | awk '{ print $3 }'`
	if [ ${SMP} ]
	then
		SMP=1
	else	SMP=0
	fi
			        
	# check that running kernel matches SMP configuration
	uname -a | grep -q SMP > /dev/null 2>&1
	if [ $? = 0 ]
	then
		SMP2=1
	else	SMP2=0
	fi

	if [ ${SMP} -ne ${SMP2} ]
	then
		if [ ${SMP} -eq 1 ]
		then
			echo "WARNING: kernel source configuration (smp)"
			echo "does not match running kernel (uniprocessor)"
			echo "Its likely that the eqnx driver will not build successfully."
		else
			echo "WARNING: kernel source configuration (uniprocessor)"
			echo "does not match running kernel (smp)"
			echo "Its likely that the eqnx driver will not build successfully."
		fi
	fi
		   
	if [ ${VERBOSE} -eq 1 ]
	then
		if [ ${SMP} -eq 1 ]
		then
			echo "     Linux SMP configuration: smp"
		else	echo "     Linux SMP configuration: uniprocessor"
		fi
	fi
		  
	# check MODVERSIONS configuration from running kernel
	MODV=`cat /proc/ksyms | grep tty_register_driver | cut -d" " -f2`
	if [ ${MODV} != tty_register_driver ]
	then
		MODV=1
	else	MODV=0
	fi

	# check the MODVERSIONS configuration, if modversions.h is available
	if [ ! -f ${MODVFILE} ]
	then
		MODV2=${MODV}
	else
		cc -E -dM -I ${KINC} ${MODVFILE} | grep tty_register_driver > /dev/null 2>&1

		if [ $? = 0 ]
		then
			MODV2=1
		else	MODV2=0
		fi
	fi

	# check that MODVERSIONS configuration matches
	if [ ${MODV} -ne ${MODV2} ]
	then
		if [ ${MODV} -eq 1 ]
		then
			echo "WARNING: module versions configuration (MODVERSIONS) mismatch between"
			echo "running kernel [enabled] and kernel source [disabled]."
			echo "Its likely that the eqnx driver will not build successfully."
		else
			echo "WARNING: module versions configuration (MODVERSIONS) mismatch between"
 
			echo "running kernel [disabled] and kernel source [enabled]."
			echo "Its likely that the eqnx driver will not build successfully."
		fi
	fi
		   
	if [ ${VERBOSE} -eq 1 ]
	then
		if [ ${MODV} -eq 1 ]
		then
			echo "     Linux module versions: enabled"
		else	echo "     Linux module versions: disabled"
		fi
	fi

	#
	# validate that the source RPM exists and has proper format
	#
	if [ -z ${SRC} ]
	then
		SRC="./eqnx-[0-9]*.src.rpm"
		if [ ! -f ${SRC} ]
		then
			echo "Source RPM package not specified and none found in current directory."
			exit 1
		fi
	fi

	if [ ! -f ${SRC} ]
	then
		echo "Source RPM ${SRC} does not exist."
		exit 1
	fi

	#
	# obtain the binary RPM name
	#
	BASE=`rpm -qp ${SRC} 2>&1`
	if [ $? -ne 0 ]
	then
		echo "rpm -qp on ${SRC} failed: ${BASE}"
		exit 1
	fi

	#
	# install the source RPM
	#
	echo " "
	echo "2) installing source RPM: ${BASE} ..."
	if [ ${VERBOSE} -eq 1 ]
	then
		echo "     Base RPM name = ${BASE}"
	fi

	rpm -ivv ${RPMPARMS} ${SRC} > ${LOG} 2>&1
	if [ $? -ne 0 ]
	then
		echo "rpm failed on installation of source RPM: ${SRC}"
		echo "please examine log file at ${LOG}"
		exit 1
	fi

	#
	# get ARCH directory
	#
	ARCH=`rpm --showrc | grep "^build arch" | awk '{print $4}'`
	BIN=${BASE}.${ARCH}.rpm   

	if [ ${VERBOSE} -eq 1 ]
	then
		echo "     ARCH = ${ARCH}"
		echo "     Binary RPM name = ${BIN}"
	fi

	RPMSDIR=${RPMDIR}/RPMS/${ARCH}
		
	if [ ${VERBOSE} -eq 1 ]
	then
		echo "     RPM SPECS dir = ${SPECDIR}"
		echo "     RPM RPMS dir = ${RPMSDIR}"
	fi

	#
	# build the binary RPM
	#
	echo " "
	echo "3) building binary RPM: ${BIN} (please wait) ..."
	rpm -bb ${RPMPARMS} ${SPECDIR}/eqnx.spec >> ${LOG} 2>&1
	if [ $? -ne 0 ]
	then
		echo "rpm failed on build of binary RPM: ${BIN}"
		echo "please examine log file at ${LOG}"
		exit 1
	fi

	#
	# install the binary RPM
	#
	echo " "
	echo "4) installing binary RPM: ${BIN} ..."
	if [ $UPDATE -ne 0 ]
	then
		rpm -Uv ${RPMPARMS} ${RPMSDIR}/${BIN} >> ${LOG} 2>&1
	else
		rpm -iv ${RPMPARMS} ${RPMSDIR}/${BIN} >> ${LOG} 2>&1
	fi
	if [ $? -ne 0 ]
	then
		echo "rpm failed on installation of binary RPM: ${BIN}"
		echo "please examine log file at ${LOG}"
		exit 1
	fi

	#
	# install driver initialization script at boot time
	#
	echo " "
	echo "5) make driver initialized + loaded at boot time ..."
	if [ ${VERBOSE} -eq 1 ]
	then
		/usr/sbin/eqnx-installrc -v
	else
		/usr/sbin/eqnx-installrc
	fi

	#
	# load driver and create device files
	#
	echo " "
	echo "6) loading device driver and initializing device files ..."

	if [ $UPDATE -ne 0 ]
	then 
		/etc/rc.d/init.d/eqnx stop >> ${LOG} 2>&1
		if [ $? -ne 0 ]
		then
			echo "device driver unload failed"
			echo "please examine log file at ${LOG}"
			exit 1
		fi
	fi

	/etc/rc.d/init.d/eqnx start >> ${LOG} 2>&1
	if [ $? -ne 0 ]
	then
		echo "device driver load failed"
		echo "please examine log file at ${LOG}"
		exit 1
	fi

	echo " "
	echo "installation complete. logfile is located at ${LOG}"
}

#
# do_remove
#	un-install eqnx package
#
do_remove() {

	#
	# locate the package version
	#
	VER=`rpm -q eqnx`
	if [ $? -eq 0 ]
	then
		echo "un-installing ${VER}"
		rpm -ev ${RPMPARMS} eqnx
		if [ $? -ne 0 ]
		then
			echo "RPM removal of eqnx failed"
			exit 1
		fi
	fi

	#
	# remove build directory
	#
	rm -rf /var/tmp/eqnx-build

	#
	# remove packages
	#
	echo -n "do you also wish to remove eqnx packages/data from rpm directory ? (y/n) "
	read ans
	echo " "
	if [ ${ans} = y -o ${ans} = Y ]
	then

		ARCH=`rpm --showrc | grep "^build arch" | awk '{print $4}'`

		if [ ${VERBOSE} -eq 1 ]
		then
			echo "     ARCH = ${ARCH}"
		fi
		
		BUILDDIR=${RPMDIR}/BUILD
		RPMSDIR=${RPMDIR}/RPMS/${ARCH}
		SRCDIR=${RPMDIR}/SOURCES
		SPECDIR=${RPMDIR}/SPECS
		SRPMDIR=${RPMDIR}/SRPMS

		if [ -d ${RPMSDIR} -a -d ${SRPMDIR} ]
		then
			if [ ${VERBOSE} -eq 1 ]
			then
				echo "     cleaning RPM directory ${RPMDIR}"
			fi
				
			rm -rf ${BUILDDIR}/eqnx* > /dev/null 2>&1
			rm -rf ${RPMSDIR}/eqnx* > /dev/null 2>&1
			rm -rf ${SRCDIR}/eqnx* > /dev/null 2>&1
			rm -rf ${SPECDIR}/eqnx* > /dev/null 2>&1
			rm -rf ${SRPMDIR}/eqnx* > /dev/null 2>&1
		fi
	fi	
}

#
# do_status
#	status of eqnx package
#
do_status() {

	#
	# locate the package version
	#
	VER=`rpm -q eqnx`
	if [ $? -eq 0 ]
	then
		echo "${VER} is installed"
		/etc/rc.d/init.d/eqnx status
	fi
}

#
# help
#	usage message
#
help() {
	echo " "
	echo "install:"
	echo "eqnx-cfg [-i][-U][-v][-o rpm-options][-d rpm-directory][src-rpm]"
	echo "   -i : install option (default)"
	echo "   -U : upgrade existing eqnx product"
	echo "   -v : verbose option"
	echo "   -o : used to specify additional parameters to rpm"
	echo "   -d : specifies rpm topdir directory"
	echo "   src-rpm : source rpm to install, else found in current dir"
	echo " "
	echo "remove:"
	echo "eqnx-cfg -r [-v]"
	echo "   -v : verbose option"
	echo " "
	echo "status:"
	echo "eqnx-cfg -s"
	echo " "
	exit 1
}

#######################################################################
#	main


#
# get the options
#
INSTALL=0;
UPDATE=0;
REMOVE=0;
STATUS=0;
VERBOSE=0;
RPMPARMS=
RPMDIR=
LOG=/var/tmp/eqnx-log

OPT=`getopt Ud:hio:rsv $*`
if [ $? -ne 0 ] ; then
	help
fi
set -- $OPT

for i in $*
do
	case $i in
	-U) UPDATE=1;
	    INSTALL=1;
	    shift ;;
	-d) RPMDIR="$2"
	    shift ; shift;;
	-h) help
	    shift ;;
	-i) INSTALL=1;
	    shift ;;
	-o) RPMPARMS="$2";
	    shift ; shift;;
	-r) REMOVE=1;
	    shift ;;
	-s) STATUS=1;
	    shift ;;
	-v) VERBOSE=1;
	    shift ;;
	--) shift ; break;;
	esac
done

#
# locate the RPM topdir directory
#
if [ -z ${RPMDIR} ] 
then
	PATH=$PATH:/usr/sbin:.
	export PATH
	RPMDIR=`rpmvar _topdir`
	if [ $? -ne 0 ]
	then
		echo "could not locate RPM directory, please use -d <RPM topdir_directory.>"
		exit 1
	fi
fi
SPECDIR=${RPMDIR}/SPECS

if [ ! -d ${SPECDIR} ]
then
	echo "$RPMDIR is not valid RPM directory";
	exit 1
fi

if [ ${REMOVE} -eq 0 -a ${STATUS} -eq 0 ]
then
	INSTALL=1
fi

SRC="$1"

if [ ${INSTALL} -eq 1 ] 
then
	do_install $SRC $UPDATE
	exit 0
fi

if [ ${REMOVE} -eq 1 ] 
then
	do_remove 
	exit 0
fi

if [ ${STATUS} -eq 1 ] 
then
	do_status
	exit 0
fi

exit 0
