# Barix configuration interface
# (c) 2012 Barix AG
#
# meta-file for automatic config-file generation

# -------- create /etc/network/interfaces

# destination file for the configuration (absolute path)
DST_FILE[0]=/etc/network/interfaces

# template file located in /barix/config/templates/templates
TEMPLATE_FILE[0]=network.interfaces

# comments are prefixed with this character
COMMENT_PREFIX[0]="#"

# function to create dynamic content
DYNAMIC_CONTENT_FN[0]=create_network_interfaces

# function to create dynamic content
function create_network_interfaces()
{
    if cfg_string_compare network.eth0.proto "dhcp" ; then
        # DHCP configuration, get all auto
        echo "iface eth0 inet dhcp"
        if cfg_string_compare network.eth0.dhcpname "" ; then 
            true
        else
            echo -n "	hostname '"
            cfg_print_param network.eth0.dhcpname
            echo -n "'"
        fi
    else
        # DHCP configuration, set all static
        ipaddr=`cfg_print_param network.eth0.ipaddr`
        netmask=`cfg_print_param network.eth0.netmask`
        gateway=`cfg_print_param network.eth0.gateway`

        echo "iface eth0 inet static"
        echo "	address $ipaddr"
        echo "	netmask $netmask"
        if [ -n "$gateway" ]; then
            echo "	gateway $gateway"
        fi
    fi

    if cfg_string_compare network.wlan0.proto "dhcp" ; then
        echo ""
        echo "auto wlan0"
        # DHCP configuration, get all auto
        echo "    iface wlan0 inet dhcp"
        if cfg_string_compare network.wlan0.dhcpname "" ; then
            true
        else
            echo -n "       hostname '"
            cfg_print_param network.wlan0.dhcpname
            echo -n "'"
        fi
    elif cfg_string_compare network.wlan0.proto "static" ; then
        # DHCP configuration, set all static
        ipaddr=`cfg_print_param network.wlan0.ipaddr`
        netmask=`cfg_print_param network.wlan0.netmask`
        gateway=`cfg_print_param network.wlan0.gateway`

        echo ""
        echo "auto wlan0"
        echo "iface wlan0 inet static"
        echo "  address $ipaddr"
        echo "  netmask $netmask"
        if [ -n "$gateway" ]; then
                echo "  gateway $gateway"
        fi
    fi

    if [ -d /sys/class/net/eth1 ]; then
        echo ""
        echo "auto eth1"
        if cfg_string_compare network.eth1.proto "dhcp" ; then
            # DHCP configuration, get all auto
            echo "iface eth1 inet dhcp"
            if cfg_string_compare network.eth1.dhcpname "" ; then 
                true
            else
                echo -n "	hostname '"
                cfg_print_param network.eth1.dhcpname
                echo -n "'"
            fi
        else
            # DHCP configuration, set all static
            ipaddr=`cfg_print_param network.eth1.ipaddr`
            netmask=`cfg_print_param network.eth1.netmask`
            gateway=`cfg_print_param network.eth1.gateway`

            echo "iface eth1 inet static"
            echo "	address $ipaddr"
            echo "	netmask $netmask"
            if [ -n "$gateway" ]; then
                echo "	gateway $gateway"
            fi
        fi
    fi
}


# -------- create /etc/resolv.conf

# destination file for the configuration (absolute path)
DST_FILE[1]=/etc/resolv.conf

# template file located in /barix/config/templates/templates
TEMPLATE_FILE[1]=

# comments are prefixed with this character
COMMENT_PREFIX[1]="#"

# function to create dynamic content
DYNAMIC_CONTENT_FN[1]=create_resolv_conf

# function to create dynamic content
function create_resolv_conf()
{
    if cfg_string_compare network.eth0.dns_type "MANUAL" ; then
        dns1=`cfg_print_param network.eth0.dns1`
        dns2=`cfg_print_param network.eth0.dns2`

        if [ "X$dns1" != "X" ] ; then echo "nameserver $dns1" ; fi
        if [ "X$dns2" != "X" ] ; then echo "nameserver $dns2" ; fi
    fi

    # no action for DHCP
}

