#!/bin/sh
# post patch script for patch 109338
# this must be run local to the system with the interface cards. 

DEV="pci114f,1c"
MYPKGINST=SUNWsaip
MAXPORT="16 X 14 X 8"		# Maximum number of ports
USE_TTYMODES=0
USE_INTERRUPTS=0
INSTALLDIR=`pwd`		# Capture original directory
PATH=$PATH:.

# Check if Hardware is present.
/usr/sbin/prtconf -p | egrep -s "${DEV}"
if [ $? -eq 0 ]; then
	hw_exists=1
else
	hw_exists=0
fi

# Check that user is root.
usid=`id | awk -F"=" ' /uid/ { printf $2 } ' | cut -f1 -d"("`
if [ "$usid" -ne 0 ] ; then
	echo "You must be a super-user to execute!"
	exit 1
fi

set /dev/null

modports()
{
	eval t1=\$nports${1}${2}c${3}m${4}
	echo $t1
}

#
# Get next available extend concid from string
#
nextid()
{
	# get the next id from the list
	if [ "$IDLIST1" = " " ]
	then
		# used all ids, start over
		IDLIST1="$IDLIST"
	fi
	id1=`expr "$IDLIST1" : " *\([^ ]*\) "`
	# remove it 
	IDLIST1=`expr "${IDLIST1}" : " *[^ ]*\( .*\)"`

	# count how many times this id has been used
	eval tmp="\$IDCOUNT${id1}"
	if [ -z "$tmp" ]
	then
		tmp=1
	else
		tmp=`expr "$tmp" + 1`
	fi
	eval IDCOUNT${id1}=$tmp
	id2=$tmp
}

#
# Configuration for SUNWsaip Xr.
#
xrconf()
{
	# save state
	tmpid1="$IDLIST1"
	tmpid2="$IDLIST2"
	typemsg="8-port adapter:"

	if [ "$SKIP_CONF" = "false" ] ; then
	echo "\n Adapter type is ${BRDNAME}.
${iodisplay}${memdisplay}
${typemsg}"

	fi # SKIP_CONF

	nconc=1
	eval nconc${n}0="1"

	i=1
	nextid
	eval id${n}0c${i}=$id1
	eval idn${n}0c${i}=$id2

	pn=$n
	pl=0
	pc=1
	pm=0

	eval nports${pn}${pl}c${pc}m${pm}=8

	eval useint${n}=0

	if [ $id2 -eq 1 ]
	then
		tmpid="$tmpid $id1"
	fi

	iostr=""
	mstr=""
	if [ "$platform" = "ISA" ]
	then
		iostr="	I/O address:	${porttmp}.
"
	fi
	if [ "$platform" != "MC" -a "$platform" != "PCI" ]
	then
		mstr="	Memory address: ${memory}.
"
	fi

	PORTS_PER_MOD=8

	grep $DEV /etc/path_to_inst | cut -f2 -d' ' > /tmp/$MYNAME.inst

	while read inst
	do
		echo "INST$cnt $inst" > /tmp/$MYNAME.inst.ed
		cnt=`expr $cnt + 1`
	done < /tmp/$MYNAME.inst
	
	i=1
	while [ "$i" -le $nconc ]
	do
		eval id=\$id${n}0c${i}
		eval pn=\$idn${n}0c${i}
		p1=`expr \( "$pn" - 1 \) \* $PORTS_PER_MOD + 1`
		if [ "$BASE_0" = "true" ] ; then
			p1=`expr $p1 - 1`
		fi

	cnt=`grep INST$n /tmp/$MYNAME.inst.ed | cut -f2 -d' ' 2>/dev/null`
	if [ -z "$cnt" ] ; then
		if [ -f /tmp/${MYNAME}.cnt ] ; then
			old_cnt=`cat /tmp/${MYNAME}.cnt 2>/dev/null`
			cnt=`expr $old_cnt + 1`
		else
			cnt=`expr $n + 1`
		fi
	else
		cnt=`expr $cnt + 1`
	fi

	newid=`echo $IDLIST | cut -f$cnt -d' ' 2>/dev/null`

	echo "$cnt" > /tmp/${MYNAME}.cnt

		string=`modports $n 0 $i 0`
		p2=`expr "$p1" + "$string" - 1`
		# add the leading zero to p1 if necessary
		p1=`expr "000$p1" : ".*\(...\)$"`
		p2=`expr "000$p2" : ".*\(...\)$"`
#		string="term/${newid}${p1} - term/${newid}${p2} -- also /dev/cua/${newid}xxx"
#		echo "	$i	$string"
		i=`expr "$i" + 1`
	done
	return 0
}

#########################################################################
# Main - install 
#########################################################################

