#!/bin/bash


last_date="/var/tmp/battery_charge_time"
if ! test -f "$last_date" ; then
    logger -s ">>>>>>>>>> Created missing $last_date"
    date "+%s" > "$last_date"
fi

sec=${1:-30}

diff=$(( $(date +%s) - $(cat $last_date) ))
if [ $diff -lt $sec ]; then
    logger -s ">>>>>>>>>> Skipping battery charge, last was ${diff}secs ago (vs $sec)"
    exit
fi

# Thinkapd
echo "Set battery to charging"
echo 85 > /sys/class/power_supply/BAT0/charge_stop_threshold ; echo 75 > /sys/class/power_supply/BAT0/charge_start_threshold
/usr/sbin/tlp  setcharge 75 85

# Dell
# https://www.dell.com/community/en/conversations/linux-general/setting-the-battery-charge-level-within-linux/647f7222f4ccf8a8defc7422
# https://github.com/glynhudson/dell-charge-limit/issues/3
# from https://www.dell.com/support/article/en-uk/sln311302/dell-command-configure?lang=en
/opt/dell/dcc/cctk --primarybatterycfg=custom:80-85


date "+%s" > "$last_date"

