#!/bin/sh
# 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 2 through 5).
#
# Origin: this script is a mix of rc.S, rc.M and rc.inet2 from Slackware 10.0
#
# GNU GPL (c) Eko M. Budi, 2004
#         (c) VLocity Linux, 2004, Uel Archuletta(bootsplash-text)-2006
#

# load functions
. /etc/rc.d/functions-display

PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"

# bootsplash
. /etc/rc.d/rc.bootsplash
# start animation to rc.S
animate startup

# Tell the viewers what's going to happen.
/etc/rc.d/rc.text 12 'Going multiuser'
# bootsplash
progressbar 60
echocl "rc.M ==> Going multiuser" yellow

## Turn ON system logger first
if [ -x /etc/rc.d/rc.syslog ]; then
 /etc/rc.d/rc.text 13 'Starting system logger'
# bootsplash
progressbar 65
  echonl "Starting system logger"
  /etc/rc.d/rc.syslog start > /dev/null 2>&1
  evaluate_retvall
fi

## Hardware initialization
echocl "Initializing hardware" cyan
for RC in modules pnp acpid serial parallel alsa cdrom samba messagebus hald; do
   if [ -x /etc/rc.d/rc.$RC ]; then
      /etc/rc.d/rc.text 14 "Setting up $RC"  
# bootsplash
progressbar 70
      echonl "Setting up $RC"
      /etc/rc.d/rc.$RC start > /dev/null 2>&1
      evaluate_retvall
   fi
done

for RC in apm; do
   if [ -x /etc/rc.d/rc.$RC ]; then
      /etc/rc.d/rc.text 15 "Setting up $RC" 
# bootsplash
progressbar 75
      echonl "Setting up $RC"
      /etc/rc.d/rc.$RC start > /dev/null 2>&1
      evaluate_retvall
   fi
done

## Setup basic networking
## Note for slackers: this replaces rc.inet1
## On VLocity rc.inet? = individual network device
/etc/rc.d/rc.text 16 ' Initializing Network' 
# bootsplash
progressbar 80
echocl "Initializing network" cyan

## Start Paranoid firewall 
if [ -x /etc/rc.d/rc.paranoid ]; then
  /etc/rc.d/rc.text 16 'Closing up the holes'
# bootsplash
progressbar 80
  echonl "Closing up the holes"
  /etc/rc.d/rc.paranoid start > /dev/null 2>&1
  evaluate_retvall
fi

## Set hostname & name server
    
if grep -qe " *(hostname|dns)=" /proc/cmdline; then
  if [ -x /etc/rc.d/rc.boot-name ]; then
    /etc/rc.d/rc.text 17 'Setting name from boot parameters'
# bootsplash
progressbar 85
    echonl "Setting name from boot parameters ..."
    /etc/rc.d/rc.boot-name start > /dev/null 2>&1
    evaluate_retvall
  fi
fi

## Local host is always ON for multiuser mode
## So I hardcoded it here
 /etc/rc.d/rc.text 17 'Setting up localhost'
# bootsplash
progressbar 85
echonl "Setting up localhost"
ifconfig lo 127.0.0.1 netmask 255.0.0.0 && \
  route add -net 127.0.0.0 netmask 255.0.0.0 lo > /dev/null 2>&1
evaluate_retvall

## set other networks
  ## Start network using boot parameter or the default inet script
  ## Boot parameter is useful for UML, e.g. you may boot linux with
  ## linux inet1=eth0,dhcp
  ## linux inet1=eth0,{IP},{NETMASK},{GATEWAY}
 /etc/rc.d/rc.network & >dev/null
# for i in 0 1 2 3 4; do
# if [ -x /etc/rc.d/rc.inet$i ]; then
#   /etc/rc.d/rc.text 17 'Setting up basic network'
# bootsplash
#progressbar 85
#   echonl "Setting up basic network"
#   /etc/rc.d/rc.inet$i start
#   evaluate_retvall
# fi
# if [ -x /etc/rc.d/rc.eth$i ]; then
#   /etc/rc.d/rc.text 17 "Starting eth$i"
# bootsplash
#progressbar 85
#    echonl "Setting up basic network"
#    /etc/rc.d/rc.eth$i start
#    evaluate_retvall
# fi
#done

## routing, gateway and firewall
## rc.route = custom routing 
## rc.firewall = custom firewall
## ip_forward = enable forwarding, if not in the firewall
for RC in route firewall; do
   if [ -x /etc/rc.d/rc.$RC ]; then
      /etc/rc.d/rc.text 18 "Setting up $RC"
