#!/bin/sh -x
#$Id: qnx.sh,v 1.6 2001/11/07 16:54:41 acormier Exp $
# 
#Modified from netbsd.sh
#

LANGUAGE=C

if [ -z $TSP_VERBOSE ]; then
   TSP_VERBOSE=0
fi

KillProcess()
{
   if [ ! -z $TSP_VERBOSE ]; then
      if [ $TSP_VERBOSE -ge 2 ]; then
         echo killing $*
      fi
   fi
   PID=`pidin | grep $1 | grep -v grep | awk '{ print $1;}'`
   echo $PID
   if [ ! -z $PID ]; then
      kill $PID
   fi
}

Display()
{
   if [ -z $TSP_VERBOSE ]; then
      return;
   fi
   if [ $TSP_VERBOSE -lt $1 ]; then
      return;
   fi
   shift
   echo $*
}

Exec()
{
   if [ ! -z $TSP_VERBOSE ]; then
      if [ $TSP_VERBOSE -ge 2 ]; then
         echo $*
      fi
   fi
   $* # Execute command
   if [ $? -ne 0 ]; then
      echo "Error while executing $1"
      echo "   Command: $*"
      exit 1
   fi
}

ExecNoCheck()
{
   if [ ! -z $TSP_VERBOSE ]; then
      if [ $TSP_VERBOSE -ge 2 ]; then
         echo $*
      fi
   fi
   $* # Execute command
}

# Programs localization 

Display 1 "--- Start of configuration script. ---"
Display 1 "Script: " `basename $0`

ifconfig=/sbin/ifconfig
route=/sbin/route
rtadvd=/usr/sbin/rtadvd
sysctl=/sbin/sysctl

rtadvdconfigfile=$TSP_HOME_DIR/rtadvd.conf

if [ -z $TSP_HOME_DIR ]; then
   echo "TSP_HOME_DIR variable not specified!;"
   exit 1
fi

if [ ! -d $TSP_HOME_DIR ]; then
   echo "Error : directory $TSP_HOME_DIR does not exist"
   exit 1
fi
#

if [ -z $TSP_HOST_TYPE ]; then
   echo Error: TSP_HOST_TYPE not defined.
   exit 1
fi

#change to upper case
TSP_HOST_TYPE=`echo $TSP_HOST_TYPE | tr [a-z] [A-Z]`

if [ X"${TSP_HOST_TYPE}" = X"HOST" ] || [ X"${TSP_HOST_TYPE}" = X"ROUTER" ]; then
   #
   # Configured tunnel config (IPv4)
   Display 1 Setting up interface $TSP_TUNNEL_INTERFACE
# Delete first the any previous tunnel

   ExecNoCheck $ifconfig $TSP_TUNNEL_INTERFACE deletetunnel
   Exec $ifconfig $TSP_TUNNEL_INTERFACE tunnel $TSP_CLIENT_ADDRESS_IPV4 $TSP_SERVER_ADDRESS_IPV4

   #
   # Configured tunnel config (IPv6) 

   # Looking if the interface has already an IPv6 configuration
   ipv6address=`$ifconfig $TSP_TUNNEL_INTERFACE inet6 | grep 'inet6 3ffe:b80' | head -1 | awk '{print $2}'`

   if [ X"$ipv6address" != X"" ];then 
      # if there is a previous IPv6 address ,then delete it first, then apply the config
      Exec $ifconfig $TSP_TUNNEL_INTERFACE inet6 $ipv6address delete
   fi

   Exec $ifconfig $TSP_TUNNEL_INTERFACE inet6 $TSP_CLIENT_ADDRESS_IPV6 $TSP_SERVER_ADDRESS_IPV6 prefixlen $TSP_TUNNEL_PREFIXLEN alias

   # 
   # Default route  
   Display 1 Adding default route to $TSP_SERVER_ADDRESS_IPV6

   # Delete first any default IPv6 route
   ExecNoCheck $route delete -inet6 default
   Exec $route add -inet6 default $TSP_SERVER_ADDRESS_IPV6
fi

# Router configuration if required
if [ X"${TSP_HOST_TYPE}" = X"ROUTER" ]; then
   Display 1 "Router configuration"
   Display 1 "Kernel setup"

   Display 1 "Adding prefix to $TSP_HOME_INTERFACE"
   first_addr="1"   # network interface could use the first address of the delegated prefix                  
   Exec $ifconfig $TSP_HOME_INTERFACE inet6 $TSP_PREFIX:$first_addr::1 prefixlen 64
   Exec $sysctl -w net.inet6.ip6.forwarding=1     # ipv6_forwarding enabled
   Exec $sysctl -w net.inet6.ip6.accept_rtadv=0   # routed must disable any router advertisement incoming
#   Exec $route add -inet6 $TSP_PREFIX::  -prefixlen $TSP_PREFIXLEN ::1
   # Router advertisement configuration 
   Display 1 Create new $rtadvdconfigfile
   echo "##### rtadvd.conf made by TSP ####" > "$rtadvdconfigfile"
   echo "default:\\ " >> "$rtadvdconfigfile"
   echo "       :raflags#0:rltime#3600:\\" >> "$rtadvdconfigfile"
   echo "       :pinfoflags#64:vltime#360000:pltime#360000:mtu#1500:" >> "$rtadvdconfigfile"
   echo "ether:\\" >> "$rtadvdconfigfile"
   echo "       :mtu#1280:tc=default:" >> "$rtadvdconfigfile"
   echo "# interfaces." >> "$rtadvdconfigfile"
   echo "$TSP_HOME_INTERFACE:\\" >> "$rtadvdconfigfile"
   echo "       :addrs#1:\\" >> "$rtadvdconfigfile"
   echo '       :addr="'$TSP_PREFIX':0001::":prefixlen#64:tc=ether:' >> "$rtadvdconfigfile" 

   if [ -f $rtadvdconfigfile ]; then
      KillProcess $rtadvdconfigfilename
      Exec $rtadvd -c $rtadvdconfigfile $TSP_HOME_INTERFACE  
   else
      echo "Error : file $rtadvdconfigfile not found"
      exit 1
   fi
fi

Display 1 "--- End of configuration script. ---"

exit 0
