#!/bin/sh
# tlp - restore ThinkPad wwan state after suspend / hibernate
#
# Copyright (c) 2013 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2 or later.

. "${PM_FUNCTIONS}"

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

suspend_device () {
	local state
	
	state=$(cat $2)
    echo 0 > $2 # workaround: switch off before suspend
	savestate $1 $state
	echo_debug "pm" "49wwan.suspend wwan=$state"
}

resume_device () {
	state_exists $1 || return
	restorestate $1 > $2
	echo_debug "pm" "49wwan.resume wwan=$(cat $2)"
}

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

read_defaults || exit 0

[ "$TLP_ENABLE" = "1" ] || exit 0

echo_debug "pm" "49wwan.$1"

get_devc wwan
[ -n "$devc" ] || exit 0

case $1 in
	hibernate|suspend)
		suspend_device tp_wwan $devc
		;;
		
	thaw|resume)
		resume_device tp_wwan $devc
		;;
		
	*) exit $NA
		;;
esac

exit 0
