#!/bin/sh
# tlp - display power save and usb autosuspend status
#
# Copyright (c) 2013 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 TLPUSB=tlp-usblist
readonly TLPPCI=tlp-pcilist

readonly SMARTCTL=smartctl
readonly LSBREL=lsb_release

readonly ASPM=/sys/module/pcie_aspm/parameters/policy
readonly NMIWD=/proc/sys/kernel/nmi_watchdog
readonly TPACPI=/sys/devices/platform/thinkpad_acpi


readonly IBMTHERMAL=/proc/acpi/ibm/thermal
readonly CORETEMP='/sys/devices/platform/coretemp.0'
readonly IBMFAN=/proc/acpi/ibm/fan
readonly HWMONFAN='/sys/class/hwmon/hwmon*/device'

readonly DEBUGLOG=/var/log/debug

# --- Variables

nodebug=1

needs_root_priv=
show_all=1
show_bat=0
show_conf=0
show_rfkill=0
show_temp=0
show_trace=0

# --- Functions

printparm () { # $1: format, $2: file, $3: namsg, $4: cutoff
    local format="$1"
    local sysf="$2"    local namsg="$3"
    local cutoff="$4"
    local val=""

    if [ -f $sysf ]; then
        # sysfile exists
        val=$(cat $sysf 2> /dev/null)
        if [ $? = 0 ]; then
            # sysfile read successful
            if [ -n "$cutoff" ]; then
                val=${val%$cutoff}
            fi
        fi
    fi

    if [ -z "$val" ]; then
        # replace empty value with n/a text
        if [ -n "$namsg" ]; then
            format=$( echo $format | sed -r "s/##(.*)##/($namsg)/" | sed -r "s/\[.*\]//" )
        else
            format=$( echo $format | sed -r "s/##(.*)##/(not available)/" | sed -r "s/\[.*\]//" )
        fi
        printf "$format\n" "$sysf"
    else
        # non empty value: strip delimiters from format str
        format=$( echo $format | sed -r "s/##(.*)##/\1/" )
        printf "$format\n" "$sysf" "$val"
    fi
    
    return 0
}

printparm_i915 () { # $1: file
    local val b

    if [ -f $1 ]; then
        val=$(cat $1 2> /dev/null)
        if [ $? = 0 ]; then
            printf "%-44s = %2d " $1 $val
            if [ "$val" = "-1" ]; then
                echo "(use per-chip default)"
            else
                echo -n "("
                if [ $(( $val & 1 )) -ne 0 ]; then
                    echo -n "enabled"
                else
                    echo -n "disabled"
                fi
                [ $(( $val & 2 )) -ne 0 ] && echo -n " + deep"
                [ $(( $val & 4 )) -ne 0 ] && echo -n " + deepest"
                echo ")"
            fi
        else
            printf "%-44s = (not available)\n" $1
        fi
    else
        printf "%-44s = (not available)\n" $1
    fi

    return 0
}

print_tpacpi_thresholds () { # $1: BAT0/BAT1; $2: bat # = 1/2
    local start_thresh stop_thresh force

    read_tpacpi_threshold ST $2
    start_thresh=$?
    if [ $start_thresh -ne 255 ]; then
        [ $start_thresh -eq 0 ] && start_thresh=96
        printf "%-59s = %6d [%%]\n" "tpacpi-bat.${1}.startThreshold" $start_thresh
    else
        printf "%-59s = (not available)\n" "tpacpi-bat.${1}.startThreshold"
    fi

    read_tpacpi_threshold SP $2
    stop_thresh=$?
    if [ $stop_thresh -ne 255 ]; then
        [ $stop_thresh -eq  0 ] && stop_thresh=100
        printf "%-59s = %6d [%%]\n" "tpacpi-bat.${1}.stopThreshold" $stop_thresh
    else
        printf "%-59s = (not available)\n" "tpacpi-bat.${1}.stopThreshold"
    fi

    get_force_discharge $2; force=$?
    if [ $force -ne 2 ]; then
        printf "%-59s = %6d\n" "tpacpi-bat.${1}.forceDischarge" "$force"
    else
        printf "%-59s = %s\n" "tpacpi-bat.${1}.forceDischarge" "(not available)"
    fi

    return 0
}

