#!/bin/bash
#####################################################################################################################################################
#
# The MervisIDE passes configuration in two arguments:
#  $1: instance name (e.g. default)
#  $2: configuration parameters as a single string !!!
#
# e.g.: /etc/mervis/configure-enet default 'change wlan wlan0 disabled ssid "CAT S31" security wpa2personal password "faster 123" dhcp'
#
#####################################################################################################################################################

# We will evaluate the string as array to preserve all the parameters
eval "arr=( $2 )"
if [ ${#arr[@]} -ge 2 ]; then
    exec "$0" "$1" "${arr[@]}"
    #exec "$0" "$1" "${arr[0]}" "${arr[1]}" "${arr[2]}" "${arr[3]}" "${arr[4]}" "${arr[5]}" "${arr[6]}" "${arr[7]}" "${arr[8]}" "${arr[9]}" "${arr[10]}" "${arr[11]}"
fi

# include configuration
. "$(dirname "$0")/config.sh"

instance="$1"
shift
op="$1"
shift

if [ -z "$instance" ]; then
    echo "Instance is required" >&2
    exit 1
fi
echo "Instance: $instance"
echo "Operation: $op"

unset type name is_dhcp is_none address mask gateway ns security ssid password vlanid state

case "$op" in
    begin)
        . "$lib/net-start.sh"
        ;;
    add|change)
        . "$lib/net-parse.sh"
        ;;
    delete)
        # delete is ignored, all undefined interfaces will be destroyed on commit
        ;;
    route)
        . "$lib/net-route.sh"
        ;;
    resolver)
        . "$lib/net-resolver.sh"
        ;;
    commit)
        . "$lib/net-commit.sh"
        ;;
    *)
        # operation name is not recognized, exit forcefully
        echo "Unknown operation '$op'" >&2
        exit 1
        ;;
esac
