#!/bin/bash
set -e

runcfg=/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper
runop=/opt/vyatta/bin/vyatta-op-cmd-wrapper

$runcfg begin

$runcfg delete service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}}

$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    start {{host.data.dhcp.start}} \
    stop {{host.data.dhcp.stop}} \

$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    default-router {{host.data.dhcp.default_router}}

$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    dns-server {{host.data.dhcp.dns_server}}

$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    bootfile-server {{host.data.dhcp.bootfile_server}}

$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    subnet-parameters "{{host.data.dhcp.subnet_parameters}}"

$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    bootfile-name {{host.data.dhcp.bootfile_name}}

{% for machine in host.data.dhcp.machines %}
$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    static-mapping {{machine.hostname}}
$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    static-mapping {{machine.hostname}} \
    ip-address {{machine.provisioning_ip}}
$runcfg set service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    subnet {{host.data.dhcp.subnet}} \
    static-mapping {{machine.hostname}} \
    mac-address {{machine.mac_address}}
{% endfor %}

$runcfg delete service dhcp-server \
    shared-network-name {{host.data.dhcp.shared_network_name}} \
    disable

$runcfg commit
$runcfg save
$runcfg end
