#!/bin/bash
#
# rc.M		This file is executed by init(8) when the system is being
#		initialized for one of the "multi user" run levels (i.e.
#		levels 1 through 6).  It usually does mounting of file
#		systems et al.
#
# Version:	@(#)/etc/rc.d/rc.M	2.23	Wed Feb 26 19:20:58 PST 2003
#
# Author:	Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
#		Heavily modified by Patrick Volkerding <volkerdi@slackware.com>
#
# Author:   Tomas M. <http://slax.linux-live.org>
#		- change root password, autostart gui, reset terminal
#
# Modified for Porteus by fanthom

# Tell the viewers what's going to happen.
echo "Going multiuser..."

# Screen blanks after 15 minutes idle time, and powers down in one hour
# if the kernel supports APM or ACPI power management:
/bin/setterm -blank 15 -powersave powerdown -powerdown 60 > /dev/null 2>&1 &

# Save the contents of 'dmesg':
/bin/dmesg -s 65536 > /var/log/dmesg &

# Start the system logger.
[ -x /etc/rc.d/rc.syslog ] && . /etc/rc.d/rc.syslog start >/dev/null &

# Update the X font indexes:
[ -x /usr/bin/fc-cache ] && echo "Updating X font indexes:  /usr/bin/fc-cache -f &" && /usr/bin/fc-cache -f &

# Initialize the networking hardware.
[ -x /etc/rc.d/rc.inet1 ] && . /etc/rc.d/rc.inet1

# Set the hostname.
/bin/hostname $(cat /etc/HOSTNAME | cut -f1 -d .) &

# Look for additional USB/SCSI/IEEE1394/etc devices on multiple LUNs:
[ -x /etc/rc.d/rc.scanluns ] && . /etc/rc.d/rc.scanluns

# Start networking daemons:
[ -x /etc/rc.d/rc.inet2 ] && . /etc/rc.d/rc.inet2

# Start the Control Script for automounter:
[ -x /etc/rc.d/rc.autofs ] && . /etc/rc.d/rc.autofs start

# Start the Network Time Protocol daemon:
[ -x /etc/rc.d/rc.ntpd ] && . /etc/rc.d/rc.ntpd start

# Start ACPI daemon.
[ -x /etc/rc.d/rc.acpid ] && . /etc/rc.d/rc.acpid start &

# Update any existing icon cache files:
if find /usr/share/icons 2>/dev/null | grep -q icon-theme.cache; then
    for theme_dir in /usr/share/icons/* ; do
	[ -r ${theme_dir}/icon-theme.cache ] && echo "Updating icon-theme.cache in ${theme_dir}..." && /usr/bin/gtk-update-icon-cache -t -f ${theme_dir} > /dev/null 2>&1 &
    done
    # This would be a large file and probably shouldn't be there.
    [ -r /usr/share/icons/icon-theme.cache ] && echo "Deleting icon-theme.cache in /usr/share/icons..." && rm -f /usr/share/icons/icon-theme.cache
fi

# Update mime database:
echo "Updating MIME database:  /usr/bin/update-mime-database /usr/share/mime &"
/usr/bin/update-mime-database /usr/share/mime > /dev/null 2>&1 &

# Start D-Bus:
[ -x /etc/rc.d/rc.messagebus ] && . /etc/rc.d/rc.messagebus start

# These GTK+/pango files need to be kept up to date
[ -x /usr/bin/update-pango-querymodules ] && /usr/bin/update-pango-querymodules &

# Update desktop database
echo "Updating desktop database:  /usr/bin/update-desktop-database &"
/usr/bin/update-desktop-database &

# Start HAL:
[ -x /etc/rc.d/rc.hald ] && . /etc/rc.d/rc.hald start

# Updating gtk files
[ -x /usr/bin/update-gtk-immodules ] && /usr/bin/update-gtk-immodules &
[ -x /usr/bin/update-gdk-pixbuf-loaders ] && /usr/bin/update-gdk-pixbuf-loaders &

# Start Bluetooth:
[ -x /etc/rc.d/rc.bluetooth ] && . /etc/rc.d/rc.bluetooth start

# Start wicd:
[ -x /etc/rc.d/rc.wicd ] && . /etc/rc.d/rc.wicd start

# Start dnsmasq, a simple DHCP/DNS server:
[ -x /etc/rc.d/rc.dnsmasq ] && /etc/rc.d/rc.dnsmasq start

# Start snmpd:
[ -x /etc/rc.d/rc.snmpd ] && /etc/rc.d/rc.snmpd start

# Start the print spooling system.  This will usually be LPRng (lpd) or CUPS.
if [ -x /etc/rc.d/rc.cups ]; then
    # Start CUPS:
    /etc/rc.d/rc.cups start >/dev/null 2>&1 &
elif [ -x /etc/rc.d/rc.lprng ]; then
    # Start LPRng (lpd):
    . /etc/rc.d/rc.lprng start
fi

# Start netatalk. (a file/print server for Macs using Appletalk)
[ -x /etc/rc.d/rc.atalk ] && . /etc/rc.d/rc.atalk

# Start smartd, which monitors the status of S.M.A.R.T. compatible
# hard drives and reports any problems.  Note some devices (which aren't
# smart, I guess ;) will hang if probed by smartd, so it's commented out
# by default.
# [ -x /usr/sbin/smartd ] && /usr/sbin/smartd

# If we're using udev, make /dev/cdrom and any other optical drive symlinks
# if some udev rule hasn't made them already:
if ! grep -wq nohotplug /proc/cmdline; then
    [ -x /lib/udev/rc.optical-symlinks -a -x /etc/rc.d/rc.udev ] && . /lib/udev/rc.optical-symlinks &
fi

# Monitor the UPS with genpowerd.
# To use this, uncomment this section and edit your settings in
# /etc/genpowerd.conf (serial device, UPS type, etc).  For more information,
# see "man genpowerd" or the extensive documentation in the
# /usr/doc/genpower-*/ directory.
# You'll also need to configure a similar block in /etc/rc.d/rc.6 if you want
# support for stopping the UPS's inverter after the machine halts.
# [ -x /sbin/genpowerd ] && { echo "Starting genpowerd daemon..."; /sbin/genpowerd; }

