#!/bin/ash

# Xgamma-GUI
# Copyright (C) JakeSFR 2018
#
# License: GNU GPL Version 3 or later

# std localisation stanza
export TEXTDOMAIN=fatdog
. gettext.sh

APPTITLE="$(gettext 'Xgamma GUI')"

# Depending on locale, decimal separator for Gtkdialog's hscale has to be either . or ,
# Use dc to get the correct one
#DECSEP=$(busybox dc 1 10 / p | tr -cd ',.')
DECSEP=$(seq .1 .1 .1 | tr -cd ',.')

RANGE_STEP=0.001
RANGE_MIN=0.1
RANGE_MAX=10

# Get current values
set -- $(xgamma 2>&1 | grep -Eo '[0-9]+.[0-9]+' | tr '\n' ' ')
XRED=$1
XGREEN=$2
XBLUE=$3

echo '
<window title="'${APPTITLE}'" resizable="false" image-name="/usr/share/pixmaps/themes/puppy16/idea16.xpm">

	<vbox>

		<text use-markup="true">
			<label>"<span font='"'serif'"' weight='"'bold'"' size='"'x-large'"'><b>'${APPTITLE}'</b></span>"</label>
		</text>
		<text use-markup="true">
			<label>"<span><i>'$(gettext "Alter gamma correction")'</i></span>"</label>
		</text>

		<frame '$(gettext "Red")'>
			<hscale update-policy="2" range-min="'${RANGE_MIN//[,.]/${DECSEP}}'" range-max="'${RANGE_MAX//[,.]/${DECSEP}}'" range-step="'${RANGE_STEP//[,.]/${DECSEP}}'" width-request="300">
				<variable>varRED</variable>
				<default>'${XRED//[,.]/${DECSEP}}'</default>
				<item>1|3</item>
				<input>echo $varBLUE</input>
				<action>xgamma -rgamma ${varRED//,/.}</action>
				<action condition="active_is_true(varSYNC)">refresh:varGREEN</action>
				<action condition="active_is_true(varSYNC)">refresh:varBLUE</action>
			</hscale>
		</frame>

		<frame '$(gettext "Green")'>
			<hscale update-policy="2" range-min="'${RANGE_MIN//[,.]/${DECSEP}}'" range-max="'${RANGE_MAX//[,.]/${DECSEP}}'" range-step="'${RANGE_STEP//[,.]/${DECSEP}}'" width-request="300">
				<variable>varGREEN</variable>
				<default>'${XGREEN//[,.]/${DECSEP}}'</default>
				<item>1|3</item>
				<input>echo $varRED</input>
				<action>xgamma -ggamma ${varGREEN//,/.}</action>
				<action condition="active_is_true(varSYNC)">refresh:varBLUE</action>
				<action condition="active_is_true(varSYNC)">refresh:varRED</action>
			</hscale>
		</frame>
		
		<frame '$(gettext "Blue")'>
			<hscale update-policy="2" range-min="'${RANGE_MIN//[,.]/${DECSEP}}'" range-max="'${RANGE_MAX//[,.]/${DECSEP}}'" range-step="'${RANGE_STEP//[,.]/${DECSEP}}'" width-request="300">
				<variable>varBLUE</variable>
				<default>'${XBLUE//[,.]/${DECSEP}}'</default>
				<item>1|3</item>
				<input>echo $varGREEN</input>
				<action>xgamma -bgamma ${varBLUE//,/.}</action>
				<action condition="active_is_true(varSYNC)">refresh:varRED</action>
				<action condition="active_is_true(varSYNC)">refresh:varGREEN</action>
			</hscale>
		</frame>
		
		<hbox>
			
			<hbox>
				<checkbox>
					<variable>varSYNC</variable>
					<label>'$(gettext "Sync sliders")'</label>
					<default>false</default>
				</checkbox>
			</hbox>
			<text space-fill="true" space-expand="true"><label>""</label></text>


		</hbox>
		
		<hseparator></hseparator>
		
		<hbox space-fill="true" space-expand="true">
			<button>
				<label>'$(gettext "Apply")'</label>
				<input file stock="gtk-apply"></input>
				<action>exit:apply</action>
			</button>
			<button>
				<label>'$(gettext "Cancel")'</label>
				<input file stock="gtk-cancel"></input>
				<action>exit:cancel</action>
			</button>		
		</hbox>

	</vbox>

	<action signal="delete-event">exit:abort</action>
</window>' |
gtkdialog -s | while read VAR; do
	eval "$VAR"

	# Restore original values if window was closed by Cancel or X btn
	case "$EXIT" in
		abort|cancel)
			xgamma -rgamma ${XRED} -ggamma ${XGREEN} -bgamma ${XBLUE}
		;;
	esac
done

exit