read_args () {
    for a in $*; do
        case $a in
            "-b"|"--battery")
                show_all=0
                show_bat=1
                needs_root_priv=1
                ;;
                
            "-c"|"--config")
                show_all=0
                show_conf=1
                : ${needs_root_priv:=0}
                ;;

            "-r"|"--rfkill")
                show_all=0
                show_rfkill=1
                : ${needs_root_priv:=0}
                ;;

            "-t"|"--temp")
                show_all=0
                show_temp=1
                : ${needs_root_priv:=0}
                ;;
                
            "-T"|"--trace")
                show_all=0
                show_trace=1
                : ${needs_root_priv:=0}
                ;;

            *)
                echo "Usage: tlp-stat [-b|--battery|-c|--config|-r|--rfkill|-t|--temp|-T|--trace]"
                exit 1
                ;;
        esac
    done
}
    
# --- Locate and source libraries

for libdir in $LIBDIRS; do [ -d $libdir ] && break; done
if [ ! -d $libdir ]; then
    echo "Error: missing library directory ($LIBDIRS)."
    exit 1
fi

for lib in $LIBS; do 
    if [ ! -f $libdir/$lib ]; then
        echo "Error: missing function library \'$libdir/$lib\'."
        exit 1
    fi
    . $libdir/$lib
done

# --- MAIN

read_args $*
: ${needs_root_priv:=1}

# check for and read conffile
read_defaults
conf_present=$?

# check prereqisites
if [ "$needs_root_priv" = "1" ]; then
    check_root
    load_tp_modules
    check_tpacpi
    check_tpsmapi
fi

echo "--- TLP $TLPVER --------------------------------------------"
echo

# --- show configuration
if [ "$show_conf" = "1" ] || [ "$show_all" = "1" ]; then
    if [ $conf_present -eq 0 ]; then
        echo "+++ Configured Settings: $CONFFILE"
        egrep -v '^#|^\s*$' $CONFFILE
        echo
    else
        echo "Warning: config file $CONFFILE not present."
        echo
    fi
fi

if [ "$show_all" = "1" ]; then
    # --- show system info
    echo "+++ System Info"
    read_dmi sys_vendor product_version product_name
    echo "System         = $dmirslt"
    read_dmi bios_version
    echo "BIOS           = $dmirslt"
    
    # --- show release & kernel info
    cmd_exists $LSBREL && echo "Release        = $($LSBREL -d -s)"
    echo "Kernel         = $(uname -r -m)"
    printparm "%-14s = %s" /proc/cmdline
    echo

    # --- show TLP status
    echo "+++ System Status"
    if check_laptop_mode_tools; then
        if [ "$TLP_ENABLE" = "1" ]; then
            echo "TLP power save = enabled"
        else
            echo "TLP power save = not enabled"
        fi
    fi

    # --- show power source
    if get_power_state; then
        echo "power source   = ac"
    else
        echo "power source   = battery"
    fi
    echo
    
    # --- show cpu info
    echo "+++ Processor"
    sed -rn 's/model name[ \t]+: (.+)/CPU Model      = \1/p' /proc/cpuinfo | head -1
    echo

    for cpuf in /sys/devices/system/cpu/cpu*/cpufreq; do
        if [ -f $cpuf/scaling_governor ]; then
            printparm "%-54s = ##%s##" $cpuf/scaling_governor
            printf "%-54s = %8d [kHz]\n" $cpuf/scaling_min_freq $(cat $cpuf/scaling_min_freq)
            printf "%-54s = %8d [kHz]\n" $cpuf/scaling_max_freq $(cat $cpuf/scaling_max_freq)
            printf "%s = " $cpuf/scaling_available_frequencies
            for freq in $(cat $cpuf/scaling_available_frequencies); do
                printf "%s " $freq 
            done
            printf "[kHz]\n\n"
        fi
    done

    
    if [ -f $CPU_BOOST_ALL_CTRL ]; then
        get_sysval $CPU_BOOST_ALL_CTRL; boost=$?

        # simple test for attribute "w" doesn't work, so actually write
        if ( echo "$boost" > $CPU_BOOST_ALL_CTRL ) 2> /dev/null; then 
            printparm "%-54s = ##%d##" $CPU_BOOST_ALL_CTRL
        else
            printparm "%-54s = ##%d## (cpu not supported)" $CPU_BOOST_ALL_CTRL
        fi
    else
         printparm "%-54s = (not available)" $CPU_BOOST_ALL_CTRL
    fi
    
    
    for pool in mc smp smt; do
        sdev="/sys/devices/system/cpu/sched_${pool}_power_savings"
        [ -f $sdev ] && printparm "%-54s = ##%d##" $sdev
    done
    
    # --- show nmi watchdog
    printparm "%-54s = ##%d##" $NMIWD
    echo

    # --- show voltages
    echo "+++ Undervolting"
    phc_avail=0
    for cpuf in /sys/devices/system/cpu/cpu*/cpufreq; do
        if [ -f $cpuf/phc_controls ]; then
            phc_avail=1
            printparm "%-58s = ##%s##" $cpuf/phc_controls
            printparm "%-58s = ##%s##" $cpuf/phc_default_controls
            echo
        fi
    done
    if [ $phc_avail = 0 ]; then
        echo "PHC kernel not available."
        echo 
    fi
    
