#!/bin/bash

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

ipRuleReadCMD="/sbin/ip rule"
ipRuleWriteCMD="/sbin/ip rule"
ymcScriptName="$(basename $0)"
ipRulesAddFile="/var/run/$ymcScriptName/ip_rule.add"
do_add=1
do_remove=1

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

run_mode=$1
if [ "$run_mode" == 'remove' ]; then
  do_add=0
elif [ "$run_mode" == 'dry-run' ]; then
  do_remove=0
  ipRuleWriteCMD="echo $ipRuleWriteCMD"
fi


if [ $do_remove -eq 1 ]; then
  echo "INFO: Removing exiting rules added by $ymcScriptName" 1>&2
  if [ -r "$ipRulesAddFile" ]; then
    while read ip_rule_line
    do
      $ipRuleWriteCMD del $ip_rule_line
    done < $ipRulesAddFile

    rm $ipRulesAddFile
  fi
fi


if [ $do_add -eq 1 ]; then
  if [ ! -d "$(dirname $ipRulesAddFile)" ]; then
    mkdir -p $(dirname $ipRulesAddFile)
  fi

  hostname=$(ymc_get_local_short_hostname)

  ### Get host based rules
  ymc_get_config_from_dns $hostname.machines.rules.routing
  if [ $? -eq 0 ]; then
    rules_to_lookup=$rules_to_lookup" "$(ymc_var_value $config_var_prefix)
  fi

  ### Get service based rules
  for service in $(fliwi-get-my-services | sed -r 's/-[0-9]+$//')
  do
    ymc_get_config_from_dns $service.services.rules.routing
    if [ $? -eq 0 ]; then
      rules_to_lookup=$rules_to_lookup" "$(ymc_var_value $config_var_prefix)
    fi
  done

  if [ -z "$rules_to_lookup" ]; then
    echo "NOTE: Can not find any ip rules for host '$hostname' or any assigned services..." 1>&2
    exit 0
  fi

  ### Load rules
  for rule_to_lookup in $rules_to_lookup
  do
    shorted_variables=''
    rule_name=$(echo $rule_to_lookup | cut -d '.' -f 1)
    rule_type=$(echo $rule_to_lookup | cut -d '.' -f 2)
    rule_interface=$(echo $rule_to_lookup | cut -d '.' -f 3)

    ymc_get_config_from_dns $rule_to_lookup.rules.routing
    for variable_name in $variables_set
    do
      shorted_variable_name=$(echo $variable_name | sed -r 's/^'$config_var_prefix'_//')
      shorted_variables=$shorted_variables' '$shorted_variable_name

      ymc_set_var_content "$shorted_variable_name" "$(ymc_var_value $variable_name)"
    done

    if [ -z "$table" ] || \
       [ -z "$from" ] || \
       [ -z "$from_netmask" ] || \
       [ -z "$to" ] || \
       [ -z "$to_netmask" ]; then
      echo "ERROR: Missing required definitions for configured ip rule '$rule_to_lookup' - SKIPPING..." 1>&2
      continue
    fi

    if [ -n "$iif" ]; then
      in_interface_option="iif $iif"
    else
      in_interface_option=""
    fi

    if [ -n "$oif" ]; then
      out_interface_option="oif $oif"
    else
      out_interface_option=""
    fi

    echo "INFO: Processing ip rule '$rule_name' " 1>&2
    set_to_add="from $from/$from_netmask to $to/$to_netmask $in_interface_option $out_interface_option table $table"
    $ipRuleWriteCMD add pref 32123 $set_to_add
    if [ $? -eq 0 ]; then
      echo $set_to_add >> $ipRulesAddFile
    fi

    ### empty out shorted and parametrized variables
    for shorted_variable in $shorted_variables
    do
      ymc_set_var_content $shorted_variable ""
    done
  done
fi

exit 0
