#!/bin/sh help() { echo webtrayctl - install and remove webtray applications echo echo " install [--open-at-startup] [--icon ]" echo " uninstall " 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 full_url_cancled=$(echo $full_url | sed 's/\//\\\//g') # get the linkt to the icon by first searching for every 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 "]*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 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") 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