#!/bin/bash
# netfs         Mount network filesystems.
# chkconfig: 345 25 75
# description: Mount NFS, SAMBA, NCP
# 
# Warning : Network and rpc must be UP first !!! This script does not check
# Use only if you don't like the classic rc.M style
#
# Eko M. Budi for Vector Linux
#
# Include the functions declared in the /etc/rc.d/functions file
source /etc/rc.d/functions

NFSFSTAB=`grep -v '^#' /etc/fstab | awk '{ if ($3 ~ /^nfs$/ && $4 !~ /noauto/) print $2}'`
SMBFSTAB=`grep -v '^#' /etc/fstab | awk '{ if ($3 ~ /^smbfs$/ && $4 !~ /noauto/) print $2}'`
NCPFSTAB=`grep -v '^#' /etc/fstab | awk '{ if ($3 ~ /^ncpfs$/ && $4 !~ /noauto/) print $2}'`

reset_fs()
{
   while [ $1 ]; do
     case $1 in
       /tmp)
          ## remove stale locks
	  rm -f /var/lock/* /var/spool/uucp/LCK..* /tmp/.X*lock /tmp/core /core &> /dev/null 
          if [ -r /tmp/hunt -o -r /tmp/hunt.stats ]; then
              rm -f /tmp/hunt*
          fi
          chmod 1777 /tmp 
	  ;;
       /var)
          chmod 1777 /var/tmp
          mkdir -p /var/log/lastlog
          ## Create a fresh utmp file:
          touch /var/run/utmp
          chown root.utmp /var/run/utmp
          chmod 664 /var/run/utmp
          ;;
        /usr/*)
          ldconfig
          if [ -x /usr/X11R6/bin/fc-cache ]; then
             /usr/X11R6/bin/fc-cache
          fi
          ;;
      esac
      shift
   done    
}


service_start()
{
    if [ "$NFSFSTAB" ]; then
	echo "Mounting NFS filesystems..."
	mount -a -t nfs
	evaluate_retval
    fi
    if [ -n "$SMBFSTAB" ]; then
        echo "Mounting SMB filesystems... "
	mount -a -t smbfs
        evaluate_retval
    fi
    if [ -n "$NCPFSTAB" ]; then
        echo "Mounting NCP filesystems... "
	mount -a -t ncpfs
        evaluate_retval
    fi
    reset_fs $NFSFSTAB $SMBFSTAB $NCPFSTAB    
}

service_stop() {
    if [ "$NFSFSTAB" ]; then
	echo "Unmounting NFS filesystems..."
	umount -f -a -t nfs
	evaluate_retval
    fi
    if [ -n "$SMBFSTAB" ]; then
        echo "Mounting SMB filesystems... "
	umount -a -t smbfs
        evaluate_retval
    fi
    if [ -n "$NCPFSTAB" ]; then
        echo "Mounting NCP filesystems... "
	umount -a -t ncpfs
        evaluate_retval
    fi
    reset_fs $NFSFSTAB $SMBFSTAB $NCPFSTAB    
}

service_status() {
    if [ "$NFSFSTAB" ]; then
	mount -l -t nfs
    fi
    if [ -n "$SMBFSTAB" ]; then
	mount -l -t smbfs
    fi
    if [ -n "$NCPFSTAB" ]; then
	mount -l -t ncpfs
    fi
}

case "$1" in
        start)
                service_start
                ;;
        stop)
                service_stop
                ;;
	reload) 
    		#echo "Reloading Network filesystems... "
    		#evaluate_retval	
		;;
        restart)
                service_start
                /bin/sleep 2
                service_stop
                ;;
	status)
		service_status
		;;
        *)
                echo "Usage: $0 {start|stop|restart|reload|status}"
                exit 1
        ;;
esac

exit 0
