#!/bin/bash
#
# lxtemp puts an icon in your systray. 
# When you hover your mouse over it, a 
# message opens and displays your temperature. 
#
# Copyright 2011-2013, 2017 
# for PCLinuxOS by Neal Brooks, D.M-Wilhelm and 
# ghostbunny.
# License - GPLv2
#
# Version 0.3.2
#
# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN=lxtemp
export TEXTDOMAINDIR="/usr/share/locale"
#
#####################################
# some variables - Einige Variablen #
#####################################
ICON=/usr/share/pixmaps/lxtemp.png
NONE=1 
TITLE=lxtemp
VERSION=0.3.3


#############################################################
# Test existence of the config directory                    #
# If the directory is not available we create the directory #
#############################################################
if [ -d $HOME/.config/lxtemp ]; then
  echo directory lxtemp exists
else
  mkdir $HOME/.config/lxtemp
fi

if [ -f $HOME/.config/lxtemp/celsius.cfg ]; then
  # for AMD
  SETTEMP="-t"
else
  # for Intel
  TEMPFORMAT="-f"
  # for AMD
  SETTEMP="-tf"
fi	

echo $FRAME

######################
# get cpu model name #
######################
SYSINFO=`cat /proc/cpuinfo | grep ^"model name" | cut -d " " -f3` 
SYS=`echo $SYSINFO | cut -d " " -f1`
echo $SYS

##########################################################
# change fahrenheit to celsius                           #
# looks for the current config file, delete the file and #
# creates a new one from the other type                  #
##########################################################
function Change() 
{ 
  if [ -f $HOME/.config/lxtemp/celsius.cfg ]; then
    yad --text $"Change Celsius to Fahrenheit?" --title 'lxtemp' \
        --window-icon=/usr/share/pixmaps/lxtemp.png 

    if [ $? = 0 ];then
      rm $HOME/.config/lxtemp/celsius.cfg

      yad --title 'lxtemp' --window-icon=/usr/share/pixmaps/lxtemp.png \
          --text $"lxtemp will now restart!" --button="gtk-ok:0"

      killall lxtemp
      exec lxtemp
    fi
  else
    yad --text $"Change Fahrenheit to Celsius?" --title 'lxtemp' \
        --window-icon=/usr/share/pixmaps/lxtemp.png

    if [ $? = 0 ];then
      echo '-frame' > $HOME/.config/lxtemp/celsius.cfg

      yad --title 'lxtemp' --window-icon=/usr/share/pixmaps/lxtemp.png \
          --text $"lxtemp will now restart!" --button="gtk-ok:0"

      killall lxtemp
      exec lxtemp
    fi
  fi		
} 

export -f Change

#######################################
# The function shows the About window #
#######################################
function Info() 
{ 
  yad --window-icon=/usr/share/pixmaps/lxtemp.png \
      --title=$"About" --button="gtk-ok:0" --text \
  $"  lxtemp puts an icon in your systray. 
  When you hover your mouse over it, a 
  message opens and displays your temperature. 

  <b>Version:</b> 0.3.3  
  <b>License:</b> GPLv2
    
  <b>Authors:</b> Neal Brooks, 
		   D.M-Wilhelm,
		   ghostbunny.
   
  Big thanks to our testers
  <b>sɔǝʌɐp, djohnston, scoundrel,</b>
  
  <b>Copyright 2011-2013, 2017</b>" 
} 

export -f Info

########################################################
# Exit the program after asking the user if he is sure #
########################################################
function Exit() 
{ 
  yad --title="lxtemp" \
      --window-icon="/usr/share/pixmaps/lxtemp.png" \
      --text $"Sure you want to quit lxtemp?"

  if [ $? = "0" ]; then 
    killall lxtemp
  fi
}
    
export -f Exit

###########################################################################
# The function creates/deletes an autostart entry for lxtemp.		  #
# If the entry is already there it will delete after asking the user else #
# we copy the .desktop file from /usr/share/applications in users 	  #
# autostart directory.							  #
###########################################################################
function Autostart()
{
  if [ -f $HOME/.config/autostart/lxtemp.desktop ]; then
    yad --text $"You want remove lxtemp from autostart?" --title 'lxtemp' \
	--window-icon=/usr/share/pixmaps/lxtemp.png 

    if [ $? = 0 ];then
      rm $HOME/.config/autostart/lxtemp.desktop
      
      yad --title 'lxtemp' --window-icon=/usr/share/pixmaps/lxtemp.png \
	  --text $"lxtemp is removed from autostart!" --button="gtk-ok:0"
    fi
  else
    yad --text $"You want set lxtemp to autostart?" --title 'lxtemp' \
	--window-icon=/usr/share/pixmaps/lxtemp.png

    if [ $? = 0 ];then
      cp /usr/share/applications/lxtemp.desktop $HOME/.config/autostart/lxtemp.desktop
      yad --title 'lxtemp' --window-icon=/usr/share/pixmaps/lxtemp.png \
	  --text $"lxtemp is now set to autostart!" --button="gtk-ok:0"
    fi
  fi		
}
	
