#!/bin/bash

Info () {
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2021"
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
}

if [ "$1" = "info" ]; then Info; exit 0; fi

expose_file="$@"

opt=""; error=false
lang_dir=$"C"
help=$"Help"
title="$(echo $"Expose File")"
title2="$(echo $"Set Application")"
ask="$(echo $"Which app shall open the file? (empty: default app)")"
file="$(basename "$expose_file")"
text=$"&#10;<b>Please select an adequate icon for the file '$file'!</b>&#10;"
if [ -e "$expose_file" ]; then
    ready=$"	The file '$file'&#10;	has been exposed!"
    image="info"
else
    [ -n "$expose_file" ] && file="$expose_file" && ready=$"&#10;    The file '$file'&#10;    doesn't exist!&#10;&#10;" || ready=$"&#10;	No file specified!"
    opt="--button=$help:yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#expose-file"
    image="dialog-warning"
    error=true
fi
cancel=$"Cancel"

if ! $error; then
 if [ -n "$expose_file" ]; then
  type=$(xdg-mime query filetype "$expose_file")

  icon="$(yad --title="$title" --center --width=900 --height=700 --text="$text" --text-align=center --window-icon=/usr/share/icons/gnome/48x48/emblems/emblem-favorite.png --file --filename=$HOME/.icons --file-filter $"Graphic Files "" | *.jpg *.jpeg *.png *.JPG *.JPEG *.PNG" --add-preview --button="$cancel":1 --button="Info":"$0 info" --button="OK":0)"

  if [ -z "$icon" ]; then
    icon=$(sed 's/\//-/g' <<< $type)
    icon_found=false
    search="$(find $HOME/.icons -name "$icon".png)" && [ -n "$search" ] && icon="$search" && icon_found=true
    ! $icon_found && search="$(find $HOME/.icons -name "$icon".svg)" && [ -n "$search" ] && icon="$search" && icon_found=true
    ! $icon_found && search="$(find /usr/share/icons/hicolor -name "$icon".png | grep 48)" && [ -n "$search" ] && icon="$search" && icon_found=true
    ! $icon_found && search="$(find /usr/share/icons/hicolor -name "$icon".svg | grep 48)" && [ -n "$search" ] && icon="$search" && icon_found=true
    ! $icon_found && search="$(find /usr/share/icons/gnome -name "$icon".png | grep 48)" && [ -n "$search" ] && icon="$search" && icon_found=true
    ! $icon_found && search="$(find /usr/share/icons/gnome -name "$icon".svg | grep 48)" && [ -n "$search" ] && icon="$search" && icon_found=true
    ! $icon_found && search="$(find /usr/share/icons/Humanity -name "$icon".svg | grep 48)" && [ -n "$search" ] && icon="$search" && icon_found=true
    ! $icon_found  && icon="/usr/share/icons/gnome/48x48/emblems/emblem-favorite.png"
  fi

  IFS=$'\n'
  cd /usr/share/applications
  app_mime=$(xdg-mime query default $type)
  app_list=($(grep -l $type * | grep -v mimeinfo.cache | grep -v defaults.list))
  app_list=($(sed 's/\.desktop//g' <<< "${app_list[*]/$app_mime}"))
  app_list=( `for i in ${app_list[@]}; do echo $i; done | sort -u` )
  app_list=(${app_mime%.*} ${app_list[@]})

  app=`yad --center --title="$title2" --window-icon=/usr/share/icons/gnome/48x48/emblems/emblem-favorite.png --fixed --borders=10 --list --editable --no-headers --column=Apps ${app_list[@]} --text="<b>$ask</b>" --entry --button="$cancel":1 --button="Info":"$0 info" --button="OK":0`

  if [ -n "$app" ]; then
    if [[ ! "$app" =~ ^x\ |^exec-desktop|.desktop$ ]]; then
	cd $HOME/.local/share/applications
	found="$(ls * | grep -iE "^(^$app|$app|${app//-}).desktop" | head -1)"
	[ -n "$found" ] && app="x $found"
	if [[ ! "$app" =~ ^x\ |^exec-desktop|.desktop$ ]]; then
	    cd /usr/share/applications
	    found="$(ls * | grep -iE "(^$app|$app|${app//-}).desktop" | head -1)"
	    [ -n "$found" ] && app="x $found"
	fi
    fi
    app="$app "
  fi

  f_menu="$HOME/.config/gtk-3.0/files.lst"
  id_fs=($(grep -n '}‖' $f_menu))
  if [ -n "$id_fs" ]; then
    for ((i=0; i<${#id_fs[*]}; i++)); do
      nr="${id_fs[$i]%%:*}"
      id_fs[$i]="${id_fs[$i]#*:}":$nr
    done
    idf=($(for ((i=0; i<${#id_fs[*]}; i++)); do
      label="${id_fs[$i]##*‖}"
      detail="${id_fs[$i]%‖*}"
      detail="${detail##*‖}"
      [ "${label::1}" = "{" ] && line="${label%\}*}}!$detail!‖$label" || line="$detail!!‖$label"
      echo "$line"
    done | sort -t'!' -k 1,1 -k 2,2 --ignore-case))
    main_id="$(echo $"Main Level:0")"
    idf=($main_id ${idf[@]//\!/})
    h=${#idf[@]}; h=$(((h+11)*20))

    IFS='¦'
    id_f=($(for ((i=0; i<${#idf[*]}; i++)); do
      idf[$i]="${idf[$i]##*:}¦${idf[$i]##*‖}"
      echo "${idf[$i]%:*}¦"
    done))
    id_f=($(echo "${id_f[*]}" | sed 's/{[^}]*}/    /g'))

    title3="$(echo $"Select Identifier")"
    text="$(echo $"Select the place of the file:")"
    oktext="$(echo $"Integrate file there")"
    cancel=$"Cancel"

    name=`yad --center --title="$title3" --window-icon=format-indent-less-rtl --width=350 --height=$h --borders=20 --text="$text"$'\n' --list --no-headers --column="Idf":HD --column="Identifiers" ${id_f[@]} --button="$cancel":1 --button="Info":"$0 info" --button="OK!!""$oktext":0`
    exit_code=$?
    [ $exit_code -eq 1 ] && exit $exit_code

    IFS=$'\n'
    n_id=${name%%\|*}
    label="${name#*▶ }"; label="${label::-1}:$n_id$"; label="${label/$'\n'/}"
    id="$(echo "${idf[*]}" | grep -e "$label")"
    id="${id%‖*}"
  else
    id=""
  fi

  label="$(basename "$expose_file")"
  label="${label%.*}"
  label="$(sed 's/-/ /g; s/_/ /g' <<< "$label")"

  echo "$icon‖$app$expose_file‖$id$label" >> $HOME/.config/gtk-3.0/files.lst
 fi
fi
yad --center --fixed --title="$title" --window-icon="info" --image="$image" --borders=20 --text="$ready" "$opt" --button="OK"

exit 0
