#!/bin/bash

BR="
"
file="${HOME}/.local/share/applications/mimeapps.list"
tmp_data="/tmp/bean_assoc_data.txt"
tmp_added="/tmp/bean_assoc_added.txt"

_txt2list()
{
	echo 0
	
	added=""
	default=""
	
	data="$(cat ${file} | sed -e 's|;$||igm' -e 's|\.desktop$|\.desktop;|igm')"
	echo "${data}" > "${tmp_data}"
	
	if [ $(echo "$data" | grep "\[Added Associations\]"   -n | cut -d: -f1) -lt \
	     $(echo "$data" | grep "\[Default Applications\]" -n | cut -d: -f1) ] ; then
		
		_added=$(echo "$data" | \
			grep    "\[Default Applications\]" -B99999999 | \
			grep -v "\[Default Applications\]" | \
			grep -v "\[Added Associations\]"   | grep -v "^$")
	
		_default=$(echo "$data" | \
			grep    "\[Default Applications\]" -A99999999 | \
			grep -v "\[Default Applications\]" | \
			grep -v "\[Added Associations\]"   | grep -v "^$")
			
	else
		_added=$(echo "$data" | | \
			grep    "\[Added Associations\]"   -A99999999 | \
			grep -v "\[Added Associations\]"   | \
			grep -v "\[Default Applications\]" | grep -v "^$")
	
		_default=$(echo "$data" | \
			grep    "\[Added Associations\]"   -B99999999 | \
			grep -v "\[Added Associations\]"   | \
			grep -v "\[Default Applications\]" | grep -v "^$")
	fi
	
	mimecnt=$(echo "$_added" | wc -l)
	for i in $(seq $mimecnt) ; do
		line=$(echo "$_added" | sed -n "$(echo $i)p")
		
		mime=$( echo $line | cut -d= -f1)
		_type=$(echo $line | cut -d= -f2)
		
		typeCNT=$(echo "$_type" | grep -o \; | wc -l)
		
		for j in $(seq $typeCNT) ; do
			
			type=$(echo "$_type" | cut -d\; -f$j)
			if [ $(echo "$_default" | grep "$mime" | grep -o "$type" | wc -l) -gt 0 ] ; then
				default="●"
			else
				default="　"
			fi
			added="${added}FALSE${BR}${default}${BR}${type}${BR}${mime}${BR}"
			
		done
	done
	
	echo "${added}" > "${tmp_added}"
}

_txt2list | \
yad --title="起動中" --on-top --center --window-icon=applications-utilities --image=applications-utilities \
--text "関連付けをリストに整形しています..." --progress --pulsate --auto-close

data="$(cat ${tmp_data})"
rm -f "${tmp_data}"

added="$(cat ${tmp_added})"
rm -f "${tmp_added}"

_result=$(echo "${added}" | yad --list --checklist  \
	--width="640" --height="480" --on-top --center --borders=5 \
	--text="削除したい関連付けを選んでOKを押してください。\n「優先」の列に●が付いているものは、同じMIMEタイプの他の関連付けに優先して使用されます。\n<span font='sans 4'> </span>" \
	--title="linuxBean 関連付けの削除" --window-icon=applications-utilities \
	--column="削除" --column="優先" --column="アプリケーション" --column="MIMEタイプ" --search-column=3)

if [ $? -gt 0 ] || [ "$_result" == "" ] ; then
	exit 1
fi

rescnt=$(echo "$_result" | wc -l)
for i in $(seq $rescnt) ; do
	line=$(echo "$_result" | sed -n "$(echo $i)p")
	
	mime=$(echo $line | cut -d\| -f4)
	type=$(echo $line | cut -d\| -f3)
	
	BEFORE=$(echo "$data" | grep -m 1 "^$mime=")
	AFTER=$( echo "$data" | grep -m 1 "^$mime=" | \
	         sed -e "s|=$type;|=|igm" -e "s|;$type;|;|igm" -e "s|;;*|;|igm" -e "s|=;|=|igm" -e "s|^$mime=$||igm")
	
	data=$(echo "$data" | sed -e "s|$BEFORE|$AFTER|igm")
	
	result="${result}  ${mime} => ${type}${BR}"
done

result=$(echo "${result}" | head -n -1)
data=$(echo "$data" | grep -v "^$" | sed -e "s|^\[|\n\[|")
	                    
echo "$data" > "${file}"

yad --width="500" --on-top --center --borders=10 \
	--title="linuxBean 関連付けの削除" --window-icon=applications-utilities --image=applications-utilities \
	--text "  以下の関連付けを削除しました\n<span font='sans 5'> </span>\n${result}"
