#!/bin/sh
###############################################################################
##
## Export all file systems.
##
##  2007-01-16  Andreas Vogel (omega)
##
###############################################################################

exportDirs="/ /app /tmpfs /logfs /cloopfs /lib/modules /opt /img/www /img/bin /raid/data /raid/sys /lib/security /usr/share/zoneinfo"

cmd=$1
srcfile=${2:-/etc/exports}
tmpfile=/tmp/tmp.exports

exportfs=/opt/nfs-utils/sbin/exportfs

case "$cmd" in
    add)
        if [ ! -w "$srcfile" ] ; then
            echo "$0: ERROR: cannot open file '$srcfile' for writing!"
            exit 1
	fi

        for dir in $exportDirs ; do
            if [ -d "$dir" ] ; then
                echo "Adding dir to $srcfile: $dir"

                grep -v -e "^\"$dir\"" -e "^$dir" $srcfile > $tmpfile
                echo "$dir *(rw,no_root_squash,sync,anonuid=99,anongid=99)" >> $tmpfile

                cp $tmpfile $srcfile
                chown root.root $srcfile
                chmod 644 $srcfile

                rm -f $tmpfile
            fi
        done

        $exportfs -r
        ;;

    del)
        if [ ! -w "$srcfile" ] ; then
            echo "$0: ERROR: cannot open file '$srcfile' for writing!"
            exit 1
	fi

        for dir in $exportDirs ; do
            echo "Removing dir from $srcfile: $dir"

            grep -v -e "^\"$dir\"" -e "^$dir" $srcfile > $tmpfile

            cp $tmpfile $srcfile
            chown root.root $srcfile
            chmod 644 $srcfile

            rm -f $tmpfile
        done

        $exportfs -r
        ;;

    *)
        echo "Usage: $0 {add|del} [exportfile]"
        exit 1
        ;;
esac

exit 0
