#!/bin/bash

#set-dpi - app to set font dpi and restart desktop session
#code by dolphin_oracle June 3, 2016  
#for antiX
#scale_conky by BitJam, from antiX live-init system

# translations stuff
TEXTDOMAINDIR=/usr/share/locale 
TEXTDOMAIN=set-dpi

APPLY=$"Apply"
CLOSE=$"Close"
TITLE=$"Set Font Size"
LABEL1=$"Font Size: "
CURRENT=$"Current"
MESSAGE=$"!!!Added by set-dpi"
MESSAGE2=$"Any open applications will require a restart for changes to take effect"

#initialize selection dialog
get_selections()
{
#get predefined Xft.dpi value from existing ~/Xresources file
DPI=$(xrdb -query |grep dpi|cut -f2)

#if Xft.dpi setting doesn't exist in ~/Xresources, then guess dpi from X 
if [ "$DPI" = "" ]; then
DPI=$(xdpyinfo |grep dots|rev|cut -d ' ' -f4|rev|cut -d 'x' -f1)
fi

SCALE=$(awk "BEGIN {printf \"%.2f\n\",$DPI/96}")

#set up selection gui
# dpi selections are multiples of 96 
selections=$(yad --form --title="$TITLE" --center --window-icon=video-display --width=300 --button="$APPLY"!emblem-default!:0 --button="$CLOSE"!process-stop!:1\
	--field="":LBL ""\
	--field="$LABEL1":CB "$SCALE ($DPI dpi) ($CURRENT)!0.9 (86 dpi)!0.95 (91 dpi)!1.00 (96 dpi)!1.17 (112 dpi)!1.25 (120 dpi)!1.33 (128 dpi)!1.5 (144 dpi)!1.67 (160 dpi)!1.75 (168 dpi)!1.84 (177 dpi)!2.00 (192 dpi)!\
2.17 (208 dpi)!2.25 (216 dpi)!2.33 (224 dpi)!2.5 (240 dpi)!2.67 (256 dpi)!2.75 (264 dpi)!2.84 (273 dpi)!3.00 (288 dpi)!3.25 (312 dpi)!3.50 (336 dpi)!3.75 (360 dpi)!4.00 (384 dpi)!4.25 (408 dpi)!4.50 (432 dpi)!"\
 --field="":LBL ""\
--field="$MESSAGE2":LBL "")
}

actions()
{
#capture the exit code from the selection dialog
status2=$?

#echo $status2

#decide what to do, based on exit code
case $status2 in

	0) adjust_settings
		   ;;
	1) quit
		   ;;
	252) quit
			;;
esac
}

adjust_settings()
{

DPI=$(echo $selections|cut -d '(' -f2|cut -d ' ' -f1)
echo Selected DPI is: $DPI

#make changes to Xresources file
echo $DPI
FILE="/home/$USER/.Xresources"
EX_SETTING=$(grep Xft.dpi $FILE)

#if existing setting is not present, write one into ~/.Xresources
if [ "$EX_SETTING" = "" ]; then
	echo " " >>$FILE
	echo $MESSAGE>>$FILE
	echo "Xft.dpi:	" $DPI>>$FILE
	else
	#if existing setting is present, change it
	sed -i s/"Xft.dpi:.*"/"Xft.dpi: $DPI"/ $FILE
fi

#replace old Xresources file with current version
xrdb $FILE

#scale conky to width for new dpi (modified live-init code from BitJam)
scale_conky

#give all edits a chance to catch up before proceeding
sleep 1

##restart antix control center if running

if [ "$(psg antixcc.sh)" ]; then
	pkill antixcc.sh
	kill $(psg -n program=ControlCenter)
	antixcc.sh &
	sleep .5
fi

#restart desktop-session which will also restart conky
/usr/local/lib/desktop-session/desktop-session-restart &

#let things settle down before bringing back the selection dialog
sleep 5
actionstatus=0
}

quit()
{
	actionstatus=1
}

scale_conky() {
#scale conky to width for new dpi (modified live-init code from BitJam)
#    local dpi=${1%.*}  user=$2  home=$3
#    local file=/$home/.conkyrc  def_width=180  def_gheight=30
local def_width=180  def_gheight=30
local conkyfile=$HOME/.conkyrc
local dpi=$DPI
    [ "$dpi" ] || return
    test -w $conkyfile || return
    head $conkyfile | grep -iq "^#[[:space:]]*Standard[[:space:]]*antiX[[:space:]].conkyrc" || return

    local width=$(($dpi * $def_width / 96 ))
  # Don't make conky smaller than the default size
    if [ $width -lt $def_width ]; then
        dpi=96
        width=$def_width
    fi
    echo "Setting Conky width to $width"
    local gwidth=$((width - 10))
    local gheight=$((2 * dpi * $def_gheight / 96 - $def_gheight))

    bash -c "sed -i -r -e 's/^\s*(maximum_width\s+).*/\1$width/' \
        -e 's/(graph\s+([a-z0-9]+\s+)?)[0-9]+\s*,\s*[0-9]+/\1$gheight,$gwidth/' $conkyfile"
}

##start gui and run actions
## this is actually gets things started.  everything up to now is definition of functions and variables

# set initial status
status=0

while [ $status = "0" ]; do

	#start the parent dialog
	get_selections
	
	#once selection is made, use action function to decide what to do
	actions

	#app status is the same as whatever the last actionstatus is.  If 0, the while loop starts everything over
	status=$actionstatus
	
done
