#!/bin/bash

tempfile="${HOME}/._bean_ppainst_tmp"

#######################################################
#   普通に起動された場合
#######################################################

if [ "$1" == "" ] ; then

	while true ; do
		
		# URLの入力
		url=`
			yad --title="linuxBean PPA管理" --window-icon=ppa --image=ppa --on-top --center --borders=10 --width=600 \
			--text " PPAのIDか、そのIDが記載されているページのURLを入力してください。\n （IDの例：ppa:libreoffice/ppa） 空欄の場合、不要なPPAの削除に移ります。" \
			--form --field=""
		`
		# キャンセルした場合は終了
		if [ $? -gt 0 ] ; then
			exit 1
		fi
		
		# ゴミを除去
		url=`echo "$url" | sed -e 's/|$//'`
		
		# 空欄ではない場合
		if [ ! "$url" == "" ] ; then
			
			# 直接IDが入力された場合も考慮しつつ、URLからIDを取り出す
			if [ $(echo $url | grep -o 'ppa:[0-9a-zA-Z/_\-]\+' | wc -l) -gt 0 ] ; then
				ppa=`echo "${url}" | grep -o 'ppa:[0-9a-zA-Z/_\-]\+' | head -n 1`
			else
				wget "${url}" --no-check-certificate -O "${tempfile}" 2>&1 | \
				yad --title="linuxBean PPA管理" --window-icon=ppa --image=ppa --on-top --center \
				--text "URLからHTMLをダウンロードしています..." --progress --pulsate --auto-close

				# wgetがエラーを吐いた場合
				if [ "`cat ${tempfile}`" == "" ] ; then
					yad --title="linuxBean PPA管理" --window-icon=ppa --image=ppa --on-top --center --borders=10 \
					--text " IDを取り出せませんでした"
					continue
				fi

				ppa=`cat "${tempfile}" | sed -e 's/<wbr \/>//igm' | grep -o 'ppa:[0-9a-zA-Z/_\-]\+'`
				rm -f "${tempfile}"
			fi
			
			
			# 別窓に渡す
			x-terminal-emulator -e "\"$0\" -i ${ppa}" & exit 0
		
		# 空欄の場合はPPAの削除
		else
			# URLの入力
			url=`
				yad --title="linuxBean PPA管理" --window-icon=ppa --image=application-exit --on-top --center --borders=10 --width=600 \
				--text " 削除したいPPAのIDか、そのIDが記載されているページのURLを入力してください。\n （IDの例：ppa:libreoffice/ppa） 空欄の場合、PPAの追加に移ります。" \
				--form --field=""
			`
			# キャンセルした場合は終了
			if [ $? -gt 0 ] ; then
				exit 1
			fi
			
			# ゴミを除去
			url=`echo "$url" | sed -e 's/|$//'`
			
			# 空欄ではない場合
			if [ ! "$url" == "" ] ; then
				
				# 直接IDが入力された場合も考慮しつつ、URLからIDを取り出す
				if [ $(echo $url | grep -o 'ppa:[0-9a-zA-Z/_\-]\+'  | wc -l) -gt 0 ] ; then
					ppa=`echo "${url}" | grep -o 'ppa:[0-9a-zA-Z/_\-]\+' | head -n 1`
				else
					wget "${url}" --no-check-certificate -O "${tempfile}" 2>&1 | \
					yad --title="linuxBean PPA管理" --window-icon=ppa --image=application-exit --on-top --center \
					--text "URLからHTMLをダウンロードしています..." --progress --pulsate --auto-close
	
					# wgetがエラーを吐いた場合
					if [ "`cat ${tempfile}`" == "" ] ; then
						yad --title="linuxBean PPA管理" --window-icon=ppa --image=application-exit --on-top --center --borders=10  \
						--text " IDを取り出せませんでした"
						continue
					fi
	
					ppa=`cat "${tempfile}" | grep -o 'ppa:[0-9a-zA-Z/_\-]\+'`
					rm -f "${tempfile}"
				fi

				# 別窓に渡す
				x-terminal-emulator -e "\"$0\" -p ${ppa}" & exit 0
			
			# 空欄の場合はPPAの追加
			else
				continue
			fi
		fi
	done

elif [ "$1" == "-i" ] ; then
	
	echo $2 をリストに追加します。
	echo 
	sudo add-apt-repository $2
	echo 
	
	echo
	echo Enterを押すと、データベースの更新を行います。
	echo Ctrl+Cを押すと、追加だけでウィンドウを閉じます。
	read ans1
	sudo apt-get update
	
	echo
	echo Enterを押すとウィンドウを閉じます。
	read ans2

elif [ "$1" == "-p" ] ; then

	echo $2 をリストから削除し、パッケージもアンインストールします。
	echo 
	sudo ppa-purge $2
	echo
	
	echo
	echo Enterを押すと、データベースの更新を行います。
	echo Ctrl+Cを押すと、追加だけでウィンドウを閉じます。
	read ans1
	sudo apt-get update
	
	echo
	echo Enterを押すとウィンドウを閉じます。
	read ans2

fi