# Turn on process accounting.  To enable process accounting, make sure the
# option for BSD process accounting is enabled in your kernel, and then
# create the file /var/log/pacct (touch /var/log/pacct).  By default, process
# accounting is not enabled (since /var/log/pacct does not exist).  This is
# because the log file can get VERY large.
[ -x /sbin/accton -a -r /var/log/pacct ] && chmod 640 /var/log/pacct && /sbin/accton /var/log/pacct &

# Start crond (Dillon's crond):
# If you want cron to actually log activity to /var/log/cron, then change
# -l notice to -l info to increase the logging level.
/usr/sbin/crond -l notice > /dev/null 2>&1 &

# Start atd (manages jobs scheduled with 'at'):
[ -x /usr/sbin/atd ] && /usr/sbin/atd -b 15 -l 1

# Slackware-Mini-Quota-HOWTO:
# To really activate quotas, you'll need to add 'usrquota' and/or 'grpquota' to
# the appropriate partitions as listed in /etc/fstab.  Here's an example:
#
# /dev/hda2      /home      ext3     defaults,usrquota      1   1
#
# You'll then need to setup initial quota files at the top of the partitions
# to support quota, like this:
# touch /home/aquota.user /home/aquota.group
# chmod 600 /home/aquota.user /home/aquota.group
#
# Then, reboot to activate the system.
# To edit user quotas, use 'edquota'.  See 'man edquota'.  Also, the
# official Quota Mini-HOWTO has lots of useful information.  That can be found
# here:  /usr/doc/Linux-HOWTOs/Quota

# Check quotas and then turn quota system on:
if grep -q quota /etc/fstab; then
    for quotafs in $(awk '/quota/ {print $2}' /etc/fstab) ; do rm -f $quotafs/{a,}quota.{group,user}.new; done
    [ -x /sbin/quotacheck ] && echo "Checking filesystem quotas:  /sbin/quotacheck -avugm" && /sbin/quotacheck -avugm
    [ -x /sbin/quotaon ] && echo "Activating filesystem quotas:  /sbin/quotaon -avug" && /sbin/quotaon -avug
fi

# Start the SASL authentication server.  This provides SASL
# authentication services for sendmail:
[ -x /etc/rc.d/rc.saslauthd ] && . /etc/rc.d/rc.saslauthd start

# Start the sendmail daemon:
[ -x /etc/rc.d/rc.sendmail ] && . /etc/rc.d/rc.sendmail start

# Load ALSA (sound) defaults:
[ -x /etc/rc.d/rc.alsa ] && . /etc/rc.d/rc.alsa

# Load a custom screen font if the user has an rc.font script.
[ -x /etc/rc.d/rc.font ] && . /etc/rc.d/rc.font

# Load a custom keymap if the user has an rc.keymap script.
[ -x /etc/rc.d/rc.keymap ] && . /etc/rc.d/rc.keymap

# Start the MySQL database:
[ -x /etc/rc.d/rc.mysqld ] && . /etc/rc.d/rc.mysqld start

# Start Apache web server:
[ -x /etc/rc.d/rc.httpd ] && . /etc/rc.d/rc.httpd start

