File: install.sh

package info (click to toggle)
arduino 2%3A1.8.19%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,964 kB
  • sloc: java: 26,464; python: 8,872; cpp: 3,573; xml: 1,237; sh: 551; perl: 90; ansic: 89; makefile: 53
file content (290 lines) | stat: -rwxr-xr-x 11,070 bytes parent folder | download | duplicates (3)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/sh

# This script adds a menu item, icons and mime type for Arduino for the current
# user. If possible, it will use the xdg-utils - or fall back to just creating
# and copying a desktop file to the user's dir.
# If called with the "-u" option, it will undo the changes.

# Resource name to use (including vendor prefix)
RESOURCE_NAME=arduino-arduinoide

# Get absolute path from which this script file was executed
# (Could be changed to "pwd -P" to resolve symlinks to their target)
SCRIPT_PATH=$( cd $(dirname $0) ; pwd )
cd "${SCRIPT_PATH}"

# Default mode is to install.
UNINSTALL=false

# If possible, get location of the desktop folder. Default to ~/Desktop
XDG_DESKTOP_DIR="${HOME}/Desktop"
if [ -f "${XDG_CONFIG_HOME:-${HOME}/.config}/user-dirs.dirs" ]; then
  . "${XDG_CONFIG_HOME:-${HOME}/.config}/user-dirs.dirs"
fi

# Install using xdg-utils
xdg_install_f() {

  # Create a temp dir accessible by all users
  TMP_DIR=`mktemp --directory`

  # Create *.desktop file using the existing template file
  sed -e "s,<BINARY_LOCATION>,\"${SCRIPT_PATH}/arduino\",g" \
      -e "s,<ICON_NAME>,${RESOURCE_NAME},g" "${SCRIPT_PATH}/lib/desktop.template" > "${TMP_DIR}/${RESOURCE_NAME}.desktop"

  # Install the icon files using name and resolutions
  xdg-icon-resource install --context apps --size 16 "${SCRIPT_PATH}/lib/icons/16x16/apps/arduino.png" $RESOURCE_NAME
  xdg-icon-resource install --context apps --size 24 "${SCRIPT_PATH}/lib/icons/24x24/apps/arduino.png" $RESOURCE_NAME
  xdg-icon-resource install --context apps --size 32 "${SCRIPT_PATH}/lib/icons/32x32/apps/arduino.png" $RESOURCE_NAME
  xdg-icon-resource install --context apps --size 48 "${SCRIPT_PATH}/lib/icons/48x48/apps/arduino.png" $RESOURCE_NAME
  xdg-icon-resource install --context apps --size 64 "${SCRIPT_PATH}/lib/icons/64x64/apps/arduino.png" $RESOURCE_NAME
  xdg-icon-resource install --context apps --size 72 "${SCRIPT_PATH}/lib/icons/72x72/apps/arduino.png" $RESOURCE_NAME
  xdg-icon-resource install --context apps --size 96 "${SCRIPT_PATH}/lib/icons/96x96/apps/arduino.png" $RESOURCE_NAME
  xdg-icon-resource install --context apps --size 128 "${SCRIPT_PATH}/lib/icons/128x128/apps/arduino.png" $RESOURCE_NAME
  xdg-icon-resource install --context apps --size 256 "${SCRIPT_PATH}/lib/icons/256x256/apps/arduino.png" $RESOURCE_NAME

  # Install the created *.desktop file
  xdg-desktop-menu install "${TMP_DIR}/${RESOURCE_NAME}.desktop"

  # Create icon on the desktop
  xdg-desktop-icon install "${TMP_DIR}/${RESOURCE_NAME}.desktop"

  # Install Arduino mime type
  xdg-mime install "${SCRIPT_PATH}/lib/${RESOURCE_NAME}.xml"

  # Install icons for mime type
  xdg-icon-resource install --context mimetypes --size 16 "${SCRIPT_PATH}/lib/icons/16x16/apps/arduino.png" text-x-arduino
  xdg-icon-resource install --context mimetypes --size 24 "${SCRIPT_PATH}/lib/icons/24x24/apps/arduino.png" text-x-arduino
  xdg-icon-resource install --context mimetypes --size 32 "${SCRIPT_PATH}/lib/icons/32x32/apps/arduino.png" text-x-arduino
  xdg-icon-resource install --context mimetypes --size 48 "${SCRIPT_PATH}/lib/icons/48x48/apps/arduino.png" text-x-arduino
  xdg-icon-resource install --context mimetypes --size 64 "${SCRIPT_PATH}/lib/icons/64x64/apps/arduino.png" text-x-arduino
  xdg-icon-resource install --context mimetypes --size 72 "${SCRIPT_PATH}/lib/icons/72x72/apps/arduino.png" text-x-arduino
  xdg-icon-resource install --context mimetypes --size 96 "${SCRIPT_PATH}/lib/icons/96x96/apps/arduino.png" text-x-arduino
  xdg-icon-resource install --context mimetypes --size 128 "${SCRIPT_PATH}/lib/icons/128x128/apps/arduino.png" text-x-arduino
  xdg-icon-resource install --context mimetypes --size 256 "${SCRIPT_PATH}/lib/icons/256x256/apps/arduino.png" text-x-arduino

  # Make Arduino IDE the default application for *.ino
  xdg-mime default ${RESOURCE_NAME}.desktop text/x-arduino

  if [ x${SUDO_USER} != x ]; then
   chown ${SUDO_USER} "${XDG_DESKTOP_DIR}/${RESOURCE_NAME}.desktop"
  fi

  # Add symlink for arduino so it's in users path
  echo "" # Ensure password request message is on new line
  if ! ln -s ${SCRIPT_PATH}/arduino /usr/local/bin/arduino; then
      echo "Adding symlink failed. Hope that's OK. If not then rerun as root with sudo."
  fi

  # Clean up
  rm "${TMP_DIR}/${RESOURCE_NAME}.desktop"
  rmdir "$TMP_DIR"

  # Launching arduino-linux-setup.sh script
  #./arduino-linux-setup.sh $(whoami)

}

