#
# Install script for Midi device driver package
#

if [ "$1" = -f ]
then
	force=true
else
	force=false
fi

TMP=/tmp/midi.err
rm -f $TMP
ERROR1=" Errors have been written to the file $TMP."
ERROR2=" The Midi Driver software was not installed, and the System has not been modified."

if [ "`id | grep root`" = "" ]
then
	message "You have to be 'root' in order to install the driver! $ERROR2"
	exit 1
fi

echo "Installing Midi Device Driver Package..."

# BEGIN DYNAMIC CREATION OF Space.c, System, and Node
# based on controller info entered interactively

cat <<EOF > Space.c
/* THIS FILE WAS CREATED BY THE Install SCRIPT. */
#include "config.h"
#include "sys/types.h"
#include "sys/midi.h"

struct midi_ctlr midi_ctlr[MIDI_UNITS];
int midi_nctlr = MIDI_UNITS;
int irq_to_ctlr[NUMIRQ];	/* to be filled in by midi_init */

EOF

echo "\nHow many Midi controllers do you have? [1] --> \c"
read nctlr
if [ "$nctlr" = "" ]
then
	nctlr=1
fi
n=0
while [ "$n" -lt $nctlr ]
do
	cn=`expr $n + 1`
	echo "\nWhat is the IRQ # for controller number $cn ? [2] --> \c"
	read vect
	if [ "$vect" = "" ]
	then
		vect=2
	fi
	# On the 6386, vector 2 gets linked to 9
	if [ "$vect" = 2 ]
	then
		vect=9
	fi
	eval vect$n=$vect

	echo "Starting address (in hex) for controller number $cn ? [330] --> \c"
	read addr
	if [ "$addr" = "" ]
	then
		addr=330
	fi
	eval saddr$n=$addr

	echo "Ending address (in hex) for controller number $cn ? [331] --> \c"
	read addr
	if [ "$addr" = "" ]
	then
		addr=331
	fi
	eval eaddr$n=$addr
	n=`expr $n + 1`
done

n=0
> System
while [ "$n" -lt $nctlr ]
do
	eval echo "midi:Y:1:7:1:\$vect$n:\$saddr$n:\$eaddr$n:0:0" |
		sed "s/:/	/g" >> System
	n=`expr $n + 1`
done

echo "int ctlr_to_irq[] = {" >> Space.c
echo MIDI_0_VECT >> Space.c
n=1
while [ "$n" -lt $nctlr ]
do
	echo ",MIDI_${n}_VECT" >> Space.c
	n=`expr $n + 1`
done
echo "};" >> Space.c

echo "int ctlr_to_data_port[] = {" >> Space.c
echo MIDI_0_SIOA >> Space.c
n=1
while [ "$n" -lt $nctlr ]
do
	echo ",MIDI_${n}_SIOA" >> Space.c
	n=`expr $n + 1`
done
echo "};" >> Space.c

echo "int ctlr_to_status_port[] = {" >> Space.c
echo "MIDI_0_SIOA + 1" >> Space.c
n=1
while [ "$n" -lt $nctlr ]
do
	echo ", MIDI_${n}_SIOA + 1" >> Space.c
	n=`expr $n + 1`
done
echo "};" >> Space.c

echo "midi	midi	c	0" > Node
n=1
while [ "$n" -lt $nctlr ]
do
	echo "midi	midi`expr $n + 1`	c	$n" >> Node
	n=`expr $n + 1`
done

echo ""

# END OF DYNAMIC CREATION OF Space.c, System, and Node

/etc/conf/bin/idcheck -p midi 2>> $TMP
if [ $? != 0 ]
then
	if [ $force = true ]
	then
		/etc/conf/bin/idinstall -d midi
	else
		message -cu "The Midi Device Driver is already installed (or partially installed).
Do you wish to overwrite the existing device driver software?"
		if [ $? = 0 ]
		then
			/etc/conf/bin/idinstall -d midi
		else
			exit 1
		fi
	fi
fi

inc=/usr/include/sys/midi.h
cp midi.h $inc
chown bin $inc
chgrp bin $inc
chmod 444 $inc

if [ -f Driver.c ]
then
	echo "Compiling Driver.c ..."
	cc -c Driver.c 2>> $TMP
	if [ $? != 0 ]
	then
		message "There was an error while compiling Driver.c. $ERROR1 $ERROR2"
		exit 1
	fi
fi

/etc/conf/bin/idinstall -a -k midi 2>> $TMP
if [ $? != 0 ]
then
	message "There was an error during package installation. $ERROR1 $ERROR2"
	exit 1
fi

/etc/conf/bin/idbuild 2>> $TMP
if [ $? != 0 ]
then
	# if an error occurs here, remove the driver components
	/etc/conf/bin/idinstall -d midi
	rm -f $inc
	message "There was an error during Kernel reconfiguration. $ERROR1 $ERROR2"
	exit 1
fi

rm -f $TMP
exit 0