fi # show_all

if [ "$show_temp" = "1" ] || [ "$show_all" = "1" ]; then    
    # --- show temperatures
    echo "+++ Temperatures"
    if [ -f $IBMTHERMAL ]; then
        # use thinkpad-specific sysfile
        echo "$IBMTHERMAL = $(cat $IBMTHERMAL | cut -f2) [°C]"
    else
        # use sensors
        cmax=0
        for sens in $CORETEMP/temp?_input; do
            if [ -f $sens ]; then
                if cat ${sens%input}label | grep -q "Physical"; then
                    # package info is available -> ignore remaining sensors
                    cmax=$(cat $sens)
                    break
                else
                    # core info -> find max value
                    ctemp=$(cat $sens)
                    [ $ctemp -gt $cmax ] && cmax=$ctemp
                fi
            fi
        done
        if [ $cmax -gt 0 ]; then
            printf "CPU temp               = %5d [°C]\n" $(( $cmax / 1000 ))
        fi
        
    fi

    # --- show fan speed
    if [ -f $IBMFAN ]; then
        # use thinkpad-specific sysfile
        cat $IBMFAN | \
            awk '$1 ~ /speed:/ { printf "'$IBMFAN'     = %5d [/min]\n", $2 }'
    else
        # use hwmon
        for fan in $HWMONFAN/fan?_input; do
            if [ -f $fan ]; then
                printf "Fan speed              = %5d [/min]\n" $(cat $fan)
                break;
            fi
        done
        if [ ! -f $fan ]; then
            printf "Fan speed              = (not available)\n"
        fi
    fi
    echo
    
fi # show_temp

