#!/bin/bash
#
# Startup script for the Flumotion streaming server
#
# flumotion: Flumotion Streaming Server
#
# chkconfig: - 80 20
#
# description: Flumotion is a streaming server for audio and video. \
#              See http://www.fluendo.com for details.
#
# Source function library.
. /etc/rc.d/init.d/functions

# paths to files and variables
service=flumotion
prog=/usr/sbin/flumotion
lockdir=/var/lock/subsys
logfile=/var/log/flumotion/service.log

# if arguments are specified, we only start/stop that service part
start() {
	if test "x$*" != "x"
	then
		startone $*
		return $?
	fi

	list=`$prog status | cut -f1,2 -d' ' | tr ' ' @`
	RETVAL=0
	for line in $list
	do
		type=`echo $line | cut -f1 -d'@'`
		name=`echo $line | cut -f2 -d'@'`
		startone $type $name || RETVAL=1
	done
        return $RETVAL
}

startone() {
	type=$1
	name=$2
	echo -n $"Starting $type $name: "
	daemon --user flumotion $prog -d 3 -l $logfile start $type $name
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && touch ${lockdir}/flumotion.$type.$name.lock
	return $RETVAL
}

stop() {
	if test "x$*" != "x"
	then
		stopone $*
		return $?
	fi

	list=`$prog status | cut -f1,2 -d' ' | tr ' ' @`
	RETVAL=0

	for line in $list
	do
		type=`echo $line | cut -f1 -d'@'`
		name=`echo $line | cut -f2 -d'@'`
		stopone $type $name || RETVAL=1
	done
	return $RETVAL
}

stopone() {
	type=$1
	name=$2

        RETVAL=0
	lockfile=${lockdir}/flumotion.$type.$name.lock
	if test -e ${lockfile}
	then
		echo -n $"Stopping $type $name: "
		$prog stop -d 3 -l $logfile $type $name
		RETVAL=$?
		[ $RETVAL = 0 ] && success || failure
		echo
		[ $RETVAL = 0 ] && rm ${lockdir}/flumotion.$type.$name.lock
	fi
	return $RETVAL
}

status() {
	$prog status
}

clean() {
	$prog clean
}

list() {
	$prog list
}

# See how we were called.
case "$1" in
  start)
	shift
	start $*
	;;
  stop)
	shift
	stop $*
	;;
  restart)
	shift
	stop $*
	start $*
	;;
  status)
        status
	;;
  clean)
        clean
	;;
  list)
        list
	;;
  *)
	echo $"Usage: $service {start|stop|restart|list|status|clean}"
	exit 1
esac

exit $RETVAL
