#!/bin/sh
#
#	pcspinstall	(C) 1994 Michael Beck :-)	
#
# Create the devices
#
#	pcsp 		(30, 3)	(simulate /dev/dsp)
#	SPARC audio	(30, 4)	(simulate /dev/audio)
#	PCSP mixer	(30, 0) (simulate /dev/mixer)
#

function major () {
	ls -l $2 $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 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 30 $2
		else
			MAJOR=`major $1`
			if [ "$MAJOR" -ne 30 ]; then
				echo "$1 exist but has the wrong MAJOR number $MAJOR."
			remove $1 && mknod -m 666 $1 c 30 $2
			fi
		fi
	else
		mknod -m 666 $1 c 30 $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 30 ]; 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."
					fi
				fi
				remove $1 && ln -sf $2 $1
			fi
		fi
	else
		ln -sf $2 $1
	fi
}

#
# main () 
#

cat <<END

Alternate Sound-driver for Linux 0.7

This script creates the PCSP-driver devices and make links
to the VoxWare-devices.

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

/dev/pcsp    PCSP device	(30, 3)	(simulate /dev/dsp)
/dev/pcaudio SPARC audio	(30, 4)	(simulate /dev/audio)
/dev/pcmixer PCSP mixer		(30, 0) (simulate /dev/mixer)

END

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

ACTION="and correct it"

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

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

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

#
# 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

echo "Installation complete."
exit 0