if [ "$show_all" = "1" ]; then    
    # --- show laptop-mode, dirty buffers params
    echo "+++ File System"
    printparm "%-38s = ##%5d##" /proc/sys/vm/laptop_mode
    printparm "%-38s = ##%5d##" /proc/sys/vm/dirty_writeback_centisecs
    printparm "%-38s = ##%5d##" /proc/sys/vm/dirty_expire_centisecs
    printparm "%-38s = ##%5d##" /proc/sys/vm/dirty_ratio
    printparm "%-38s = ##%5d##" /proc/sys/vm/dirty_background_ratio
    printparm "%-38s = ##%5d##" /proc/sys/fs/xfs/age_buffer_centisecs
    printparm "%-38s = ##%5d##" /proc/sys/fs/xfs/xfssyncd_centisecs   
    printparm "%-38s = ##%5d##" /proc/sys/fs/xfs/xfsbufd_centisecs
    echo 
    
    # --- show disk info form hdparm
    echo "+++ Storage Devices"
    DISK_DEVICES=${DISK_DEVICES:=sda}
    for dev in $DISK_DEVICES; do # iterate all devices
        get_disk_dev $dev
    
        if [ -b /dev/$disk_dev ]; then
            get_disk_state $disk_dev
            check_disk_hdparm_cap $disk_dev
            if [ $? = 0 ]; then
                echo "/dev/$disk_dev:"
                    
                if [ -n "$disk_id" ]; then
                    echo "          Disk ID   = $disk_id"
                fi
                
                echo -n "          Model     = "
                echo_disk_model $disk_dev
                
                echo -n "          Firmware  = "
                echo_disk_firmware $disk_dev
                
                get_disk_apm_level $disk_dev
                apm=$?
                echo -n "          APM Level = "
                case $apm in
                    0|255) echo "none/disabled" ;;
                    *)     echo $apm ;;
                esac

                echo "          Status    = $disk_state"

                get_disk_trim_capability $disk_dev
                trim=$?
                case $trim in
                    0) echo "          TRIM      = not supported" ;;
                    1) echo "          TRIM      = supported" ;;
                esac
                
                echo "          scheduler = $(cat /sys/block/$disk_dev/queue/scheduler | sed -r 's/.*\[(.*)\].*/\1/')"
            
                if cmd_exists $SMARTCTL ; then
                    # --- show SMART data
                    echo 
                    echo "        SMART info:"
                    $SMARTCTL -A /dev/$disk_dev | grep -v '<==' | \
                      awk -F ' ' '$2 ~ /Start_Stop_Count|Load_Cycle_Count|Reallocated_Sector_Ct|Used_Rsvd_Blk_Cnt_Chip|Used_Rsvd_Blk_Cnt_Tot/ \
                                        { printf "          %3d %-25s = %8d \n", $1, $2, $10 } ; \
                                  $2 ~ /Power_On_Hours/ \
                                        { printf "          %3d %-25s = %8d %s\n", $1, $2, $10, "[h]" } ; \
                                  $2 ~ /Temperature_Celsius/ \
                                        { printf "          %3d %-25s = %8d %s %s %s %s\n", $1, $2, $10, $11, $12, $13, "[°C]" } ; \
                                  $2 ~ /Airflow_Temperature_Cel/ \
                                        { printf "          %3d %-25s = %8d %s\n", $1, $2, $10, "[°C]" } ; \
                                  $2 ~ /Host_Writes/ \
                                        { printf "          %3d %-25s = %8.3f %s\n", $1, $2, $10 / 32768.0, "[TB]" } ; \
                                  $2 ~ /Total_LBAs_Written/ \
                                        { printf "          %3d %-25s = %8.3f %s\n", $1, $2, $10 / 2147483648.0, "[TB]" } ; \
                                  $2 ~ /Available_Reservd_Space|Media_Wearout_Indicator|Wear_Leveling_Count/ \
                                        { printf "          %3d %-25s = %8d %s\n", $1, $2, $4, "[%]" }'
                fi
                echo

                # restore standby state 
                [ "$disk_state" = "standby" ] && spindown_disk $disk_dev
            fi
        fi
    done
    echo
    
    # --- show sata alpm mode
    echo "+++ SATA Aggressive Link Power Management"
    cnt=0    
    for i in /sys/class/scsi_host/host* ; do
        if [ -f $i/link_power_management_policy ]; then
            printparm "%-56s = ##%s##" $i/link_power_management_policy
            cnt=$((cnt+1))
        fi
    done
    if [ $cnt = 0 ]; then
        echo "No AHCI-enabled host controller detected."
    fi  
    echo 
    
    # --- show pcie aspm state
    echo "+++ PCIe Active State Power Management"
    if [ -f $ASPM ]; then
        pol=$(cat $ASPM | sed -r 's/.*\[(.*)\].*/\1/')
        echo "$pol" > $ASPM 2> /dev/null
        if [ $? = 0 ]; then
            echo "$ASPM = $pol"
        else
            echo "$ASPM = $pol (using bios preferences)"
        fi 
    else
        echo "$ASPM = (not available)"
    fi
    echo
    
    # --- show i915 power mgmt
    if [ -d $I915D ]; then 
        echo "+++ Intel Graphics"
        printparm_i915 $I915D/powersave
        printparm_i915 $I915D/i915_enable_rc6
        printparm_i915 $I915D/i915_enable_fbc
        printparm_i915 $I915D/lvds_downclock
        printparm_i915 $I915D/semaphores
        echo
    fi
    
    # --- show radeon power profile
    for card in /sys/class/drm/card[0-9]/device ; do
        if [ -f $card/power_method ] && [ -f $card/power_profile ]; then
            echo "+++ Radeon Graphics"
            printparm "%-25s = ##%s##" $card/power_method
            printparm "%-25s = ##%s##" $card/power_profile
            echo
            break
        fi
    done
