aboutsummaryrefslogtreecommitdiff
path: root/webtrayctl
blob: a612914a1b1c961f445c2c3f3887950f7e641ff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/sh

help() {
	echo webtrayctl - install and remove webtray applications
	echo
	echo "    install   <url> <name> [--open-at-startup] [--icon <icon-file>]"
	echo "    uninstall <name>"
	echo
	exit
}

install_webapp() {
	mkdir -p ~/.local/share/webtray/icons
	mkdir -p ~/.local/share/webtray/applications/

	url="$1"
	name="$2"
	open_at_startup="$3"
	iconfile="$4"
	

	full_url=$(curl -Ls -o /dev/null -w%{url_effective} $url)

	if [ -z "$iconfile" ]; then
		echo using favicon as icon file
		full_url_cancled=$(echo $full_url | sed 's/\//\\\//g')
		# get the linkt to the icon by first searching for every <link> tag, containing a icon link
		# then getting the string of the first href in the first link, and then appending the extended url to the beginning, if it is not a relative path (only works for http and https protocols)
		iconfile=$(curl -Ls "$url" | grep -o "<link[^>]*rel=[\"']\\(shortcut \\)\\?icon[\"'][^>]*>" | head -n 1 | sed "s/.*href=[\"']\\([^\"]*\\)[\"'].*/\\1/g" | sed "/^http/!s/.*/$full_url_cancled\\/&/")
	
		file_extension=$(echo "$iconfile" | grep -o '\.[^.]*$')
		tmp_icon_name="$name$file_extension"

		if [ -n "$iconfile" ]; then
			curl -s "$iconfile" --output "/tmp/${tmp_icon_name}" 
			magick "/tmp/${tmp_icon_name}[0]" "$HOME/.local/share/webtray/icons/$name.png"
			rm "/tmp/$tmp_icon_name"
			iconfile="$HOME/.local/share/webtray/icons/$name.png"
		else
			echo "the website does not seem to have a favicon"
			echo "consider adding the local path to a favicon on your computer with the --icon argument"
		fi
	fi

	echo "[Desktop Entry]
Type=Application
Name=$name
Exec=$(which webtray) '$full_url' "$open_at_startup" "$iconfile"
Icon="$iconfile"
Terminal=false
Categories=WebApp
" > "$HOME/.local/share/webtray/applications/webtray-$name.desktop"

	xdg-desktop-menu install "$HOME/.local/share/webtray/applications/webtray-$name.desktop"

	exit
}

remove_webapp() {
	name="$1"
	xdg-desktop-menu uninstall "$HOME/.local/share/webtray/applications/webtray-$name.desktop"
	rm "$HOME/.local/share/webtray/applications/webtray-$name.desktop" "$HOME/.local/share/webtray/icons/$name.png"
	exit
}

while true
do
	case "$1" in
		install)
			action="install"
			shift
			url="$1"
			name="$2"
			shift 2
			;;
		uninstall)
			action="uninstall"
			shift
			toRemove="$1"
			shift
			;;
		"--open-at-startup")
			echo we are opening at startup
			echo "$argument"
			open_at_startup="--open-at-startup"
			shift
			;;
		"--icon")
			shift
			icon="$1"
			[ -z "$1" ] && help
			shift
			;;
		*)help;;
	esac
	if [ -z "$1" ]; then
		break
	fi
	#shift 1 || break
done

case "$action" in
	install)
		[ -z "$url" -o -z "$name" ] && help
		install_webapp "$url" "$name" "$open_at_startup" "$icon"
		;;
	uninstall)
		[ -z "$toRemove" ] && help
		remove_webapp "$toRemove"
		;;
	*)help;;
esac