#!/bin/bash
#
# owfs        Startup script for the 1-Wire networks
#
# chkconfig: - 95 05
# description: OWFS is a userspace virtual filesystem providing access to 1-Wire networks.
#
# config: /etc/sysconfig/owfs

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/owfs ]; then
        . /etc/sysconfig/owfs
fi

numfs=${#MOUNTPOINT[*]}
if [ $numfs -eq 0 ]; then
	exit 0
fi

LOCKFILE=/var/lock/subsys/owfs
PIDFILE=/var/run/owfs.pid
owfs=/usr/sbin/owfs
RETVAL=0

start() {
        echo -n $"Mounting One Wire file system: "
	/sbin/modprobe -q fuse
	i=0; n=0
	while [ $n -lt $numfs ]; do
		mountpoint=${MOUNTPOINT[$i]}
		options=${OPTIONS[$i]}
		if [ "$mountpoint" != "" ]; then
			[ -d $mountpoint ] || mkdir -p $mountpoint
			$owfs --pid-file "$PIDFILE" $options $mountpoint >/dev/null
			RETVAL=$?
			[ $RETVAL = 0 ] || {
				echo_failure
				echo
				return $RETVAL
			}
			n=`expr $n + 1`
		fi
		i=`expr $i + 1`
	done
	echo_success
	echo
	touch ${LOCKFILE}
	return 0
}

stop() {
	echo -n $"Umounting One Wire file system: "
#	i=0; n=0
#	while [ $n -lt $numfs ]; do
#		mountpoint=${MOUNTPOINT[$i]}
#		interface=${INTERFACE[$i]}
#		options=${OPTIONS[$i]}
#		if [ "$mountpoint" != "" ]; then
#			/bin/umount $mountpoint
#			RETVAL=$?
#			[ $RETVAL = 0 ] || {
#				echo_failure
#				echo
#				return $RETVAL
#			}
#		n=`expr $n + 1`
#		fi
#		i=`expr $i + 1`
#	done
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" -- owfs
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f ${LOCKFILE}
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	for mountpoint in ${MOUNTPOINT[@]}; do
		/bin/mount | /bin/grep $mountpoint
	done
	status $owfs
	;;
  restart)
	stop
	start
	;;
  condrestart)
	if [ -f ${LOCKFILE} ]; then
		stop
		start
	fi
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $RETVAL
