#!/bin/bash
# --------------------------------------------------------
# UrxvtControl-1.1 (Generic Name : Urxvt Terminal Control)
# Set configuration options of the urxvt terminal emulator
# Roger Grider (C) 2015 GPL3, http://www.gnu.org/licenses/
# --------------------------------------------------------
# Dependencies: urxvt, gtkdialog, xdialog, perl (optional)
# --------------------------------------------------------
# v1.1 # improved internationalization by 'ASRI éducation'
# --------------------------------------------------------

export TEXTDOMAIN=urxvtcontrol
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
. gettext.sh

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@}

if [ ! "`which urxvt`" ]; then
 Xdialog --title Alert --beep --msgbox "\n  $(gettext 'Urxvt terminal emulator not found!')  \n" 0 0 && exit 0
fi

if [ "`which gtkdialog4`" ]; then
 GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
 GTKDIALOG=gtkdialog3
else
 GTKDIALOG=gtkdialog
fi
export GTKDIALOG

export APPDIR="/usr/local/UrxvtControl"
export APPNAME="UrxvtControl"
export VERSION="1.1"
export URXVT_VER=$(urxvt -help 2>&1 | head -n1 | awk '{print $3}' | sed 's/v//') # define urxvt version

#set $XDEFAULTS and backup
export XDEFAULTS="$HOME/.Xdefaults"
if [ ! -r $XDEFAULTS ]; then
 if [ -r $XDEFAULTS.bak ]; then
  cp -f $XDEFAULTS.bak $XDEFAULTS
 fi
else
 touch $XDEFAULTS
fi
[ ! -r $XDEFAULTS.bak ] && [ -r $XDEFAULTS ] && cp -f $XDEFAULTS $XDEFAULTS.bak

#check Perl extensions
export PERL_SUPPORT=$(urxvt --help 2>&1 | grep perl-ext-common 2>/dev/null) # check if urxvt supports Perl extensions
if [ "$PERL_SUPPORT" ] && [ "`grep perl-ext-common $XDEFAULTS 2>/dev/null | cut -d ':' -f2`" != "" ]; then
 PERL_EXT=$(cat $APPDIR/perlextensions 2>/dev/null)
 [ ! "$PERL_EXT" ] && PERL_EXT="On" && echo "On" > $APPDIR/perlextensions
else
 PERL_EXT="Off" && echo "Off" > $APPDIR/perlextensions
fi
export PERL_EXT="$PERL_EXT"
[ "$PERL_EXT" = "On" ] && PERL_EXT_STATUS="$(gettext 'Enabled')" || PERL_EXT_STATUS="$(gettext 'Disabled')"

#set Browser for clickable URLs
if [ "$PERL_SUPPORT" ]; then
 if [ "`which defaultbrowser`" ]; then
  BROWSER="defaultbrowser"
 elif [ "`which firefox`" ]; then
  BROWSER="firefox"
 elif [ "`which seamonkey`" ]; then
  BROWSER="seamonkey"
 elif [ "`which palemoon`" ]; then
  BROWSER="palemoon"
 elif [ "`which googlechrome`" ]; then
  BROWSER="googlechrome"
 elif [ "`which opera`" ]; then
  BROWSER="opera"
 elif [ "`which midori`" ]; then
  BROWSER="midori"
 elif [ "`which qupzilla`" ]; then
  BROWSER="qupzilla"
 elif [ "`which slimboat`" ]; then
  BROWSER="slimboat"
 elif [ "`which dillo`" ]; then
  BROWSER="dillo"
 fi
 export BROWSER
fi

#check standard urxvt options in $XDEFAULTS
[ ! "`grep foreground $XDEFAULTS`" ] && echo -e "\nurxvt.foreground:#000000" >> $XDEFAULTS
[ ! "`grep background $XDEFAULTS`" ] && echo -e "\nurxvt.background:#FFFFFF" >> $XDEFAULTS
if ((`bc <<< "$URXVT_VER>=9.19"`)); then
 [ ! "`grep cursorColor $XDEFAULTS`" ] && echo -e "\nurxvt.cursorColor:#FF0000" >> $XDEFAULTS
 [ ! "`grep colorUL $XDEFAULTS`" ] && echo -e "\nurxvt.colorUL:#4682B4" >> $XDEFAULTS
 [ ! "`grep fading $XDEFAULTS`" ] && echo -e "\nurxvt.fading: 20" >> $XDEFAULTS
