#!/bin/bash

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

if [ $(ymc_is_chroot) -ne 0 ]; then
  echo "WARNING: $0 can not be run inside a chroot..."
  exit 0
fi


### Get own networks
networks=$(ymc_get_hostname_from_dns_txt $(hostname -s) networks.)

### Process all assigned networks
for network in $networks
do
  ### Get the netmask
  netmask=$(ymc_get_field_from_ifconfig "$network" "Mask:")

  ### Some vars
  active_aliases=''
  failed_ips=''

  ### Get own services in network
  services=$(fliwi-get-my-services --network=$network)

  ### Add service-ips to interface
  for service in $services
  do
    # Get the ip for the service
    ip=$(fliwi-get-ip-for-hostname $service)
    if [ $? -ne 0 ] || [ "$ip" == '' ]; then
      echo "WARNING: Could not get ip for service $service" 1>&2
      continue
    fi

    # Calculate the service-alias
    alias=$(ymc_calculate_service_alias $ip)
    if [ $? -ne 0 ] || \
       [ -z "$alias" ]; then
      echo "WARNING: Could not calculate service-alias out of ip $ip" 1>&2
      continue
    fi

    # Setup the service-ip
    if [ $(ymc_contains $alias $active_aliases) -eq 0 ]; then
      echo "INFO: Taking $network:$alias online with ip $ip" 1>&2
      ifconfig $network:$alias $ip netmask $netmask up 2>/dev/null
      if [ $? -ne 0 ]; then
        echo "WARNING: Failed to enable service $service with IP $ip - will try again later..." 1>&2
        failed_ips=$failed_ips' '$ip
      fi

      # Add the alias to the active ones
      active_aliases=$active_aliases' '$alias
    else
      echo "WARNING: Not taking $network:$alias online with ip $ip - DUPLICATE SERVICE" 1>&2
    fi
  done


  ### Get all currently configured service aliases
  configured_aliases=$(ymc_get_all_configured_service_aliases $network)

  if [ "$network" == "lan" ]; then
    ### Get all aliases, that might have been configured by older
    ### versions of fliwi-tools
    legacy_configured_aliases=$(ymc_get_all_configured_service_ids)
  else
    legacy_configured_aliases=""
  fi

  for configured_alias in $configured_aliases $legacy_configured_aliases
  do
    if [ $(ymc_contains $configured_alias $active_aliases) -eq 0 ]; then
      echo "INFO: Shutting down $network:$configured_alias" 1>&2
      ifconfig $network:$configured_alias down
    fi
  done

  ### Now we have shutdown old service-ips, try to enabled the failed ones...
  for failed_ip in $failed_ips
  do
    # Calculate the service-alias
    alias=$(ymc_calculate_service_alias $failed_ip)
    if [ $? -ne 0 ] || \
       [ -z "$alias" ]; then
      echo "WARNING: Could not calculate service-alias out of ip $failed_ip" 1>&2
      continue
    fi
    echo "INFO: Taking previously failed $network:$alias online with ip $failed_ip" 1>&2
    ifconfig $network:$alias $failed_ip netmask $netmask up
    if [ $? -ne 0 ]; then
      echo "ERROR: Failed to enable previously failed service $service with IP $failed_ip" 1>&2
    fi
  done

done

### Finally flush the local arp-cache (just to be sure)
ip neigh flush all

exit 0
