#!/bin/bash

# =======================
# Define Colors
RED='\e[41m'
NC='\e[0m' # No color
BG='\e[7m' # Highlighting Background color
TC='\e[1m' # Highlighting Text color

MINFONTSIZE=6
MAXFONTSIZE=72
STATUSVAR=1
CANCELSTATUS=0
SELECTEDITEM=""
# EDITOR is only used when it points at a real command
function resolve_editor() {
	if [ -n "$EDITOR" ] && command -v "$EDITOR" >/dev/null 2>&1; then
		echo "$EDITOR"
		return 0
	fi
	for ed in nano vi vim; do
		command -v "$ed" >/dev/null 2>&1 && { command -v "$ed"; return 0; }
	done
	return 1
}
TEXTEDITOR=$(resolve_editor)
# Theme and icon roots, read only
THEMES_DIR=${BMENU_THEMES_DIR:-/usr/share/themes}
ICONS_DIR=${BMENU_ICONS_DIR:-/usr/share/icons}
USINGEDITORMSG="No editor found. Install one with: sudo pacman -Syu nano"

function run_editor() { # <file>, owned by the user
	if [ -z "$TEXTEDITOR" ]; then
		echo -e "$RED$USINGEDITORMSG${NC}"
		return 1
	fi
	"$TEXTEDITOR" "$1"
}

# Service messages section
# Wrong Option
ERRORMSG="$RED Wrong option $NC"
TRYAGAINMSG=" Please try again..."
#
CONTINMSG="$RED To return press ENTER $NC"
CURRENTPREFIXMSG="(current: "
CURRENTSUFFIXMSG="):"
AVAILABLETHEMESMSG="Available themes "
PS31MSG="Choose new gtk2 theme:"
PS32MSG="Choose new gtk3 theme:"
PS33MSG="Choose new icon theme:"
PS34MSG="Choose new pointer theme:"
SETTINGGTK2THEMEMSG='Setting theme to %s'
SETTINGGTK3THEMEMSG='Setting theme to %s'
SETTINGICONTHEMEMSG='Setting theme to %s'
SETTINGPOINTERTHMSG='Setting theme to %s'
GTK2THEMEPREFIXMSG="GTK2 theme "
GTK3THEMEPREFIXMSG="GTK3 theme "
ICONTHEMEPREFIXMSG="Icon theme "
POINTERTHPREFIXMSG="Pointer theme "
FONTSETPREFIXMSG="Font "
FONTSIZESETMSG=" with size "
FONTSIZEERRMSG="$RED Value is out of range$NC"
SELECTEDFONTMSG="Selected font is: "
INPUTFONTSIZEMSG="Input font size value in range from $MINFONTSIZE to $MAXFONTSIZE or just press Escape to discard changes"
SELECTFONTSIZEMSG="Select font size value "
CHOOSEFONTPROMPTMSG="Choose new font to use "
SETSUFFIXMSG=" was set successfully"
TOGGLEDARKTHEMEMSG="Done"

# The GTK files are created when a change is applied, not at startup
function ensure_gtk_files() {
	if ! [ -d "$HOME/.config/gtk-3.0" ]; then
		mkdir -p "$HOME/.config/gtk-3.0"
	fi
	if ! [ -f "$HOME/.gtkrc-2.0" ]; then
		cat >"$HOME/.gtkrc-2.0" <<EOL
include "$HOME/.gtkrc-2.0.mine"
gtk-theme-name="Adwaita"
gtk-icon-theme-name="Adwaita"
gtk-font-name="DejaVu Sans 12"
gtk-cursor-theme-name="Adwaita"
gtk-cursor-theme-size=18
gtk-toolbar-style=GTK_TOOLBAR_ICONS
gtk-toolbar-icon-size=GTK_ICON_SIZE_LARGE_TOOLBAR
gtk-button-images=1
gtk-menu-images=0
gtk-enable-event-sounds=0
gtk-enable-input-feedback-sounds=0
gtk-xft-antialias=1
gtk-xft-hinting=1
gtk-xft-hintstyle="hintmedium"
gtk-xft-rgba="none"
EOL
	fi

	if ! [ -f "$HOME/.config/gtk-3.0/settings.ini" ]; then
		cat >"$HOME/.config/gtk-3.0/settings.ini" <<EOL
[Settings]
gtk-theme-name=Adwaita
gtk-icon-theme-name=Adwaita
gtk-font-name=DejaVu Sans 12
gtk-cursor-theme-name=Adwaita
gtk-cursor-theme-size=18
gtk-toolbar-style=GTK_TOOLBAR_ICONS
gtk-toolbar-icon-size=GTK_ICON_SIZE_LARGE_TOOLBAR
gtk-button-images=1
gtk-menu-images=0
gtk-enable-event-sounds=0
gtk-enable-input-feedback-sounds=0
gtk-xft-antialias=1
gtk-xft-hinting=1
gtk-xft-hintstyle=hintmedium
gtk-xft-rgba=none
gtk-decoration-layout=:menu,appmenu,close
EOL
	fi

	if ! [ -f "$HOME/.icons/default/index.theme" ]; then
		mkdir -p "$HOME/.icons/default/"
		cat >"$HOME/.icons/default/index.theme" <<EOL
# This file is written by LXAppearance. Do not edit.
[Icon Theme]
Name=Default
Comment=Default Cursor Theme
Inherits=Adwaita
EOL
	fi
	return 0
}

