#!/bin/sh

. ./.directories

TXT2MAN=$SRCDIR/setup/txt2man

if test "`uname -p`" = "sparc"
then
	KERNEL32=sparc
	KERNEL64=sparcv9
	MACH=sun4u
else
	case `uname -r` in
	"5.8")
		KERNEL32=i386
		KERNEL64=NULL
		;;
	"5.9")
		KERNEL32=i386
		KERNEL64=NULL
		;;
	*)
		KERNEL32=i386
		KERNEL64=amd64
		;;
	esac
	MACH=i86pc
fi

if test "`uname -r`" = "5.9"
then
	AMSRC=amsrc1
else
	AMSRC=amsrc2
fi

# Re-create the prototype directory
rm -rf prototype

mkdir prototype
mkdir prototype/platform
mkdir prototype/platform/$MACH
mkdir prototype/platform/$MACH/kernel
mkdir prototype/platform/$MACH/kernel/drv
mkdir prototype/platform/$MACH/kernel/drv/$KERNEL64

mkdir prototype/usr
mkdir prototype/usr/bin
mkdir prototype/usr/sbin
mkdir prototype/usr/man
mkdir prototype/usr/man/man1
mkdir prototype/usr/man/man1m
mkdir prototype/usr/man/man7d

mkdir prototype/usr/include
mkdir prototype/usr/include/sys
mkdir prototype/usr/include/sys/ossddk

mkdir prototype/usr/lib
mkdir prototype/usr/lib/oss
mkdir prototype/usr/lib/oss/etc
mkdir prototype/usr/lib/oss/logs
mkdir prototype/usr/lib/oss/conf
mkdir prototype/usr/lib/oss/include
mkdir prototype/usr/lib/oss/include/sys

mkdir prototype/etc
echo "OSSLIBDIR=/usr/lib/oss" > prototype/etc/oss.conf

cp -r $SRCDIR/oss prototype/usr/lib
cp -r $SRCDIR/setup/SunOS/oss prototype/usr/lib/

cp $KERNEL32/.version prototype/usr/lib/oss/version.dat

# Copy the files to their right place

