## -*- mode: shell-script; -*- 
##
## To be able to make changes to the part of configuration created
## from this configlet you need to copy this file to the directory
## fwbuilder/configlets/linux24/ in your home directory and modify it.
## Double "##" comments are removed during processing but single "#"
## comments are be retained and appear in the generated script. Empty
## lines are removed as well.  
##
## Configlets support simple macro language with these constructs:
## {{$var}} is variable expansion
## {{if var}} is conditional operator.
##

## The recent versions of DD-WRT V24 preSP2 Build 13064 and newer are
## missing modprobe and mktemp. This configlet does not use mktemp to
## create temporary file

# ============== ROUTING RULES ============== 

TMPDIRNAME="/tmp/.fwbuilder.tempdir.$$"
TMPFILENAME="$TMPDIRNAME/.fwbuilder.out"
(umask 077 && mkdir $TMPDIRNAME) || exit 1

#
# This function stops stdout redirection
# and sends previously saved output to terminal
restore_script_output()
{
  exec 1>&3 2>&1
  cat $TMPFILENAME
  rm -rf $TMPDIRNAME
}

# if any routing rule fails we do our best to prevent freezing the firewall
route_command_error()
{
  echo "Error: Routing rule $1 couldn't be activated"
  echo "Recovering previous routing configuration..."
  # delete current routing rules
  $IP route show | while read route ; do $IP route del $route ; done
  # restore old routing rules
  (IFS="
"; for route in $oldRoutes; do (IFS=' '; $IP route add $route); done)
  echo "...done"
  restore_script_output
  epilog_commands
  exit 1
}

# redirect output to prevent ssh session from stalling
exec 3>&1
exec 1> $TMPFILENAME
exec 2>&1

# store previous routing configuration (sort: 'via' GW has to be
# inserted after device routes)
oldRoutes=$($IP route show | sort -k 2)

echo "Deleting routing rules previously set by user space processes..."
$IP route show | grep -v {{$proto_filter}} | \
    while read route ; do $IP route del $route ; done
    
echo "Activating non-ecmp routing rules..."