# Drop an ENTER left in the input buffer by a menu key
function drain_enter() {
	local _rest
	read -r -n 1 -t 0.2 _rest || true
}

function pressanykeyrequest() {
	echo -e "$CONTINMSG"
	read -r
	STATUSVAR=1
}

# One line per theme name from a directory tree
function collect_names() {
	# The name comes from the path relative to the searched directory
	local dir="$1" type="$2" pattern="$3"
	[ -d "$dir" ] || return 0
	find "$dir" -mindepth 2 -maxdepth 2 -type "$type" -name "$pattern" 2>/dev/null |
		sed -e "s|^$dir/||" -e "s|/[^/]*$||" | sort -u
}

# Replace one key in a settings file; append it when it is missing
# $1 file, $2 key, $3 value (already in the file's own quoting form)
function set_key_in_file() {
	file="$1"
	key="$2"
	value="$3"
	# A value must not break the sed replacement: |, &, /, backslash and newline are refused
	case "$value" in
	*"|"* | *"&"* | *"\\"* | *"/"* | *$'\n'*)
		echo "Unsupported characters in the chosen name."
		return 1
		;;
	esac
	[ -e "$file" ] || return 1
	if grep -qF "${key}=" "$file"; then
		sed -i "s|^${key}=.*|${key}=${value}|" "$file"
	else
		echo "${key}=${value}" >>"$file"
	fi
	return 0
}

# The value of one key in a settings file, empty when the file is missing
function current_item_get() {
	local cig_input="${1:-}" file
	CURRENTITEM=""
	case $cig_input in
	gtk2theme)
		file="$HOME/.gtkrc-2.0"
		[ -f "$file" ] && CURRENTITEM=$(grep -Po 'gtk-theme-name="\K[^"]*' "$file")
		echo "$CURRENTITEM"
		;;
	gtk3theme)
		file="$HOME/.config/gtk-3.0/settings.ini"
		[ -f "$file" ] && CURRENTITEM=$(grep -Po 'gtk-theme-name=\K.*' "$file")
		echo "$CURRENTITEM"
		;;
	icontheme)
		file="$HOME/.config/gtk-3.0/settings.ini"
		[ -f "$file" ] && CURRENTITEM=$(grep -Po 'gtk-icon-theme-name=\K.*' "$file")
		echo "$CURRENTITEM"
		;;
	pointertheme)
		file="$HOME/.config/gtk-3.0/settings.ini"
		[ -f "$file" ] && CURRENTITEM=$(grep -Po 'gtk-cursor-theme-name=\K.*' "$file")
		echo "$CURRENTITEM"
		;;
	gtkfontsize)
		file="$HOME/.config/gtk-3.0/settings.ini"
		# The size is the last number of gtk-font-name=<family> <size>
		[ -f "$file" ] && CURRENTITEM=$(grep -Po 'gtk-font-name=\K.*' "$file" | grep -oE '[0-9]+$' | head -1)
		echo "$CURRENTITEM"
		;;
	gtkfontname)
		file="$HOME/.config/gtk-3.0/settings.ini"
		[ -f "$file" ] && CURRENTITEM=$(grep -Po 'gtk-font-name=\K.*' "$file" | grep -Po '.*[^0-9]{1,2}' | sed 's/[[:blank:]]*$//')
		echo "$CURRENTITEM"
		;;
	esac
	return 0
}

