|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# This script adds a menu item, icons and mime type for Arduino for the current |
| 4 | +# user. If possible, it will use the xdg-utils - or fall back to just creating |
| 5 | +# and copying a desktop file to the user's dir. |
| 6 | +# If called with the "-u" option, it will undo the changes. |
| 7 | + |
| 8 | +# Resource name to use (including vendor prefix) |
| 9 | +RESOURCE_NAME=processing-processingide |
| 10 | + |
| 11 | +# Get absolute path from which this script file was executed |
| 12 | +# (Could be changed to "pwd -P" to resolve symlinks to their target) |
| 13 | +SCRIPT_PATH=$( cd $(dirname $0) ; pwd ) |
| 14 | +cd "${SCRIPT_PATH}" |
| 15 | + |
| 16 | +# Default mode is to install. |
| 17 | +UNINSTALL=false |
| 18 | + |
| 19 | +# If possible, get location of the desktop folder. Default to ~/Desktop |
| 20 | +XDG_DESKTOP_DIR="${HOME}/Desktop" |
| 21 | +if [ -f "${XDG_CONFIG_HOME:-${HOME}/.config}/user-dirs.dirs" ]; then |
| 22 | + . "${XDG_CONFIG_HOME:-${HOME}/.config}/user-dirs.dirs" |
| 23 | +fi |
| 24 | + |
| 25 | +# Install using xdg-utils |
| 26 | +xdg_install_f() { |
| 27 | + |
| 28 | + # Create a temp dir accessible by all users |
| 29 | + TMP_DIR=`mktemp --directory` |
| 30 | + |
| 31 | + # Create *.desktop file using the existing template file |
| 32 | + sed -e "s,<BINARY_LOCATION>,${SCRIPT_PATH}/processing,g" \ |
| 33 | + -e "s,<ICON_NAME>,${RESOURCE_NAME},g" "${SCRIPT_PATH}/lib/desktop.template" > "${TMP_DIR}/${RESOURCE_NAME}.desktop" |
| 34 | + |
| 35 | + # Install the icon files using name and resolutions |
| 36 | + xdg-icon-resource install --context mimetypes --size 16 "${SCRIPT_PATH}/lib/icons/pde-16.png" $RESOURCE_NAME |
| 37 | + xdg-icon-resource install --context mimetypes --size 32 "${SCRIPT_PATH}/lib/icons/pde-32.png" $RESOURCE_NAME |
| 38 | + xdg-icon-resource install --context mimetypes --size 48 "${SCRIPT_PATH}/lib/icons/pde-48.png" $RESOURCE_NAME |
| 39 | + xdg-icon-resource install --context mimetypes --size 64 "${SCRIPT_PATH}/lib/icons/pde-64.png" $RESOURCE_NAME |
| 40 | + xdg-icon-resource install --context mimetypes --size 128 "${SCRIPT_PATH}/lib/icons/pde-128.png" $RESOURCE_NAME |
| 41 | + xdg-icon-resource install --context mimetypes --size 256 "${SCRIPT_PATH}/lib/icons/pde-256.png" $RESOURCE_NAME |
| 42 | + xdg-icon-resource install --context mimetypes --size 512 "${SCRIPT_PATH}/lib/icons/pde-512.png" $RESOURCE_NAME |
| 43 | + xdg-icon-resource install --context mimetypes --size 1024 "${SCRIPT_PATH}/lib/icons/pde-1024.png" $RESOURCE_NAME |
| 44 | + |
| 45 | + # Install the created *.desktop file |
| 46 | + xdg-desktop-menu install "${TMP_DIR}/${RESOURCE_NAME}.desktop" |
| 47 | + |
| 48 | + # Create icon on the desktop |
| 49 | + xdg-desktop-icon install "${TMP_DIR}/${RESOURCE_NAME}.desktop" |
| 50 | + |
| 51 | + # Install Processing mime type |
| 52 | + xdg-mime install "${SCRIPT_PATH}/lib/${RESOURCE_NAME}.xml" |
| 53 | + |
| 54 | + # Install icons for mime type |
| 55 | + xdg-icon-resource install --context mimetypes --size 16 "${SCRIPT_PATH}/lib/icons/pde-16.png" text-x-processing |
| 56 | + xdg-icon-resource install --context mimetypes --size 32 "${SCRIPT_PATH}/lib/icons/pde-32.png" text-x-processing |
| 57 | + xdg-icon-resource install --context mimetypes --size 48 "${SCRIPT_PATH}/lib/icons/pde-48.png" text-x-processing |
| 58 | + xdg-icon-resource install --context mimetypes --size 64 "${SCRIPT_PATH}/lib/icons/pde-64.png" text-x-processing |
| 59 | + xdg-icon-resource install --context mimetypes --size 128 "${SCRIPT_PATH}/lib/icons/pde-128.png" text-x-processing |
| 60 | + xdg-icon-resource install --context mimetypes --size 256 "${SCRIPT_PATH}/lib/icons/pde-256.png" text-x-processing |
| 61 | + xdg-icon-resource install --context mimetypes --size 512 "${SCRIPT_PATH}/lib/icons/pde-512.png" text-x-processing |
| 62 | + xdg-icon-resource install --context mimetypes --size 1024 "${SCRIPT_PATH}/lib/icons/pde-1024.png" text-x-processing |
| 63 | + |
| 64 | + # Make Processing IDE the default application for *.pde |
| 65 | + xdg-mime default ${RESOURCE_NAME}.desktop text/x-processing |
| 66 | + |
| 67 | + # Clean up |
| 68 | + rm "${TMP_DIR}/${RESOURCE_NAME}.desktop" |
| 69 | + rmdir "$TMP_DIR" |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +# Install by simply copying desktop file (fallback) |
| 74 | +simple_install_f() { |
| 75 | + |
| 76 | + # Create a temp dir accessible by all users |
| 77 | + TMP_DIR=`mktemp --directory` |
| 78 | + |
| 79 | + # Create *.desktop file using the existing template file |
| 80 | + sed -e "s,<BINARY_LOCATION>,${SCRIPT_PATH}/processing,g" \ |
| 81 | + -e "s,<ICON_NAME>,${SCRIPT_PATH}/lib/icons/pde-128.png,g" "${SCRIPT_PATH}/lib/desktop.template" > "${TMP_DIR}/${RESOURCE_NAME}.desktop" |
| 82 | + |
| 83 | + mkdir -p "${HOME}/.local/share/applications" |
| 84 | + cp "${TMP_DIR}/${RESOURCE_NAME}.desktop" "${HOME}/.local/share/applications/" |
| 85 | + |
| 86 | + mkdir -p "${HOME}/.local/share/metainfo" |
| 87 | + cp "${SCRIPT_PATH}/lib/appdata.xml" "${HOME}/.local/share/metainfo/${RESOURCE_NAME}.appdata.xml" |
| 88 | + |
| 89 | + # Copy desktop icon if desktop dir exists (was found) |
| 90 | + if [ -d "${XDG_DESKTOP_DIR}" ]; then |
| 91 | + cp "${TMP_DIR}/${RESOURCE_NAME}.desktop" "${XDG_DESKTOP_DIR}/" |
| 92 | + # Altering file permissions to avoid "Untrusted Application Launcher" error on Ubuntu |
| 93 | + chmod u+x "${XDG_DESKTOP_DIR}/${RESOURCE_NAME}.desktop" |
| 94 | + fi |
| 95 | + |
| 96 | + # Clean up temp dir |
| 97 | + rm "${TMP_DIR}/${RESOURCE_NAME}.desktop" |
| 98 | + rmdir "${TMP_DIR}" |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +# Uninstall using xdg-utils |
| 103 | +xdg_uninstall_f() { |
| 104 | + |
| 105 | + # Remove *.desktop file |
| 106 | + xdg-desktop-menu uninstall ${RESOURCE_NAME}.desktop |
| 107 | + |
| 108 | + # Remove icon from desktop |
| 109 | + xdg-desktop-icon uninstall ${RESOURCE_NAME}.desktop |
| 110 | + |
| 111 | + # Remove icons |
| 112 | + xdg-icon-resource uninstall --size 16 $RESOURCE_NAME |
| 113 | + xdg-icon-resource uninstall --size 32 $RESOURCE_NAME |
| 114 | + xdg-icon-resource uninstall --size 48 $RESOURCE_NAME |
| 115 | + xdg-icon-resource uninstall --size 64 $RESOURCE_NAME |
| 116 | + xdg-icon-resource uninstall --size 128 $RESOURCE_NAME |
| 117 | + xdg-icon-resource uninstall --size 256 $RESOURCE_NAME |
| 118 | + xdg-icon-resource uninstall --size 512 $RESOURCE_NAME |
| 119 | + xdg-icon-resource uninstall --size 1024 $RESOURCE_NAME |
| 120 | + |
| 121 | + # Remove MIME type icons |
| 122 | + xdg-icon-resource uninstall --size 16 text-x-processing |
| 123 | + xdg-icon-resource uninstall --size 32 text-x-processing |
| 124 | + xdg-icon-resource uninstall --size 48 text-x-processing |
| 125 | + xdg-icon-resource uninstall --size 64 text-x-processing |
| 126 | + xdg-icon-resource uninstall --size 128 text-x-processing |
| 127 | + xdg-icon-resource uninstall --size 256 text-x-processing |
| 128 | + xdg-icon-resource uninstall --size 512 text-x-processing |
| 129 | + xdg-icon-resource uninstall --size 1024 text-x-processing |
| 130 | + |
| 131 | + # Remove Arduino MIME type |
| 132 | + xdg-mime uninstall "${SCRIPT_PATH}/lib/${RESOURCE_NAME}.xml" |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +# Uninstall by simply removing desktop files (fallback), incl. old one |
| 137 | +simple_uninstall_f() { |
| 138 | + |
| 139 | + # delete legacy cruft .desktop file |
| 140 | + if [ -f "${HOME}/.local/share/applications/processing.desktop" ]; then |
| 141 | + rm "${HOME}/.local/share/applications/processing.desktop" |
| 142 | + fi |
| 143 | + |
| 144 | + # delete another legacy .desktop file |
| 145 | + if [ -f "${HOME}/.local/share/applications/processing-processingide.desktop" ]; then |
| 146 | + rm "${HOME}/.local/share/applications/processing-processingide.desktop" |
| 147 | + fi |
| 148 | + |
| 149 | + if [ -f "${HOME}/.local/share/applications/${RESOURCE_NAME}.desktop" ]; then |
| 150 | + rm "${HOME}/.local/share/applications/${RESOURCE_NAME}.desktop" |
| 151 | + fi |
| 152 | + |
| 153 | + if [ -f "${HOME}/.local/share/metainfo/${RESOURCE_NAME}.appdata.xml" ]; then |
| 154 | + rm "${HOME}/.local/share/metainfo/${RESOURCE_NAME}.appdata.xml" |
| 155 | + fi |
| 156 | + |
| 157 | + if [ -f "${XDG_DESKTOP_DIR}/processing.desktop" ]; then |
| 158 | + rm "${XDG_DESKTOP_DIR}/processing.desktop" |
| 159 | + fi |
| 160 | + |
| 161 | + if [ -f "${XDG_DESKTOP_DIR}/${RESOURCE_NAME}.desktop" ]; then |
| 162 | + rm "${XDG_DESKTOP_DIR}/${RESOURCE_NAME}.desktop" |
| 163 | + fi |
| 164 | + |
| 165 | +} |
| 166 | + |
| 167 | +# Update desktop file and mime databases (if possible) |
| 168 | +updatedbs_f() { |
| 169 | + |
| 170 | + if [ -d "${HOME}/.local/share/applications" ]; then |
| 171 | + if command -v update-desktop-database > /dev/null; then |
| 172 | + update-desktop-database "${HOME}/.local/share/applications" |
| 173 | + fi |
| 174 | + fi |
| 175 | + |
| 176 | + if [ -d "${HOME}/.local/share/mime" ]; then |
| 177 | + if command -v update-mime-database > /dev/null; then |
| 178 | + update-mime-database "${HOME}/.local/share/mime" |
| 179 | + fi |
| 180 | + fi |
| 181 | + |
| 182 | +} |
| 183 | + |
| 184 | +# Check availability of xdg-utils |
| 185 | +xdg_exists_f() { |
| 186 | + |
| 187 | + if ! command -v xdg-icon-resource > /dev/null; then return 1; fi |
| 188 | + if ! command -v xdg-desktop-menu > /dev/null; then return 1; fi |
| 189 | + if ! command -v xdg-desktop-icon > /dev/null; then return 1; fi |
| 190 | + if ! command -v xdg-mime > /dev/null; then return 1; fi |
| 191 | + return 0 |
| 192 | + |
| 193 | +} |
| 194 | + |
| 195 | +# Shows a description of the available options |
| 196 | +display_help_f() { |
| 197 | + printf "\nThis script will add a Processing IDE desktop shortcut, menu item,\n" |
| 198 | + printf "icons and file associations for the current user.\n" |
| 199 | + if ! xdg_exists_f; then |
| 200 | + printf "\nxdg-utils are recommended to be installed, so this script can use them.\n" |
| 201 | + fi |
| 202 | + printf "\nOptional arguments are:\n\n" |
| 203 | + printf "\t-u, --uninstall\t\tRemoves shortcut, menu item and icons.\n\n" |
| 204 | + printf "\t-h, --help\t\tShows this help again.\n\n" |
| 205 | +} |
| 206 | + |
| 207 | +# Check for provided arguments |
| 208 | +while [ $# -gt 0 ] ; do |
| 209 | + ARG="${1}" |
| 210 | + case $ARG in |
| 211 | + -u|--uninstall) |
| 212 | + UNINSTALL=true |
| 213 | + shift |
| 214 | + ;; |
| 215 | + -h|--help) |
| 216 | + display_help_f |
| 217 | + exit 0 |
| 218 | + ;; |
| 219 | + *) |
| 220 | + printf "\nInvalid option -- '${ARG}'\n" |
| 221 | + display_help_f |
| 222 | + exit 1 |
| 223 | + ;; |
| 224 | + esac |
| 225 | +done |
| 226 | + |
| 227 | +# If possible, use xdg-utils, if not, use a more basic approach |
| 228 | +if xdg_exists_f; then |
| 229 | + if [ ${UNINSTALL} = true ]; then |
| 230 | + printf "Removing desktop shortcut and menu item for Processing IDE..." |
| 231 | + xdg_uninstall_f |
| 232 | + simple_uninstall_f |
| 233 | + else |
| 234 | + printf "Adding desktop shortcut, menu item and file associations for Processing IDE..." |
| 235 | + xdg_uninstall_f |
| 236 | + simple_uninstall_f |
| 237 | + xdg_install_f |
| 238 | + fi |
| 239 | +else |
| 240 | + if [ ${UNINSTALL} = true ]; then |
| 241 | + printf "Removing desktop shortcut and menu item for Processing IDE..." |
| 242 | + simple_uninstall_f |
| 243 | + else |
| 244 | + printf "Adding desktop shortcut and menu item for Processing IDE..." |
| 245 | + simple_uninstall_f |
| 246 | + simple_install_f |
| 247 | + fi |
| 248 | +fi |
| 249 | +updatedbs_f |
| 250 | +printf " done!\n" |
| 251 | + |
| 252 | +exit 0 |
0 commit comments