export -f Autostart	

##############################################
# Some variables for the yad command option. #
# Seems yad couldn't work with the $ symbol  #
# in the command option.		     #
##############################################
title2=$TITLE"--"$VERSION
f1=$"Change:btn"
f2=$"Autostart:btn"
f3=$"Info:btn"
f4=$"Exit:btn"
b1="gtk-close:0"
exe1="\"sh -c Change\""
exe2="\"sh -c Autostart\""
exe3="\"sh -c Info\""
exe4="\"sh -c Exit\""
help=$"Your current running temperature is"

########################################
# Launche the options dialog of lxtemp #
########################################
exec 3> >(yad --notification \
  --command="yad --title $title2 \
  --window-icon=$ICON --button=$b1 \
  --form --field $f1 --field $f2 \
  --field $f3 --field $f4 \
  --width=200 $exe1 $exe2 $exe3 $exe4" \
  --image=$ICON --listen)

############################################
# Run necessary code for Intel processors. #
# Collect the sensor informations.	   #
############################################
if [ "$SYS" = "Intel(R)" ];then 
  
  while [ $NONE ]; do 
    if [ $(sensors | grep -c "temp1") -gt 0 ]; then
      TEMP="\n"$(sensors $TEMPFORMAT | grep -wm 1 "temp1" | cut -d " " -f1,8-9)
    elif [ $(sensors | grep -c "Core 0") -gt 0 ]; then
      TEMP5="\n"$(sensors $TEMPFORMAT | grep -wm 1 "Core 0" | cut -d " " -f1-2,8-10)
    else
      TEMP=$"Temperature sensor not found."
    fi  

    if [ $(sensors | grep -c "temp2") -gt 0 ]; then
      TEMP1="\n"$(sensors $TEMPFORMAT | grep -wm 1 "temp2" | cut -d " " -f1,8-9)
    fi

    if [ $(sensors | grep -c "temp3") -gt 0 ]; then
      TEMP2="\n"$(sensors $TEMPFORMAT | grep -wm 1 "temp3" | cut -d " " -f1,8-9)
    fi

    if [ $(sensors | grep -c "temp4") -gt 0 ]; then
      TEMP3="\n"$(sensors $TEMPFORMAT | grep -wm 1 "temp4" | cut -d " " -f1,8-9)
    fi

    if [ $(sensors | grep -c "temp5") -gt 0 ]; then
      TEMP4="\n"$(sensors $TEMPFORMAT | grep -wm 1 "temp5" | cut -d " " -f1,8-9)
    fi
    if [ $(sensors | grep -c "Core 1") -gt 0 ]; then
      TEMP6="\n"$(sensors $TEMPFORMAT | grep -wm 1 "Core 1" | cut -d " " -f1-2,8-10)
    fi
    if [ $(sensors | grep -c "Core 2") -gt 0 ]; then
      TEMP7="\n"$(sensors $TEMPFORMAT | grep -wm 1 "Core 2" | cut -d " " -f1-2,8-10)
    fi
    if [ $(sensors | grep -c "Core 3") -gt 0 ]; then
      TEMP8="\n"$(sensors $TEMPFORMAT | grep -wm 1 "Core 3" | cut -d " " -f1-2,8-10)
    fi
    if [ $(sensors | grep -c "Core 4") -gt 0 ]; then
      TEMP9="\n"$(sensors $TEMPFORMAT | grep -wm 1 "Core 4" | cut -d " " -f1-2,8-10)
    fi

    ####################################
    # add tooltip for Intel processors #
    ####################################
    echo tooltip: "$help <b>$TEMP $TEMP1 $TEMP2 $TEMP3 $TEMP4 $TEMP5 $TEMP6 $TEMP7 $TEMP8 $TEMP9</b>" >&3
    sleep 3
  done
  exec 3>&-
else
  ##########################################
  # Run necessary code for AMD processors. #
  # Collect sensor informations.	   #
  ##########################################
  while [ $NONE ]; do
    if [ $(acpi $SETTEMP | grep -ci "Thermal 0") -gt 0 ]; then
      TEMP="\n"$(acpi $SETTEMP | grep -e "Thermal 0")
    else
      TEMP=$"Temperature sensor not found."  
    fi

    if [ $(acpi $SETTEMP | grep -ci "Thermal 1") -gt 0 ]; then
      TEMP1="\n"$(acpi $SETTEMP | grep -e "Thermal 1")
    fi

    ##################################
    # add tooltip for AMD processors #
    ##################################
    echo tooltip: "$help <b>$TEMP $TEMP1</b>" >&3
    sleep 1
  done
  exec 3>&-
fi
