#!/bin/bash

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

[ "$1" = "info" ] && Info && exit 0
user=$(whoami)

button1=$"No"; button0=$"Yes"; button=$"Help"
lang_dir=$"C"

if [ "$user" != "root" ]; then
    yad --center --title=$"Create New MIME Type" --window-icon="$HOME/.icons/mime-edit.png" --image="dialog-question" --borders=10 --text=$"&#10;&#9;Define new MIME type?&#10;&#10;" --button=$button1:1 --button="$button":"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#create-new-mime" --button="Info":"$0 info" --button=$button0:0
    [ $? -gt 0 ] && exit 1
    gsu $0 & exit 0
fi

user="$(id -nu 1000)"
mime_list="$(cat /home/$user/.config/mimeapps.list)"

mimes=$(find /usr/share/mime/* -type d)
mimes="$(echo "$mimes" | sed ':a;N;$!ba;s/\n/\/\!/g; s@/usr/share/mime/@@g')"

cd /home/$user/.config/menus
menu_files=($(ls *.lst))
unset menu_files[0]
IFS=$'\n'
names=()
for m in ${menu_files[@]}; do names+=($(cat $m | grep \%[fFuU] | grep -v "▶")); done
names=(${names[@]##*‖})
sort_names=($(sort <<<"${names[*]}"))
names="${sort_names[*]}"
names="${names//$'\n'/\!}"

cancel=$"Cancel"
field1=$"MIME category:"; mime_ctg="text/"
field2=$"MIME format:"; mime_fmt="plain"
field3=$"File extension:"; pattern="txt"
field4=$"File icon:"; icon_path="/usr/share/icons/gnome/48x48/mimetypes/text-x-generic.png"
field5=$"Comment":; comment=$"plain text document"
field6=$"Link to:"; spec_name=$"Text Editor"" gEdit"
new_mime=`yad --center --title=$"Create New MIME Type" --window-icon="$HOME/.icons/mime-edit.png" --height=50 --width=540 --borders=10 --text=$"&#9;&#9;The default settings for the fields are exemplary!&#10;" --form --separator="|" --field="$field1":CB  "$mime_ctg!$mimes" --field="$field2" "$mime_fmt" --field="$field3" "$pattern" --field="$field4":SFL "$icon_path" --filename=/usr/share/icons/gnome/48x48/mimetypes --add-preview --field="$field5" "$comment" --field="$field6":CBE "$spec_name!$names" --button="$cancel":1 --button="$button":"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#create-new-mime" --button="Info":"$0 info" --button="OK":0`
[ $? -gt 0 ] && exit 2

mime_ctg="$(echo "$new_mime" | cut -d"|" -f1)"
mime_fmt="$(echo "$new_mime" | cut -d"|" -f2)"
pattern="$(echo "$new_mime" | cut -d"|" -f3)"
icon_path="$(echo "$new_mime" | cut -d"|" -f4)"
comment="$(echo "$new_mime" | cut -d"|" -f5)"
app_link="$(echo "$new_mime" | cut -d"|" -f6)"

mime="$mime_ctg$mime_fmt"
[[ "$mime_list" =~ "$mime" ]] && yad --center --title=$"Create New MIME Type" --window-icon="$HOME/.icons/mime-edit.png" --image="dialog-warning" --borders=10 --text=$"&#10;&#9;MIME type '$mime' already exists!&#10;&#10;" --button="$button":"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#create-new-mime" --button="Info":"$0 info" --button="OK":0 && exit 2

[ "$icon_path" = "$(basename "$icon_path")" ] && icon_path="$(locate "$icon_path" | grep icons | head -1)"
icon="$(basename "$icon_path")"
icon="$(echo "$icon" | sed 's/.png//;s/.PNG//;s/.svg//;s/.SVG//')"
dsk_file_path="$(grep -l "$app_link" /home/$user/.local/share/applications/*.desktop)"
[ -z "$dsk_file_path" ] && dsk_file_path="$(grep -l "$app_link" /usr/share/applications/*.desktop)"
dsk_file="$(basename "$dsk_file_path")"
f_name="${dsk_file%.*}.xml"
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">
	<mime-type type=\"$mime\">
		<comment>$comment</comment>
		<icon name=\"$icon\"/>
		<glob pattern=\"*.$pattern\"/>
	</mime-type>
</mime-info>" > /usr/share/mime/packages/$f_name
update-mime-database /usr/share/mime

if [ "${icon_path##*.}" = "svg" ]; then
    convert -background none "$icon_path" -resize 48x48 /tmp/hsperfdata_root/tmp.png
    icon_path=/tmp/hsperfdata_root/tmp.png
fi
for u in $(ls /home); do
    [ ! -f "/home/$u/.local/share/applications/$dsk_file" ] && cp $dsk_file_path /home/$u/.local/share/applications/$dsk_file && chown $u:$u /home/$u/.local/share/applications/$dsk_file
    su $u -c "xdg-mime default $dsk_file $mime"
    mime_="$(echo "$mime" | sed 's@/@_@g')"
    cp $icon_path /home/$u/.config/rox.sourceforge.net/MIME-icons/$mime_.png
    chown $u:$u /home/$u/.config/rox.sourceforge.net/MIME-icons/$mime_.png
    cont='#! /bin/sh
exec all-open "$@"'
    echo "$cont" > /home/$u/.config/rox.sourceforge.net/MIME-types/$mime_
    chown $u:$u /home/$u/.config/rox.sourceforge.net/MIME-types/$mime_
    chmod a+x /home/$u/.config/rox.sourceforge.net/MIME-types/$mime_
done

yad --center --title=$"Create New MIME Type" --window-icon="$HOME/.icons/mime-edit.png" --image="info" --width=300 --borders=10 --text="&#10;&#9;&#9;<b>"$"Done!""</b>&#10;" --button="OK":0
