#!/bin/bash

Info () {
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2020"
app="$(basename $0)"
msgid="The Starbuntu application
<i>\$app</i>
was written by"
info="$(gettext "$msgid" | (export PATH `envsubst --variables "$msgid"`; envsubst "$msgid"))"

yad --center --fixed --title="$title" --window-icon="info" --image="info" --borders=20 --text="$info"$'\n'$'\n'"$author"$'\n'"$mail"$'\n'$'\n'"$year"$'\n' --text-align=center --selectable-labels --button="OK" &
wunwa "$title" > /dev/null; xdotool key Home key Down
}

test-variable () {
    declare -p $1 &>/dev/null
}

param="$1"
[ "$param" = "info" ] && Info && exit 0
params="$@"

primary="$(xsel -p)"
if [ "$param" = "-p" ]; then
    entry_text="$primary"
elif [[ "$(pstree -s $$)" =~ "ROX-Filer" ]]; then
    entry_text=' $@ '
else
    entry_text=""
fi

IFS=$'\n'
[ "$(whoami)" = "root" ] && HOME="/root"
. $HOME/.config/openbox/environment
ow_file="$HOME/.config/rox.sourceforge.net/ROX-Filer/open-with.history"
if [[ ! -f "$ow_file" ]]; then
    touch $ow_file
fi
if [[ ! -f "$ow_file.bak" ]]; then
    touch $ow_file.bak
fi
size_ow_file=$(stat --printf="%s" $ow_file)
size_ow_file_bak=$(stat --printf="%s" $ow_file.bak)
if [ $((size_ow_file+100)) -lt $size_ow_file_bak ]; then
    cat $ow_file $ow_file.bak > $ow_file~
    mv $ow_file~ $ow_file
fi

ow_hist=($(cat $ow_file))

title="Application"
text="<b>"$"Command&#58; ""</b>"
button1=$"Cancel"
button2=$"Help"
lang_dir=$"C"

readarray -t ow_hist_uniq < <(printf '%s\n' "${ow_hist[@]}" | awk '!x[$0]++')
ow_hist=(${ow_hist_uniq[@]})

[ -e "$HOME/.bash_aliases" ] && acro="$(grep -E "^[ ]*alias" $HOME/.bash_aliases | sed -E 's/^ *alias (.*$)/\1/g')" || acro="$(grep -E "^[ ]*alias" $HOME/.bashrc | sed -E 's/^ *alias (.*$)/\1/g')"

bash -c 'wunwa "$title" > /dev/null; xdotool key Home' &
app=`yad --center --title="$title" --window-icon="$HOME/.icons/run.png" --height=100 --width=300 --borders=10 --undecorated --list --editable --no-headers --column=History ${ow_hist[@]} --text="$text" --entry-text="$entry_text" --entry --completion --complete=all --button="$button1":1 --button=$button2:"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#open-with" --button="Info":"$0 info" --button="OK":0`

if [ -n "$app" ]; then
    app="$(sed "s~\$@~$params~" <<< $app)"
    pre="${app%% *}"; env_var=""
    if [ "$pre" != "$app" ]; then
	while [[ "$pre" =~ "=" ]]; do
	    env_var="$env_var $pre"
	    app="${app#* }"
	    pre="${app%% *}"
	done
    fi
    [ -n "$(which "$pre")" ] && pre=""

    rm $ow_file
    ow_hist=("$app" ${ow_hist[@]})
    if [ ${#ow_hist[@]} -gt $max_recent_lines ]; then
	unset "ow_hist[((${#ow_hist[@]}-1))]"
    fi
    echo "${ow_hist[*]}" > $ow_file
    cp -f $ow_file $ow_file.bak
    test-variable $pre
    err=$?

    app="$(echo "$app" | sed -E "s/\{&/\&/g; s/&\}/\&/g")"
    [ "${app#* }" != "$app" ] && suffix="${app#* }" || suffix=""
    h_file="$HOME/.config/x-scheme-handlers"
    handlers="$(cat $h_file)"
    hdl=""; for h in $handlers; do hdl="$hdl"\|$(echo "$h" | cut -d/ -f2 | cut -d= -f1)://; done
    hdl="${hdl/mailto:\/\//mailto:}"
    hdl="^(${hdl:1})"

    [ ! -e "$app" ] && [ -z "$(which "${app%% *}")" ] && [[ ! "$app" =~ $hdl ]] && app="$env_var $(grep "${app%% *}" <<< $acro | cut -d= -f2 | tr "'" ' ') $suffix"

    if [ $err -eq 0 ]; then
	cmd="$env_var $app"
    elif [ -n "$(which "${app#* }")" ]; then
	cmd="$env_var $pre ${app#* }"
    elif [ -e "$app" ]; then
	cmd="cd \"$(dirname "$app")\"; all-open $env_var \"$(basename "$app")\""
    elif [[ "$app" =~ $hdl ]]; then
	cmd="all-open $app"
    else
	cmd="bash -c '$app'"
    fi

    msg="$(eval "$cmd &" 2>&1)"
    [ -n "$msg" ] && [[ ! "$msg" =~ $(cat $HOME/.config/gtk-3.0/suppress-err-msg | tr ' ' '|') ]] && yad --fixed --title=$"Error" --window-icon="$HOME/.icons/run.png" --image="dialog-warning" --text='\n\t'"$msg"'\n' --borders=20 --button="OK":0
fi

exit 0