#!/bin/bash
#
# live: Init script for live image
#
# chkconfig: 345 00 99
# description: Init script for live image.

. /etc/init.d/functions

if ! strstr "`cat /proc/cmdline`" liveimg || [ "$1" != "start" ] || [ -e /.liveimg-configured ] ; then
    exit 0
fi

/bin/touch /.liveimg-configured

# mount live image
if [ -b /dev/live ]; then
	/bin/mkdir -p /mnt/live
	/bin/mount -o ro /dev/live /mnt/live
	# Import variables and run any embeded scripts.
	if [ -f "/mnt/live/config.txt" ]; then
		. /mnt/live/config.txt
	fi
fi

# configure X
/sbin/lspci | /bin/grep VirtualBox > /dev/null 2>&1
if [ "$?" = "0" ]; then
	VDRIVER="--set-driver=vboxvideo"
fi
/usr/bin/system-config-display --noui --reconfig --set-depth=24 $VDRIVER

# add default user with no password
/usr/sbin/useradd -c "LiveCD default user" centos > /dev/null 2>&1
/usr/bin/passwd -d centos > /dev/null 2>&1

# disable screensaver locking
/usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t bool /apps/gnome-screensaver/lock_enabled false > /dev/null 2>&1

# Do we have a home on flash?
if [ -f "/mnt/live/centos-home.tgz" ]; then
	cp -f /mnt/live/centos-home.tgz /home
fi
# Unpack centos home
cd /home
/bin/tar xzvf centos-home.tgz > /dev/null 2>&1
#rm -f centos-home.tgz
cd /
# Double-check the ownership
/bin/chown -R centos:centos /home/centos > /dev/null 2>&1

# unmute sound card
/bin/alsaunmute 0 -s 100 > /dev/null 2>&1
ALSAMICS=`/usr/bin/amixer -c 0 scontrols  | /bin/grep -i mic | /bin/grep -iv intmic | /bin/grep -iv boost | /bin/grep -iv gain`
/bin/echo -e "$ALSAMICS" \
	| /bin/awk -F \' '{ print "/usr/bin/amixer -c 0 set \""$2"\" 100% unmute cap" }' \
	| /bin/sh > /dev/null 2>&1

#Insert XFCE Mic volume controls
XMLIN=@Master,0@`/bin/echo -e "$ALSAMICS" | /bin/awk -F \' '{ print $2",0" }' | /usr/bin/tr '\n' '@'`
#/usr/bin/perl -pi -e "\$tmp = '$XMLIN'; s|XMLIN|\$tmp|g" /home/centos/.config/xfce4/mcs_settings/sound.xml
XSFILE="/home/centos/.config/xfce4/mcs_settings/sound.xml"
echo '<?xml version="1.0" encoding="UTF-8"?>' > $XSFILE
echo '<!DOCTYPE mcs-option SYSTEM "mcs-option.dtd">' >> $XSFILE
echo '' >> $XSFILE
echo '<mcs-option>' >> $XSFILE
echo "	<option name=\"Sound/Mixer/DefaultDevice\" type=\"string\" value=\"default\"/>" >> $XSFILE
echo "	<option name=\"Sound/Mixer/Useful:default\" type=\"string\" value=\"$XMLIN\"/>" >> $XSFILE
echo '</mcs-option>' >> $XSFILE

# set up timed auto-login for after 10 seconds
/bin/sed -i -e 's/\[daemon\]/[daemon]\nAutomaticLoginEnable=true\nAutomaticLogin=centos\nTimedLoginEnable=true\nTimedLogin=centos\nTimedLoginDelay=5/' /etc/gdm/custom.conf

# give default user sudo privileges
/bin/echo "centos     ALL=(ALL)     NOPASSWD: ALL" >> /etc/sudoers

# turn off firstboot for livecd boots
/bin/echo "RUN_FIRSTBOOT=NO" > /etc/sysconfig/firstboot

# don't start cron/at as they tend to spawn things which are
# disk intensive that are painful on a live image
/sbin/chkconfig --level 345 yum-updatesd off > /dev/null 2>&1
/sbin/chkconfig --level 345 crond off > /dev/null 2>&1
/sbin/chkconfig --level 345 atd off > /dev/null 2>&1
/sbin/chkconfig --level 345 anacron off > /dev/null 2>&1
/sbin/chkconfig --level 345 readahead_early off > /dev/null 2>&1
/sbin/chkconfig --level 345 readahead_later off > /dev/null 2>&1

# Stopgap fix for RH #217966; should be fixed in HAL instead
/bin/touch /media/.hal-mtab

echo "/etc/cron.hourly/ntpdate > /dev/null 2>&1" >> /etc/rc.local
