#!/bin/sh

set -e

timestamp=`date +"%y%m%d%H%M%S"`
YARDBIN="/usr/share/yard"
YARDBINBACK="/usr/share/yard.off"
YARDETC="/etc/yard"
YARDETCBACK="/etc/yard$timestamp"
YARDLOG="/var/log/yard"
YARDCONFLOG="$YARDLOG/yard.inst.log"
YARDDOC="/usr/share/doc/yard"
YARDFIRST="/sbin/make_root_fs"
goahead=0

die () {
	tail -n 5 $YARDCONFLOG
	rm $YARDCONFLOG

	# Okay, remove my new binaries
	/var/lib/dpkg/info/yard.postrm remove

	# Are there old binaries? Move 'em back
	if [ -e $YARDBINBACK ]; then
		mv $YARDBINBACK $YARDBIN
	fi

	# In case we already have made a configuration, remove it!
	if [ -e $YARDETCBACK ]; then
		rm -rf $YARDETC
		mv $YARDETCBACK $YARDETC
	fi

	echo "Sorry an error occurd while bulding yard binaries"
	echo "Please see $YARDCONFLOG for more details"
	echo
	echo "Press return to continue"
	read dummy
	goahead=1
}

if [ -e $YARDFIRST ]; then
	if [ -z "`grep 'not really YARD' $YARDFIRST`" ]; then
		echo "YARD is already configured!"
		exit 0
	fi
fi


VERSION=`uname -r`
(expr "$VERSION" \< "2.0.0" >/dev/null)

cd $YARDBIN
 
if [ -L /vmlinuz ]; then
	kernelfile=`file /vmlinuz | cut -f5 -d" "`
	kernelfile="/$kernelfile"
	if [ "$VERSION" != "`file -b -m kernel.magic $kernelfile`" ]; then
		echo "Your /vmlinz points to a kernel which is no kernel or has a different version"
		echo "than uname -r reports."
		echo
		echo "You probably changed that yourself. Please correct that and run"
		echo "/usr/share/yard/conf_yard again to finally configure YARD "
		echo "automatically. If you're in the process of upgrading your kernel"
		echo "please reboot and start /usr/share/yard/conf_yard again."
		echo "See also the docs for more details."
		echo
		echo "Press return to continue"
		read dummy
  	goahead=1
	fi
fi

if [ "$goahead" -eq "0" ]; then
	if [ ! -d "/lib/modules/$VERSION" ]; then
		echo "Sorry, the corresponding modules directory to your $VERSION kernel doesn't"
		echo "exist. If you are in the process of upgrading your kernel please reboot"
		echo "and configure yard by calling /usr/share/yard/conf_yard again."
		echo "Otherwise please rename your modules directory to /lib/modules/$VERSION"
		echo "and start the final configuration of yard again."
		echo "See docs for more details."
		echo
		echo "Press return to continue"
		read dummy
		goahead=1
	fi
fi


if [ "$goahead" -eq "0" ]; then
	# Save old yard settings
	if [ -e $YARDETC ]; then
		if [ -n "`ls $YARDETC`" ]; then
			cp -r $YARDETC $YARDETCBACK
			cd $YARDETC
			rm -rf *
		fi
	fi

	cd $YARDBIN
	#trap 'cat $YARDCONFLOG' INT SIGTERM SIGILL
	# run yard initalization
	>$YARDCONFLOG
	(perl configure >> $YARDCONFLOG 2>>$YARDCONFLOG)
	if [ $? -ne "0" ]; then
		die
	else
		(make all >> $YARDCONFLOG 2>>$YARDCONFLOG)
		if [ $? -ne "0" ]; then
			die
		else
			make install >> $YARDCONFLOG 2>>$YARDCONFLOG
			if [ $? -ne "0" ]; then
				die
			fi
		fi
	fi

	if [ "$goahead" -eq "0" ]; then
		# add loaded modules to configuration
		lsmod | cut -f 1 -d" " | \
		xargs -n 1 -i@ find /lib/modules/$VERSION -name @.o >> /etc/yard/Bootdisk_Contents

		# move docs to $YARDDOC
		rm -f $YARDDOC/*Makefile*
		rm -f $YARDDOC/.cvsignore

		# remove old yard binaries when all went well
		if [ -e $YARDBINBACK ]; then
			rm -rf $YARDBINBACK
		fi

		# Restore old yard settings
		if [ -e $YARDETCBACK ]; then
			echo
			echo "The previous configuration in $YARDETC has been moved to $YARDETCBACK!"
		else
			echo
			echo "The configuration has gone to $YARDETC. See $YARDDOC for documentation."
		fi

		# remove the old binary (could have been made when last installation was unsuccessful)
		if [ -f /sbin/make_root_fs.old ]; then
			rm -f /sbin/make_root_fs.old
		fi
	fi
fi
