#!/bin/sh

set -e

# Read the KEY=value lines omr-admin writes for this user without running the
# file as shell code: it carries values a router sent through the API, and a
# `. file` as root would run anything one of them smuggled in. Only the keys
# given are set, and only to a value that can be an address, a number or a
# name, which every one of them is.
read_conf() {
        conf="$1"
        shift
        while IFS= read -r line || [ -n "$line" ]; do
                key="${line%%=*}"
                [ "$key" = "$line" ] && continue
                case "$key" in
                        ""|*[!A-Z0-9_]*) continue ;;
                esac
                case " $* " in
                        *" $key "*) ;;
                        *) continue ;;
                esac
                value="${line#*=}"
                case "$value" in
                        *[!A-Za-z0-9:./_-]*)
                                echo "$(basename "$0"): ignoring invalid $key in $conf" >&2
                                continue
                                ;;
                esac
                eval "$key=\$value"
        done < "$conf"
}

if [ ! -f "$2" ]; then
        echo "usage: $(basename "$0") start FILE"
        exit 0
fi

read_conf "$(readlink -f "$2")" LOCALIP REMOTEIP LOCALIP6 REMOTEIP6 ULA

DEV="omr-6in4-$(basename "$2")"

if [ "$1" = "start" ]; then
        [ -n "$(ip tunnel show ${DEV})" ] && ip tunnel del ${DEV} 2>&1 >/dev/null
        ip tunnel add ${DEV} mode sit remote ${REMOTEIP} local ${LOCALIP}
        ip -6 addr add ${LOCALIP6} dev ${DEV}
        ip link set ${DEV} up
        [ -n "$ULA" ] && [ "$ULA" != "auto" ] && ip route replace ${ULA} via $(echo ${REMOTEIP6} | cut -d/ -f1) dev ${DEV}
fi
if [ "$1" = "stop" ]; then
        ip tunnel del ${DEV}
fi