# Install by simply copying desktop file (fallback)
simple_install_f() {

  # Create a temp dir accessible by all users
  TMP_DIR=`mktemp --directory`

  # Create *.desktop file using the existing template file
  sed -e "s,<BINARY_LOCATION>,${SCRIPT_PATH}/arduino,g" \
      -e "s,<ICON_NAME>,${SCRIPT_PATH}/lib/arduino.png,g" "${SCRIPT_PATH}/lib/desktop.template" > "${TMP_DIR}/${RESOURCE_NAME}.desktop"

  mkdir -p "${HOME}/.local/share/applications"
  cp "${TMP_DIR}/${RESOURCE_NAME}.desktop" "${HOME}/.local/share/applications/"

  mkdir -p "${HOME}/.local/share/metainfo"
  cp "${SCRIPT_PATH}/lib/appdata.xml" "${HOME}/.local/share/metainfo/${RESOURCE_NAME}.appdata.xml"

  # Copy desktop icon if desktop dir exists (was found)
  if [ -d "${XDG_DESKTOP_DIR}" ]; then
   cp "${TMP_DIR}/${RESOURCE_NAME}.desktop" "${XDG_DESKTOP_DIR}/"
   # Altering file permissions to avoid "Untrusted Application Launcher" error on Ubuntu
   chmod u+x "${XDG_DESKTOP_DIR}/${RESOURCE_NAME}.desktop"
   if [ x${SUDO_USER} != x ]; then
    chown ${SUDO_USER} "${XDG_DESKTOP_DIR}/${RESOURCE_NAME}.desktop"
   fi
  fi

  # Add symlink for arduino so it's in users path
  echo "" # Ensure password request message is on new line
  if ! ln -s ${SCRIPT_PATH}/arduino /usr/local/bin/arduino; then
      echo "Adding symlink failed. Hope that's OK. If not then rerun as root with sudo."
  fi

  # Clean up temp dir
  rm "${TMP_DIR}/${RESOURCE_NAME}.desktop"
  rmdir "${TMP_DIR}"

}

# Uninstall using xdg-utils
xdg_uninstall_f() {

  # Remove *.desktop file
  xdg-desktop-menu uninstall ${RESOURCE_NAME}.desktop

  # Remove icon from desktop
  xdg-desktop-icon uninstall ${RESOURCE_NAME}.desktop

  # Remove icons
  xdg-icon-resource uninstall --size 16 ${RESOURCE_NAME}
  xdg-icon-resource uninstall --size 24 ${RESOURCE_NAME}
  xdg-icon-resource uninstall --size 32 ${RESOURCE_NAME}
  xdg-icon-resource uninstall --size 48 ${RESOURCE_NAME}
  xdg-icon-resource uninstall --size 64 ${RESOURCE_NAME}
  xdg-icon-resource uninstall --size 72 ${RESOURCE_NAME}
  xdg-icon-resource uninstall --size 96 ${RESOURCE_NAME}
  xdg-icon-resource uninstall --size 128 ${RESOURCE_NAME}
  xdg-icon-resource uninstall --size 256 ${RESOURCE_NAME}

  # Remove MIME type icons
  xdg-icon-resource uninstall --size 16  text-x-arduino
  xdg-icon-resource uninstall --size 24  text-x-arduino
  xdg-icon-resource uninstall --size 32  text-x-arduino
  xdg-icon-resource uninstall --size 48  text-x-arduino
  xdg-icon-resource uninstall --size 64  text-x-arduino
  xdg-icon-resource uninstall --size 72  text-x-arduino
  xdg-icon-resource uninstall --size 96  text-x-arduino
  xdg-icon-resource uninstall --size 128  text-x-arduino
  xdg-icon-resource uninstall --size 256  text-x-arduino

  # Remove Arduino MIME type
  xdg-mime uninstall "${SCRIPT_PATH}/lib/${RESOURCE_NAME}.xml"

  # Remove symlink for arduino
  echo "" # Ensure password request message is on new line
  if [ -f /usr/local/bin/arduino ]; then
      rm /usr/local/bin/arduino || echo "Removing symlink failed. Hope that's OK. If not then rerun as root with sudo."
  fi

}

