#!/bin/sh
# tlp-rdw - handle dock/undock events
#
# Copyright (c) 2012 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2.

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

# --- 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

# Get state
ddir=/sys/devices/platform/$1
type=$(cat $ddir/type)
docked=$(cat $ddir/docked)
get_power_state; onbat=$?

echo_debug "udev" "rdw_udev($*).$EVENT dev=$ddir type=$type docked=$docked onbat=$onbat"

case $EVENT in
    dock) # laptop was docked
    
        # enable configured radios
        for dev in $DEVICES_TO_ENABLE_ON_DOCK; do 
            [ -n "$dev" ] && device_on $dev
        done
        
        # disable configured radios
        for dev in $DEVICES_TO_DISABLE_ON_DOCK; do 
            [ -n "$dev" ] && device_off $dev
        done
        ;;
        
    undock) # laptop was undocked

        # enable configured radios
        for dev in $DEVICES_TO_ENABLE_ON_UNDOCK; do 
            [ -n "$dev" ] && device_on $dev
        done
        
        # disable configured radios
        for dev in $DEVICES_TO_DISABLE_ON_UNDOCK; do 
            [ -n "$dev" ] && device_off $dev
        done
        ;;
esac

exit 0