fi
[ ! "`grep underlineColor $XDEFAULTS`" ] && echo -e "\nurxvt.underlineColor:#FF0000" >> $XDEFAULTS
[ ! "`grep xft $XDEFAULTS`" ] && echo -e "\nurxvt.font: xft:Nimbus Mono L:style=Regular:pixelsize=15" >> $XDEFAULTS
[ ! "`grep antialias $XDEFAULTS`" ] && sed -i "s/^.*xft.*$/urxvt.font: xft:Nimbus Mono L:style=Regular:pixelsize=15:antialias=true:hinting=true/" $XDEFAULTS
[ ! "`grep hinting $XDEFAULTS`" ] && sed -i "s/^.*xft.*$/urxvt.font: xft:Nimbus Mono L:style=Regular:pixelsize=15:antialias=true:hinting=true/" $XDEFAULTS
((`bc <<< "$URXVT_VER>=9.07"`)) && [ ! "`grep letterSpace $XDEFAULTS`" ] && echo -e "\nurxvt.letterSpace: 0" >> $XDEFAULTS
[ ! "`grep geometry $XDEFAULTS`" ] && echo -e "\nurxvt.geometry: 80x24" >> $XDEFAULTS
[ ! "`grep borderLess $XDEFAULTS`" ] && echo -e "\nurxvt.borderLess:Off" >> $XDEFAULTS
[ ! "`grep transparent $XDEFAULTS`" ] && echo -e "\nurxvt.transparent: Off" >> $XDEFAULTS
[ ! "`grep shading $XDEFAULTS`" ] && echo -e "\nurxvt.shading: 85" >> $XDEFAULTS
[ ! "`grep scrollBar: $XDEFAULTS`" ] && echo -e "\nurxvt.scrollBar: True" >> $XDEFAULTS
[ ! "`grep scrollBar_right $XDEFAULTS`" ] && echo -e "\nurxvt.scrollBar_right: False" >> $XDEFAULTS
[ ! "`grep scrollstyle $XDEFAULTS`" ] && echo -e "\nurxvt.scrollstyle: rxvt" >> $XDEFAULTS
[ ! "`grep thickness $XDEFAULTS`" ] && echo -e "\nurxvt.thickness: 10" >> $XDEFAULTS
[ ! "`grep urxvt\.saveLines $XDEFAULTS`" ] && echo -e "\nurxvt.saveLines: 1000" >> $XDEFAULTS
[ ! "`grep scrollTtyOutput $XDEFAULTS`" ] && echo -e "\nurxvt.scrollTtyOutput: False" >> $XDEFAULTS
[ ! "`grep scrollTtyKeypress $XDEFAULTS`" ] && echo -e "\nurxvt.scrollTtyKeypress: True" >> $XDEFAULTS
[ ! "`grep secondaryScroll $XDEFAULTS`" ] && echo -e "\nurxvt.secondaryScroll: True" >> $XDEFAULTS
[ ! "`grep cursorUnderline $XDEFAULTS`" ] && echo -e "\nurxvt.cursorUnderline: True" >> $XDEFAULTS
[ ! "`grep cursorBlink $XDEFAULTS`" ] && echo -e "\nurxvt.cursorBlink: True" >> $XDEFAULTS
if [ "$PERL_SUPPORT" ]; then
 [ ! "`grep perl-ext-common $XDEFAULTS`" ] && echo -e "\nurxvt.perl-ext-common: default,tabbed,matcher" >> $XDEFAULTS
 [ ! "`grep tabbar-fg $XDEFAULTS`" ] && echo -e "\nURxvt.tabbed.tabbar-fg: 2" >> $XDEFAULTS
 [ ! "`grep tabbar-bg $XDEFAULTS`" ] && echo -e "\nURxvt.tabbed.tabbar-bg: 0" >> $XDEFAULTS
 [ ! "`grep tab-fg $XDEFAULTS`" ] && echo -e "\nURxvt.tabbed.tab-fg: 0" >> $XDEFAULTS
 [ ! "`grep tab-bg $XDEFAULTS`" ] && echo -e "\nURxvt.tabbed.tab-bg: 6" >> $XDEFAULTS
 [ ! "`grep url-launcher $XDEFAULTS`" ] && echo -e "\nurxvt.url-launcher:" >> $XDEFAULTS
 sed -i "s/^.*urxvt.url-launcher.*$/urxvt.url-launcher: $BROWSER/" $XDEFAULTS
 [ ! "`grep matcher\.button $XDEFAULTS`" ] && echo -e "\nurxvt.matcher.button: 1" >> $XDEFAULTS
fi
awk '!NF || !seen[$0]++' $XDEFAULTS > /tmp/tmp && mv -f /tmp/tmp $XDEFAULTS #remove duplicate text lines
sed -i '/^$/N;/\n$/D' $XDEFAULTS #remove consecutive blank lines and EOF blank lines

#auto-adjust header text color for light or dark GTK themes
if [ "`grep 'Stardust_dark' $HOME/.gtkrc-2.0 2>/dev/null`" ]; then #gold text for unique Stardust dark-grey-orange themes
  COLOR=#D7B740
else #light-blue text for common dark themes, otherwise medium blue
  [ "`grep -Ei 'black|dark|night' $HOME/.gtkrc-2.0 2>/dev/null`" ] && COLOR=#84aad9 || COLOR=#3272C0
fi
export COLOR

