#!/bin/sh
#       Copyright (c) 2008 Eugene Prokopiev
#
#       This file is part of Connexion project.
#
#       Connexion 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
#       (at your option) any later version.
#
#       Connexion 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 Connexion; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#
#       This code is partialy based on initconf script from etcnet project - thanks to pilot@

echo -e "!"
echo -e "configure"
echo -e "\t!"
echo -e "\tnetwork"
echo -e "\t\t!"
echo -e "\t\tinterfaces"
echo -e "\t\t\t!"
echo -e "\t\t\tloopback"
echo -e "\t\t\t\tenable"

IP="/sbin/ip"

for IFACE in `$IP li | egrep '^[[:digit:]]+: ' | cut -d' ' -f2 | sed 's/://' | sed 's/@.*$//'`; do
    LINKTYPE=`$IP li sh dev $IFACE | fgrep 'link/' | sed 's/^ *link\///' | cut --delimiter=' ' --fields=1`
    if [ "$LINKTYPE" = "ether" ]; then
        if [ "${IFACE//[0-9]*/}" = "eth" ]; then
            IFNAME=`echo $IFACE | sed 's/eth//'`
        else
            IFNAME=$IFACE
        fi
        echo -e "\t\t\t!"
        echo -e "\t\t\tethernet "$IFNAME
        HWADDR=`$IP li sh dev $IFACE | fgrep 'link/ether' | sed 's/^ *link\/ether //' | cut --delimiter=' ' --fields=1`
        echo -e "\t\t\t\t!"
        echo -e "\t\t\t\thwaddr "$HWADDR
        IPV4ADDR=`$IP -4 ad sh dev $IFACE | grep '^ *inet' | sed 's/^ *inet //' | cut -d' ' -f1-1 | tr '\n' '\t'`
        echo -e "\t\t\t\taddress "$IPV4ADDR
    fi
done

echo -e "\t\t!"
echo -e "\t\trouting"
echo -e "\t\t\t!"
echo -e "\t\t\ttable"
echo -e "\t\t\t\t!"
$IP ro | grep -v 'scope link' | awk '{ print "\t\t\t\troute "$0}'
