#!/bin/sh
# Copyright (C) 2007 Krzysztof Kozlowski
#   License: GNU General Public License version 2
#            http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program 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.
#
# Ostatnia zmiana: 22.10.2007
#
# Skrypt konfigurujacy trasy sieciowe po zmianie IP-ka przez dhcpcd


GATEWAY_IP="192.168.0.1"
#GATEWAY_IP="10.0.0.1"
PUBLIC_INTERFACE="eth0"

if [ $# -lt 2 ]; then
	logger -t dhcpcd.sh -p daemon.error "Wrong usage of dhcpcd.sh script"
	exit 1
fi

hostinfo="$1"
state="$2"

. "${hostinfo}"


if [ "${INTERFACE}" != "${PUBLIC_INTERFACE}" ]; then
	exit 0
fi

if [ "${DHCPSID}" != "${GATEWAY_IP}" ]; then
	logger -t dhcpcd.sh -p daemon.error "DHCP server IP (${DHCPSID}) is different then configured gateway IP (${GATEWAY_IP}). Possible bad configuration of dhdpcd.sh script or an rogue DHCP server (holy shit!). Stopping - fix this!"
	exit 2
fi

case "${state}" in
	up|new)
		logger -t dhcpcd.sh -p daemon.info "Adding routes to interface ${INTERFACE} (for IP ${IPADDR})..."
		if [ "$(ip route list exact ${GATEWAY_IP})" == "" ]; then
			logger -t dhcpcd.sh -p daemon.info "Adding route to ${GATEWAY_IP}..."
			ip route add ${GATEWAY_IP} dev ${PUBLIC_INTERFACE}
		fi
		if [ "$(ip route list exact 0.0.0.0/0)" == "" ]; then
			logger -t dhcpcd.sh -p daemon.info "Adding route to 0.0.0.0/0..."
			ip route add 0.0.0.0/0 via ${GATEWAY_IP}
		fi
		;;
	#down)
	#	echo "Stopping interface..."
	#	;;
	#*)
	#	echo "Bad command"
	#	;;
esac