# bootsplash
progressbar 90
      echonl "Setting up $RC"
      /etc/rc.d/rc.$RC start > /dev/null 2>&1
      evaluate_retvall
   fi
done

## at this point, we are ready to talk to The World...
## Here comes the Network file system
## Note for slackers: this is taken from rc.inet2

NFSTAB=`grep -E "^[^#].*[[:space:]].*[[:space:]](nfs|smbfs|ncpfs)[[:space:]]" /etc/fstab`
if [ "$NFSTAB" ]; then
  echocl "Mounting network file system" cyan
  if echo $NFSTAB | grep -qw nfs; then
    # Start the RPC portmapper if we find NFS volumes defined in /etc/fstab.
    if ! /usr/sbin/rpcinfo -p 1> /dev/null 2> /dev/null ; then
      if [ -x /sbin/rpc.portmap ]; then
        /etc/rc.d/rc.text 19 'Starting RPC portmapper'
# bootsplash
progressbar 95
        echonl "Starting RPC portmapper"
        /sbin/rpc.portmap > /dev/null 2&>1
	evaluate_retvall
      fi
    fi
    /etc/rc.d/rc.text 19 'Mounting remote (NFS) file systems'
# bootsplash
progressbar 95
    echonl "Mounting remote (NFS) file systems ..."
    /sbin/mount -a -t nfs  > /dev/null 2&>1
    evaluate_retvall
  fi

  if echo $NFSTAB | grep -qE "[[:space:]](smbfs|ncpfs)[[:space:]]"; then
    /etc/rc.d/rc.text 19 'Mounting remote (SMB/NCP) file systems'
# bootsplash
progressbar 96
    echonl "Mounting remote (SMB/NCP) file systems"
    /sbin/mount -a -t smbfs,ncpfs > /dev/null 2&>1
    evaluate_retvall
  fi

  ## If the new mounted FS is /tmp or /var, reset the file system again
  mount | while read LINE; do
     fsystem=`echo $LINE | cut -f5 -d ' '`
     case $fsystem in nfs|smbfs|ncpfs)
       mpoint=`echo $LINE | cut -f3 -d ' '`
       case $mpoint in
         /tmp|/tmp/*) 
	    rm -f /tmp/.X*lock /tmp/core /tmp/menu* /tmp/reply* /tmp/vasm.log
	    [ -r /tmp/hunt -o -r /tmp/hunt.stats ] && rm -f /tmp/hunt*
	    mkdir -p /tmp/.ICE-unix /tmp/.X11-unix
	    chmod 1777 /tmp /tmp/.ICE-unix /tmp/.X11-unix
	    ;;
	 /var|/var/*)
	    rm -f /var/run/utmp /var/run/*pid /var/run/lpd* /var/run/ppp* /var/run/usb/* \
    		/var/lock/* /var/lock/usb/* /var/spool/uucp/LCK..*
	    touch /var/run/utmp
	    chown root.utmp /var/run/utmp
	    chmod 664 /var/run/utmp
	    /etc/rc.d/rc.syslog restart
	    ;;
	 /usr|/usr/*|/opt|/opt/*)
	    ## Updating libraries
	    ldconfig
           ;;	 
       esac
     esac
  done 
fi

## avahi initialization
echocl "Initializing hardware" cyan
for RC in avahidaemon avahidnsconfd; do
   if [ -x /etc/rc.d/rc.$RC ]; then
      /etc/rc.d/rc.text 14 "Setting up $RC"
# bootsplash
progressbar 96
      echonl "Setting up $RC"
      /etc/rc.d/rc.$RC start > /dev/null 2>&1
      evaluate_retvall
   fi
done

# Set console and X fonts
# setfont &> /dev/null
if [ -x /usr/X11R6/bin/fc-cache ]; then
    /etc/rc.d/rc.text 19 'Caching fonts'
# bootsplash
progressbar 97
    echonl "Caching fonts"
    /usr/X11R6/bin/fc-cache &> /dev/null
    evaluate_retvall
fi

###uncomment next 3 lines for nice console background
#for i in 1 2 3 4 5 6 ; do
# splash -s -u $i /etc/bootsplash/themes/current/config/verbose-`fbresolution 2>/dev/null`.cfg 2>/dev/null
#done

/etc/rc.d/rc.text 19 'Starting the System'
# bootsplash
progressbar 97
