#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2018 Volker Theile
#
# OpenMediaVault 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
# any later version.
#
# OpenMediaVault 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 OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_ROUTE_CONFIG=${OMV_ROUTE_CONFIG:-"/etc/network/if-up.d/openmediavault-route"}

mkconf() {
	xmlstarlet sel -t \
	  -o "#!/bin/sh" -n \
	  -o "# This configuration file is auto-generated." -n \
	  -o "# WARNING: Do not edit this file, your changes will be lost." -n \
	  -o "[ \"\$IFACE\" != \"lo\" ] || exit 0" -n \
	  -m "//services/staticroutes/route" \
		-v "concat('ip route replace ',network,' via ',gateway)" \
		-i "string-length(dev) > 0" -v "concat(' dev ',dev)" -b \
		-n \
	  -b \
	  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_ROUTE_CONFIG}
	chmod 755 ${OMV_ROUTE_CONFIG}
}

case "$1" in
	mkconf|*)
		mkconf
		;;
esac