ARCH=`uname -p`

SUN_OEM=true
TPO="Transparent Print"			# Transparent print name
platform="PCI"
MYNAME="saip"
MYPKGNAME="SUNWsaip"
BOARDNAME="SunSAI/P"
PRODNAME="Sun PCI Serial Asynchronous Interface"
FULLNAME="Sun PCI Serial Asynchronous Interface"
MAX_NBRDS=16
MAX_INST=52
# Zero Based Max Instance
ZB_MAX_INST=51
BASE_0=true


TTYNAME="saip"
PRNAME="spr"  # not used

> /tmp/$MYNAME.inst
> /tmp/$MYNAME.inst.ed
CUST_INST=false

# If we detect PCI devices, we will ask to skip over the adapter
# identification stage
sai_pci=`prtconf | grep "pci114f,1c" | wc -l `   # Use Sun ID
sai_pci=`expr $sai_pci + 0`
SKIP_CONF=true
nbrds=$sai_pci
LN_TERM=yes
USE_TTYMODES=1
USE_INTERRUPTS=0

ID1=
ID2=
IDLIST=

# If we are re-installing over top of ourselves, we will 
# assume that all the ttys are still available. This
# may be a dangerous way to proceed, if there are other 
# Digi products installed that use the same directories
# but the preremove script would delete them all anyway


if [ -f /kernel/drv/${MYNAME} -o -f /kernel/drv/sparcv9/${MYNAME} ] ; then

	if ls /dev/${TTYNAME}[a-z][01][0-9][0-9][ms] > /dev/null 2>&1 ; then
		echo "\nRe-using TTY device names from previous installation \n"
	fi
	IDLIST="a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
else
	for i in a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
	{
		if ls /dev/${TTYNAME}${i}[01][0-9][0-9][ms] > /dev/null 2>&1
		then
			ID2="${ID2} ${i}"
		else
			ID1="${ID1} ${i}"
		fi
	}

	IDLIST="${IDTMP}${ID1} "
fi

IDLIST1="$IDLIST"
IDLIST2="$IDLIST"

if [ "$SKIP_CONF" = "false" ] ; then
	echo "\nThis script installs the ${FULLNAME} driver :"

	echo "\n
This script also installs the information needed by ${OPSYS} to use
the additional ports available through this driver.  Depending upon 
your system, this driver may support up to ${MAX_NBRDS} Host Adapters and 
(0-${ZB_MAX_INST}) instances."

	echo "\n
Press <CR> to proceed or \"Q\" to quit: \c"

	read aa
	case $aa in
	[Qq])   exit 1
	;;
	esac


	LN_TERM=yes
	USE_TTYMODES=1


fi # SKIP_CONF

#
# Configuration loop
#
# Initialize variables
#
fepport=""
fepmem=""
nports=""
config=""

if [ "$SKIP_CONF" = "false" ] ; then
	#
	# Custom installation
	#
	echo "\nNOTE: Although (0-${ZB_MAX_INST}) instances are allowed, this driver"
	echo "only supports ${MAX_NBRDS} actual adapters at one time."
	echo "How many adapters do you wish to install (1-${MAX_NBRDS})? \c"

	while :
	do
		read nbrds
		[ ! "$nbrds" ] && nbrds=0
		if [ "$nbrds" -ge 1 -a "$nbrds" -le ${MAX_NBRDS} ] ; then
			break
		else
			echo "Please enter a number between 1 and ${MAX_NBRDS}: \c"
		fi
	done

fi # SKIP_CONF

tmpid=""
n=0
if [ "$CUST_INST" = "false" ] ; then
	cnt=0
fi

