#!/bin/bash

### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash

if [ -z "$IFACE" ]; then
  ### We do not want to break networking in this script...
  echo "W: Can not setup routing for undefined interface..." 1>&2
  exit 0
fi

# For now only deal with IPv4 in here
if [ "$ADDRFAM" != "inet" ]; then
  exit 0
fi

if [ "$IFACE" = "lo" ]; then
  ### Update the local virtual IPs for interface with name 'lo'
  if [ -x "/usr/sbin/fliwi-update-virtual-ips" ]; then
    /usr/sbin/fliwi-update-virtual-ips
  else
    echo "W: Not updating virtual-IPs for Fliwi!" 1>&2
  fi
else
  ### Update the ARPing settings for any other interface if needed
  if [ $(ymc_contains $IFACE $(ymc_get_real_interfaces)) -eq 1 ]; then
    ### ARP-Config
    echo "# Dynamic sysctl.conf(5) file for sysctl(8) generated by $(basename $0)" > /etc/sysctl.d/fliwi-sane-virtual-ips-$IFACE.conf
    echo "#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN" >> /etc/sysctl.d/fliwi-sane-virtual-ips-$IFACE.conf


    # When an arp request is received on $IFACE, only respond if that address is
    # configured on $IFACE. In particular, do not respond if the address is
    # configured on lo
    echo "net.ipv4.conf.$IFACE.arp_ignore=1" >> /etc/sysctl.d/fliwi-sane-virtual-ips-$IFACE.conf

    # When making an ARP request sent through $IFACE, always use an address that
    # is configured on $IFACE as the source address of the ARP request.  If this
    # is not set, and packets are being sent out $IFACE for an address that is on
    # lo, and an arp request is required, then the address on lo will be used.
    # As the source IP address of arp requests is entered into the ARP cache on
    # the destination, it has the effect of announcing this address.  This is
    # not desirable in this case as adresses on lo on the real-servers should
    # be announced only by the linux-director.
    echo "net.ipv4.conf.$IFACE.arp_announce=2" >> /etc/sysctl.d/fliwi-sane-virtual-ips-$IFACE.conf

    ### Reload sysctl
    /etc/init.d/procps restart
  fi
fi

### We want manual configured interfaces to not have an IP...
if [ -n "$METHOD" ] &&\
   [ "$METHOD" = "manual" ]; then
  echo "I: Removing possibly existing IPs on interface '$IFACE' as its method is '$METHOD'." 1>&2
  ifconfig $IFACE 0.0.0.0 up
  ip -family inet6 addr flush dev $IFACE
fi

### Update the local service IPs for interface with name 'lan'
if [ "$IFACE" = "lan" ]; then
  if [ -x "/usr/sbin/fliwi-update-service-ips" ]; then
    /usr/sbin/fliwi-update-service-ips
  else
    echo "W: Not updating service-IPs for Fliwi!" 1>&2
  fi
fi

if [ "$METHOD" != "manual" ] &&
   [ "$IFACE" != "lo" ]; then
  my_hostname=$(ymc_get_local_short_hostname)
  ymc_get_config_from_dns $my_hostname".network"
  if [ $? -ne 0 ]; then
    ### We do not want to break networking in this script...
    echo "W: Failed to setup routing for $IFACE..." 1>&2
    exit 0
  fi

  var_hostname=$(echo $my_hostname | sed -r 's/\./_dot_/g' | sed -r 's/-/_dash_/g')
  route_var=$var_hostname"_dot_network_"$IFACE"_routes"
  route_names=$(ymc_var_value $route_var)

  for route_name in $route_names
  do
    var_route_name=$(echo $route_name | sed -r 's/\./_dot_/g' | sed -r 's/-/_dash_/g')
    ymc_get_config_from_dns "$route_name.routes"
    type=$(ymc_var_value $var_route_name'_dot_routes_type')
    table=$(ymc_var_value $var_route_name'_dot_routes_table')
    target=$(ymc_var_value $var_route_name'_dot_routes_target')
    netmask=$(ymc_var_value $var_route_name'_dot_routes_netmask')
    gateway=$(ymc_var_value $var_route_name'_dot_routes_gateway')
    metric=$(ymc_var_value $var_route_name'_dot_routes_metric')

    if [ -n "$type" ] && \
       [ -n "$target" ] && \
       [ -n "$type" ] && \
       [ -n "$gateway" ] && \
       [ -n "$metric" ]; then
      if [ -n "$table" ]; then
        table_option="table $table"
        table_via_hint="in table $table"
      else
        table_option=""
        table_via_hint=""
      fi
      echo -n "I: Adding route to $target/$netmask via $gateway with metric $metric $table_via_hint for dev $IFACE..." 1>&2

      if [ "$gateway" != "0.0.0.0" ]; then
        gw_option="via $gateway"
      else
        gw_option=""
      fi

      ip route add $target/$netmask $gw_option dev $IFACE metric $metric $table_option 1>/dev/null 2>/dev/null
      route_exit_status=$?
      if [ $route_exit_status -eq 0 ]; then
        echo "Done" 1>&2
      else
        echo "FAILED" 1>&2
      fi
    fi
  done
fi

exit 0
