#!/bin/sh
#
#	pcspinstall	(C) 1995 Michael Beck
#
# Create the devices
#
#	PCSP mixer	(13, 0) (simulate /dev/mixer)
#	pcsp 		(13, 3)	(simulate /dev/dsp)
#	SPARC audio	(13, 4)	(simulate /dev/audio)
#	
#	and (just kidding, because the driver converts 16bit audio to 8 :-)
#
#	pcsp16		(13, 5) (simulate /dev/dsp16)
#

function major () {
	ls -l $1 | gawk '{ print $5 }' | sed 's/,//'
}

function remove () {
	echo -n "Remove $1 and $ACTION ? "
	read ANSWER
	if [ "$ANSWER" = "y" -o "$ANSWER" = "Y" ]; then
		if rm -f $1 ;then
			echo ok.
			return 0
		else
			echo "Could not remove $1."
			echo "Please log in as root and remove it, then restart $0."
			exit 1
		fi
	else
		echo "Did not remove $1, but you may run into difficulties later."
	fi
	return 1
}

function testdev () {
	if [ -e $1 ]; then
		if [ ! -c $1 ]; then
			ANSWER=N
		else
			MAJOR=`major $1`
			if [ "$MAJOR" -ne 13 ]; then
				ANSWER=N
			fi
		fi
	else
		ANSWER=N
	fi
}

function makedev () {

	if [ -e $1 ]; then
		if [ ! -c $1 ]; then
			echo "$1 exist but isn't a character device."
			remove $1 && mknod -m 666 $1 c 13 $2
		else
			MAJOR=`major $1`
			if [ "$MAJOR" -ne 13 ]; then
				if [ "$MAJOR" -ne 30 ]; then
					echo "$1 exist but has the wrong MAJOR number $MAJOR."
				else
					echo "$1 exists but has the old MAJOR number $MAJOR."
				fi
				remove $1 && mknod -m 666 $1 c 13 $2
			fi
		fi
	else
		mknod -m 666 $1 c 13 $2
	fi
}
		
function makelnk () {

	if [ -e $1 ]; then
		if [ -L $1 ]; then
			LINK=`ls -l $1 | gawk '{ print $11 }'`
			BASE=`basename $2`
			if [ "$LINK" = "$BASE" -o "$LINK" = "$2" ] ; then
				return 0
			else
				echo "$1 is a symbolic link but points to $LINK instead to $2."
				remove $1 && ln -sf $2 $1
			fi
		else
			if [ ! -c $1 ]; then
				echo "$1 exist but isn't a character device or a symbolic link."
				remove $1 && ln -sf $2 $1
			else
				MAJOR=`major $1`
				if [ "$MAJOR" -ne 13 ]; then
					if [ "$MAJOR" -eq 14 ]; then
						cat <<END

$1 points to the VoxWare sounddriver!
Remove this device if you don't have a soundcard, otherwise use
$2 instead of $1 if you want redirect soundoperations
to the PCSP-driver.

END
					else
						echo "$1 exist but has the wrong MAJOR number $MAJOR instead of 14."
					fi
				fi
				remove $1 && ln -sf $2 $1
			fi
		fi
	else
		ln -sf $2 $1
	fi
}

#
# main () 
#

#
# check for <sys/pcsp.h> and create one if not exist
#
if [ ! -e /usr/include/sys/pcsp.h ]; then
	echo '#include <linux/pcsp.h>' > /usr/include/sys/pcsp.h
fi
#
# check for <sys/soundcard.h> and create one if not exist
#
if [ ! -e /usr/include/sys/soundcard.h ]; then
	echo "/usr/include/sys/soundcard.h didn't exist, create one."
	echo '#include <sys/pcsp.h>' > /usr/include/sys/soundcard.h
fi

#
# check for the right major number
#
ANSWER=Y
testdev /dev/pcmixer
testdev /dev/pcsp
testdev /dev/pcaudio
testdev /dev/pcsp16
if [ "$ANSWER" = 'Y' ]; then
	exit 0
fi

cat <<END

Alternate Sound-driver for Linux 0.9

Beginning with version 0.9 the PCSP-driver uses a new major number 13 to
resolve the number conflict which was introduced by the iBCS2.
The new number was given by the Linux Device Registrar and will hopefully
be used only by PCSP.
This script creates the new PCSP-driver devices and make links
to the VoxWare-devices. If you use both VoxWare and PCSP don't make links!

It will create the following devices and (optional) the corresponding links:

/dev/pcmixer PCSP mixer		(13, 0) (simulate /dev/mixer)
/dev/pcsp    PCSP device	(13, 3)	(simulate /dev/dsp)
/dev/pcaudio SPARC audio	(13, 4)	(simulate /dev/audio)
/dev/pcsp16  pseudo 16bit	(13, 5)	(simulate /dev/dsp16)

END

echo -n "Continue ? "
read ANSWER
if [ "$ANSWER" != "y" -a "$ANSWER" != "Y" ]; then
	echo
	echo "Installation aborted."
	exit 1
fi

ACTION="and correct it"

makedev /dev/pcmixer 0
makedev /dev/pcsp    3
makedev /dev/pcaudio 4
makedev /dev/pcsp16  5

ACTION="and install a link to the corresponding PCSP-device"

makelnk /dev/mixer /dev/pcmixer
makelnk /dev/dsp   /dev/pcsp
makelnk /dev/audio /dev/pcaudio
makelnk /dev/dsp16 /dev/pcsp16

echo
echo "Installation complete."
exit 0