fi # show_all

if [ "$show_rfkill" = "1" ] || [ "$show_all" = "1" ]; then    
    echo "+++ Wireless"
    # --- show rfkill state
    for i in bluetooth wifi wwan; do
        get_devc $i
        get_devs $i
        echo_device_state $i $devs
    done
    echo
fi # show_rfkill

if [ "$show_all" = "1" ]; then        
    # --- show wifi power mode
    get_wifi_ifaces
    for iface in $wifaces; do
        if [ -n "$iface" ]; then
            iwrc=1
            if cmd_exists $IW; then
                wifipm=$($IW dev $iface get power_save 2> /dev/null)
                iwrc=$?
                if [ $iwrc = 0 ]; then
                    wifipm=$(echo $wifipm | sed -r 's/.*(on|off).*/\1/')
                fi
            fi
            if [ $iwrc != 0 ]; then
                wifipm=$($IWC $iface 2> /dev/null | grep "Power Management" | \
                    sed -r 's/.*Power Management:(on|off).*/\1/')
            fi

            get_wifi_driver $iface
            echo -n "$iface($wifidrv): power management = "
            case $wifipm in
                on)
                    echo "on"
                    ;;
                    
                off)
                    if cmd_exists $IW && [ $iwrc = 0 ]; then
                        $IW dev $iface set power_save off > /dev/null 2>&1
                    else
                        $IWC $iface power off > /dev/null 2>&1
                    fi
                    
                    if [ $? = 0 ] ; then
                        echo "off"
                    else
                        echo "off (not supported)"
                    fi
                    ;;
                    
                *) 
                    echo "unknown"
                    ;;
            esac
        fi
    done
    [ -n "$wifaces" ] && echo

    # --- show sound power mode
    echo "+++ Audio"
    if [ -d /sys/module/snd_hda_intel ]; then
        printparm "%-58s = ##%s##" /sys/module/snd_hda_intel/parameters/power_save
        printparm "%-58s = ##%s##" /sys/module/snd_hda_intel/parameters/power_save_controller
    fi
    if [ -d /sys/module/snd_ac97_codec ]; then
        printparm "%s = ##%s##" /sys/module/snd_ac97_codec/parameters/power_save
    fi
    echo
    
fi # show_all