# Back one file up and write one key into it; both results count
function apply_key() { # <file> <key> <value>
	cp "$1" "$1.bak" && set_key_in_file "$1" "$2" "$3"
}

function set_gtk2_theme() {
	new_theme="$1"
	ensure_gtk_files
	apply_key "$HOME/.gtkrc-2.0" gtk-theme-name "\"$new_theme\""
	STATUSVAR=$?
	return $STATUSVAR
}

function choose_gtk2_theme() {
	CURRENTITEM=$(current_item_get "gtk2theme")
	echo "$AVAILABLETHEMESMSG$CURRENTPREFIXMSG$CURRENTITEM$CURRENTSUFFIXMSG"
	PS3="$PS31MSG"
	CANCELSTATUS=0
	SELECTEDITEM=""
	# Gather the results in an array.
	themes=()
	while IFS= read -r name; do
		[ -n "$name" ] && themes+=("$name")
	done < <(collect_names "$THEMES_DIR" d "gtk-2.0")
	themes+=("cancel")

	select pick in "${themes[@]}"; do
		case $pick in
			cancel)
				# A cancel is not a success
				CANCELSTATUS=1
				STATUSVAR=1
				break
				;;
			"")
				# ENTER re-shows the list
				continue
				;;
			*)
				SELECTEDITEM="$pick"
				printf "$SETTINGGTK2THEMEMSG" "$SELECTEDITEM"
				set_gtk2_theme "$SELECTEDITEM"
				break
				;;
			esac
	done

	if [ $CANCELSTATUS -eq 1 ]; then
		echo ""
	fi
	return $STATUSVAR
}

function set_gtk3_theme() {
	new_theme="$1"
	ensure_gtk_files
	apply_key "$HOME/.config/gtk-3.0/settings.ini" gtk-theme-name "$new_theme"
	STATUSVAR=$?
	return $STATUSVAR
}

function choose_gtk3_theme() {
	CURRENTITEM=$(current_item_get "gtk3theme")
	echo "$AVAILABLETHEMESMSG$CURRENTPREFIXMSG$CURRENTITEM$CURRENTSUFFIXMSG"
	PS3="$PS32MSG"
	CANCELSTATUS=0
	SELECTEDITEM=""
	# Gather the results in an array.
	themes=()
	while IFS= read -r name; do
		[ -n "$name" ] && themes+=("$name")
	done < <(collect_names "$THEMES_DIR" d "gtk-3.0")
	themes+=("cancel")

	select pick in "${themes[@]}"; do
		case $pick in
			cancel)
				# A cancel is not a success
				CANCELSTATUS=1
				STATUSVAR=1
				break
				;;
			"")
				# ENTER re-shows the list
				continue
				;;
			*)
				SELECTEDITEM="$pick"
				printf "$SETTINGGTK3THEMEMSG" "$SELECTEDITEM"
				set_gtk3_theme "$SELECTEDITEM"
				break
				;;
			esac
	done

	if [ $CANCELSTATUS -eq 1 ]; then
		echo ""
	fi
	return $STATUSVAR
}

function set_icon_theme() {
	new_theme="$1"
	ensure_gtk_files
	STATUSVAR=0
	apply_key "$HOME/.config/gtk-3.0/settings.ini" gtk-icon-theme-name "$new_theme" || STATUSVAR=1
	apply_key "$HOME/.gtkrc-2.0" gtk-icon-theme-name "\"$new_theme\"" || STATUSVAR=1
	return $STATUSVAR
}

function choose_icon_theme() {
	CURRENTITEM=$(current_item_get "icontheme")
	echo "$AVAILABLETHEMESMSG$CURRENTPREFIXMSG$CURRENTITEM$CURRENTSUFFIXMSG"
	PS3="$PS33MSG"
	CANCELSTATUS=0
	SELECTEDITEM=""
	# Gather the results in an array.
	themes=()
	while IFS= read -r name; do
		[ -n "$name" ] && themes+=("$name")
	done < <(collect_names "$ICONS_DIR" f "index.theme")
	themes+=("cancel")

	select pick in "${themes[@]}"; do
		case $pick in
			cancel)
				# A cancel is not a success
				CANCELSTATUS=1
				STATUSVAR=1
				break
				;;
			"")
				# ENTER re-shows the list
				continue
				;;
			*)
				SELECTEDITEM="$pick"
				printf "$SETTINGICONTHEMEMSG" "$SELECTEDITEM"
				set_icon_theme "$SELECTEDITEM"
				break
				;;
			esac
	done

	if [ $CANCELSTATUS -eq 1 ]; then
		echo ""
	fi
	return $STATUSVAR
}

