#!/bin/sh
# Copyright (C) 2006-2010 OpenWrt.org

export HOTPLUG_TYPE="$1"

. /lib/functions.sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin
LOGNAME=root
USER=root
export PATH LOGNAME USER
export DEVICENAME="${DEVPATH##*/}"

if [ "$HOTPLUG_TYPE" != "net" -a "$HOTPLUG_TYPE" != "block" -a "$HOTPLUG_TYPE" != "tty" ]; then
    boot_log "<begin...> hotplug-call $(env)"
fi

[ \! -z "$1" -a -d /etc/hotplug.d/$1 ] && {
    res=$(echo $INTERFACE | grep "alias")
    if [[ $res != "" ]]; then
       
            if [ "$1" == balance ]; then
                echo "donothing" > /dev/console
            elif [ "$1" == iface ]; then
                #echo "iface start alias" > /dev/console
                #hp_start=$(date +%s)
                [ -f "/etc/hotplug.d/iface/00-netstate" ] && . /etc/hotplug.d/iface/00-netstate
                [ -f "/etc/hotplug.d/iface/01-zone" ] && . /etc/hotplug.d/iface/01-zone
                [ -f "/etc/hotplug.d/iface/02-split_access" ] && . /etc/hotplug.d/iface/02-split_access
                [ -f "/etc/hotplug.d/iface/02-vnet.sh" ] && . /etc/hotplug.d/iface/02-vnet.sh
                [ -f "/etc/hotplug.d/iface/1-lanhook.sh" ] && . /etc/hotplug.d/iface/1-lanhook.sh
                [ -f "/etc/hotplug.d/iface/21-nat.sh" ] && . /etc/hotplug.d/iface/21-nat.sh
                [ -f "/etc/hotplug.d/iface/29-static_route" ] && . /etc/hotplug.d/iface/29-static_route
                [ -f "/etc/hotplug.d/iface/99-hotplug_done" ] && . /etc/hotplug.d/iface/99-hotplug_done
                [ -f "/etc/hotplug.d/iface/99-z3g4g-connect" ] && . /etc/hotplug.d/iface/99-z3g4g-connect
                #hp_end=$(date +%s)
                #hp_cost=`expr $hp_end - $hp_start`
                #echo "HOTPLUG_TYPE $HOTPLUG_TYPE cost $hp_cost seconds " > /dev/console 
            else
                #hp_start=$(date +%s)
                    for script in $(ls /etc/hotplug.d/$1/* 2>&-); do (
                    #script_start=$(date +%s)
                    [ -f $script ] && . $script
                    #script_end=$(date +%s)
                    #time_cost=`expr $script_end - $script_start`
                    #echo "cost $time_cost seconds at $script | ACTION=$ACTION INTERFACE=$INTERFACE DEVICENAME=$DEVICENAME" > /dev/console
                    ); done
                #hp_end=$(date +%s)
                #hp_cost=`expr $hp_end - $hp_start`
                #echo "HOTPLUG_TYPE $HOTPLUG_TYPE cost $hp_cost seconds at $script | ACTION=$ACTION INTERFACE=$INTERFACE DEVICENAME=$DEVICENAME" > /dev/console 
            fi
    else
        local if_logical=`echo $INTERFACE |grep -E "^wan[0-9]+_eth_[0-9]+$"`
        if [ -n "$if_logical" ]; then
            if [ "$1" == wanhook ]; then
                [ -f "/etc/hotplug.d/iface/10-firewall.sh" ] && . /etc/hotplug.d/iface/10-firewall.sh
                [ -f "/etc/hotplug.d/iface/21-nat.sh" ] && . /etc/hotplug.d/iface/21-nat.sh
                [ -f "/etc/hotplug.d/iface/70-policy_route.sh" ] && . /etc/hotplug.d/iface/70-policy_route.sh
                [ -f "/etc/hotplug.d/iface/89-remote_mngt.sh" ] && . /etc/hotplug.d/iface/89-remote_mngt.sh
                [ -f "/etc/hotplug.d/iface/95-online.sh" ] && . /etc/hotplug.d/iface/95-online.sh
                [ -f "/etc/hotplug.d/iface/97-load_balance.sh" ] && . /etc/hotplug.d/iface/97-load_balance.sh
                [ -f "/etc/hotplug.d/iface/97-mwan3.sh" ] && . /etc/hotplug.d/iface/97-mwan3.sh
                [ -f "/etc/hotplug.d/iface/97-route.sh " ] && . /etc/hotplug.d/iface/97-route.sh              
                [ -f "/etc/hotplug.d/iface/99-wan_hook.sh" ] && . /etc/hotplug.d/iface/99-wan_hook.sh
            elif [ "$1" == iface ]; then
                [ -f "/etc/hotplug.d/iface/00-netstate" ] && . /etc/hotplug.d/iface/00-netstate
                [ -f "/etc/hotplug.d/iface/01-zone" ] && . /etc/hotplug.d/iface/01-zone
                [ -f "/etc/hotplug.d/iface/02-split_access" ] && . /etc/hotplug.d/iface/02-split_access
                [ -f "/etc/hotplug.d/iface/02-vnet.sh" ] && . /etc/hotplug.d/iface/02-vnet.sh
                [ -f "/etc/hotplug.d/iface/1-lanhook.sh" ] && . /etc/hotplug.d/iface/1-lanhook.sh
                [ -f "/etc/hotplug.d/iface/100-hwnat_ipstat.sh" ] && . /etc/hotplug.d/iface/100-hwnat_ipstat.sh
                [ -f "/etc/hotplug.d/iface/15-online.sh" ] && . /etc/hotplug.d/iface/15-online.sh
                [ -f "/etc/hotplug.d/iface/20-firewall" ] && . /etc/hotplug.d/iface/20-firewall
                [ -f "/etc/hotplug.d/iface/21-nat.sh" ] && . /etc/hotplug.d/iface/21-nat.sh
                [ -f "/etc/hotplug.d/iface/29-static_route" ] && . /etc/hotplug.d/iface/29-static_route
                [ -f "/etc/hotplug.d/iface/30-policy_route.sh" ] && . /etc/hotplug.d/iface/30-policy_route.sh
                [ -f "/etc/hotplug.d/iface/50-improxy" ] && . /etc/hotplug.d/iface/50-improxy
                [ -f "/etc/hotplug.d/iface/60-dnsmasq" ] && . /etc/hotplug.d/iface/60-dnsmasq
                [ -f "/etc/hotplug.d/iface/80-balance.sh" ] && . /etc/hotplug.d/iface/80-balance.sh
                [ -f "/etc/hotplug.d/iface/89-remote_mngt.sh" ] && . /etc/hotplug.d/iface/89-remote_mngt.sh
                [ -f "/etc/hotplug.d/iface/99-hotplug_done" ] && . /etc/hotplug.d/iface/99-hotplug_done
                [ -f "/etc/hotplug.d/iface/99-z3g4g-connect" ] && . /etc/hotplug.d/iface/99-z3g4g-connect
            fi
        else

        if [ "$1" == "vnet" ] && [ "$ACTION" == "notify" ]; then
            path_prefix="/etc/hotplug.d/vnet/"
            # Specify scripts to be executed by notify action in vnet dir
            vnet_notify_scripts="${path_prefix}60-dhcpsvnet.sh ${path_prefix}90-portal_mgmt ${path_prefix}98-ipsec.sh"
            scripts="$vnet_notify_scripts"
        elif [ "$HOTPLUG_TYPE" = "iface" -a "$DEVICE" = "lo" ]; then
            scripts="/etc/hotplug.d/iface/99-vnet.sh"
        else
            scripts=$(ls /etc/hotplug.d/$1/* 2>/dev/null)
        fi
        # hp_start=$(awk '{printf "%.3f\n", $1}' /proc/uptime)
            for script in $scripts; do (
            # script_start=$(awk '{printf "%.3f\n", $1}' /proc/uptime)
            if [ "$HOTPLUG_TYPE" != "net" -a "$HOTPLUG_TYPE" != "block" -a "$HOTPLUG_TYPE" != "tty" ]; then
                boot_log "<begin...> $script"
            fi
            [ -f $script ] && . $script
            if [ "$HOTPLUG_TYPE" != "net" -a "$HOTPLUG_TYPE" != "block" -a "$HOTPLUG_TYPE" != "tty" ]; then
                boot_log "<end.....> $script"
            fi
            # script_end=$(awk '{printf "%.3f\n", $1}' /proc/uptime)
            # time_cost=$(awk "BEGIN {print ($script_end - $script_start)}")
            # echo "cost $time_cost seconds at $script | ACTION=$ACTION INTERFACE=$INTERFACE DEVICENAME=$DEVICENAME" >> /tmp/hotplug
            ); done
        fi
        # hp_end=$(awk '{printf "%.3f\n", $1}' /proc/uptime)
        # hp_cost=$(awk "BEGIN {print ($hp_end - $hp_start)}")
        # echo "HOTPLUG_TYPE $HOTPLUG_TYPE cost $hp_cost seconds at dir:$1 | ACTION=$ACTION INTERFACE=$INTERFACE DEVICENAME=$DEVICENAME" >> /tmp/hotplug
    fi
}

if [ "$HOTPLUG_TYPE" != "net" -a "$HOTPLUG_TYPE" != "block" -a "$HOTPLUG_TYPE" != "tty" ]; then
    boot_log "<end.....> hotplug-call"
fi