if [ "$show_bat" = "1" ] || [ "$show_all" = "1" ]; then
    # --- show battery info & charge thresholds

    # --- show availability of ThinkPad battery functions
    if [ -d $TPACPI ]; then
        echo "+++ ThinkPad Extended Battery Functions"
        echo -n "tp-smapi   = "
        case $tpsmapi in
            0)   echo "active" ;;
            2)   echo "inactive (kernel module 'tp_smapi' load error)" ;;
            127) echo "inactive (kernel module 'tp_smapi' not installed)" ;;
            *)   echo "unknown status"
        esac

        echo -n "tpacpi-bat = "
        case $tpacpi in
            0)   echo "active" ;;
            2)   echo "inactive (kernel module 'acpi_call' load error)" ;;
            4)   echo "inactive (disabled by user configuration)" ;;
            127) echo "inactive (kernel module 'acpi_call' not installed)" ;;
            255) echo "inactive (unsupported hardware)" ;;
        esac
        echo
    fi
    
    if [ $tpsmapi -eq 0 ]; then
        # it's a ThinkPad with tp-smapi
        
        for batd in $SMAPIDIR/BAT[01]; do
            if [ -d $batd ]; then
                batt=${batd##/*/}
            
                if bat_exists $batt ; then
                    case $batt in
                        BAT0) bati=1; echo "+++ ThinkPad Battery Status (Main)" ;;
                        BAT1) bati=2; echo "+++ ThinkPad Battery Status (Ultrabay/Slice)" ;;
                        *)    bati=1  echo "+++ ThinkPad Battery Status" ;;
                    esac
    
                    printparm "%-59s = ##%s##" $batd/manufacturer
                    printparm "%-59s = ##%s##" $batd/model
                    printparm "%-59s = ##%s##" $batd/manufacture_date
                    printparm "%-59s = ##%s##" $batd/first_use_date
                    printparm "%-59s = ##%6d##" $batd/cycle_count
                    printparm "%-59s = ##%6d## [mWh]" $batd/design_capacity
                    printparm "%-59s = ##%6d## [mWh]" $batd/last_full_capacity
                    printparm "%-59s = ##%6d## [mWh]" $batd/remaining_capacity
                    printparm "%-59s = ##%6d## [%%]" $batd/remaining_percent
                    printparm "%-59s = ##%6s## [min]" $batd/remaining_running_time_now
                    printparm "%-59s = ##%6s## [min]" $batd/remaining_charging_time
                    printparm "%-59s = ##%6d## [mW]" $batd/power_now
                    printparm "%-59s = ##%6d## [mW]" $batd/power_avg
                    echo
    
                    if [ $tpacpi -eq 0 ]; then
                        # --- show ThinkPad charge thresholds via tpacpi-bat
                        print_tpacpi_thresholds $batt $bati
                    else
                        # show thresholds via tp-smapi
                        printparm "%-59s = ##%6d## [%%]" $batd/start_charge_thresh
                        printparm "%-59s = ##%6d## [%%]" $batd/stop_charge_thresh 
                        printparm "%-59s = ##%6d##" $batd/force_discharge
                    fi
                    echo
                fi
            fi
        done
    elif [ -d $ACPIBATDIR ]; then
        # --- show ACPI data
        
        for batd in $ACPIBATDIR/*; do
            if [ -d $batd ] \
                && [ "$(cat $batd/type)" = "Battery" ] \
                && [ "$(cat $batd/present)" = "1" ]; then

                batt=${batd##/*/}
                
                if [ $tpacpi -eq 0 ]; then
                    # it's a ThinkPad with tpacpi-bat only
                    case $batt in
                        BAT0) bati=1; echo "+++ ThinkPad Battery Status (Main)" ;;
                        BAT1) bati=2; echo "+++ ThinkPad Battery Status (Ultrabay/Slice)" ;;
                        *)    bati=1; echo "+++ ThinkPad Battery Status" ;;
                    esac
                else
                    # it's some other laptop model or brand 
                    echo "+++ Battery Status"
                fi
                
                printparm "%-59s = ##%s##" $batd/manufacturer
                printparm "%-59s = ##%s##" $batd/model_name

                cc=$(cat $batd/cycle_count 2> /dev/null)
                if [ $? -eq 0 ] && [ -n "$cc" ] && [ $cc -gt 0 ]; then
                    printf "%-59s = %6d\n" $batd/cycle_count $cc
                else
                    printf "%-59s = (not supported)\n" $batd/cycle_count
                fi

                if [ -f $batd/energy_full ]; then 
                    printparm "%-59s = ##%6d## [mWh]" $batd/energy_full_design "" 000
                    printparm "%-59s = ##%6d## [mWh]" $batd/energy_full "" 000
                    printparm "%-59s = ##%6d## [mWh]" $batd/energy_now "" 000
                    printparm "%-59s = ##%6d## [mW]" $batd/power_now "" 000
                elif [ -f $batd/charge_full ]; then
                    printparm "%-59s = ##%6d## [mAh]" $batd/charge_full_design "" 000
                    printparm "%-59s = ##%6d## [mAh]" $batd/charge_full "" 000
                    printparm "%-59s = ##%6d## [mAh]" $batd/charge_now "" 000
                    printparm "%-59s = ##%6d## [mA]" $batd/current_now "" 000
                fi
                printparm "%-59s = ##%s##" $batd/status
                echo

                if [ $tpacpi -eq 0 ]; then
                    # --- show ThinkPad charge thresholds via tpacpi-bat
                    print_tpacpi_thresholds $batt $bati
                    echo
                fi # if $tpcacpi
            fi # if $batd
        done # $batd
    
    fi # if /sys/class/power_supply
    