function set_pointer_theme() {
	new_theme="$1"
	ensure_gtk_files
	STATUSVAR=0
	apply_key "$HOME/.icons/default/index.theme" Inherits "$new_theme" || STATUSVAR=1
	apply_key "$HOME/.config/gtk-3.0/settings.ini" gtk-cursor-theme-name "$new_theme" || STATUSVAR=1
	apply_key "$HOME/.gtkrc-2.0" gtk-cursor-theme-name "\"$new_theme\"" || STATUSVAR=1
	return $STATUSVAR
}

function choose_pointer_theme() {
	CURRENTITEM=$(current_item_get "pointertheme")
	echo "$AVAILABLETHEMESMSG$CURRENTPREFIXMSG$CURRENTITEM$CURRENTSUFFIXMSG"
	PS3="$PS34MSG"
	CANCELSTATUS=0
	SELECTEDITEM=""
	# Gather the results in an array.
	themes=()
	while IFS= read -r name; do
		# The cursor directory name goes into the config unchanged
		[ -n "$name" ] && themes+=("$name")
	done < <(collect_names "$ICONS_DIR" d "cursors")
	themes+=("cancel")

	select pick in "${themes[@]}"; do
		case $pick in
			cancel)
				# A cancel is not a success
				CANCELSTATUS=1
				STATUSVAR=1
				break
				;;
			"")
				# ENTER re-shows the list
				continue
				;;
			*)
				SELECTEDITEM="$pick"
				printf "$SETTINGPOINTERTHMSG" "$SELECTEDITEM"
				set_pointer_theme "$SELECTEDITEM"
				break
				;;
			esac
	done

	if [ $CANCELSTATUS -eq 1 ]; then
		echo ""
	fi
	return $STATUSVAR
}

function read_number() {
	# Turn a digit string into a number, empty or junk gives 0
	local raw="${1:-0}"
	if [[ $raw =~ ^[0-9]+$ ]]; then
		number=$raw
	else
		number=0
	fi
}

function numberinputprocedure() {
	font_family="$SELECTEDITEM"
	numberstring=""
	number=0
	schar=""
	# This function checks font family and tries to find
	# fonts with fixed pixel size value using fc-cat command.
	# fc-cat output is huge and can be used in other procedures.
	# If we found nothing user should enter font size manually
	# because selected font is a size scalable one.
	# If we found pixelsize items for selected font
	# user selects size in range of these items.
	CURRENTITEM=$(current_item_get "gtkfontsize")
	fccatmsggrep=""

	fccatmsggrep=$(fc-cat -r 2>/dev/null | grep "\"$font_family" | grep -o "pixelsize=[0-9][0-9]*" | grep -o "[0-9][0-9]*")
	if [ "$fccatmsggrep" != "" ]; then
		numberstring=$(echo "$fccatmsggrep" | sort -n | uniq | fzf -e --reverse --prompt="< $SELECTFONTSIZEMSG$CURRENTPREFIXMSG$CURRENTITEM$CURRENTSUFFIXMSG >")
		if [ "$numberstring" == "" ]; then
			number=0
			return 1
		fi
		read_number "$numberstring"
		# The chosen pixelsize has to be inside the font-size range too
		if ! checkfontsize "$number"; then
			echo ""
			echo -e "$ERRORMSG$FONTSIZEERRMSG"
			pressanykeyrequest
			return 1
		fi
		return 0
	else
		echo -e "$INPUTFONTSIZEMSG$CURRENTPREFIXMSG$CURRENTITEM$CURRENTSUFFIXMSG"
		manualnumberinput
		STATUSVAR=$?
		if [ $STATUSVAR -eq 1 ]; then
			echo ""
			echo -e "$ERRORMSG$FONTSIZEERRMSG"
			pressanykeyrequest
			return 1
		fi
		if ! checkfontsize "$number"; then
			echo ""
			echo -e "$ERRORMSG$FONTSIZEERRMSG"
			pressanykeyrequest
			return 1
		fi
		return 0
	fi
}

