#!/bin/bash
#
# Copyright 2008 o3
# All rights reserved.
#


#echo $@ >> /tmp/.qos  2>> /tmp/.qos-

DIR=`dirname $0`

cd $DIR

CONFIG=/ips/setup/.config

. $CONFIG

###############################################################################

if [ "$1" = "--init" ]; then

date

. /ips/ip2/.functions

function allips {

  ALLIPS=

  E=$1
  LIST=`ifconfig |  grep $E | cut -f1-1 -d " "`

  for T in $LIST;  do
  
      IP=`/sbin/ifconfig $T 2> /dev/null | grep 'inet addr' | cut -f2-2 -d ':'  | cut -f1-1 -d ' '` 
      if [ $? != 0 ]; then 
        continue 
      fi	
      if [ "$IP" = "" ]; then 
        continue 
      fi	
      #echo $IP

      if [ "$ALLIPS" != "" ]; then 
         S=" " 
      else
         S=
      fi	
      ALLIPS="$ALLIPS$S$IP"
  
  done    
  
}


if [  -f /etc/pptpd.conf ]; then

 VPNS=`cat /etc/pptpd.conf | grep remoteip | grep -v '#' | cut -f2-2 -d ' ' 2>/dev/null`
 export VPNS
 #echo $VPNS; exit

fi


 for DEV in $LANDEV; do
    IP_=`/sbin/ifconfig $DEV 2> /dev/null`
    if [ $? != 0  ]; then
       LANDEV=
       export LANDEV
       echo "$DEV doesn't exist, (an)other LAN(s) disabled !"
       break    
    fi

    IP_=`/sbin/ifconfig $DEV | grep 'inet addr' | cut -f2-2 -d ':'  | cut -f1-1 -d ' '` 

    #echo $IP_ ; continue

    echo $IP_ > IP_$DEV
    eval IP_$DEV=$IP_
    export IP_$DEV


    MA_=`/sbin/ifconfig $DEV | grep 'inet addr' | cut -f4-4 -d ':'  | cut -f1-1 -d ' '` 

    #echo $MA_ ; exit

    echo $MA_ > MA_$DEV
    eval MA_$DEV=$MA_
    export MA_$DEV

    
 done

#echo "$ANYDEV->$DEVOUT $OURDEV->$DEVIN" ; exit

IP_MARKS=
s=
ALLMARKS=
for MA in $ALL; do
    if [ "$MA" = "$ADR" ]; then
       continue
    fi   
    
maybeali $MA

   if [ "$ALIIP" != ""  ]; then
         # nr > 254 can be a computer with IP ALIP
      IP_MARKS="$IP_MARKS _${ALIIP}_$MA"

      continue 
   else
      Z=$[ MA > 254 ]
      if [ "$Z" = "1" ]; then
        continue
      fi   
      if [ -f /ips/gro/c-$MA ]; then
      # class
        continue
      fi
      if [ -f /ips/gro/s-$MA ]; then
      # class
        continue
      fi
    fi

    ALLMARKS="$ALLMARKS$s$MA"
    s=" "
    
done

export IP_MARKS
export ALLMARKS
#echo $ALLMARKS; exit
#env | grep IP_MARKS ; exit

if [ "$QOS_BRIDGE" = "1" ]; then
   allips br0
else
   allips $ANYDEV
fi   
   
export ANYIPS="$ALLIPS"
#echo $ANYIPS; exit

echo "ALLMARKS $ALLMARKS"
echo "ANYIPS $ANYIPS"
echo "IP_MARKS $IP_MARKS"

fi

###############################################################################

ME=`basename $0`

export DOIT=1

if [ "$QOS_HTB" = "1"  ];  then

    if [ "$ME" = ".qos.out" ]; then

       exec ./.qos-htb.out "$@" 

    else

       exec ./.qos-htb.in  "$@"

    fi
    
    #safe sex
    exit 

fi

if [ "$QOS_HFSC" = "1"  ];  then

    if [ "$ME" = ".qos.out" ]; then

       exec ./.qos-hfsc.out "$@" 

    else

       exec ./.qos-hfsc.in  "$@"

    fi
    
    #safe sex
    exit 

fi




    if [ "$ME" = ".qos.out" ]; then

       exec ./.qos-cbq.out "$@" 

    else

       exec ./.qos-cbq.in  "$@"

    fi


