#!/bin/sh
v=241230 #sfs
export TEXTDOMAIN=power-man
killall swayidle power-exe
yad --version |grep -E '^0' && gtk=gtk || gtk=yad
#http://ptspts.blogspot.ru/2009/10/screen-blanking-dpms-screen-saver.html
#date +"%M %H %d" --date="@3601 1 days ago" -u ;exit
#d="`date +"%d" -d @$s -u`" #;exit
t1="$(gettext 'Power management')"
t="$(gettext '<b>Disable screen blanking</b> when idle (DPMS).')"

#which cbatticon && cb="--button=Батарея!battery!\\\"Настройка\ cbattery\ -\ мониторинг\ батареи\ в\ трее\\\":1"
which cbatticon-gui && cb="--button=$(gettext 'Battery')!battery!cbatticon:1"
Y(){
yad --title="`basename "$0"` v.$v" --f1-action=power-hlp --uri-handler=xdg-open \
    --text=" <big>$t1</big>" \
    --image="battery" \
    --name="battery" \
    --window-icon="battery" \
    --fixed --image-on-top --center \
    --form \
	--field=":LBL"  \
    --field="$t:LBL"  \
    --field="$(gettext 'minutes:!0=not extinguish. Relevant when watching movies in the browser, etc.'):NUM" \
    --field="$(gettext 'Screen Keeper:!For example: slimlock i3lock csxlock xlock slock. If you need to specify keys, create a script'):FL" \
    --field="$(gettext 'password request for resuming operation'):CHK" \
	--field=":LBL"  \
    --field="<b>$(gettext 'Standby mode')</b> (suspend):LBL"  \
    --field="$(gettext 'password request for resuming operation'):CHK" \
    --field="$(gettext 'suspend instead of turning off the screen'):CHK" \
	--field=":LBL"  \
    --Xfield="Документация":link  \
    --Xfield="Принцип работы, кнопки питания, сна, крышка!gtk-help!:FBTN" \
    --Xbutton=$(gettext 'Help')!gtk-help!"$(gettext 'Operating principle, power, sleep, and cover buttons')":3  $cb \
    --button=$gtk-ok:0  \
    --button=$gtk-cancel:2  \
    -- \
    1 \
    2 \
	"$min" \
    "$ssaver" \
	"$pdpms" \
    7 \
    8 \
	"$psus" \
	"$ss" \
    11 \
    'bash -c "power-hlp &"' 
}

p="$HOME/.config/power-man.conf"

#i3lock -n -k --indicator -c 000000  --time-color ffffff --date-color ffffff --greeter-color ffffff --time-str=%H:%M --date-str=%A:%d.%m.%y --keylayout 2 --layout-color ffffff
if [ ! -f "$p" ] ;then
    for i in slimlock i3lock csxlock xlock slock ;do
	i3lock="`which $i`" && break
    done
    mkdir -p "`dirname "$p"`" &&
    printf "dpms=`xset -q |awk '/timeout: / {print $2}'`\nssaver=\"$i3lock\"\npdpms=0\npsus=0\nss=0\nsudo=\n" >"$p"
fi
. "$p"

min="`echo $dpms |awk '{print int($1/60)}'`"
y="$(Y)"
r="$?"
echo "$p: $y $r"

    PARSE(){
d="`echo $y |awk -F"|" '{print int($3*60)}'`"
sed -i 's/^dpms=.*//' "$p"
echo "dpms=$d" >>"$p"
#echo "dpms=10" >>"$p"

sed -i 's/^ssaver=.*//' "$p"
echo $y |awk -F"|" '{print "ssaver=\""$4"\""}' >>"$p"

sed -i 's/^pdpms=.*//' "$p"
[ "`echo $y |awk -F"|" '{print $5}'`" = "TRUE" ] && echo "pdpms=true" >> $p || echo "pdpms=false" >> $p 

sed -i 's/psus=.*//' "$p"
if [ "`echo $y |awk -F"|" '{print $8}'`" = "TRUE" ] ;then
    echo "---systemctl enable  suspend@`whoami` "
    echo "psus=true" >>"$p"
    [ "$sudo" ] && sudo systemctl enable  suspend@`whoami` 
else
    echo "---systemctl disable  suspend@`whoami` "
    echo "psus=false" >>"$p"
    [ "$sudo" ] && sudo  systemctl disable suspend@`whoami` 
fi

sed -i 's/^ss=.*//' "$p" 
[ "`echo $y |awk -F"|" '{print $9}'`" = "TRUE" ] && echo "ss=true" >>"$p" || echo "ss=false" >>"$p"

sed -i '/^$/d' "$p"
. "$p"
power-exe --start &
    }
    
case $r in
    0) PARSE	;;
    1) cbatticon-gui & ;;
    3)	power-hlp & 
	"$0" & ;;
    *) exit ;;
esac
