#!/bin/sh
# $Id: livecd,v 1.13 2006/07/12 09:19:14 havner Exp $

# livecd	Autodetection for livecd
#
# chkconfig:	345 94 06
#
# description:	DHCP and mixer
#
# author:	Lukasz Pawelczyk <havner@pld-linux.org> 2004

# Source function library
. /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/functions-live

if [ -f /etc/sysconfig/livecd ]; then
	. /etc/sysconfig/livecd
fi

case "$1" in
	start|restart|reload|force-reload)

	if [ "$CONFIG_DHCP" = "yes" -a "$CONFIG_NET" = "yes" -a -x "`which dhcpcd 2>/dev/null`" ] \
			&& ! getparam nodhcp && ! getparam nonet; then
		for I in `awk '/^[ \t]*alias[ \t]+eth/ {print $2}' /etc/modprobe.conf`; do
			modprobe $I > /dev/null 2>&1
		done

		for I in $DHCP_INTERFACES; do
			if [ "`dmesg | grep $I`" != "" -o "`grep $I /etc/modprobe.conf`" != "" ]; then
				show "Backgrounding DHCP for $I"
				busy
				dhcpcd "$I" &
				ok
			fi
		done
	fi

	if [ "$CONFIG_MIXER" = "yes" -a "$CONFIG_SOUND" = "yes" -a -x "`which aumix 2>/dev/null`" ] \
	       		&& ! getparam nomixer && ! getparam nosound; then
		show "Loading mixer settings for detected audio devices"
		busy
		COUNT=0
		for I in `awk '/snd-card-[0-9]/ {print $3}' /etc/modprobe.conf`; do
			if [ ${COUNT} -eq 0 ]; then
				aumix -d /dev/mixer -L > /dev/null 2>&1
			else
				aumix -d /dev/mixer${COUNT} -L > /dev/null 2>&1
			fi
			let $((++COUNT))
		done
		ok
	fi

	touch /var/lock/subsys/livecd
	;;
	stop)
	if [ "`cat /proc/mounts | grep \/home\/users`" != "" -a -x "`which lsof 2>/dev/null`" ]; then
		for I in `lsof +D /home/users | awk '!/[C]OMMAND/ {print $2}' | sort | uniq`; do
			kill -15 $I > /dev/null 2>&1
			kill -9 $I > /dev/null 2>&1
		done
		run_cmd "Unmounting home filesystem" umount /home/users 2>/dev/null
	fi
	rm -f /var/lock/subsys/livecd
	;;
	status)
	;;
	*)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit 0
