#!/bin/sh                                             
count=1                                                           
ifaceno=1                                                 
iface1=`uci get mwan3.m1.interface`                            
#iface2=`uci get mwan3.m2.interface`                    
iface2=`uci get mwan3.m3.interface`                         
#iface4=`uci get mwan3.m4.interface`                
iface3=`uci get mwan3.m5.interface` 
                    
while [ $count -lt 4 ]                                  
do                                                          
eval assign="\$iface$count"                             
# echo $assign                                            
if [ "$assign" != "pptp" ] && [ "$assign" != "openvpn" ]         
    then                                                             
    if [ $ifaceno -eq 1 ];                                   
        then                          
        priority1="$assign"                  
    elif [ $ifaceno -eq 2 ];                           
        then                                             
        priority2="$assign"                   
    elif [ $ifaceno -eq 3 ];         
        then                           
        priority3="$assign"                  
    elif [ $ifaceno -eq 4 ];                                  
        then                                                     
        priority4="$assign"                                      
    elif [ $ifaceno -eq 5 ];
        then                             
        priority5="$assign"             
    fi                               
    eval name="\$priority$ifaceno"                   
#              echo $name                                    
ifaceno=$(expr "$ifaceno" + 1)                           
fi                                                                                             
count=$(expr "$count" + 1)                                                                               
#       echo $count                                                                                              
done                                                                                                    

/usr/bin/logger -t services -p info "SERVICES $INTERFACE $ACTION "
/usr/bin/logger -t services -p info "SERVICES $priority1 $priority2 $priority3 $priority4 $priority5"
if [ "$ACTION" == "ifupdate" ]; then
    /usr/bin/logger -t services -p info "ifupdate occured for $INTERFACE. Will ignore"
    exit
fi
if [ "$ACTION" != "ifup" -a "$ACTION" != "ifdown" ]; then
    exit
fi
#  add in else case of no pptp running 
# ps | grep pppd | grep pptp | grep -v grep | awk '{print $1}' | xargs kill -9
server=`uci get network.pptp.server`                                   
if [ "$INTERFACE"  == "$priority1" ];                 
    then                                                                
    /etc/init.d/agents reload
    hwcfg=`cat /proc/device-tree/lantronix/hwcfg`
    if ! echo $hwcfg | grep -q "G526RP"; then
        /etc/init.d/gps reload
    fi
    #/etc/init.d/ntpcl reload                                        
    REASON="hotplug" /etc/init.d/serialinit reload
    if [ -f /etc/config/serial1 ]; then
            REASON="hotplug" /etc/init.d/serialinit2 reload
    fi
    if [ -f /etc/config/percepxion ]; then
            REASON="hotplug" /etc/init.d/percepxion reload
    fi
    /etc/init.d/d2sphere reload
    killall -9 dota.sh 
    #/etc/init.d/openvpn restart
elif [ "$INTERFACE"  == "$priority2" ];                           
    then                                                             
    wanstatus=`get_interface_status.sh get_"$priority1"_status`
    if [ "$wanstatus" == "" ] || [ $wanstatus == "offline" ];
        then                                                     
        /etc/init.d/agents reload
        hwcfg=`cat /proc/device-tree/lantronix/hwcfg`
        if ! echo $hwcfg | grep -q "G526RP"; then
               /etc/init.d/gps reload
        fi
        #/etc/init.d/ntpcl reload 
        REASON="hotplug" /etc/init.d/serialinit reload
        if [ -f /etc/config/serial1 ]; then
            REASON="hotplug" /etc/init.d/serialinit2 reload
        fi
        if [ -f /etc/config/percepxion ]; then
            REASON="hotplug" /etc/init.d/percepxion reload
        fi 
        /etc/init.d/d2sphere reload
        killall -9 dota.sh 
        #/etc/init.d/openvpn restart  
    fi                                                                             
elif [ "$INTERFACE"  == "$priority3" ];                                             
    then                                                                              
    wanstatus=`get_interface_status.sh get_"$priority1"_status`                
    if [ "$wanstatus" == "" ] || [ "$wanstatus" == "offline" ];                        
        then                                   
        wwanstatus=`get_interface_status.sh get_"$priority2"_status`
        if [ "$wwanstatus" == "" ] || [ "$wwanstatus" == "offline" ];                        
            then                                             
            /etc/init.d/agents reload
            hwcfg=`cat /proc/device-tree/lantronix/hwcfg`
            if ! echo $hwcfg | grep -q "G526RP"; then
                /etc/init.d/gps reload
            fi
            #/etc/init.d/ntpcl reload 
            REASON="hotplug" /etc/init.d/serialinit reload
            if [ -f /etc/config/serial1 ]; then
                REASON="hotplug" /etc/init.d/serialinit2 reload
            fi
            /etc/init.d/d2sphere reload                          
            killall -9 dota.sh 
            #/etc/init.d/openvpn restart
        fi                                                                               
    fi  
fi                                                                                   


