#!/bin/sh

# PROVIDE: relaxmdconf
# REQUIRE: preseedrandom
# BEFORE: initdiskless

[ -f /RelaxBSD/COPYRIGHT.RelaxBSD ] && exit 0

echo "Mounting /RelaxBSD... (ignore error messages)"

FOUNDDISK=NO

for i in /dev/acd* /dev/cd*
do
	if mount_cd9660 $i /RelaxBSD 2>&1 > /dev/null && [ -f /RelaxBSD/COPYRIGHT.RelaxBSD ] ; then
		FOUNDDISK=YES
		FOUNDCD9660=YES
		break
	fi
	umount /RelaxBSD 2>&1 > /dev/null
done

if [ "$FOUNDDISK" = "NO" ]; then
	for i in `ls /dev/da?s?? /dev/ad?s??`
	do
		fsck -F -p $i
		mount $i /RelaxBSD 2>&1 > /dev/null
		if [ $? = 0 ] && [ -f /RelaxBSD/COPYRIGHT.RelaxBSD ] ; then
			FOUNDUFS=YES
			break
		fi
		umount /RelaxBSD 2>&1 > /dev/null
	done
fi

/rescue/ln -s /RelaxBSD/boot /boot
/rescue/ln -s /RelaxBSD/libexec /libexec
/rescue/ln -s /RelaxBSD/lib /lib

echo "Mounting /usr..."
MD_LOCAL=`mdconfig -a -t vnode -f /RelaxBSD/uzip/usr.uzip`
/RelaxBSD/bin/sleep 1 #Waiting for kernel recognition
mount_cd9660 /dev/$MD_LOCAL.uzip /usr >/dev/null 2>&1

if [ -f /RelaxBSD/uzip/x11r6.uzip ]; then
  echo "Mounting /usr/x11r6..."
  MD_LOCAL=`mdconfig -a -t vnode -f /RelaxBSD/uzip/x11r6.uzip`
  /RelaxBSD/bin/sleep 1 #Waiting for kernel recognition
  mount_cd9660 /dev/$MD_LOCAL.uzip /usr/X11R6 >/dev/null 2>&1
fi

if [ -f /RelaxBSD/uzip/openoffice.uzip ]; then
  echo "Mounting /usr/local/openoffice..."
  MD_LOCAL=`mdconfig -a -t vnode -f /RelaxBSD/uzip/openoffice.uzip`
  /RelaxBSD/bin/sleep 1 #Waiting for kernel recognition
  mount_cd9660 /dev/$MD_LOCAL.uzip /usr/local/OpenOffice.org1.1.4 >/dev/null 2>&1
fi

echo "Mounting /var..."
MD_LOCAL=`mdconfig -a -f /RelaxBSD/uzip/var.uzip`
/RelaxBSD/bin/sleep 1 #Waiting for kernel recognition
mount_cd9660 /dev/$MD_LOCAL.uzip /var

echo -n "Memory devices:"
MDDEVICE=`/sbin/mdconfig -a -t malloc -o compress -s 16m`
/sbin/newfs -U /dev/${MDDEVICE} > /dev/null 2>&1
/sbin/mount -o noatime /dev/${MDDEVICE} /.var >/dev/null 2>&1
/sbin/mount_unionfs /.var /var >/dev/null 2>&1
echo -n " /var"
	
MDDEVICE=`/sbin/mdconfig -a -t malloc -o compress -s 16m`
/sbin/newfs -U /dev/${MDDEVICE} > /dev/null 2>&1
/sbin/mount -o nodev,noexec,nosuid,noatime /dev/${MDDEVICE} /tmp >/dev/null 2>&1
/bin/chmod 0777 /tmp 
echo -n " /tmp"

/usr/sbin/chown daemon:wheel /var/at/jobs
/usr/sbin/chown daemon:wheel /var/at/spool

if [ ! "${FOUNDUFS}" = "YES" ]; then
  MDDEVICE=`/sbin/mdconfig -a -t malloc -o compress -s 25m`
  /sbin/newfs -U /dev/${MDDEVICE} > /dev/null
  /sbin/mount -o noatime /dev/${MDDEVICE} /.root
  /sbin/mount_unionfs /.root /root
  echo -n " /root"
  echo "."
else
  rm -rf /root
  ln -s /RelaxBSD/root /root
fi

rm -f /bin /sbin /scripts
/rescue/ln -s /RelaxBSD/bin /bin >/dev/null 2>&1
/rescue/ln -s /RelaxBSD/sbin /sbin >/dev/null 2>&1
/rescue/ln -s /RelaxBSD/scripts /scripts >/dev/null 2>&1

#Detect the audio card and load the appropriate driver
/usr/local/bin/relaxsnddetect
