#!/bin/sh
##-----------------------------------------------------------------------------
## cron2do_1                                                              0.6.1
##
## Creation:     04.05.2002  tc
## Last Update:  26.02.2003  tc
##
## Copyright (c) 2003 Thomas Creutz <thomas.creutz@epost.de>
##                    http://projekte.alientxc.de
##
## This program 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.
##-----------------------------------------------------------------------------


usage="cron2do_1 [Option]
--------------------------------------------------------------------------------
       [Option] =  Angabe was zu tun ist.
Format: setoff  = Setzt den Dialmode auf OFF
        seton   = Setzt den Dialmode auf MANUELL
        setauto = Setzt den Dialmode auf AUTO
        rmc     = Trennt die zweite Leitung bei einer Multiline-Verbindung(ISDN)
        addc    = Schaltet eine zweite Leitung zur aktuellen Verbindung(ISDN)
        dial	= Verbindung herstellen
        hangup  = Verbindung trennen
        def     = Wechselt zur automatischen Circuit-Route
--------------------------------------------------------------------------------
Dieser Skipt lscht den entsprechenden Eintrag (mit der dieser Zeit und Option)
nach der Ausfhrung in der imonc2do_conf und in der Cron-Tabelle!"

if [ $# -ne 1 ] ; then
    echo "$usage"
    exit 1
fi

case "$1" in
    "setoff") ;;
    "seton") ;;
    "setauto") ;;
    "rmc") ;;
    "addc") ;;
    "dial") ;;
    "hangup") ;;
    "def") ;;
    *) echo "$usage"
       exit 1
       ;;
esac

port=`cat /var/run/imond.port`

. /etc/imonc2do.conf
pfad=$IMONC2DO_HTTP_PATH

#-----------------------------------------------
# Befehle fr setoff
#--------------------------------------------------

if [ "$1" = "setoff" ] ; then
	{ echo "dialmode off"; echo "quit"; } | netcat -w 15 localhost $port

	echo Der Dialmode wurde auf OFF gestellt.

	if [ "$OPT_IMONC2DO_HTTP" = "yes" ]; then
		sed "/cron2do_1 setoff/d" /var/cron/tabs/root > /tmp/cron_root.tmp
		mv /tmp/cron_root.tmp /var/cron/tabs/root
	fi
	sed "/cron2do_1 setoff/d" $pfad/imonc2do_conf > /tmp/imonc2do.tmp
	mv /tmp/imonc2do.tmp $pfad/imonc2do_conf
fi

#-----------------------------------------------
# Befehle fr seton
#--------------------------------------------------

if [ "$1" = "seton" ] ; then
	{ echo "dialmode manual"; echo "quit"; } | netcat -w 15 localhost $port

	echo Der Dialmode wurde auf MANUAL gestellt.

	if [ "$OPT_IMONC2DO_HTTP" = "yes" ]; then
		sed "/cron2do_1 seton/d" $pfad/imonc2do_conf > /tmp/imonc2do.tmp
		mv /tmp/imonc2do.tmp $pfad/imonc2do_conf
	fi
	sed "/cron2do_1 seton/d" /var/cron/tabs/root > /tmp/cron_root.tmp
	mv /tmp/cron_root.tmp /var/cron/tabs/root
fi

#-----------------------------------------------
# Befehle fr setauto
#--------------------------------------------------

if [ "$1" = "setauto" ] ; then
	{ echo "dialmode auto"; echo "quit"; } | netcat -w 15 localhost $port

	echo Der Dialmode wurde auf AUTO gestellt.

	if [ "$OPT_IMONC2DO_HTTP" = "yes" ]; then
		sed "/cron2do_1 setauto/d" $pfad/imonc2do_conf > /tmp/imonc2do.tmp
		mv /tmp/imonc2do.tmp $pfad/imonc2do_conf
	fi
	sed "/cron2do_1 setauto/d" /var/cron/tabs/root > /tmp/cron_root.tmp
	mv /tmp/cron_root.tmp /var/cron/tabs/root