cp $SRCDIR/include/soundcard.h prototype/usr/lib/oss/include/sys
cp $SRCDIR/include/soundcard.h prototype/usr/include/sys
cp $SRCDIR/include/ossddk/* prototype/usr/include/sys/ossddk
cp $SRCDIR/setup/SunOS/S89oss prototype/usr/lib/oss/etc
cp $SRCDIR/setup/SunOS/sbin/* prototype/usr/sbin
chmod 500 prototype/usr/sbin/*
chmod 744 prototype/usr/lib/oss/etc/S89oss
cp $KERNEL32/target/bin/* prototype/usr/bin
cp $KERNEL32/target/sbin/* prototype/usr/sbin
chmod 500 prototype/usr/sbin/*
chmod 700 prototype/usr/lib/oss/scripts
echo "autosave_mixer yes" > prototype/usr/lib/oss/etc/userdefs

cp -r $SRCDIR/oss prototype/usr/lib

rm -f devlist.txt

# Create the driver modules (for 64 bit)

if test "`ls $KERNEL64/target/modules/*.o 2>/dev/null` " != " "
then
	FPSUPPORT=
	if test -f $SRCDIR/setup/fpsupport_$MACH.c && gcc -c -m64 -O6 $SRCDIR/setup/fpsupport_$MACH.c -o fpsupport.o
	then
		FPSUPPORT=fpsupport.o
	fi

	# osscore
	if ld -64 -dy -r -Nmisc/$KERNEL64/usba -o prototype/platform/$MACH/kernel/drv/$KERNEL64/osscore $KERNEL64/target/objects/*.o $FPSUPPORT
	then
		$TXT2MAN -v "OSS Devices" -s 7 $KERNEL64/kernel/framework/osscore/osscore.man > prototype/usr/man/man7d/osscore.7d
	else
		exit 1
	fi

	rm -f fpsupport.o

	# Man pages
	for n in $SRCDIR/misc/man7/*.man
	do
		N=`basename $n .man`
		$TXT2MAN -t "$CMD" -v "OSS Devices" -s 7 $n > prototype/usr/man/man7d/$N.7d
	done

	# Other modules for 64bit kernel
	for n in $KERNEL64/target/modules/*.o
	do
  		N=`basename $n .o`
  		if ld -64 -dy -r -N drv/$KERNEL64/osscore -o prototype/platform/$MACH/kernel/drv/$KERNEL64/$N $n 
  		then
   			OK=1
		else
   			exit 1
		fi

  		if test $KERNEL64 = "sparcv9"
		then
			grep "^$N[ 	]" $KERNEL64/devices.list >> devlist.txt
  		fi
	done

	# USB Module
	if test -f $KERNEL64/target/modules/ossusb.o
	then
		if ld -64 -dy -r -Ndrv/$KERNEL64/osscore -Nmisc/$KERNEL64/usba -o prototype/platform/$MACH/kernel/drv/$KERNEL64/ossusb $KERNEL64/target/modules/ossusb.o
 		then
  			OK=1
 		else
  			exit 1
 		fi
 	fi

	# SADA compatibility module
	if test `uname -r` != "5.8"
	then
	  if ld -64 -dy -r -Ndrv/$KERNEL64/osscore -Nmisc/$KERNEL64/$AMSRC -Nmisc/$KERNEL64/audiosup -Nmisc/$KERNEL64/mixer -o prototype/platform/$MACH/kernel/drv/$KERNEL64/sadasupport $KERNEL64/target/modules/sadasupport.o
	  then
  		OK=1
	  else
		exit 1
	  fi
	fi
fi # 64 bit modules done

# Handle 32 bit modules

if test "`ls $KERNEL32/target/modules/*.o 2>/dev/null` " != " "
then

	# osscore
	FPSUPPORT=
	if test -f $SRCDIR/setup/fpsupport_$MACH.c && gcc -c -O6 $SRCDIR/setup/fpsupport_$MACH.c -o fpsupport.o && gcc -c $SRCDIR/setup/SunOS/__xtol-i86pc-i386.s -o __xtol.o
	then
		FPSUPPORT="fpsupport.o __xtol.o"
	fi

	if ld -dy -r -Nmisc/usba -o prototype/platform/$MACH/kernel/drv/osscore $KERNEL32/target/objects/*.o $FPSUPPORT
	then
		$TXT2MAN -v "OSS Devices" -s 7 $KERNEL32/kernel/framework/osscore/osscore.man > prototype/usr/man/man7d/osscore.7d
	else
		exit 1
	fi

	rm -f fpsupport.o __xtol.o

	# Other modules for 32bit kernel
	for n in $KERNEL32/target/modules/*.o
	do
		N=`basename $n .o`
		if ld -dy -r -N drv/osscore -o prototype/platform/$MACH/kernel/drv/$N $n
		then
			OK=1
		else
			exit 1
		fi
		grep "^$N[ 	]" $KERNEL32/devices.list >> devlist.txt
	done

	# USB Modules
	if test -f $KERNEL32/target/modules/ossusb.o
	then
		if ld -dy -r -N drv/osscore -N misc/usba -o prototype/platform/$MACH/kernel/drv/ossusb $KERNEL32/target/modules/ossusb.o
		then
			OK=1
		else
			exit 1
		fi
	fi
	
	# SADA Compatibility
	if test `uname -r` != "5.8"
	then
	   if ld -dy -r -Ndrv/osscore -Nmisc/$AMSRC -Nmisc/audiosup -Nmisc/mixer -o prototype/platform/$MACH/kernel/drv/sadasupport $KERNEL32/target/modules/sadasupport.o
	   then
		OK=1
	   else
		exit 1
	   fi
	fi

fi # 32 bit modules done

if test "$KERNEL64 " = "sparcv9 "
then
	# Drop SB Live! from the list of supported devices for Sparc
	rm -f tmplist
	mv devlist.txt tmplist
	grep -v "Sound Blaster Live" < tmplist|sort|uniq >devlist.txt
	rm -f tmplist
fi

cp devlist.txt prototype/usr/lib/oss/etc/devices.list

if test -d $KERNEL32/origdir/devlists
then
	cp -f devlist.txt $KERNEL32/origdir/devlists/Solaris-`uname -p`
	#sed 's/.*	//' <  devlist.txt|sort|uniq >$KERNEL64/origdir/devlists/Solaris-`uname -p`
fi

# Generate the config files
rm -f confgen
cc -o confgen $KERNEL32/setup/SunOS/confgen.c
./confgen prototype/platform/$MACH/kernel/drv \\/platform\\/$MACH\\/kernel\\/drv $KERNEL32/kernel/drv/* $KERNEL32/kernel/nonfree/drv/* $KERNEL32/kernel/framework/*
rm -f confgen

# Generate Man pages for user commands
for i in $KERNEL32/target/bin/*
do
	CMD=`basename $i`
	$TXT2MAN -t "$CMD" -v "OSS User Commands" -s 1 $KERNEL32/cmd/$CMD/$CMD.man > prototype/usr/man/man1/$CMD.1
	echo done $CMD
done

# Generate Man pages for system commands
for i in $KERNEL32/target/sbin/*
do
	CMD=`basename $i`
	if test -f $KERNEL32/cmd/$CMD/$CMD.man
	then
		$TXT2MAN -t "$CMD" -v "OSS System Administration Commands" -s 1m $KERNEL32/cmd/$CMD/$CMD.man > prototype/usr/man/man1m/$CMD.1m
		echo done $CMD
	fi
done

# Generate pages for Maintenance Commands 
rm -f prototype/usr/man/man1m/ossdetect.1m
$TXT2MAN -t "ossdetect" -v "OSS User Commands" -s 1m $KERNEL32/os_cmd/SunOS/ossdetect/ossdetect.man > prototype/usr/man/man1m/ossdetect.1m
echo done ossdetect

# Licensing stuff
if test -f $KERNEL32/4front-private/osslic.c
then
	cc -o prototype/usr/sbin/osslic -I$KERNEL32/setup -I$KERNEL32/kernel/nonfree/include -I$KERNEL32/kernel/framework/include -I$KERNEL32/include -I$KERNEL32/kernel/OS/SunOS $KERNEL32/4front-private/osslic.c
	strip prototype/usr/sbin/osslic
	
	if test -f prototype/platform/$MACH/kernel/drv/osscore
	then 
		prototype/usr/sbin/osslic -q -u -3prototype/platform/$MACH/kernel/drv/osscore
	fi
	
	if test -f prototype/platform/$MACH/kernel/drv/$KERNEL64/osscore
	then
		prototype/usr/sbin/osslic -q -u -6prototype/platform/$MACH/kernel/drv/$KERNEL64/osscore
	fi
fi

if test -f $KERNEL32/4front-private/ossupdate.c
then
  # ossupdate
  cc -I$KERNEL32 $KERNEL32/4front-private/ossupdate.c -s -o prototype/usr/sbin/ossupdate -lsocket -lnsl
fi

exec sh $SRCDIR/setup/build_common.sh $SRCDIR

exit 0
