#!/bin/sh
# tlp-rdw - network manager dispatcher hook:
#           enable/disable radios on ifup/ifdown
#
# Copyright (c) 2015 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2 or later.

# --- Constants
readonly LIBDIRS="/usr/lib/tlp-pm /usr/lib64/tlp-pm"
readonly LIBS="tlp-functions tlp-rf-func"

readonly RDW_NM_LOCK="rdw_nm"
readonly RDW_NM_LOCKTIME=2

# --- Locate and source libraries
for libdir in $LIBDIRS; do [ -d $libdir ] && break; done
[ -d $libdir ] || exit 0

for lib in $LIBS; do
    [ -f $libdir/$lib ] || exit 0
    . $libdir/$lib
done

# --- MAIN
read_defaults
check_tlp_enabled || exit 0
add_sbin2path

# Get args
iface="$1"
action="$2"
itype="unknown"

# Quit for invalid interfaces
[ -n "$iface" ] && [ "$iface" != "none" ] || exit 0

echo_debug "nm" "rdw_nm($iface).$action"
if [ -n "$addpath" ]; then
    echo_debug "path" "PATH=$oldpath[$addpath]"
else
    echo_debug "path" "PATH=$oldpath"
fi

# Quit if timed lock in progress
check_timed_lock $RDW_NM_LOCK && exit 0

# Determine interface type
if cmd_exists $NMCLI ; then
    # nmcli is available --> check if nmcli dev output matches interface
    itype="$($NMCLI dev | awk '$1 ~ /'$iface'/ { print $2; }')"

    if [ -z "$itype" ]; then
        # iface is not found in nmcli dev output: many WWAN devices have
        # different devices for control and the actual network connection
        # --> check if interface matches a WWAN device
        get_wwan_ifaces
        if wordinlist "$iface" "$wanifaces"; then
            itype="wwan"
        else
            itype="unknown"
        fi
    fi
    echo_debug "nm" "rdw_nm($iface).$action: type=$itype [nmcli]"
else
    # nmcli is not available
    echo_debug "nm" "rdw_nm($iface)$action.nmcli_not_available"
fi

case $action in
    up) # interface up, disable configured interfaces

        set_timed_lock $RDW_NM_LOCK $RDW_NM_LOCKTIME # lock rdw events

        case $itype in
            *ethernet)
                for dev in $DEVICES_TO_DISABLE_ON_LAN_CONNECT; do
                    [ -n "$dev" ] && device_switch $dev off
                done
                ;;

            *wireless|wifi)
                for dev in $DEVICES_TO_DISABLE_ON_WIFI_CONNECT; do
                    [ -n "$dev" ] && [ "$dev" != wifi ] && device_switch $dev off
                done
                ;;

            gsm|wwan)
                for dev in $DEVICES_TO_DISABLE_ON_WWAN_CONNECT; do
                    [ -n "$dev" ] && [ "$dev" != wwan ] && device_switch $dev off
                done
                ;;
        esac
        ;; # up

    down) # interface down, enable configured interfaces
        case $itype in
            *ethernet)
                for dev in $DEVICES_TO_ENABLE_ON_LAN_DISCONNECT; do
                    [ -n "$dev" ] && device_switch $dev on
                done
                ;;

            *wireless|wifi)
                for dev in $DEVICES_TO_ENABLE_ON_WIFI_DISCONNECT; do
                    [ -n "$dev" ] && [ "$dev" != wifi ] && device_switch $dev on
                done
                ;;

            gsm)
                for dev in $DEVICES_TO_ENABLE_ON_WWAN_DISCONNECT; do
                    [ -n "$dev" ] && [ "$dev" != wwan ] && device_switch $dev on
                done
                ;;
        esac
        ;; # down

esac

exit 0
