#!/bin/bash
USAGE="alias_sdn.sh up(default)|down|check"
[ "$1" == "-h" ] && echo "$USAGE" && exit

[  -z "$1" ] && echo "Task not found" && exit 1
TASK="$1"
IP_LIST=$2
[  -z "$2" ] && echo "IP List not found" && exit 1
SETUP_MODE=${SETUP_MODE:-alias}
INTERFACE=${INTERFACE:-eth2:1}
DEV_INTERFACE=${DEV_INTERFACE:-eth2}
NETMASK=${NETMASK:-255.255.255.0}

ROOT_INTERFACES=${ROOT_INTERFACES:-/etc/network/interfaces}
BACKUP_INTERFACES=${BACKUP_INTERFACES:-/etc/network/_interfaces}

_ping_ip_host() {
  [ -z "$1" ] && return 1
  local str_wait=""
  case $(uname -s) in 
    Darwin|darwin) str_wait="" ;;
    *) str_wait="-w2"
  esac 
  ping "$1" -c2 -q $str_wait >/dev/null 2>/dev/null
}
_add_interface() {
echo "
auto $INTERFACE
iface $INTERFACE inet static
address $IP
netmask $NETMASK
" >> "$ROOT_INTERFACES"
}
_add_as_alias() {
  if _ping_ip_host "$IP" ; then
    echo "$IP is alive" 
    return
  fi
  ip addr add "$IP"/24 dev "$DEV_INTERFACE" label "$INTERFACE"
}
_remove_as_alias() {
  if ! _ping_ip_host "$IP" ; then
    echo "$IP is not alive" 
    return 
  fi
  ip addr delete "$IP"/24 dev "$DEV_INTERFACE" label "$INTERFACE"
}
_add_as_system() {
  if _ping_ip_host "$IP" ; then
    echo "$IP is alive" 
    return
  fi
  local has_ip=""
  has_ip=$(grep "$IP" "$ROOT_INTERFACES") 
  if [ -z "$has_ip" ] ; then
     [ ! -r "$BACKUP_INTERFACES" ] && cp "$ROOT_INTERFACES" "$BACKUP_INTERFACES"
     _add_interface
   fi
}
_remove_as_system() {
  local has_ip=""
  has_ip=$(grep "$IP" "$ROOT_INTERFACES") 
  if [ -n "$has_ip" ] ; then
     [ -r "$BACKUP_INTERFACES" ] && cp "$BACKUP_INTERFACES" "$ROOT_INTERFACES" 
     has_ip=$(grep "$IP" "$ROOT_INTERFACES") 
     [ -n "$has_ip" ] && echo "Unable to remove $IP from $$ROOT_INTERFACES" && exit 1
  fi
}

_check_interface() {
  local ip_a
  #ifaces_data=$(ip a | grep "inet " | grep dynamic | sed 's/inet //g' | awk '{print $7":"$1}' | grep "$INTERFACE")
  ip_a=$(ip a | grep "inet " | grep "$INTERFACE" | awk '{print $2}' | cut -f1 -d"/" | grep "$IP")
  if [ "$IP" != "$ip_a" ] ; then 
     echo "$IP for $INTERFACE not found"
     IP_ACTIVE=""
  else
     echo "$IP active on $INTERFACE" 
     IP_ACTIVE="on"
  fi 
  if _ping_ip_host "$IP" ; then
    echo "$IP is alive" 
  fi
}

_restart_networking() {
  systemctl restart networing
}

_on_ip() {
  IP_ACTIVE=""
  _check_interface

  case "$TASK" in 
    up|u)  [ -n "$IP_ACTIVE" ] && return
      TASK="up"
      ;;
    down|d) [ -z "$IP_ACTIVE" ] && return
      TASK="down"
      ;; 
    check|c|status|s)
      return 
      ;; 
    ping|p|resp|r)
      if _ping_ip_host "$IP" ; then
        echo "$IP responding"
      else
        echo "$IP not responding"
      fi
      return 
      ;; 
    *) echo "Option $TASK unknown" 
        exit 1
  esac

  case "$SETUP_MODE" in
    system|sys)
      if [ "$TASK" == "up" ] ; then
        _add_as_system
      else 
        _remove_as_system
      fi
      _restart_networking
      _check_interface
      ;;
    alias|a)
      if [ "$TASK" == "up" ] ; then
        _add_as_alias
      else 
        _remove_as_alias
      fi
      _check_interface
      ;;
  esac
}

if [ -r "$IP_LIST" ] ; then 
  TARGET_IPS=$(grep -v "^#" "$IP_LIST")
else
  TARGET_IPS=$IP_LIST
fi
for it in $TARGET_IPS
do
  IP="$it"
  _on_ip 
done
