#!/bin/bash
case $1 in
  1)
      if [ -f "/usr/local/solusvm/includes/trafficloader.php" ]; then
	      mv -f /usr/local/solusvm/includes/trafficloader.php /usr/local/bin/trafficloader.php
      fi
      eth=`/usr/bin/php /usr/local/bin/trafficloader.php --mode=all --debug=1 2>/dev/null | grep WITH | grep SRC | awk '{print $4}' | uniq`
      arr1=`/usr/bin/php /usr/local/bin/trafficloader.php --mode=all --debug=1 2>/dev/null | tr '.' ' ' | grep "ADDING CLASS TO " | grep "kvm" | awk '{print $4,$8}'`
      echo $arr1 >> /run/temp_speed
      sed -i 's/mbit/\n/g' /run/temp_speed
      arr2=`grep kvm /run/temp_speed | awk '{print $1,$2}'`
      arr5=`grep kvm /run/temp_speed | awk '{print $1}'`
      for arr6 in ${arr5[@]}
      do
        /sbin/tc qdisc del dev ${arr6}.0 ingress 2>/dev/null
        sleep 0.0005
      done
      /sbin/tc qdisc del dev $eth root 2>/dev/null
      if [ -f "/run/temp_speed" ]; then
        rm -f /run/temp_speed
      fi
      touch /run/speed_config.bak
      for arr3 in ${arr2[@]}
      do
        echo $arr3 >> /run/speed_config.bak
      done
      mv -f /run/speed_config.bak /run/speed_config
      arr4=`grep -v kvm /run/speed_config`
      for arr5 in ${arr4[@]}
      do
        echo $arr5 >> /run/limit_config.bak
      done
      sort /run/limit_config.bak | uniq > /run/limit_config
      rm -f /run/limit_config.bak
      rmmod ifb 2>/dev/null
      sleep 0.0005
      ;;
  2)  
      /usr/sbin/ip link set dev ifb${2} up 2>/dev/null
      sleep 0.0005
      /sbin/tc qdisc del dev ifb${2} root 2>/dev/null
      /sbin/tc qdisc add dev ifb${2} root 2>/dev/null
      /sbin/tc qdisc add dev ifb${2} root handle 1: htb default 1 2>/dev/null
      /sbin/tc class add dev ifb${2} parent 1: classid 1:1 htb rate ${3}mbit 2>/dev/null
      /sbin/tc qdisc add dev ifb${2} parent 1:1 sfq perturb 10 2>/dev/null
      sleep 0.0005
      ;;
  3)
      /sbin/tc qdisc del dev ${2}.0 root 2>/dev/null
      sleep 0.0005
      /sbin/tc qdisc del dev ${2}.0 ingress 2>/dev/null
      sleep 0.0005
      ;;
  4)
      arr2=`cat /run/ip`
      echo $arr2 > /run/ip
      ;;
  5)
      modprobe ifb numifbs=${2}
      ;;
  6)
      /sbin/tc qdisc add dev ${2}.0 ingress 2>/dev/null
      sleep 0.0005
      /sbin/tc filter add dev ${2}.0 parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb${3} 2>/dev/null
      sleep 0.0005
esac
