#!/bin/sh /etc/rc.common

######################################################################
#
# Function  : Run adbyby in RAM
#
# Creater   : ywb94@qq.com, 2017-02-16
#
# Copyright : yushi studio
#
######################################################################

START=80


start() {
/etc/init.d/adbyby_ram enabled
if [ $? == 1 ]; then
    return
fi
#ظ
icount=`ps -w|grep adbyby.sh|grep -v grep|wc -l`
icount2=`netstat -nautp|grep adbyby|grep -v grep|wc -l`

if [ $icount = 0 -a $icount2 = 0 ] ;then
/usr/bin/adbyby.sh &
fi

}

stop() {
#ȷǽ
i=0
icount=`iptables -t nat -L|grep "ports 8118"|wc -l`  
while [ $i -le 10  -a $icount -gt 1 ]  
do 
let i=i+1
iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8118
icount=`iptables -t nat -L|grep "ports 8118"|wc -l`  
done


if [ -f /tmp/adbyby/bin/stopadbb ] ;then
cd /tmp/adbyby/bin
./stopadbb 2>/dev/null
fi

icount=`ps -w|grep adbyby.sh|grep -v grep|wc -l`
if [ $icount -gt 0 ] ;then
mid=`ps -w|grep adbyby.sh|grep -v grep|awk '{print $1}'`
kill -9 $mid
fi
}

restart() {
  stop
  start
}