fi # show_bat

if [ "$show_all" = "1" ]; then
    # -- show runtime pm
    echo "+++ Runtime Power Management"
    doall=${RUNTIME_PM_ALL:-0}
    
    if cmd_exists $TLPPCI; then
        $TLPPCI 
    else
        echo "Error: missing subcommand $TLPPCI."
    fi
    echo  
    
    # -- show usb autosuspend
    echo "+++ USB"
    if [ "$USB_AUTOSUSPEND" = "1" ]; then
        echo "tlp usb autosuspend = enabled"
    else
        echo "tlp usb autosuspend = not enabled"
    fi
    echo "tlp usb blacklist   = ${USB_BLACKLIST:=(not configured)}"
    echo
    
    if cmd_exists $TLPUSB; then
        $TLPUSB 
    else
        echo "Error: missing subcommand $TLPUSB."
    fi
    echo

    # -- show suggestions
    suout=""

    if is_thinkpad; then
        # Add ThinkPad specific suggestions
        read_dmi product_version
        model=${dmirslt#ThinkPad }

        case $model in
            X230*|T430*|T530|W530)
                [ $tpacpi -eq 127 ]  && suout="${suout}Install acpi-call kernel module for ThinkPad advanced battery functions\n"
                ;;

            X220*|T420*|T520|W520)
                [ $tpacpi -eq 127 ]  && suout="${suout}Install acpi-call kernel module for ThinkPad advanced battery functions\n"
                [ $tpsmapi -eq 127 ] && suout="${suout}Install tp-smapi kernel modules for ThinkPad advanced battery functions\n"
                ;;

            SL?00) ;;

            *) [ $tpsmapi -eq 127 ]  && suout="${suout}Install tp-smapi kernel modules for ThinkPad advanced battery functions\n"
                ;;
        esac
    fi

    # Add other suggestions
    cmd_exists ethtool || suout="${suout}Install ethtool to disable Wake on LAN\n"
    cmd_exists smartctl || suout="${suout}Install smartmontools for disk drive health info\n"

    if [ -n "$suout" ]; then
        echo "+++ Suggestions"
        echo -n $suout | sed -r 's/^/\* /'
        echo
    fi

fi # show_all

if [ "$show_trace" = "1" ]; then
    # --- show debug log
    if [ -f $DEBUGLOG ]; then
        grep "tlp\[" $DEBUGLOG
    else
        echo "Error: $DEBUGLOG does not exist."
        echo 
        echo "Solution: create an rsyslog conffile /etc/rsyslog.d/90-debug.conf with the following contents"
        echo " *.=debug;\\"
        echo " mail,authpriv,cron.none;\\"
        echo " local0,local1,local3,local4,\\"
        echo " local5,local6,local7.none    -/var/log/debug"
        echo "and restart the rsyslog daemon."
        echo
    fi
fi # show_trace

exit 0
