#!/bin/bash

CURRENT=$(grep 'file(' $HOME/.config/sfwbar/temp.widget | cut -d'"' -f2)
TARGET="$HOME/.config/sfwbar/temp.widget"

SENSORS=$(find /sys/devices/ -name temp*_input | sort)
	echo "$SENSORS" > /tmp/tmp
	echo -n > /tmp/tmp1
	while read -r line
	   do
	     INPUT=$(tail -n1 "$line" 2>/dev/null)
	     [ "$INPUT" ] && SENSORINPUT="$(expr $INPUT / 1000)" || SENSORINPUT="NA"
	     [[ ! $(echo "$SENSORINPUT" | grep -E '^-|^0|^NA') ]] && echo "$line" >> /tmp/tmp1
	   done < /tmp/tmp
	SENSORS=$(cat /tmp/tmp1 | sed 's/_input/_input \//g')

Xdialog --title "Temperature Sensors" \
        --menu "Select a sensor input for the taskbar temperature monitor\n\n\
(Current Sensor)\n\
$CURRENT" 24 100 6 \
$SENSORS 2> /tmp/sensor.tmp.$$

retval=$?
choice=`tail -n1 /tmp/sensor.tmp.$$`
rm -f /tmp/sensor.tmp.$$

case $retval in
  0)
    echo "Sensor selected: $choice"
    sed -i "s%file(\".*$%file(\"$choice\") {%" $TARGET
    sleep 1
    killall -SIGHUP sfwbar & ;;
  1)
    echo "Cancel pressed.";;
  255)
    echo "Close pressed";;
esac