# Start OpenLDAP:
[ -x /etc/rc.d/rc.openldap ] && . /etc/rc.d/rc.openldap start

# Start Samba (a file/print server for Win95/NT machines).
# Samba can be started in /etc/inetd.conf instead.
[ -x /etc/rc.d/rc.samba ] && . /etc/rc.d/rc.samba start

# Start the GPM mouse server:
[ -x /etc/rc.d/rc.gpm ] && . /etc/rc.d/rc.gpm start

# Run SystemV init scripts.
[ -x /etc/rc.d/rc.sysvinit ] && . /etc/rc.d/rc.sysvinit

# Start the local setup procedure.
[ -x /etc/rc.d/rc.local ] && . /etc/rc.d/rc.local

# rc.slax script part
. /usr/lib/liblinuxlive

# change password
NEWROOTPASS="`cmdline_value passwd`"
[ "$NEWROOTPASS" = "ask" ] && { echo -ne "\nEnter new password for root: "; read -s NEWROOTPASS; }
[ "$NEWROOTPASS" = "" ] || echo "root:$NEWROOTPASS" | /usr/sbin/chpasswd

# switch to root account or ask for changing default passwords
if grep -wq toroot /proc/cmdline; then
    sed -i /usr/share/config/kdm/kdmrc -e s/AutoLoginUser=guest/AutoLoginUser=root/g 2>/dev/null
    sed -i /etc/lxdm/lxdm.conf -e s/autologin=guest/autologin=root/g 2>/dev/null
else
    root=`grep root /etc/shadow | cut -d: -f-2`
    guest=`grep guest /etc/shadow | cut -d: -f-2`

    if [ "$root" = 'root:$1$MKy0eqPM$auerQwMpGYcqgBqVddkfO/' ]; then
	clear
	echo "Warning! the user root is still using the default [1;33mtoor[0m password. Please provide new password or press enter to continue using the default:" | fmt -w 80
	read psswd
	[ "$psswd" = "" ] || { echo "root:$psswd" | /usr/sbin/chpasswd; new=y; }
    fi

    if [ "$guest" = 'guest:$1$A0zCA/md$Z8FzMvCgnClVRbs8QyPVr.' ]; then
	echo "Warning! the user guest is still using the default [1;33mguest[0m password. Please provide new password or press enter to continue using the default:" | fmt -w 80
	read psswd
	[ "$psswd" = "" ] || { echo "guest:$psswd" | /usr/sbin/chpasswd; new=y; }
    fi

    if [ "$new" = "y" ]; then
	mkdir /tmp/psswd$$ && cp -P --parents /etc/shadow /tmp/psswd$$
	moddir=`awk 'c-->0;/Porteus data found in/{c=1}' /mnt/live/var/log/livedbg`
	touch $moddir/_test 2>/dev/null && rm $moddir/_test
	if [ $? -eq 0 ]; then
	    dir2xzm /tmp/psswd$$ $moddir/modules/psswd.xzm >/dev/null 2>&1
	    echo "[1;33mYour passwords are stored in the $moddir/modules/psswd.xzm module and are vaid as of now. To restore the default passwords, delete psswd.xzm from the /porteus/modules folder.[0m" | fmt -w 80
	    echo "press enter to continue"
	else
	    echo "($moddir/modules is on a read-only filesystem - psswd.xzm cannot be created in this location)" | fmt -w 80
	    dir2xzm /tmp/psswd$$ /home/guest/Desktop/psswd.xzm >/dev/null 2>&1
	    echo "[1;33mYour passwords are stored in the /home/guest/Desktop/psswd.xzm module, please move this module to your /porteus/modules folder and you wont be prompted for changing the default passwords anymore.[0m" | fmt -w 80
	    echo "press enter to continue"
	fi
	read
    fi
fi

# if autoexec= boot parameter is present, automatically start the command given.
# If you need to execute several commands, separate them using ';'
# If you need to use spaces in your command, use ~ instead
# If you wish to reboot/poweroff after the command's execution is finished,
#  simply use autoexec=yourCommand;poweroff for example.
AUTOCMD=$(cmdline_value "autoexec" | tr "~" " ")
[ "$AUTOCMD" = "" ] || { echo "starting autoexec commands"; su --login -c "$AUTOCMD"; }

# Make sure that we have 10 free loop devices at start
x=`grep /dev/loop /proc/mounts | tail -n1 | cut -d " " -f1 | sed 's@/dev/loop@@'`; let y=x+10
while [ $x -le $y ]; do
    [ ! -e /dev/loop$y ] && mknod /dev/loop$y b 7 $y || break
    let y=y-1
done

# make sure the terminal is fully usable even with framebuffer
[ -e /dev/fb0 ] && { for i in $(seq 1 30); do echo; done; reset; }

# All done.
