#!/bin/sh
#
#
# Copyright (c)2004 DuckCorp(tm)
#
#
#
# This file is part of EMIR 
#
# EMIR 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.
#
# EMIR 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 EMIR  if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

CONF="/etc/emir.conf"
SCRIPTDIR="/usr/share/emir"
SCRIPTDIR2="/var/lib/emir"
LOGDIR="/var/log/emir"

PHP="/usr/bin/php4"
IPTP="/sbin"
XMLLINT="/usr/bin/xmllint"

#--------------------------------------------------------------------------

PROGNAME="EMIR"
CMDNAME="emir-ctrl"
VERSION="0.1.0"

msg_version() {
	cat <<EOF
${PROGNAME} V${VERSION}
Copyright (c)2004 DuckCorp(tm)

EMIR 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.

EMIR 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 EMIR; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

EOF
}

msg_help() {
	cat <<EOF
Usage : ${CMDNAME} [OPTION | COMMAND]
Control Internet configuration with EMIR.

Options :
        -h --help    Display this help message
        -v --version Display the version number

See manpage for available commands.

EOF
}

status ()
{
	Z=$(${IPTP}/iptables-save 2>/dev/null | grep emir_configuration)
	if [[ $Z  ]]
	then
		return 0
	else
		return 1
	fi
}

check ()
{
	Z=$(${XMLLINT} --valid --noblanks --noout ${CONF} 2>&1)
	if [[ $Z ]]
	then
		echo "EMIR configuration is BAD ! (see '${LOGDIR}/conf_check.log' for more information)"
		echo "$Z" >${LOGDIR}/conf_check.log
		exit
	else
		echo "EMIR configuration is OK !"
		echo "All is OK !" >${LOGDIR}/conf_check.log
	fi
}

gen ()
{
	echo -n "Generating Configuration for EMIR..."
	${PHP} -q ${SCRIPTDIR}/emir_gen.php
	echo "done"
}

start ()
{
	echo -n "Starting EMIR..."
	${IPTP}/iptables-save >${SCRIPTDIR2}/old_ipv4.sav
	${IPTP}/ip6tables-save >${SCRIPTDIR2}/old_ipv6.sav
	Z=$(${SCRIPTDIR2}/emir_start.sh 2>&1)
	if [[ ${Z} ]]
	then
		echo "${Z}" >${LOGDIR}/start_check.log
		${IPTP}/iptables-restore < ${SCRIPTDIR2}/old_ipv4.sav
		${IPTP}/ip6tables-restore < ${SCRIPTDIR2}/old_ipv6.sav
		echo "failed ! (using old config) (see '${LOGDIR}/start_check.log' for more information)"
	else
		echo "done"
	fi
}

stop ()
{
	echo -n "Stoping EMIR..."
	Z=$(${SCRIPTDIR2}/emir_stop.sh 2>&1)
	if [[ ${Z} ]]
	then
		echo "${Z}" >${LOGDIR}/stop_check.log
		echo "failed ! (see '${LOGDIR}/stop_check.log' for more information)"
	else
		echo "done"
	fi
}



case "$1" in
  --help|-h)
	msg_help
	exit 0
	;;
  --version|-v)
	msg_version
	exit 0
	;;
  start)
  	if status
	then
		echo "Starting EMIR... already started."
	else
		check
		gen
  		start
	fi
	;;
  stop)
  	if status
	then
  		stop
	else
		echo "Stoping EMIR... already stopped."
	fi
	;;
  restart)
  	if status
	then
		check
		gen
  		stop
		start
	else
		echo "EMIR not started."
		check
		gen
		start
	fi
	;;
  status)
  	if status
	then
		echo "EMIR started."
	else
		echo "EMIR stoped."
	fi
	;;
  gen)
	check
  	gen
	;;
  check)
  	check
	;;
  *)
	msg_help
    ;;
esac