#functions
func_help (){
export URXVT_CONTROL_HELP="
<window title=\"$APPNAME - $(gettext 'Help')\" icon-name=\"urxvtcontrol\" window-position=\"2\">
 <notebook labels=\"$(gettext 'About')|$(gettext 'License')\">
  <frame>
   <pixmap><input file>$APPDIR/icons/urxvtcontrol.svg</input><height>48</height><width>48</width></pixmap>
   <text use-markup=\"true\"><label>\"<b><span size='"'x-large'"'>Urxvt</span><span size='"'x-large'"' color='"$COLOR"'>Control</span> $VERSION</b>\"</label></text>
   <text use-markup=\"true\"><label>\"<b>$(gettext 'Rxvt-unicode Terminal Control')</b>\"</label></text>
   <text><label>\"$(gettext 'Copyright (C) 2015 Roger D. Grider')\"</label></text>
   <text height-request=\"10\"><label>\"\"</label></text>
   <hbox homogeneous=\"true\">
    <button width-request=\"170\">
     <label>\"$(gettext 'Online Help')\"</label>
     <action>defaulthtmlviewer http://www.murga-linux.com/puppy/viewtopic.php?p=821592#821592 &</action>
    </button>
    <button width-request=\"170\">
     <label>\"$(gettext 'Online Manual')\"</label>
     <action>defaultbrowser https://wiki.archlinux.org/index.php/rxvt-unicode &</action>
    </button>
   </hbox>
  </frame>
  <frame>
   <text use-markup=\"true\"><label>\"$APPNAME (Urxvt Terminal Control) $(gettext 'is free software which you can redistribute and/or modify under the terms of the <b>GNU General Public License</b> as published by the Free Software Foundation; either GPLv3 or later version.')

$(gettext 'This program is distributed in the hope that it will be useful, but') <b><span color='"'red'"'>$(gettext 'WITHOUT ANY WARRANTY')</span></b>. $(gettext 'See the GNU General Public License homepage below for additional information.')\"</label></text>
   <hbox homogeneous=\"true\">
    <button>
     <label>\"   http://www.gnu.org/licenses/   \"</label>
     <action>defaulthtmlviewer http:\/\/www.gnu.org\/licenses\/</action>
    </button>
   </hbox>
  </frame>
 </notebook>
</window>"
$GTKDIALOG -p URXVT_CONTROL_HELP
unset URXVT_CONTROL_HELP
}
export -f func_help

func_allplugins (){ 
if [ "$PERL_EXT" = "On" ]; then
  REPLY=$(Xdialog --stdout --title "$(gettext 'Urxvt Plugins')" --no-tags --no-cancel --radiolist "$(gettext 'Status: Enabled')" 10 35 2  "1" "$(gettext 'Enable plugin functions')" "on"  "2" "$(gettext 'Disable plugin functions')" "off" )
  if [ "$REPLY" = "1" ]; then
   echo -n "On" > $APPDIR/perlextensions
   sed -i "s/^.*perl-ext-common.*$/urxvt.perl-ext-common: default,selection-to-clipboard,tabbed,matcher/" $XDEFAULTS
   sed -i '/URxvt.perl-ext:/d' $XDEFAULTS
  elif [ "$REPLY" = "2" ]; then
   echo -n "Off" > $APPDIR/perlextensions
   sed -i "s/^.*perl-ext-common.*$/urxvt.perl-ext-common:/" $XDEFAULTS
   sed -i '/perl-ext-common/a URxvt.perl-ext:' $XDEFAULTS
  else
   UrxvtControl &
   exit 1
  fi
else 
  REPLY=$(Xdialog --stdout --title "$(gettext 'Urxvt Plugins')" --no-tags --no-cancel --radiolist "$(gettext 'Status: Disabled')" 10 35 2  "1" "$(gettext 'Disable plugin functions')" "on"  "2" "$(gettext 'Enable plugin functions')" "off" )
  if [ "$REPLY" = "1" ]; then
   echo -n "Off" > $APPDIR/perlextensions
   sed -i "s/^.*perl-ext-common.*$/urxvt.perl-ext-common:/" $XDEFAULTS
   sed -i '/perl-ext-common/a URxvt.perl-ext:' $XDEFAULTS
  elif [ "$REPLY" = "2" ]; then
   echo -n "On" > $APPDIR/perlextensions
   sed -i "s/^.*perl-ext-common.*$/urxvt.perl-ext-common: default,selection-to-clipboard,tabbed,matcher/" $XDEFAULTS
   sed -i '/URxvt.perl-ext:/d' $XDEFAULTS
  else
   UrxvtControl &
   exit 1
  fi
fi
UrxvtControl &
}
export -f func_allplugins

func_tabplugin (){ 
((`bc <<< "$URXVT_VER>=9.20"`)) && CLIPBOARD=",selection-to-clipboard" || CLIPBOARD=""
if [ "$TABS" = "On" ]; then
 sed -i "s/^.*perl-ext-common.*$/urxvt.perl-ext-common: default$CLIPBOARD,tabbed,matcher/" $XDEFAULTS
 sed -i '/URxvt.perl-ext:/d' $XDEFAULTS
else
 sed -i "s/^.*perl-ext-common.*$/urxvt.perl-ext-common: default$CLIPBOARD,matcher/" $XDEFAULTS
 sed -i '/URxvt.perl-ext:/d' $XDEFAULTS
fi
}
export -f func_tabplugin

