#! /bin/sh
#
# ncsh		Connexion network configurator
#
# chkconfig: 2345 10 90
# description: Connexion network configurator

# 	Copyright (c) 2005-2007 ALT Linux, Peter V. Saveliev
#
# 	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 2 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

socket="/var/run/connexion/socket"
register="/var/run/connexion/register"
flag="/var/run/connexion/flag"

modules="/usr/share/connexion-modules/ncsh"
basedict="/usr/share/connexion-modules/basedict"
events="/usr/share/connexion-modules/events"

acl="/etc/connexion/acl"
config="/etc/connexion/config"

connexion="connexion"
connexion_cli="connexion-cli"
connexion_registrator="connexion-registrator"

udev="/etc/udev/rules.d/55-cx.rules"

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

clear_cx()
{
	$connexion_cli -abs $socket -e shutdown >/dev/null 2>&1
	rm -f $socket $register $flag $udev
	/sbin/udevcontrol reload_rules
}

start_cx()
{
	check_hostname
	clear_cx
	mkfifo $flag
	###
	# force PATH env variable
	###
	export PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH"
	nohup \
		$connexion \
			-w xshell:$modules \
			-W basedict:$basedict \
			-W events:$events \
			-s $socket \
			-r $register \
			-f $flag >/dev/null 2>&1 &

	cat $flag >/dev/null
	rm -f $flag

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


start()
{
	start_cx

	# FIXME: acl loading must be moved to "backup restore" module
	[ -e "$acl" ] && {
		cat "$acl" | $connexion_registrator -s $register >/dev/null 2>&1
	}

	$connexion_cli -abs $socket -e ".source $config; commit $1" >/dev/null 2>&1
	RETVAL=$?

	return $RETVAL
}

stop()
{
	[ "$1" = "resurrect" ] || {
		$connexion_cli -abs $socket -e "no configure;commit $1" >/dev/null 2>&1
	}
	clear_cx
	return 0
}

restart()
{
	stop
	start
}

reload()
{
	$connexion_cli -abs $socket -e "merge; .source $config; commit" >/dev/null 2>&1
}

update()
{
	stop noexec
	start noexec
}

case "$1" in
    start)
	# start the service
	start
	;;
    stop)
	# stop the service
	stop
	;;
    restart)
	# restart the service
	restart
	;;
    reload)
	# apply config changes
	reload
	;;
    update)
	# restart with 'commit noexec' -- for system update
	update
	;;
    *)
	msg_usage "${0##*/} {start|stop|restart|reload|update}"
	RETVAL=1
	;;
esac
exit $RETVAL