fi

#-----------------------------------------------
# Befehle fr rmc
#--------------------------------------------------

if [ "$1" = "rmc" ] ; then
	{ echo "removelink 0"; echo "quit"; } | netcat -w 15 localhost $port

	echo "Die zweite Leitung wurde getrennt."

	if [ "$OPT_IMONC2DO_HTTP" = "yes" ]; then
		sed "/cron2do_1 rmc/d" $pfad/imonc2do_conf > /tmp/imonc2do.tmp
		mv /tmp/imonc2do.tmp $pfad/imonc2do_conf
	fi
	sed "/cron2do_1 rmc/d" /var/cron/tabs/root > /tmp/cron_root.tmp
	mv /tmp/cron_root.tmp /var/cron/tabs/root
fi

#-----------------------------------------------
# Befehle fr addc
#--------------------------------------------------

if [ "$1" = "addc" ] ; then
	{ echo "addlink 0"; echo "quit"; } | netcat -w 15 localhost $port

	echo "Es wird versucht die zweite Leitung aufzubauen."

	if [ "$OPT_IMONC2DO_HTTP" = "yes" ]; then
		sed "/cron2do_1 addc/d" $pfad/imonc2do_conf > /tmp/imonc2do.tmp
		mv /tmp/imonc2do.tmp $pfad/imonc2do_conf
	fi
	sed "/cron2do_1 addc/d" /var/cron/tabs/root > /tmp/cron_root.tmp
	mv /tmp/cron_root.tmp /var/cron/tabs/root
fi

#-----------------------------------------------
# Befehle fr dial
#--------------------------------------------------

if [ "$1" = "dial" ] ; then
	{ echo "dial"; echo "quit"; } | netcat -w 15 localhost $port

	echo "Es wird versucht die Verbindung aufzubauen."

	if [ "$OPT_IMONC2DO_HTTP" = "yes" ]; then
		sed "/cron2do_1 addc/d" $pfad/imonc2do_conf > /tmp/imonc2do.tmp
		mv /tmp/imonc2do.tmp $pfad/imonc2do_conf
	fi
	sed "/cron2do_1 addc/d" /var/cron/tabs/root > /tmp/cron_root.tmp
	mv /tmp/cron_root.tmp /var/cron/tabs/root
fi

#-----------------------------------------------
# Befehle fr hangup
#--------------------------------------------------

if [ "$1" = "hangup" ] ; then
	{ echo "hangup"; echo "quit"; } | netcat -w 15 localhost $port

	echo "Die Verbindung wird beendet."

	if [ "$OPT_IMONC2DO_HTTP" = "yes" ]; then
		sed "/cron2do_1 hangup/d" $pfad/imonc2do_conf > /tmp/imonc2do.tmp
		mv /tmp/imonc2do.tmp $pfad/imonc2do_conf
	fi
	sed "/cron2do_1 hangup/d" /var/cron/tabs/root > /tmp/cron_root.tmp
	mv /tmp/cron_root.tmp /var/cron/tabs/root
fi

#-----------------------------------------------
# Befehle fr def
#--------------------------------------------------

if [ "$1" = "def" ] ; then
	{ echo "route 0"; echo "quit"; } | netcat -w 15 localhost $port

	echo "Die Default Route wurde auf Automatic gestellt."

	if [ "$OPT_IMONC2DO_HTTP" = "yes" ]; then
		sed "/cron2do_1 def/d" $pfad/imonc2do_conf > /tmp/imonc2do.tmp
		mv /tmp/imonc2do.tmp $pfad/imonc2do_conf
	fi
	sed "/cron2do_1 def/d" /var/cron/tabs/root > /tmp/cron_root.tmp
	mv /tmp/cron_root.tmp /var/cron/tabs/root
fi

# Neustart des Cron um die nderungen wirksam zu machen
killall cron
/usr/sbin/cron

exit 0
