#!/bin/bash
. bean_func.fnc

desktop=$(Bean_UserDirs DESKTOP)
list=""
br="
"

# インストール済アプリのリストアップ
cd "${HOME}/.local/share/applications"
for i in `ls -1 | grep wine-Programs` ; do
	cd "${HOME}/.local/share/applications"
	if [ `cat "$i" | grep --ignore-case ^Exec=bean_wine | wc -l` -gt 0 ] ; then
		name=$(cat "$i" | grep --ignore-case ^Name= | sed -e 's/^\S*=//')
		comment=$(cat "$i" | grep --ignore-case ^Comment= | sed -e 's/^\S*=//')
		list="${list}FALSE${br}${name}${br}${comment}${br}${i}${br}"
	fi
done

# 項目が空の場合は終了
if [ "${list}" == "" ] ; then
	yad --title="エラー" --on-top --center \
	--window-icon=wine-winetricks --image=wine-winetricks \
	--text "簡単インストールから登録されたアプリケーションはありません。"
	exit 1
fi

echo "${list}"

# リストの表示
checked=$(
	echo "${list}" | head -n -1 | yad \
	--width="640" --height="480" --on-top --center \
	--title="linuxBean Wine 簡単インストールの解除" --window-icon=wine-winetricks \
	--text "メニューから除去したい項目にチェックを入れてください。（※アプリケーション自体は削除されません。）" \
	--list --checklist --hide-column=4 \
	--column="削除" --column="アプリケーション名" --column="説明" --column="ファイル名"
)
echo "${checked}"

# キャンセルした場合は終了
if [ $? -gt 0 ] ; then
	exit 1
fi

# 不要な部分を削除
checked=$(echo "${checked}" | grep -v "^|*$")

# チェックを入れていない場合は終了
if [ "${checked}" == "" ] ; then
	exit 1
fi

# 確認
desclist=$(echo "${checked}" | awk -F"|" '{print $2}')
yad --title="確認" --on-top --center \
--window-icon=wine-winetricks --image=wine-winetricks \
--text "以下の項目を削除しますか？${br}${br}${desclist}"

# キャンセルした場合は終了
if [ $? -gt 0 ] ; then
	exit 1
fi

# 削除処理
for i in `echo "${checked}" | awk -F"|" '{print $4}'` ; do
	cd "${HOME}/.local/share/applications"
	if [ `cat "$i" | grep --ignore-case ^Exec=bean_wine | wc -l` -gt 0 ] ; then
		icon=$(cat "$i" | grep --ignore-case ^Icon= | sed -e 's/^\S*=//')
		cd "${HOME}/.local/share/icons/hicolor"
		find . | grep ${icon}.png | xargs rm
		
		cd "${HOME}/.local/share/applications"
		rm ${desktop}/$(basename $i)
		rm ./$(basename $i)
	fi
done

# 完了
yad --title="完了" --on-top --center \
--window-icon=wine-winetricks --image=wine-winetricks \
--text "処理が完了しました。${br}左のパネルからは手動で取り除いてください。"