function manualnumberinput() {
	while true; do
		if ! read -s -n 1 schar; then
			echo ""
			number=0
			return 1
		fi
		case $schar in
			1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0)
				numberstring+=$schar
				echo -n "$schar"
				;;
			"") # Enter: read -n 1 returns an empty value
				# Drop the ENTER of the menu key, nothing was typed yet
				if [ -z "$numberstring" ]; then
					continue
				fi
				break
				;;
			$'\177') # Backspace
				numberstring="${numberstring%?}"
				echo -n -e "\b"
				echo -n " "
				echo -n -e "\b"
				;;
			$'\e') # Escape
				numberstring="0"
				break
				;;
		esac
	done
	# read_number gives a defined default for a non-digit input
	read_number "${numberstring:-0}"
	return 0
}

function checkfontsize() {
	local value="${1:-0}"
	if ! [[ $value =~ ^[0-9]+$ ]]; then
		return 1
	fi
	if [ "$value" -lt "$MINFONTSIZE" ] || [ "$value" -gt "$MAXFONTSIZE" ]; then
		return 1
	else
		return 0
	fi
}

function set_font() {
	new_font="$1"
	new_size="$2"
	ensure_gtk_files
	STATUSVAR=0
	apply_key "$HOME/.config/gtk-3.0/settings.ini" gtk-font-name "$new_font $new_size" || STATUSVAR=1
	apply_key "$HOME/.gtkrc-2.0" gtk-font-name "\"$new_font $new_size\"" || STATUSVAR=1
	return $STATUSVAR
}

function choose_font() {
	new_font=""
	font_size=0
	CURRENTITEM=$(current_item_get "gtkfontname")
	SELECTEDITEM=$(fc-list : family | tr , \\n | sort | uniq | fzf -e --reverse --prompt="< $CHOOSEFONTPROMPTMSG$CURRENTPREFIXMSG$CURRENTITEM$CURRENTSUFFIXMSG >")
	if [ "$SELECTEDITEM" != "" ]; then
		new_font="$SELECTEDITEM"
		echo -e "$SELECTEDFONTMSG$SELECTEDITEM"
		numberinputprocedure
		STATUSVAR=$?
		if [ $STATUSVAR -eq 1 ]; then
			return $STATUSVAR
		else
			font_size=$number
			set_font "$new_font" "$font_size"
			return $STATUSVAR
		fi
	fi
	STATUSVAR=1
	return $STATUSVAR
}

function toggle_dark_theme() {
	ini="$HOME/.config/gtk-3.0/settings.ini"
	ensure_gtk_files
	if ! grep -q "^gtk-application-prefer-dark-theme=" "$ini"; then
		# first use: enable the dark theme instead of adding a value and flipping it again
		apply_key "$ini" gtk-application-prefer-dark-theme 1
		STATUSVAR=$?
		return $STATUSVAR
	fi
	# A failed backup must not be followed by an edit
	cp "$ini" "$ini.bak" || {
		echo "Backup of the settings file failed, nothing was changed."
		STATUSVAR=1
		return 1
	}
	case $(awk -F= '/^gtk-application-prefer-dark-theme/{print $2}' "$ini") in
	true)
		sed -i "s/^gtk-application-prefer-dark-theme=true/gtk-application-prefer-dark-theme=false/" "$ini"
		;;
	false)
		sed -i "s/^gtk-application-prefer-dark-theme=false/gtk-application-prefer-dark-theme=true/" "$ini"
		;;
	1)
		sed -i "s/^gtk-application-prefer-dark-theme=1/gtk-application-prefer-dark-theme=0/" "$ini"
		;;
	0)
		sed -i "s/^gtk-application-prefer-dark-theme=0/gtk-application-prefer-dark-theme=1/" "$ini"
		;;
	*)
		apply_key "$ini" gtk-application-prefer-dark-theme 1
		;;
	esac
	STATUSVAR=$?
	return $STATUSVAR
}