while [ "$n" -lt "$nbrds" ]
do
	iodisplay=""
	memdisplay=""

	if [ "$SKIP_CONF" = "false" ] ; then
		echo "\n Configuring adapter ${n}."
	fi

	#
	# Set up adapter type index variables N...
	# and corresponding printable strings P... 
	#
	if [ "$ARCH" = "sparc" ] ; then
		# Leave this stuff here, in case we start supporting
		# more adapters.  
		P0="99"; P1="99"; P2="99"; P3="99"; P4="99"; 
		P5="99"; P6="99"; P7="99"; P8="99"; P9="99"; 
		P10=" 0"; P11=" 1"; P12=" 2"; P13=" 3";

		N0=99; N1=99; N2=99; N3=99; N4=99; 
		N5=99; N6=99; N7=99; N8=99; N9=99; 
		N10=0; N11=99; N12=99; N13=99;
		MAXTYPE=0
	fi

	if [ "$ANCIENT" = "true" ] ; then
		P10="99"; P11="99"; P12="99"; P13="99";
		N10=99; N11=99; N12=99; N13=99;
		MAXTYPE=`expr $MAXTYPE - 4`
	fi

	BTYPE=A ; 
	BRDNAME="8-port PCI Async Intelligent Adapter"
	platform=PCI

	#
	# PCI Adapters
	#
	# No I/O port or memory asked for PCI
	porttmp=0
	memory=0
	conftmp="${conftmp} 0 0"
	memtmp="${memtmp} 0"

	xrconf

	if [ $? -eq 0 ]
	then
		fepmem="$fepmem $memtmp"
		config="$config $conftmp"
		fepport="$fepport $porttmp"
		eval BTYPE${n}=\$BTYPE
		eval bname${n}=\$BRDNAME
		eval fepmem$n="$memory"
		eval fepport$n="$porttmp"
		memtmp=""
		conftmp=""
		porttmp=""
		setok=1
	else
		memtmp=""
		conftmp=""
		porttmp=""
		memory=""
		echo "\nHit <CR> to re-configure adapter #${n}: \c"
		read aa
		setok=0
	fi

	eval altpin${n}=0
	if [ $setok -eq 1 ] ; then
		n=`expr $n + 1`
	fi
done

# this is new stuff that I added to export the variables to 
# the postinstall script
cat >>$1 <<!
MYNAME="$MYNAME"
MYPKGNAME="$MYPKGNAME"
BOARDNAME="$BOARDNAME"
SUN_OEM="$SUN_OEM"
TTYNAME="$TTYNAME"
PRNAME="$PRNAME"
INSTALLDIR=`pwd`
NBRDS=$nbrds
TTYNAME=$TTYNAME
PRNAME=$PRNAME
LN_TERM=$LN_TERM
USE_TTYMODES=$USE_TTYMODES
ANCIENT=$ANCIENT
BASE_0="$BASE_0"
!
n=0
while [ $n -lt "$nbrds" ]
do

	eval tmp=\$useint$n
	echo USE_INTERRUPTS${n}=$tmp >> $1
	eval tmp=\$bname$n
	echo BNAME${n}=$tmp >> $1
	eval tmp=\$altpin$n
	echo ALTPIN${n}=$tmp >> $1
	eval ALTPIN${n}=\$tmp
	eval tmp=\$fepmem$n
	echo FEPMEM${n}=$tmp >> $1
	eval tmp=\$fepport$n
	echo FEPPORT${n}=$tmp >> $1
	eval bt=\$BTYPE$n
	echo BTYPE${n}=$bt >> $1

	# Xem modules
	eval nconc=\$nconc${n}0
	eval nconc${n}=\$nconc
	echo NCONC${n}0=$nconc >> $1
	c=1
	while [ $c -le $nconc ]
	do
		eval id=\$id${n}0c${c}
		echo ID${n}0C${c}=$id >> $1
		eval pn=\$idn${n}0c${c}
		echo IDN${n}0C${c}=$pn >> $1
		eval tmp=\$nports$n${l}c$c
		if [ "$tmp" ] ; then  # This isn't always set
			echo NPORTS$n${l}C${c}=$tmp >> $1
		fi
		pn=`expr \( $pn - 1 \) \* 16 + 1`

		eval nm=\${nmod${n}${l}c${c}:=0}
		echo NMOD${n}${l}C${c}=$nm >> $1
		eval tmp=\$nports${n}0c${c}m0
		echo NPORTS${n}0C${c}M0=$tmp >> $1
		if [ "$nm" != 0 ]
			then
			m=1
			while [ $m -le $nm ]
			do
				eval id=\$id$n${l}c${c}m$m
				echo ID$n${l}C${c}M${m}=$id >> $1
				eval pn=\$idn$n${l}c${c}m$m
				echo IDN$n${l}C${c}M${m}=$pn >> $1
				eval tmp=\$nports$n${l}c${c}m$m
				echo NPORTS$n${l}C${c}M$m=$tmp >> $1
				pn=`expr \( $pn - 1 \) \* 16 + 1`

				m=`expr $m + 1`
			done
		fi

		c=`expr $c + 1`
	done

	n=`expr $n + 1`
done
#end of request script

# Main processing
ARCH=`uname -p`

PLAT_SUFFIX=cmn
DL_DEV=saip
DITTY=sitty
DPA=spm
SYSINCLUDE=saip.h

# Use some unwanted keywords in the non-OEM version
MOV=`echo "m\c" ; echo "v"`
REM=`echo "r\c" ; echo "m"`
EXPLODE=`echo "unc\c" ; echo "ompress"`

# Test if we are installing overtop of ourselves.  If we are,
# then remove the drivers and kill the download daemon
##############################################################

