#! /sbin/sh
####################################################################
#	$Id: configure,v 1.11 2004/03/29 17:06:57 scottk Exp $
# 	Copyright (c) Digi, Digi International Inc.
#
#	Configuration script for Digi RealPort Driver.
#	Copyright (c) 1988-98 Digi International Inc.
#
#	For HP-UX 10.X & 11.X
#
#	NOTE: It is expected that the swinstall program will call this
#             script after the postinstall has executed, or at the
#             direction of the user via "swconfig".
#				
####################################################################

PATH=$SW_PATH
export PATH


majorrev=`uname -r | cut -d '.' -f 2`
minorrev=`uname -r | cut -d '.' -f 3`


#
# ONLY manipulate the /stand/system file and the gettydefs if we
# were deferred.
#
# Also copy the operating system version specific files to the
# appropriate locations if necessary.
#
if [[ "x${SW_DEFERRED_KERNBLD}x" != "xx" ]]
then


	case ${majorrev}${minorrev} in
	1123)

		UTILS="/usr/lbin/sw/control_utils"
		if [[ ! -f $UTILS ]]
		then
			msg ERROR "Cannot find $UTILS"
			exit 1
		fi
		. $UTILS
		exitval=$SUCCESS


		# Copy driver binary into correct position.
		cp /etc/conf/digi/${majorrev}.${minorrev}/drp /usr/conf/mod/drp

		mod_systemfile ${SW_SYSTEM_FILE_PATH} -a drp
		retval=$?
		if [[ $retval -ne $SUCCESS ]]
		then
			msg ERROR "Could not enter 'drp' in the ${SW_SYSTEM_FILE_PATH}"
			[[ $exitval -ne $FAILURE ]] && exitval=$retval
			exit $exitval
		fi

		cp /etc/conf/digi/${majorrev}.${minorrev}/ditty /usr/bin/ditty
		cp /etc/conf/digi/${majorrev}.${minorrev}/drpd  /sbin/drpd
		cp /etc/conf/digi/${majorrev}.${minorrev}/dgipserv /sbin/dgipserv
		cp /etc/conf/digi/${majorrev}.${minorrev}/dgelreset /sbin/dgelreset
		# Location requested by HP. Compat with FAS EtherLite driver.
		if [ ! -d /usr/local/bin ]
		then
			mkdir /usr/local/bin
		fi
		cp /etc/conf/digi/${majorrev}.${minorrev}/dinc  /usr/local/bin/dinc
		ln -s /usr/local/bin/dinc /usr/bin/dinc

		;;

	*)

		# Move master file into location...
		cp /etc/conf/digi/drp.master /usr/conf/master.d/drp

		#
		# Manipulate the /stand/system file.
		#
		if [[ ! -f $SW_SYSTEM_FILE_PATH ]]
		then
			echo "ERROR:   Couldn't find the system file."
			exit 1
		fi

		cat $SW_SYSTEM_FILE_PATH | \
			awk '( $0 != "drp" ) { print $0 }' | \
			awk '/^[^*]/ && zz==0 { zz=1 ; print "drp" } { print $0 }' > \
			${SW_SYSTEM_FILE_PATH}.new.tmp.digi

		if [[ ! -f ${SW_SYSTEM_FILE_PATH}.new.tmp.digi ]]
		then
			echo "ERROR:   Couldn't create new system file."
			exit 1
		fi

		cp ${SW_SYSTEM_FILE_PATH}.new.tmp.digi ${SW_SYSTEM_FILE_PATH}
		if [[ $? -ne 0 ]]
		then
			echo "ERROR:   Couldn't replace system file."
			exit 1
		fi

		rm -f ${SW_SYSTEM_FILE_PATH}.new.tmp.digi


		#
		# Copy OS version specific files to the appropriate places
		# I do very little sanity checking here, as the appropriate
		# checking should already have taken place in "checkinstall".
		#
		majrev=`uname -r | cut -f 2 -d '.'`
		numbits=`getconf KERNEL_BITS 2>&1`
		if [ $? -ne 0 ] ;then numbits=32 ;fi
		minrev=`uname -r | cut -d '.' -f 3`
		if [ $minrev -lt 11] ;then minrev=00 ;fi

		case ${majrev} in
			10)
				revstring="10.20"
				;;
			11)
				case ${numbits} in
					32)
						revstring="11.${minrev}"
						;;
					64)
						revstring="11.${minrev}W"
						;;
					*)
						echo "MAJOR ERROR!  Unexpected" \
						     "number of bits ({numbits})"
						exit 1
						;;
				esac
				;;
			*)
				echo "MAJOR ERROR!  Couldn't find files for this" \
				     "revision of the operating system!"
				exit 1
				;;
		esac

		cp /etc/conf/digi/${revstring}/drp.a	/usr/conf/lib/drp.a
		cp /etc/conf/digi/${revstring}/ditty	/usr/bin/ditty
		cp /etc/conf/digi/${revstring}/drpd	/sbin/drpd
		cp /etc/conf/digi/${revstring}/dgipserv	/sbin/dgipserv
		cp /etc/conf/digi/${revstring}/dgelreset /sbin/dgelreset
		# Location requested by HP. Compat with FAS EtherLite driver.
		if [ ! -d /usr/local/bin ]
		then
			mkdir /usr/local/bin
		fi
		cp /etc/conf/digi/${revstring}/dinc	/usr/local/bin/dinc
		ln -s /usr/local/bin/dinc /usr/bin/dinc

		;;

	esac


	#
	# Update the "gettydefs" file (to remove IXANY)
	#
	if	[ \! -f /etc/gettydefs.ixany ] &&
		grep ' IXANY ' /etc/gettydefs >/dev/null &&
		sed -e 's/ IXANY / /g' /etc/gettydefs >/etc/gettydefs.digi
	then
		mv /etc/gettydefs /etc/gettydefs.ixany
		mv /etc/gettydefs.digi /etc/gettydefs

		echo "The file /etc/gettydefs was edited to remove IXANY references."
		echo "The original /etc/gettydefs is saved in /etc/gettydefs.ixany."
	fi


	# Convert config file to new style.

	if [ -f /etc/conf/digi/drp.conf ]
	then
		echo "Converting drp.conf file..."
		rm -rf /tmp/drp.conf.new

		while read psn node ndev devid start socket encrypt encrypt_socket speed
		do
			case $psn in
			'#*')
				printf "$psn\t%-8s\t$ndev\t$devid\t$start\t$socket\t$encrypt\t$encrypt_socket\t$speed\n" $node >> /tmp/drp.conf.new  
				continue;;
			esac

			# Must be old style config
			if [ "$socket" = "" ]
			then
				speed=$start
				start=01
				socket=771
				encrypt=never
				encrypt_socket=1027
			fi

			printf "$psn\t%-8s\t$ndev\t$devid\t$start\t$socket\t$encrypt\t$encrypt_socket\t$speed\n" $node >> /tmp/drp.conf.new

		done < /etc/conf/digi/drp.conf

		mv /etc/conf/digi/drp.conf /etc/conf/digi/drp.conf.bak
		mv /tmp/drp.conf.new /etc/conf/digi/drp.conf
	fi

fi


exit 0