func_urxvtrestore (){
Xdialog --title "$(gettext 'Restore')" --cancel-label "$(gettext 'No')" --ok-label "$(gettext 'Yes')"  --yesno "\n $(gettext 'Restore original Urxvt settings?') \n" 0 0
if [[ $? == 0 ]]; then
 if [ -f $XDEFAULTS.bak ]; then
  cp -f $XDEFAULTS.bak $XDEFAULTS
  Xdialog --title Urxvt --no-buttons --infobox "\n     $(gettext 'Original') $HOME/.Xdefaults $(gettext 'restored!')     \n" 0 0 3000
 else
  Xdialog --title Urxvt --msgbox "\n     $(gettext 'No backup of') $HOME/.Xdefaults $(gettext 'available!')     \n" 0 0
 fi
fi
UrxvtControl &
}
export -f func_urxvtrestore

func_urxvtset (){
if [ "$PERL_EXT" = "On" ]; then
 func_tabplugin
else
 sed -i "s/^.*perl-ext-common.*$/urxvt.perl-ext-common:/" $XDEFAULTS
 sed -i '/perl-ext-common/a URxvt.perl-ext:' $XDEFAULTS
fi
FONT=$(echo "$FONT" | sed 's/:/:style=/')
[ "$ANTIALIAS" = "On" ] && ANTIALIAS="true" || ANTIALIAS="false"
[ "$HINTING" = "On" ] && HINTING="true" || HINTING="false"
[ "$SCROLLBAR" = "Off" ] && SCROLLBAR="False" || SCROLLBAR="True"
[ "$SCROLLSIDE" = "Right" ] && SCROLLSIDE="True" || SCROLLSIDE="False"
sed -i -e "s/^.*foreground.*$/urxvt.foreground:$FGCOLOR/" \
-e "s/^.*background.*$/urxvt.background:$BGCOLOR/" \
-e "s/^.*cursorColor.*$/urxvt.cursorColor:$CURSORCOLOR/" \
-e "s/^.*colorUL.*$/urxvt.colorUL:$URLCOLOR/" \
-e "s/^.*underlineColor.*$/urxvt.underlineColor:$UNDERLINECOLOR/" \
-e "s/^.*tabbar-fg.*$/URxvt.tabbed.tabbar-fg: $TABBAR_FGCOLOR/" \
-e "s/^.*tabbar-bg.*$/URxvt.tabbed.tabbar-bg: $TABBAR_BGCOLOR/" \
-e "s/^.*tab-fg.*$/URxvt.tabbed.tab-fg: $TAB_FGCOLOR/" \
-e "s/^.*tab-bg.*$/URxvt.tabbed.tab-bg: $TAB_BGCOLOR/" \
-e "s/^.*xft.*$/urxvt.font: xft:$FONT:pixelsize=$FONTSIZE:antialias=$ANTIALIAS:hinting=$HINTING/" \
-e "s/^.*letterSpace.*$/urxvt.letterSpace: $FONTSPACING/" \
-e "s/^.*geometry.*$/urxvt.geometry: $GUIWIDTH"x"$GUIHEIGHT/" \
-e "s/^.*borderLess.*$/urxvt.borderLess: $BORDERLESS/" \
-e "s/^.*transparent.*$/urxvt.transparent: $TRANSPARENCY/" \
-e "s/^.*shading.*$/urxvt.shading: $SHADING/" \
-e "s/^.*scrollBar:.*$/urxvt.scrollBar: $SCROLLBAR/" \
-e "s/^.*scrollBar_right.*$/urxvt.scrollBar_right: $SCROLLSIDE/" \
-e "s/^.*scrollstyle.*$/urxvt.scrollstyle: $SCROLLSTYLE/" \
-e "s/^.*thickness.*$/urxvt.thickness: $SCROLLWIDTH/" \
-e "s/^.*urxvt.saveLines.*$/urxvt.saveLines: $SCROLLBACK/" $XDEFAULTS 2>/dev/null
Xdialog --title Urxvt --no-buttons --infobox "\n     $(gettext 'Urxvt configuration updated!')     \n" 0 0 3000
}
export -f func_urxvtset

