#!/bin/bash
# fancontrol Dell Optiplex7050  Small Form Factor (peacok March 2024)
#
# This script control the fan, that you stand unter  80°C, normally up to 92°C by Bios Control.
#
# Bios Disabling  Perfomance,Intel Turbo Boost (so we stay under 80 °C, depends on the heatsink metal)
#
# If you installed some tools like fancontrol or so, please uninstall/purge them first, and reboot. THX
#
# Save as /usr/local/bin/fancontrol_DellOptiplex7050
# chmod 744 /usr/local/bin/fancontrol_DellOptiplex7050
# sudo fancontrol_DellOptiplex7050 i   (this install the needed packages and install boot services)

# example for other maschines  (maybe other help tools for pwm, maybee not i8kutils)
#temp_0=55
#temp_1=62
#temp_2=62
#temp_3=62
#temp_4=66
#temp_5=66
#temp_6=70
#temp_7=70
#temp_8=74
#temp_9=74
#pwm_min=60
#pwm_max=255

# For Dell Optiplex 7050
temp_0=55
temp_1=62
temp_2=68
temp_shutdown=82
fan_delay=30
pwm_min=60
pwm_mid=100
pwm_max=255

pwm_file=`find /sys/devices/platform/ -name 'pwm1'`
#pwm_file=/sys/class/hwmon/hwmon3/pwm1
#echo $pwm_file

#
#
#
install_i8kutils () {
sudo apt -y install i8kutils lm-sensors
if [ -z "$(cat /etc/modules | grep dell-smm-hwmon)" ];then
	sudo echo "dell-smm-hwmon" >> /etc/modules
fi
if [ -z "$(cat /etc/modules | grep coretemp)" ];then
	sudo echo "coretemp" >> /etc/modules
fi
echo -e "options dell-smm-hwmon ignore_dmi=1 #restricted=0" | sudo tee /etc/modprobe.d/dell-smm-hwmon.conf
systemctl stop i8kmon.service
systemctl disable i8kmon.service
echo '
[Unit]
Description=fancontrol_DellOptiplex7050

[Service]
Type=simple
StandardOutput=null
ExecStart=/usr/local/bin/fancontrol_DellOptiplex7050

[Install]
WantedBy=multi-user.target
' > /etc/systemd/system/fancontrol_DellOptiplex7050.service
systemctl enable fancontrol_DellOptiplex7050.service

echo -e "\nplease reboot" 
}

fancontrol () {
cpu_temp=`sensors | awk '{if (match($0, "Package id 0")){printf("%d",$4);} }'`
#sensors | awk '{if (match($0, "Package id 0")){printf("%d",$4);} }'

## This lines not support (we have only 3 speeds), proporzional speedcontrol, for other maschines examples
#if [[ "$cpu_temp" -ge "$temp_9" ]];then echo "$pwm_max">"$pwm_file";fi
#if [[ "$cpu_temp" -ge "$temp_8" && "$cpu_temp" -lt "$temp_9" ]];then echo $((($pwm_max-$pwm_min)/9*8+$pwm_min))>"$pwm_file";fi
#if [[ "$cpu_temp" -ge "$temp_7" && "$cpu_temp" -lt "$temp_8" ]];then echo $((($pwm_max-$pwm_min)/9*7+$pwm_min))>"$pwm_file";fi
#if [[ "$cpu_temp" -ge "$temp_6" && "$cpu_temp" -lt "$temp_7" ]];then echo $((($pwm_max-$pwm_min)/9*6+$pwm_min))>"$pwm_file";fi
#if [[ "$cpu_temp" -ge "$temp_5" && "$cpu_temp" -lt "$temp_6" ]];then echo $((($pwm_max-$pwm_min)/9*5+$pwm_min))>"$pwm_file";fi
#if [[ "$cpu_temp" -ge "$temp_4" && "$cpu_temp" -lt "$temp_5" ]];then echo $((($pwm_max-$pwm_min)/9*4+$pwm_min))>"$pwm_file";fi
#if [[ "$cpu_temp" -ge "$temp_3" && "$cpu_temp" -lt "$temp_4" ]];then echo $((($pwm_max-$pwm_min)/9*3+$pwm_min))>"$pwm_file";fi
#if [[ "$cpu_temp" -ge "$temp_2" && "$cpu_temp" -lt "$temp_3" ]];then echo $((($pwm_max-$pwm_min)/9*2+$pwm_min))>"$pwm_file";fi
#if [[ "$cpu_temp" -ge "$temp_1" && "$cpu_temp" -lt "$temp_2" ]];then echo $((($pwm_max-$pwm_min)/9*1+$pwm_min))>"$pwm_file";fi
#if [[ "$cpu_temp" -ge "0" && "$cpu_temp" -lt "$temp_0" ]];then echo "$pwm_min">"$pwm_file";fi

## This three states are supportet, three fan speeds
if [[ "$cpu_temp" -ge "$temp_2" ]];then echo "$pwm_max">"$pwm_file";fi
if [[ "$cpu_temp" -ge "$temp_1" && "$cpu_temp" -lt "$temp_2" ]];then echo "$pwm_mid">"$pwm_file";fi
if [[ "$cpu_temp" -ge "0" && "$cpu_temp" -lt "$temp_0" ]];then echo "$pwm_min">"$pwm_file";fi

# Over Temp behavor
if [[ "$cpu_temp" -ge "$temp_shutdown" ]];then
   systemd-cat -t `basename $0` -p 3 echo "!!!!!!!!!!!!!!!! CPU overtemperature alarm !!!!!!!!!!!!!!!!!!!!!"
   sync
   systemctl poweroff
   exit 0
fi
}

if [[ "$1" = "i" ]];then install_i8kutils;exit 0;fi
if [[ -n "$1" ]];then echo "$1" >"$pwm_file";exit 0;fi

while true; do
fancontrol
sleep "$fan_delay"
done

exit 0
