#! /bin/sh
#
# connexion		Connexion framework
#
# chkconfig: 2345 10 90
# description: Connexion framework

# 	Copyright (c) 2008 ALT Linux Team, Eugene Prokopiev
#
# 	This file is part of Connexion project.
#
# 	Connexion is free software; you can redistribute it and/or modify
# 	it under the terms of the GNU General Public License as published by
# 	the Free Software Foundation; either version 3 of the License, or
# 	(at your option) any later version.
#
# 	Connexion is distributed in the hope that it will be useful,
# 	but WITHOUT ANY WARRANTY; without even the implied warranty of
# 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 	GNU General Public License for more details.
#
# 	You should have received a copy of the GNU General Public License
# 	along with Connexion; if not, write to the Free Software
# 	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

WITHOUT_RC_COMPAT=1

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

conf="/etc/connexion"
run="/var/run/connexion"

check_hostname()
{
	HN=`hostname`
	[ -z "$HN" ] && { HN=localhost; hostname $HN; }
	IP=`cat /etc/hosts | grep "$HN" | awk '{print $1}'`
	[ -z "$IP" ] && echo "127.0.0.1 $HN" >>/etc/hosts
}

clear_cx()
{
	connexion-cli -abs $run/$1-socket -e shutdown >/dev/null 2>&1
}

start_cx()
{
	####
	# ensure environment variables
	####
	export PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH"
	export CX_SOCKET=$run/$1-socket
	export CX_CONFIG=$conf/$1/tree

	[ -f $conf/$1/cmd ] && cmd=`cat $conf/$1/cmd`
	connexion $cmd -l /var/log/connexion-$1 -s $run/$1-socket

	chmod 660 $run/$1-socket
	chgrp netadmin $run/$1-socket 2>/dev/null ||:
}


start()
{
	echo -n "Starting connexion instance : "$1

	start_cx $1

	connexion-cli -abs $run/$1-socket -e ".source $conf/$1/tree; commit" >/dev/null 2>&1
	RETVAL=$?
	
	if [ $RETVAL = 0 ]; then
	    success
	else
	    failure
	fi
	
	echo

	return $RETVAL
}

stop()
{
	echo -n "Stopping connexion instance : "$1
	
	connexion-cli -abs $run/$1-socket -e "no configure;commit" >/dev/null 2>&1
	clear_cx $1
	
	RETVAL=$?
	
	if [ $RETVAL = 0 ]; then
	    success
	else
	    failure
	fi
	
	echo
	
	return $RETVAL
}

restart()
{
	stop $1
	start $1
}

reload()
{
	echo -n "Reloading connexion instance : "$1
	connexion-cli -abs $run/$1-socket -e "merge; .source $conf/$1/tree; commit" >/dev/null 2>&1
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
	    success
	else
	    failure
	fi
	echo
}

case "$1" in
    start)
	# start the service
	check_hostname
	while read instance; do
	   start $instance
	done < /etc/sysconfig/connexion
	;;
    stop)
	# stop the service
	while read instance; do
	   stop $instance
	done < /etc/sysconfig/connexion
	;;
    restart)
	# restart the service
	while read instance; do
	   restart $instance
	done < /etc/sysconfig/connexion
	;;
    reload)
	# apply config changes
	while read instance; do
	   reload $instance
	done < /etc/sysconfig/connexion
	;;
    *)
	msg_usage "${0##*/} {start|stop|restart|reload|update}"
	RETVAL=1
	;;
esac
exit $RETVAL