#define current urxvt settings
FGCOLOR=$(grep foreground $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$FGCOLOR" ] && FGCOLOR="#000000"
BGCOLOR=$(grep background $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$BGCOLOR" ] && BGCOLOR="#FFFFFF"
if ((`bc <<< "$URXVT_VER>=9.19"`)); then
 CURSORCOLOR=$(grep cursorColor $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
 [ ! "$CURSORCOLOR" ] && CURSORCOLOR="#FF0000"
 URLCOLOR=$(grep colorUL $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
 [ ! "$URLCOLOR" ] && URLCOLOR="#4682B4"
fi
UNDERLINECOLOR=$(grep underlineColor $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$UNDERLINECOLOR" ] && UNDERLINECOLOR="#FF0000"
#
FONT=$(grep xft $XDEFAULTS | cut -d ':' -f3,4 | sed 's/style=//g')
[ ! "$FONT" ] && FONT="Nimbus Mono L:Regular"
FONT_ITEMS=$(fc-list | sed 's/: /#/g' | cut -d '#' -f2- | cut -d ',' -f1 | awk '{print "<item>"$1" "$2" "$3" "$4" "$5"</item>"}' | sed -e 's/style=//g' -e 's/  //g' -e 's/ </</g' | sort -fu)
FONTSIZE=$(grep xft $XDEFAULTS | cut -d ':' -f5 | cut -d '=' -f2)
[ ! "$FONTSIZE" ] && FONTSIZE"15"
ANTIALIAS=$(grep xft $XDEFAULTS | cut -d ':' -f6 | cut -d '=' -f2)
[ ! "$ANTIALIAS" ] && ANTIALIAS="true"
[ "$ANTIALIAS" = "false" ] && ANTIALIAS="Off" || ANTIALIAS="On"
HINTING=$(grep xft $XDEFAULTS | cut -d ':' -f7 | cut -d '=' -f2)
[ ! "$HINTING" ] && HINTING="true"
[ "$HINTING" = "false" ] && HINTING="Off" || HINTING="On"
if ((`bc <<< "$URXVT_VER>=9.07"`)); then
 FONTSPACING=$(grep letterSpace $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
 [ ! "$FONTSPACING" ] && FONTSPACING="0"
fi
#
GEOMETRY=$(grep geometry $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
GUIWIDTH=$(echo $GEOMETRY | cut -d 'x' -f1)
[ ! "$GUIWIDTH" ] && GUIWIDTH="80"
GUIHEIGHT=$(echo $GEOMETRY | cut -d 'x' -f2)
[ ! "$GUIHEIGHT" ] && GUIHEIGHT="24"
BORDERLESS=$(grep borderLess $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$BORDERLESS" ] && BORDERLESS="Off"
TRANSPARENCY=$(grep transparent $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$TRANSPARENCY" ] && TRANSPARENCY="Off"
SHADING=$(grep shading $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$SHADING" ] && SHADING="85"
#
SCROLLBAR=$(grep scrollBar: $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$SCROLLBAR" ] && SCROLLBAR="True"
[ "$SCROLLBAR" = "True" ] && SCROLLBAR="On" || SCROLLBAR="Off"
SCROLLSIDE=$(grep scrollBar_right $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$SCROLLSIDE" ] && SCROLLSIDE="False"
[ "$SCROLLSIDE" = "False" ] && SCROLLSIDE="Left" || SCROLLSIDE="Right"
SCROLLSTYLE=$(grep scrollstyle $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$SCROLLSTYLE" ] && SCROLLSTYLE="rxvt"
SCROLLWIDTH=$(grep thickness $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$SCROLLWIDTH" ] && SCROLLWIDTH="10"
SCROLLBACK=$(grep urxvt\.saveLines $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
[ ! "$SCROLLBACK" ] && SCROLLBACK="1000"
#
TABS=$(grep 'perl-ext-common' $XDEFAULTS | grep tabbed)
[ "$TABS" ] && TABS="On" || TABS="Off"
if [ "$PERL_SUPPORT" ]; then
 TABBAR_FGCOLOR=$(grep tabbar-fg $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
 [ ! "$TABBAR_FGCOLOR" ] && TABBAR_FGCOLOR="2"
 TABBAR_BGCOLOR=$(grep tabbar-bg $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
 [ ! "$TABBAR_BGCOLOR" ] && TABBAR_BGCOLOR="0"
 TAB_FGCOLOR=$(grep tab-fg $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
 [ ! "$TAB_FGCOLOR" ] && TAB_FGCOLOR="0"
 TAB_BGCOLOR=$(grep tab-bg $XDEFAULTS | cut -d ':' -f2 | sed 's/ //g')
 [ ! "$TAB_BGCOLOR" ] && TAB_BGCOLOR="6"
fi

#check urxvt version and add appropriate color buttons to GUI
if ((`bc <<< "$URXVT_VER>=9.19"`)); then
  COLORBUTTONS="
  <vbox>
	<text><label>$(gettext 'Cursor')</label></text>
	<colorbutton width-request=\"103\" title=\"$(gettext 'Select Cursor Color')\" tooltip-text=\" $(gettext 'Window cursor color') \">
	 <input>echo '"$CURSORCOLOR"'</input>
	 <variable>CURSORCOLOR</variable>
	</colorbutton>
  </vbox>
  <vbox>
	<text><label>$(gettext 'URL')</label></text>
	<colorbutton width-request=\"103\" title=\"$(gettext 'Select URL Color')\" tooltip-text=\" $(gettext 'Clickable web-address color') \">
	 <input>echo '"$URLCOLOR"'</input>
	 <variable>URLCOLOR</variable>
	</colorbutton>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Underline')</label></text>
	<colorbutton width-request=\"103\" title=\"$(gettext 'Select Underline Color')\" tooltip-text=\" $(gettext 'Clickable web-address underline color') \">
	 <input>echo '"$UNDERLINECOLOR"'</input>
	 <variable>UNDERLINECOLOR</variable>
	</colorbutton>
  </vbox>"
else
  COLORBUTTONS="
  <vbox>
	<text><label>$(gettext 'Cursor')</label></text>
	<colorbutton width-request=\"103\" title=\"$(gettext 'Select Cursor Color')\" tooltip-text=\" $(gettext 'Window cursor color') \">
	 <input>echo '"$UNDERLINECOLOR"'</input>
	 <variable>UNDERLINECOLOR</variable>
	</colorbutton>
  </vbox>"
fi

#if urxvt tabs are supported, add tab-color options to GUI
TABCOLORBUTTONS=""
if [ "$PERL_EXT" = "On" ]; then
  TABCOLORTOOLTIP="
00 - Black
01 - Dark Red
02 - Dark Green
03 - Dark Yellow
04 - Dark Blue
05 - Dark Magenta
06 - Dark Cyan
07 - Grey
08 - Dark Grey
09 - Light Red
10 - Light Green
11 - Light Yellow
12 - Light Blue
13 - Light Magenta
14 - Light Cyan
15 - White"
  ITEMS="
	 <item>0</item>
	 <item>1</item>
	 <item>2</item>
	 <item>3</item>
	 <item>4</item>
	 <item>5</item>
	 <item>6</item>
	 <item>7</item>
	 <item>8</item>
	 <item>9</item>
	 <item>10</item>
	 <item>11</item>
	 <item>12</item>
	 <item>13</item>
	 <item>14</item>
	 <item>15</item>"
  TABCOLORBUTTONS="
  <vbox>
  <hbox homogeneous=\"true\">
  <vbox>
	<text><label>$(gettext 'Tabs')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'Enable/Disable urxvt-tabs plugin') 

      (Tab Keyboard Shortcuts)

Shift+Down 	New tab
Shift+Left 	Go to left tab
Shift+Right 	Go to right tab
Ctrl+Left 	Move tab to left
Ctrl+Right 	Move tab to right
Ctrl+d		Close tab \">
	 <variable>TABS</variable>
	 <default>$TABS</default>
	 <item>On</item>
	 <item>Off</item>
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'TabBar-FG')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'TabBar foreground color') 
$(gettext "$TABCOLORTOOLTIP") \">
	 <variable>TABBAR_FGCOLOR</variable>
	 <default>$TABBAR_FGCOLOR</default>
	 $ITEMS
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'TabBar-BG')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'TabBar background color') 
$(gettext "$TABCOLORTOOLTIP") \">
	 <variable>TABBAR_BGCOLOR</variable>
	 <default>$TABBAR_BGCOLOR</default>
	 $ITEMS
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Tab-FG')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'Tab foreground color') 
$(gettext "$TABCOLORTOOLTIP") \">
	 <variable>TAB_FGCOLOR</variable>
	 <default>$TAB_FGCOLOR</default>
	 $ITEMS
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Tab-BG')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'Tab background color') 
$(gettext "$TABCOLORTOOLTIP") \">
	 <variable>TAB_BGCOLOR</variable>
	 <default>$TAB_BGCOLOR</default>
	 $ITEMS
	</comboboxtext>
  </vbox> 
  </hbox>
  </vbox>"
fi

#check urxvt version and enable/disable sensitive state of Spacing (letterSpace) spinbutton
SS="False"
((`bc <<< "$URXVT_VER>=9.07"`)) && SS="True"

#check urxvt support and enable/disable extra scrollstyles
EXTRASTYLES=$(urxvt --help 2>&1 | grep NeXT+xterm 2>/dev/null)
SCROLLSTYLES=""
TOOLTIP="tooltip-text=\" $(gettext 'rxvt:default | plain:no decoration')"
if [ "$EXTRASTYLES" ]; then
 SCROLLSTYLES="
	 <item>next</item>
	 <item>xterm</item>"
 TOOLTIP="tooltip-text=\" $(gettext 'rxvt:default | plain:no decoration | next:bottom scroll buttons') 
 $(gettext 'xterm:click-and-drag the scrollbar with middle mouse button')"
fi

#check urxvt Perl-extension support and enable/disable tray Plugins button
PLUGINS_BUTTON=""
if [ "$PERL_SUPPORT" ] && [ "`grep perl-ext-common $XDEFAULTS 2>/dev/null`" ]; then
PLUGINS_BUTTON="
	<button height-request=\"30\" width-request=\"85\" use-underline=\"true\" tooltip-text=\" $(gettext 'Enable/Disable all urxvt plugins (Perl extensions) 
 such as copy to X-Clipboard, Tabs & URL launch.') \">
	 <label>\"$(gettext '_Plugins')\"</label>
	 <action>func_allplugins &</action>
	 <action type=\"exit\">quit_now</action>
	</button>"
fi

#define custom font
echo "style \"customsize\"
{
  font_name=\"Sans 10\"
}
widget \"*\" style \"customsize\"
class \"*\" style \"customsize\"

style \"customframe\"
{
  font_name=\"Sans bold 8\"
}
widget \"*.GtkFrame.GtkLabel\" style \"customframe\"
class \"*.GtkFrame.GtkLabel\" style \"customframe\"" > "$APPDIR/gtkrc_custom"

export GTK2_RC_FILES="$APPDIR/gtkrc_custom:${HOME}/.gtkrc-2.0"

#define title-bar icon
ln -sf $APPDIR/icons/urxvtcontrol.svg /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

#main dialog
export URXVT_CONTROL="
<window title=\"UTC\" icon-name=\"urxvtcontrol\" resizable=\"false\" window-position=\"1\">
 <vbox>
	<text height-request=\"2\"><label>\"\"</label></text>
	<text use-markup=\"true\" tooltip-text=\"$(eval_gettext ' $APPNAME $VERSION 
 Rxvt-unicode $URXVT_VER 
 Perl plugins: $PERL_EXT_STATUS ')\"><label>\"<b><span size='"'large'"'>$(gettext 'Urxvt Terminal Control')</span></b>\"</label></text>
  <text height-request=\"1\"><label>\"\"</label></text>
  <vbox>
  <frame $(gettext 'Color')>
  <vbox>
  <hbox homogeneous=\"true\">
  <vbox>
	<text><label>$(gettext 'Text')</label></text>
	<colorbutton width-request=\"103\" title=\"$(gettext 'Select Text Color')\" tooltip-text=\" $(gettext 'Window foreground color') \">
	 <input>echo '"$FGCOLOR"'</input>
	 <variable>FGCOLOR</variable>
	</colorbutton>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Background')</label></text>
	<colorbutton width-request=\"103\" title=\"$(gettext 'Select Background Color')\" tooltip-text=\" $(gettext 'Window background color') \">
	 <input>echo '"$BGCOLOR"'</input>
	 <variable>BGCOLOR</variable>
	</colorbutton>
  </vbox>
	$COLORBUTTONS
  </hbox>
  </vbox>
	$TABCOLORBUTTONS
  </frame>

  <frame $(gettext 'Font')>
  <vbox>
  <hbox>
  <vbox>
	<text><label>$(gettext 'Name & Style')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"265\" tooltip-text=\" $(gettext 'Font name and style attributes') 
 $(gettext 'Default: fixed-width mono fonts') \">
	 <variable>FONT</variable>
	 <default>$FONT</default>
	$FONT_ITEMS
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Size')</label></text>
	<spinbutton height-request=\"30\" width-request=\"62\" range-min=\"10\" range-max=\"24\" range-step=\"1\" range-value=\"15\" tooltip-text=\" $(gettext 'Default:15  Range:10-24') \">
	 <variable>FONTSIZE</variable>
	 <input>echo '"$FONTSIZE"'</input>
	</spinbutton>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Antialias')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"63\" tooltip-text=\" $(gettext 'Enable/Disable anti-aliased fonts') \">
	 <variable>ANTIALIAS</variable>
	 <default>$ANTIALIAS</default>
	 <item>On</item>
	 <item>Off</item>
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Hinting')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"63\" tooltip-text=\" $(gettext 'Enable/Disable font hinting') \">
	 <variable>HINTING</variable>
	 <default>$HINTING</default>
	 <item>On</item>
	 <item>Off</item>
	</comboboxtext>
  </vbox>

  <vbox>
	<text><label>$(gettext 'Spacing')</label></text>
	<spinbutton sensitive=\"$SS\" height-request=\"30\" width-request=\"62\" range-min=\"-5\" range-max=\"0\" range-step=\"1\" range-value=\"0\" tooltip-text=\" $(gettext 'Control the distance (in pixels) between terminal characters') 
 $(gettext '(0 is normal spacing and -1 decreases spacing by one pixel)') \">
	 <variable>FONTSPACING</variable>
	 <input>echo '"$FONTSPACING"'</input>
	</spinbutton>
  </vbox>
  </hbox>
  </vbox>
  </frame>

  <frame $(gettext 'Interface')>
  <vbox>
  <hbox homogeneous=\"true\">
  <vbox>
	<text><label>$(gettext 'Width')</label></text>
	<spinbutton height-request=\"30\" width-request=\"103\" range-min=\"50\" range-max=\"200\" range-step=\"1\" range-value=\"80\" tooltip-text=\" $(gettext 'Default:80  Range:50-200') \">
	 <variable>GUIWIDTH</variable>
	 <input>echo '"$GUIWIDTH"'</input>
	</spinbutton>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Height')</label></text>
	<spinbutton height-request=\"30\" width-request=\"103\" range-min=\"10\" range-max=\"80\" range-step=\"1\" range-value=\"24\" tooltip-text=\" $(gettext 'Default:24  Range:10-80') \">
	 <variable>GUIHEIGHT</variable>
	 <input>echo '"$GUIHEIGHT"'</input>
	</spinbutton>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Borderless')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'Enable/Disable borderless terminal') \">
	 <variable>BORDERLESS</variable>
	 <default>$BORDERLESS</default>
	 <item>On</item>
	 <item>Off</item>
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Transparency')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'Enable/Disable transparency') \">
	 <variable>TRANSPARENCY</variable>
	 <default>$TRANSPARENCY</default>
	 <item>On</item>
	 <item>Off</item>
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Shading')</label></text>
	<spinbutton height-request=\"30\" width-request=\"103\" range-min=\"-100\" range-max=\"100\" range-step=\"1\" range-value=\"85\" tooltip-text=\" $(gettext 'Transparency shading: default value 85') 
 $(gettext 'Darken (0 .. 100) or lighten (-1 .. -100)') \">
	 <variable>SHADING</variable>
	 <input>echo '"$SHADING"'</input>
	</spinbutton>
  </vbox>
  </hbox>
  </vbox>

  <vbox>
  <hbox homogeneous=\"true\">
  <vbox>
	<text><label>$(gettext 'Scroll Bar')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'Enable/Disable scrollbar') \">
	 <variable>SCROLLBAR</variable>
	 <default>$SCROLLBAR</default>
	 <item>On</item>
	 <item>Off</item>
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Scroll Side')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" tooltip-text=\" $(gettext 'Set scrollbar right or left') \">
	 <variable>SCROLLSIDE</variable>
	 <default>$SCROLLSIDE</default>
	 <item>Left</item>
	 <item>Right</item>
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Scroll Style')</label></text>
	<comboboxtext wrap-width=\"1\" height-request=\"30\" width-request=\"103\" $TOOLTIP \">
	 <variable>SCROLLSTYLE</variable>
	 <default>$SCROLLSTYLE</default>
	 <item>rxvt</item>
	 <item>plain</item>
	 $SCROLLSTYLES
	</comboboxtext>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Scroll Width')</label></text>
	<spinbutton height-request=\"30\" width-request=\"103\" range-min=\"7\" range-max=\"20\" range-step=\"1\" range-value=\"10\" tooltip-text=\" $(gettext 'Set the scrollbar width 
 Default:10 Range:7-20') \">
	 <variable>SCROLLWIDTH</variable>
	 <input>echo '"$SCROLLWIDTH"'</input>
	</spinbutton>
  </vbox>
  <vbox>
	<text><label>$(gettext 'Scroll Back')</label></text>
	<spinbutton height-request=\"30\" width-request=\"103\" range-min=\"0\" range-max=\"65535\" range-step=\"10\" range-value=\"1000\" tooltip-text=\" $(gettext 'Number of lines in scrollback buffer') 
 $(gettext 'Default:1000 lines  Range:0-65535') \">
	 <variable>SCROLLBACK</variable>
	 <input>echo '"$SCROLLBACK"'</input>
	</spinbutton>
  </vbox>
  </hbox>
  </vbox>
  </frame>
  </vbox>

  <frame $(gettext 'Manage')>
  <hbox homogeneous=\"true\">
	<button height-request=\"30\" width-request=\"85\" use-underline=\"true\" tooltip-text=\" $(eval_gettext '$APPNAME help') \">
	 <label>\"$(gettext '_Help')\"</label>
	 <action>func_help &</action>
	</button>
	$PLUGINS_BUTTON
	<button height-request=\"30\" width-request=\"85\" use-underline=\"true\" tooltip-text=\" $(gettext 'Restore original urxvt settings') \">
	 <label>\"$(gettext '_Restore')\"</label>
	 <action>func_urxvtrestore &</action>
	 <action type=\"exit\">quit_now</action>
	</button>
	<button height-request=\"30\" width-request=\"85\" use-underline=\"true\" tooltip-text=\" $(gettext 'Edit urxvt settings in default text editor') \">
	 <label>\"$(gettext '_Edit')\"</label>
	 <action>defaulttexteditor $XDEFAULTS &</action>
	</button>
	<button height-request=\"30\" width-request=\"85\" use-underline=\"true\" tooltip-text=\" $(gettext 'Apply new urxvt settings') \">
	 <label>\"$(gettext '_Apply')\"</label>
	 <action>func_urxvtset &</action>
	</button>
	<button height-request=\"30\" width-request=\"85\" use-underline=\"true\">
	 <label>\"$(gettext '_Close')\"</label>
	 <action type=\"exit\">quit_now</action>
	</button>
  </hbox>
  </frame>
 </vbox>
</window>"

$GTKDIALOG -p URXVT_CONTROL
unset URXVT_CONTROL

exit 0
