#!/bin/bash
myname=${0##*/}
log() { logger -p user.info -t "${myname}[$$]" "$* $IFACE $ACTION"; }
IFACE=$1
ACTION=$2
CWD=$(pwd)
wlan_net=()
wlan_iface=()
cd /etc/sysconfig/network-scripts
interfaces=$(/bin/ls ifcfg* | \
	    LANG=C sed -e "$__sed_discard_ignored_files" \
		       -e '/\(ifcfg-lo$\|:\|ifcfg-.*-range\)/d' \
		       -e '/ifcfg-[A-Za-z0-9#\._-]\+$/ { s/^ifcfg-//g;s/[0-9]/ &/}' | \
	    LANG=C sort -k 1,1 -k 2n | \
	    LANG=C sed 's/ //')
	    for i in $interfaces ; do
                unset TYPE DEFROUTE DEVICE
                eval $(LANG=C grep -F "TYPE=" ifcfg-$i)
                eval $(LANG=C grep -F "DEFROUTE=" ifcfg-$i)
                eval $(LANG=C grep -F "DEVICE=" ifcfg-$i)
                if ! LANG=C grep -El "^DEFROUTE=['\"]?[Nn][Oo]['\"]?" ifcfg-$i >/dev/null 2>&1 && LANG=C grep -El "^TYPE=['\"]?[Wireless]['\"]?" ifcfg-$i >/dev/null 2>&1 ; then
                wlan_net+=(${DEVICE} ) && wlan_iface+=($wlan_iface $i)
                fi
done
sorted_wlan_net=($(LANG=C printf "%s\n" "${wlan_net[@]}" | sort -u)) && wlan_net="${sorted_wlan_net[@]}"
wlan_iface=($(LANG=C printf "%s\n" "${wlan_iface[@]}"))
case ${IFACE} in
    eth*|usb*|en*)
        case ${ACTION} in
            up)
                unset DEFROUTE
                eval $(LANG=C grep -F "DEFROUTE=" ifcfg-${IFACE})
                if ! LANG=C grep -El "^DEFROUTE=['\"]?[Nn][Oo]['\"]?" ifcfg-${IFACE} >/dev/null 2>&1 ; then
                iface_subnet=$(/sbin/ip route | grep ${IFACE} | awk '/default/ { print $3 }')
                log "the gateway of $IFACE is $iface_subnet - need to find the wlan(s) gateway too because.."
                for j in ${wlan_net} ; do
                sleep 5
                wlan_subnet=$(/sbin/ip route | grep $j | awk '/default/ { print $3 }')
                log "the gateway of $wlan_net is $wlan_subnet asked by action .."
                    if [ "$wlan_subnet" = "$iface_subnet" ]; then
                        for i in $wlan_iface ; do
                            unset NAME
                            eval $(LANG=C grep -F "NAME=" ifcfg-$i)
                            [ -z ${NAME} ] && NAME="$(/usr/bin/nmcli -f NAME c s -a | grep $i | awk '{print $0}')"
                            log "same gateway/subnet for $IFACE and $wlan_net ! Need too bring down $wlan_net.."
                            log "bringing down $wlan_net and keep .."
                            /usr/bin/nmcli connection down "${NAME}"
                        done
                    else
                    log " different gateway for $IFACE and $wlan_net no action needed by.."
                    fi
                    done
                fi
            ;;
            down)
                for i in $wlan_iface ; do
                unset NAME
                eval $(LANG=C grep -F "NAME=" ifcfg-$i)
                [ -z ${NAME} ] && NAME="$(/usr/bin/nmcli -f NAME c s -a | grep $i | awk '{print $0}')"
                log "Bringing up $wlan_iface because.."
                /usr/bin/nmcli connection up "${NAME}"
                done
            ;;
            esac
        ;;
esac
