#!/bin/sh
#
# radosgw	RESTful rados gateway
#
# chkconfig: - 95 5
# description:	radosgw is an S3 HTTP REST gateway for the RADOS object store. It is \
#		implemented as a FastCGI module using libfcgi, and can be used in \
#		conjunction with any FastCGI capable web server.
# processname: radosgw
# config: /etc/ceph/ceph.conf

### BEGIN INIT INFO
# Provides:          radosgw
# Required-Start:    $remote_fs $named $network $time
# Required-Stop:     $remote_fs $named $network $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: radosgw RESTful rados gateway
# Description: radosgw RESTful rados gateway
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

# prefix for radosgw instances in ceph.conf
PREFIX='client.radosgw.'

LOGDIR='/var/log/radosgw'
RETVAL=0

start()
{
	for name in `ceph-conf --list-sections $PREFIX`;
	do
	    auto_start=`ceph-conf -n $name 'auto start'`
	    if [ "$auto_start" = "no" ] || [ "$auto_start" = "false" ] || [ "$auto_start" = "0" ]; then
		continue
	    fi

            # is the socket defined?  if it's not, this instance shouldn't run as a daemon.
	    rgw_socket=`ceph-conf -n $name 'rgw socket path'`
	    if [ -z "$rgw_socket" ]; then
		continue
	    fi

            # mapped to this host?
	    host=`ceph-conf -n $name host`
	    if [ -n "$host" ] && [ "$host" != `hostname` ]; then
		continue
	    fi

	    user=`ceph-conf -n $name user`
	    if [ -z "$user" ]; then
		continue
	    fi

	    log_file=`ceph-conf -n $name log_file`
	    if [ -n "$log_file" ] && [ ! -e "$log_file" ]; then
		touch "$log_file"
		chown $user $log_file
	    fi

            echo "Starting $name..."
	    start_daemon --expect-user $user -- radosgw  -- -n $name
	    RETVAL=$?
	done

	return $RETVAL
}

stop()
{
	stop_daemon -- radosgw
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading template
	stop_daemon -HUP -- radosgw
	RETVAL=$?
	return $RETVAL
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart|force-reload)
		restart
		;;
	status)
		status -- radosgw
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|status}"
		RETVAL=1
esac

exit $RETVAL
