#!/bin/sh
#
# @(#)@@SIMPLEVERSIONTAG@@
#
# Daily script to keep the IPv6 routing working
#

# If there is a global system configuration file, suck it in.
if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

#
# Default configuration variables
#
: ${daily_local_ipv6_refresh_enable:="NO"}
: ${daily_local_ipv6_refresh_flags="-n -w2 -q2 -m1 -I"}
: ${daily_local_ipv6_refresh_target=""}

rc=0

# Go on only if this script is enabled in the configuration
case "${daily_local_ipv6_refresh_enable}" in
[Yy][Ee][Ss])
    ;;
[Nn][Oo])
    exit 0
    ;;
*)
    echo "$0: WARNING: \$daily_local_ipv6_refresh_enable is not set properly - assuming \"NO\" has been set" 1>&2
    exit 0
    ;;
esac

if [ -z "${daily_local_ipv6_refresh_target}" ]; then
    daily_local_ipv6_refresh_target="$(/usr/sbin/sysrc -i -n ipv6_defaultrouter)"
fi

case "${daily_local_ipv6_refresh_target}" in
[Nn][Oo])
    # SKIP because no target or no static IPv6 router given
    echo "$0: WARNING: \$daily_local_ipv6_refresh_target is \`NO'" 1>&2
    ;;
'')
    # SKIP because this is an unresolvable router specification
    echo "$0: WARNING: \$ipv6_defaultrouter has an unexpected empty value" 1>&2
    ;;
*)
    #if type anticongestion >/dev/null 2>&1; then
    #    anticongestion_sleeptime=60
    #    anticongestion
    #fi

    # Wait up to 10 seconds to avoid router congestion
    /bin/sleep $(/usr/bin/jot -r 1 0 10)
    /usr/sbin/traceroute6 ${daily_local_ipv6_refresh_flags} "${daily_local_ipv6_refresh_target}"
    rc=$?
    ;;
esac

exit $rc
