#! /bin/sh
#
# startfile for built in nfs, by newbiefan @ wl500g.info
# For Olegs & Ily's FW for Asus Routers
# store this script in /opt/etc/init.d if you've installed optware
# This file mounts an extern nfs from an other asus
#Consider that the nfs server has to be enabled or this script can not run
#or you start manually portmap, mountd and statd (nfsd)

#mountpoints and ip
mntpoint_1="/tmp/mnt/extern"
mntpoint_2="/tmp/mnt/opt"
othermountpoint_1="/tmp/mnt/disc0_3"
othermountpoint_2="/opt"
otherip="server:" #consider to write the ip of server to /etc/hosts or use just the ip

#location of the module nfs & nfsd
nfsmodule="/lib/modules/2.6.22.19/nfs.ko"
shortname="nfs"
#nfsdmodule="/lib/modules/2.6.22.19/nfsd.ko"


###########################End of Script configuration###################

start() {
        # Code here to start the program
        insmod $nfsmodule
################ 1 st mountpoint#############################
        if [ -d $mntpoint_1 ] ; then
           mount -t nfs $otherip$othermountpoint_1 $mntpoint_1
         else
           mkdir $mntpoint_1
           chmod 777 $mntpoint_1
           logger -t NFS "Directory $mntpoint_1 created"
           mount -t nfs $otherip$othermountpoint_1 $mntpoint_1
        fi
        if [ -z `mount | grep $othermountpoint_1` ] ; then #when nfs mount fails
           logger -t NFS "NFS fails to mount $othermountpoint_1, seems nfs module is missing"
          else
           logger -t NFS "NFS $othermountpoint_1 mounted to $mntpoint_1"
        fi
################# 2 nd mountpoint##############################
        if [ -d $mntpoint_2 ] ; then
           mount -t nfs $otherip$othermountpoint_2 $mntpoint_2
         else
           mkdir $mntpoint_2
           chmod 777 $mntpoint_2
           logger -t NFS "Directory $mntpoint_2 created"
           mount -t nfs $otherip$othermountpoint_2 $mntpoint_2
        fi
        if [ -z `mount | grep $othermountpoint_2` ] ; then #when nfs mount fails
           logger -t NFS "NFS fails to mount $othermountpoint_2, seems nfs module is missing"
          else
           logger -t NFS "NFS $othermountpoint_2 mounted to $mntpoint_2"
        fi
        return 0
}

stop() {
        # Code here to stop the program and check it's dead
####################### 1st mountpoint######################
        if [ -d $mntpoint_1 ] ; then
           umount $mntpoint_1
           rmdir $mntpoint_1
           logger -t NFS "$mntpoint_1 unmounted and removed"
        fi
######################## 2nd mountpoint######################
        if [ -d $mntpoint_2 ] ; then
           umount $mntpoint_2
           rmdir $mntpoint_2
           logger -t NFS "$mntpoint_2 unmounted and removed"
        fi
           rmmod $shortname
        return 0
}
##########################start here##########################
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        logger -t NFS "Restart executed from $(whoami)"
        stop
        sleep 2
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac
sleep 1
exit
