#!/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
}

browsers () {
    stdbrw=$(ls -l /etc/alternatives/x-www-browser)
    stdbrw=${stdbrw##*\/}
    brws=$(update-alternatives --list x-www-browser)
    brw2=$(echo $brws | sed "s/\/usr\/bin\/$stdbrw//g")    
    brw2=${brw2##*\/}
}

webaddress_standardbrowser () {
#    xdotool search --onlyvisible --name "$stdbrw_win" windowactivate --sync key F6 key Ctrl+c
    xdotool mousemove ${act_window[2]} ${act_window[3]}
    xdotool mousemove_relative 400 60
    xdotool click 1
    xdotool key Control+a key Control+c
    sleep 1
}

webaddress_browser2 () {
    xdotool mousemove ${act_window[2]} ${act_window[3]}
    xdotool key Alt+f
    xdotool mousemove_relative 100 245
    xdotool click 1
}

param="$@"
[ "$1" = "info" ] && Info && exit 0
. $HOME/.config/openbox/environment

brw=""; browsers; stdbrw="${stdbrw%%-*}"; brw2="${brw2%%-*}"
[ "${param::1}" = '%' ] && param=""
if [ ${#param} -le 1 ] || [ "$param" = "-s" ]; then
  if [[ $param =~ ^[0-9]$ ]]; then brw="$param"; fi
  w_id=$(printf '%x\n' $(xdotool getactivewindow))
  act_window=($(wmctrl -lG | grep $w_id))
  if [ "$param" = "-s" ]; then
    if [[ ${act_window[@]} =~ "$stdbrw_win" ]]; then
	brw=2
    elif [[ ${act_window[@]} =~ $brw2_win ]]; then
	brw=1
    else
	brw=""
    fi
  fi
  sleep 0.5
  [ "$param" = "-s" ] && xsel -bc
  url=$(xsel -b)
  if [[ ! -f "$url" ]] && [[ ! "$url" =~ "://" ]] && [[ "${url:0:7}" != "mailto:" ]]; then
    if [[ ${act_window[@]} =~ "$stdbrw_win" ]]; then
	webaddress_standardbrowser
    elif [[ ${act_window[@]} =~ $brw2_win ]]; then
	webaddress_browser2
    fi
    sleep 0.2
    url="$(xsel -p)"
    field="URL: "
    if [[ ! -f "$url" ]] && [[ ! "$url" =~ '://' ]] && [[ "${url:0:7}" != "mailto:" ]]; then
      title="display-url"
      text=$"&#10;Which URL shall be opened?&#10;"
      button1=$"Cancel"
      button2=$"Help"
      lang_dir=$"C"
      cd $HOME/Urls
      entries=`yad --center --title="$title" --width=600 --height=200 --window-icon="$HOME/.icons/webbrowser.png" --borders=10 --text="<b>$text</b>" --text-align=center --form --file-filter="*.url" --file-filter="*" --field="$field":SFL --button="$button1":1 --button=$button2:"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#display-url" --button="Info":"$0 info" --button="OK":0`
      [ $? -eq 0 ] && url="${entries::-1}" || exit 1
    fi
  fi
elif [ $(wc -w <<< "$param") -gt 1 ] && [[ ${param%% *} =~ ^[0-9]$ ]]; then
  brw="${param%% *}"
  url="${param#* }"
else
    url="$param"
fi
url="$(echo "$url" | sed "s|\$HOME|$HOME|; s|~/|$HOME/|")"
if [[ ! "$url" =~ "://" ]] && [ ! -e "$url" ] && [ "$param" != "-s" ]; then
    file="$url"
    error=$"&#10;    The file '$file'&#10;    doesn't exist!&#10;&#10;"
    yad --center --title=$"Error" --window-icon="$HOME/.icons/list.png" --image="dialog-warning" --borders=20 --fixed --text="$error" --button="Info":"$0 info" --button="OK":0
    exit 404
fi

if [ "$(xdg-mime query filetype "$url" 2> /dev/null)" = "application/x-mswinurl" ]; then
    line="$(grep "URL=" "$url")"
elif [[ "$url" =~ "://" ]] || [ "${url:0:7}" = "mailto:" ] || [ "$(xdg-mime query filetype "$url")" = "text/html" ]; then
    line="URL=$url"
fi

if [ "${line::3}" = "URL" ]; then
    line=$(echo "$line" | sed 's/URL\=//g')
    sleep 0.5
    case ${line%%:*} in
	http|https) browser "$(echo "$brw $line" | sed 's~^ *~~')" ;;
	file) if [ "$param" = "-s" ] || [ -n "$brw" ]; then
		browser "$brw $line"
	      else
		line="${line//file\:\/\//}"
		first=${line%% *}
		if [ $(wc -w <<< "$line") -gt 1 ]; then
		  if [ -n "$(which $first)" ]; then
		    $line
		  else
		    all-open $line
		  fi
		else
		  all-open $line
		  if [ $? -gt 0 ] && [ -n "$(which $first)" ]; then
		    $line
		  fi
		fi
	      fi ;;
	mailto) x-email-client "$line" ;;
	ftp|sftp|ssh|dav|davs) user=""; passw=""; button=0
	     if [[ "$line" =~ "@" ]]; then
		user=${line%%@*}
		user=${user##*\/}
		if [[ "$user" =~ ":" ]]; then
		    passw=${user#*:}
		    user=${user%:*}
		fi
	     fi
	     if [ -z "$passw" ]; then
		if [ -z "$user" ]; then
		    user=`yad --center --window-icon="$HOME/.icons/Golden_key.svg" --title="User:" --entry`
		    button=$?
		fi
		if [ $button -eq 0 ]; then
		    passw=`yad --center --window-icon="$HOME/.icons/Golden_key.svg" --title="Password:" --entry --hide-text`
		    button=$?
		fi
	     fi
	     if [ $button -eq 0 ]; then
		line=${line//:$passw/}
		line=${line//$user@/}
		msg=$"The URL is not available!"
		case ${line%%:*} in
		    dav|davs)  line="${line/dav/http}"
				udevil mount $line <<< "$user"$'\n'"$passw" ;;
		    ftp) line_="$(echo "$line" | sed "s/\/\//\/\/$user:$passw@/g")"
		         udevil mount $line_ ;;
		    ssh|sftp) line_="$(echo "$line" | sed "s/\/\//\/\/$user@/g")"
		         udevil mount -o password_stdin $line_ <<< "$passw" ;;
		esac
		if [ $? -gt 0 ]; then
		    yad --center --title="$(echo $"Error")" --window-icon="dialog-warning" --image="dialog-warning" --height=100 --borders=20 --button="OK":0 --text=$'\n'"$msg"
		else
		    mp="/media/$(whoami)/${line/:\/\//-}"
		    [[ $mp =~ "sftp" ]] && mp="$(echo "$mp" | sed 's/sftp/ssh/g')"
		    mp="${mp%:*}"
		    exec-desktop Rox2 "$mp"
		    udevil umount "$mp"
		fi
	     fi ;;
	*) if [ -n "$brw" ]; then
	     browser "$brw $line"
	   else
	     line="${line#*\:\/\/}"
	     all-open "$line"
	   fi ;;
    esac
fi