if [ -f /kernel/drv/${MYNAME} -o -f /kernel/drv/sparcv9/${MYNAME} ] ; then

	# use ps 
	ps -ef | grep "${MYNAME}dl" > /dev/null
	[ $? = 0 ] && {
		pid=`ps -ef | grep ${MYNAME}dl | awk '{print $2}'`
		[ ! -z "$pid" ] && kill ${pid} 2>/dev/null
	}

	sleep 3  #  Give the ps time to finish
fi

sync
rem_drv ${MYNAME}dl 1>/dev/null 2>/dev/null
rem_drv ${MYNAME}   1>/dev/null 2>/dev/null
sync

##############################################################

#
# Now we're done asking questions.  Here is where the
# actual configuring starts to happen.
#

PORTS_PER_MOD=8
n=0
total=0
lasttotal=0

echo "\
#
# This file contains the values needed by the driver
# at load time.  For the most part the information can
# be changed without adverse affect. In order to reduce response 
# time to small packets (latency), it may be helpful to enable 
# interrupts on the adapter.  However, doing this will significantly 
# increase driver CPU usage on your Solaris system.  
# By default, interrupts are disabled.  
#
# Example: To enable interrupts on two SAI/P device with instance numbers of
#          zero and one, the entries in this file should look like:
#
#pci_saip_0_use_interrupts=1;
#pci_saip_1_use_interrupts=1;
#" > /tmp/${MYNAME}.conf
${REM} -f /tmp/${MYNAME}dl.conf

XR_PCI_INDEX=0

n=0
old_pci_index=0
while [ $n -lt "$nbrds" ]
do
	reg_id=`expr $n \* 2 - 2`

	eval altn=\$ALTPIN$n
	NPORTS=$PORTS_PER_MOD
	eval fepsize="8000"
	eval FEPMEM=\$FEPMEM$n
	eval m=\$BTYPE$n
	family="saip"
	pci_index_default=$XR_PCI_INDEX
	XR_PCI_INDEX=`expr $XR_PCI_INDEX + 1`
	pci_index=`grep "INST$n " /tmp/$MYNAME.inst.ed | cut -f2 -d' ' 2>/dev/null`
	if [ -z "$pci_index" ] ; then
		pci_index=$pci_index_default
		if [ $pci_index -lt $old_pci_index ] ; then
			pci_index=`expr $old_pci_index + 1`
		fi
	fi

	old_pci_index=$pci_index

	if [ $n -ge 0 ] ; then
		boardnum=$n
	else
		# flag an error and try to continue
		echo ERROR: bad board number
		boardnum=1
	fi
 
	eval tmp=\$useint$n
	eval tmpstring="pci_${family}_${pci_index}_use_interrupts=\${tmp}\;"
	echo $tmpstring >> /tmp/${MYNAME}.conf

echo "name=\"saipdl\" parent=\"pseudo\" instance=$pci_index;" >> /tmp/${MYNAME}dl.conf

	n=`expr $n + 1`
done	

${REM} /tmp/${MYNAME}.cnt
${REM} /tmp/${MYNAME}.inst
${REM} /tmp/${MYNAME}.inst.ed

/usr/bin/chmod 644 /tmp/${MYNAME}.conf
${MOV} /tmp/${MYNAME}.conf /kernel/drv/${MYNAME}.conf
/usr/bin/chown root /kernel/drv/${MYNAME}.conf
/usr/bin/chgrp root /kernel/drv/${MYNAME}.conf
/usr/bin/chmod 644 /tmp/${MYNAME}dl.conf
${MOV} /tmp/${MYNAME}dl.conf /kernel/drv/${MYNAME}dl.conf
	

sync
if [ $hw_exists = "1" ]; then
	# Hardware Present.  Modify system files and attach driver.
	#
	ADD_DRV="add_drv"
else
	# Hardware NOT Present.  Modify system files only.
	#
	ADD_DRV="add_drv -n"
fi
# Download driver gets loaded whether hdwr is present or not.
# (saipdl : download deamon won't start without it)
ADD_DLDRV="add_drv"

# Add driver.
${ADD_DRV} -m '* 0666 root sys','*cu 0600 uucp uucp' -i ' "pci114f,1c"  ' $MYNAME >/dev/null 2>&1

ADD_STATUS=$?
[ $ADD_STATUS != 0 ] && {
	echo Driver load did not succeed.  Package install continuing.
}

sync
# Add download driver.
${ADD_DLDRV} -m '* 0666 root sys','saip[0-9]* 0666 root sys','saip 0600 root sys' ${MYNAME}dl >/dev/null 2>&1
sync

/etc/rc2.d/S29$MYNAME start

exit 0