function main() {

	# Define Menu entries which will be run with the echo command
	E0="\e[1mQ\e[0muit"
	E1="Set GTK2 Theme"
	E2="Set GTK3 Theme"
	E3="Set Font"
	E4="Set Icon Theme"
	E5="Toggle Dark Theme"
	E6="Set Pointer Theme"
	E7="Edit GTK2 Settings"
	E8="Edit GTK3 Settings"
	E9="Edit Xresources"

	while true; do
		printf '\33[H\33[2J' # Use this instead of the clear command

		echo
		echo -e "                             $BG$TC Appearance $NC"
		echo -e " $TC+----------------------------------------------------------------+$NC"
		echo -e " $TC|$NC    $BG 1 $NC $E1          $TC|$NC    $BG 2 $NC $E2         $TC|$NC"
		echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
		echo -e " $TC|$NC    $BG 3 $NC $E3                $TC|$NC    $BG 4 $NC $E4         $TC|$NC"
		echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
		echo -e " $TC|$NC    $BG 5 $NC $E5       $TC|$NC    $BG 6 $NC $E6      $TC|$NC"
		echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
		echo -e " $TC|$NC    $BG 7 $NC $E7 $NC     $TC|$NC    $BG 8 $NC $E8     $TC|$NC"
		echo -e " $TC|$NC--------------------------------$TC|$NC-------------------------------$TC|$NC"
		echo -e " $TC|$NC    $BG 9 $NC $E9         $TC|$NC    $BG$TC T $NC$BG$TC O $NC$BG$TC O $NC$BG$TC L $NC $BG B $NC $BG O $NC $BG X $NC   $TC|$NC"
		echo -e " $TC+----------------------------------------------------------------+$NC"
		echo ""
		echo -e "  Enter marked number or letter   -    $BG 0 $NC $E0 "

		# save entered number/letter in variable "choice"
		read -r -n 1 -e choice || exit 0
		[ -n "$choice" ] && drain_enter
		if [ -z "$choice" ]; then
			# An empty answer is ENTER: redraw the menu
			if [ -t 0 ]; then
				continue
			fi
			EMPTY_RUNS=$((${EMPTY_RUNS:-0} + 1))
			[ "$EMPTY_RUNS" -lt 3 ] || exit 0
			continue
		fi
		EMPTY_RUNS=0

		# test, whether "choice" fits any of the following numbers or letters
		case "$choice" in
		1)
			echo
			choose_gtk2_theme
			STATUSVAR=$?
			if [ $STATUSVAR -eq 0 ]; then
				echo ""
				echo -e "$GTK2THEMEPREFIXMSG$SELECTEDITEM$SETSUFFIXMSG"
				pressanykeyrequest
			fi
			;;
		2)
			echo
			choose_gtk3_theme
			STATUSVAR=$?
			if [ $STATUSVAR -eq 0 ]; then
				echo ""
				echo -e "$GTK3THEMEPREFIXMSG$SELECTEDITEM$SETSUFFIXMSG"
				pressanykeyrequest
			fi
			;;
		3)
			echo
			choose_font
			STATUSVAR=$?
			if [ $STATUSVAR -eq 0 ]; then
				echo ""
				echo -e "$FONTSETPREFIXMSG$SELECTEDITEM$FONTSIZESETMSG$font_size$SETSUFFIXMSG"
				pressanykeyrequest
			fi
			;;
		4)
			echo
			choose_icon_theme
			STATUSVAR=$?
			if [ $STATUSVAR -eq 0 ]; then
				echo ""
				echo -e "$ICONTHEMEPREFIXMSG$SELECTEDITEM$SETSUFFIXMSG"
				pressanykeyrequest
			fi
			;;
		5)
			echo
			toggle_dark_theme
			STATUSVAR=$?
			if [ $STATUSVAR -eq 0 ]; then
				echo ""
				echo -e "$TOGGLEDARKTHEMEMSG"
				pressanykeyrequest
			fi
			;;
		6)
			echo
			choose_pointer_theme
			STATUSVAR=$?
			if [ $STATUSVAR -eq 0 ]; then
				echo ""
				echo -e "$POINTERTHPREFIXMSG$SELECTEDITEM$SETSUFFIXMSG"
				pressanykeyrequest
			fi
			;;
		7)
			echo
			# The edit entries open the file the GTK settings really live in
			ensure_gtk_files
			run_editor "$HOME/.gtkrc-2.0"
			echo ""
			;;
		8)
			echo
			ensure_gtk_files
			run_editor "$HOME/.config/gtk-3.0/settings.ini"
			echo ""
			;;
		9)
			echo
			run_editor "$HOME/.Xresources"
			echo ""
			;;
		0 | Q | q)
			printf '\33[H\33[2J' && exit
			;;
		*)
			echo -e "$ERRORMSG"
			echo -e "$TRYAGAINMSG"
			sleep 2
			;;
		esac
	done
}

main
