#!/usr/bin/env bash

# 2019-02-08: having XMODIFIERS='@im=ibus' crashes dmenu on keypress
unset XMODIFIERS

# History file
hist_file=${XDG_CACHE_HOME:-$HOME/.cache}/dmenu.history

# Commands not stored in history
# (Currently: 'mm <song>' for mpd control)
hist_ignore='mm'
hist_ignore="^($hist_ignore)( |\$)"

prompt='run:'

# wmii theme
#normal_fg='#bbbbbb'
#normal_bg='#222222'
sel_fg='#eeeeee'
sel_bg='#005577'

# Higher-contrast version
normal_fg='#cacaca'
normal_bg='#121212'

screen_width=$(xprop -root _NET_DESKTOP_GEOMETRY | sed -r 's/.* = ([0-9]+), [0-9]+/\1/')
if (( screen_width >= 1920 )); then
	font='Monaco'
	size='13'
else
	font='Monaco'
	size='10'
fi

wm_name=$(wmctrl -m | sed -n 's/^Name: //p')
if [[ $wm_name == Openbox ]]; then
	normal_fg='#bbbbbb'
	normal_bg='#222222'
	sel_bg='#303436'
	sel_fg='#94d01c'
fi
if [[ $wm_name == "GNOME Shell" ]]; then
	font='Source Sans Pro'
	if (( screen_width >= 1920 )); then
		size='15'
	else
		size='12'
	fi
fi

# for bemenu & rofi
#title_fg='#d81860'
#title_bg='#121212'
title_fg=$sel_fg
title_bg=$sel_bg

case $tool in
	bemenu)
		menu_cmd=(bemenu
			-p "$prompt"
			-l 7
			--fn "$font $size"
			--nb "$normal_bg"
			--nf "$normal_fg"
			--hb "$sel_bg"
			--hf "$sel_fg"
			--tb "$title_bg"
			--tf "$title_fg"
		)
		;;
	rofi)
		max=10
		normal_fg='#bbbbbb'
		normal_bg='#222222'
		menu_cmd=(rofi
			-dmenu
			# Read all data before displaying window
			-sync
			# Do not override-redirect (for XWayland)
			-normal-window
			# Prompt string
			-p "${prompt%:}"
			# Max number of lines
			-l $max
			# Match letters anywhere (tks ≈ tweaks)
			-matching fuzzy
			# Don't highlight matching text
			-no-show-match
			# Border
			-bw 0
			# Internal padding
			-padding 2
			# bg, fg, separator line
			-color-window "$normal_bg,$title_fg,#666666"
			-color-normal "$normal_bg,$normal_fg,$normal_bg,$sel_bg,$sel_fg"
		)
		;;
	*)
		menu_cmd=(
			dmenu
			-p "$prompt"
			-l 7
			-fn "$font:size=$size"
			-nb "$normal_bg"
			-nf "$normal_fg"
			-sb "$sel_bg"
			-sf "$sel_fg"
		)
		;;
esac

# Run the actual 'dmenu'
input=$(dmenu_history "$hist_file" | "${menu_cmd[@]}")

# Hack for bypassing dmenu's insistent autocompletion. It is impossible to
# provide input that's an exact prefix of a history entry (e.g. 'foo' when
# history contains 'foobar'). To bypass this, input '+foo' and strip the +.
input=${input#+}

if [[ "$input" ]]; then
	if ! command -v "${input%% *}" >&/dev/null; then
		zenity --error --text="Command '${input%% *}' not found." --width=300
		exit
	fi

	if ! [[ "$input" =~ $hist_ignore ]]; then
		echo "$(date +%s) $input" >> "$hist_file"
	fi

	tag="run.$$.$RANDOM"
	tstart=$(date +%s)

	systemd-cat -t "$tag" -- ${SHELL:-/bin/sh} -c "$input"; r=$?

	tend=$(date +%s)
	if (( r > 0 && tend - tstart <= 1 )); then
		msg="Command '${input%% *}' exited with $r in $[tend-tstart] seconds."
		journal=$(journalctl -b -t "$tag" -o cat)
		if [[ $journal ]]; then
			zenity --error --text="$msg"$'\n\n'"$journal" --width=550
		else
			zenity --error --text="$msg" --width=300
		fi
		exit
	fi
fi &