# Uninstall by simply removing desktop files (fallback), incl. old one
simple_uninstall_f() {

  # delete legacy cruft .desktop file
  if [ -f "${HOME}/.local/share/applications/arduino.desktop" ]; then
    rm "${HOME}/.local/share/applications/arduino.desktop"
  fi

  # delete another legacy .desktop file
  if [ -f "${HOME}/.local/share/applications/arduino-arduinoide.desktop" ]; then
    rm "${HOME}/.local/share/applications/arduino-arduinoide.desktop"
  fi

  if [ -f "${HOME}/.local/share/applications/${RESOURCE_NAME}.desktop" ]; then
    rm "${HOME}/.local/share/applications/${RESOURCE_NAME}.desktop"
  fi

  if [ -f "${HOME}/.local/share/metainfo/${RESOURCE_NAME}.appdata.xml" ]; then
    rm "${HOME}/.local/share/metainfo/${RESOURCE_NAME}.appdata.xml"
  fi

  if [ -f "${XDG_DESKTOP_DIR}/arduino.desktop" ]; then
    rm "${XDG_DESKTOP_DIR}/arduino.desktop"
  fi

  if [ -f "${XDG_DESKTOP_DIR}/${RESOURCE_NAME}.desktop" ]; then
    rm "${XDG_DESKTOP_DIR}/${RESOURCE_NAME}.desktop"
  fi

  # Remove symlink for arduino
  echo "" # Ensure password request message is on new line
  if [ -f /usr/local/bin/arduino ]; then
      rm /usr/local/bin/arduino || echo "Removing symlink failed. Hope that's OK. If not then rerun as root with sudo."
  fi

}

# Update desktop file and mime databases (if possible)
updatedbs_f() {

  if [ -d "${HOME}/.local/share/applications" ]; then
    if command -v update-desktop-database > /dev/null; then
      update-desktop-database "${HOME}/.local/share/applications"
    fi
  fi

  if [ -d "${HOME}/.local/share/mime" ]; then
    if command -v update-mime-database > /dev/null; then
      update-mime-database "${HOME}/.local/share/mime"
    fi
  fi

}

# Check availability of xdg-utils
xdg_exists_f() {

  if ! command -v xdg-icon-resource > /dev/null; then return 1; fi
  if ! command -v xdg-desktop-menu > /dev/null; then return 1; fi
  if ! command -v xdg-desktop-icon > /dev/null; then return 1; fi
  if ! command -v xdg-mime > /dev/null; then return 1; fi
  return 0

}

# Shows a description of the available options
display_help_f() {
  printf "\nThis script will add a Arduino IDE desktop shortcut, menu item,\n"
  printf "icons and file associations for the current user.\n"
  if ! xdg_exists_f; then
    printf "\nxdg-utils are recommended to be installed, so this script can use them.\n"
  fi
  printf "\nOptional arguments are:\n\n"
  printf "\t-u, --uninstall\t\tRemoves shortcut, menu item and icons.\n\n"
  printf "\t-h, --help\t\tShows this help again.\n\n"
}

# Check for provided arguments
while [ $# -gt 0 ] ; do
  ARG="${1}"
  case $ARG in
      -u|--uninstall)
        UNINSTALL=true
        shift
      ;;
      -h|--help)
        display_help_f
        exit 0
      ;;
      *)
        printf "\nInvalid option -- '${ARG}'\n"
        display_help_f
        exit 1
      ;;
  esac
done

# If possible, use xdg-utils, if not, use a more basic approach
if xdg_exists_f; then
  if [ ${UNINSTALL} = true ]; then
    printf "Removing desktop shortcut and menu item for Arduino IDE..."
    xdg_uninstall_f
    simple_uninstall_f
  else
    printf "Adding desktop shortcut, menu item and file associations for Arduino IDE..."
    xdg_uninstall_f
    simple_uninstall_f
    xdg_install_f
  fi
else
  if [ ${UNINSTALL} = true ]; then
    printf "Removing desktop shortcut and menu item for Arduino IDE..."
    simple_uninstall_f
  else
    printf "Adding desktop shortcut and menu item for Arduino IDE..."
    simple_uninstall_f
    simple_install_f
  fi
fi
updatedbs_f
printf " done!\n"

exit 0