|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# The purpose of this script is to provide lightweight desktop integration |
| 4 | +# into the host system. |
| 5 | +# If you want to use it, then place this in usr/bin/$APPNAME.wrapper |
| 6 | +# and set it as the Exec= line of the .desktop file in the AppImage. |
| 7 | + |
| 8 | +# TODO: |
| 9 | +# Handle mime types as well |
| 10 | +# Handle multiple versions of the same AppImage? |
| 11 | +# Handle removed AppImages. Currently we are just setting TryExec= |
| 12 | +# See http://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html#DELETE |
| 13 | +# Possibly move this to the C runtime that is part of every AppImage? |
| 14 | + |
| 15 | +# Makes it easy to distinguish from other desktop files |
| 16 | +# and to clean up |
| 17 | +VENDORPREFIX=appimages |
| 18 | + |
| 19 | +error() |
| 20 | +{ |
| 21 | + if [ -x /usr/bin/zenity ] ; then |
| 22 | + LD_LIBRARY_PATH="" zenity --error --text "${1}" 2>/dev/null |
| 23 | + elif [ -x /usr/bin/kdialog ] ; then |
| 24 | + LD_LIBRARY_PATH="" kdialog --msgbox "${1}" 2>/dev/null |
| 25 | + elif [ -x /usr/bin/Xdialog ] ; then |
| 26 | + LD_LIBRARY_PATH="" Xdialog --msgbox "${1}" 2>/dev/null |
| 27 | + else |
| 28 | + echo "${1}" |
| 29 | + fi |
| 30 | + exit 1 |
| 31 | +} |
| 32 | + |
| 33 | +yesno() |
| 34 | +{ |
| 35 | + TITLE=$1 |
| 36 | + TEXT=$2 |
| 37 | + if [ -x /usr/bin/zenity ] ; then |
| 38 | + LD_LIBRARY_PATH="" zenity --question --title="$TITLE" --text="$TEXT" || SKIP=1 |
| 39 | + elif [ -x /usr/bin/kdialog ] ; then |
| 40 | + LD_LIBRARY_PATH="" kdialog --caption "Disk auswerfen?" --title "$TITLE" -yesno "$TEXT" || SKIP=1 |
| 41 | + elif [ -x /usr/bin/Xdialog ] ; then |
| 42 | + LD_LIBRARY_PATH="" Xdialog --title "$TITLE" --clear --yesno "$TEXT" 10 80 || SKIP=1 |
| 43 | + else |
| 44 | + echo "zenity, kdialog, Xdialog missing. Skipping $0." |
| 45 | + SKIP=1 |
| 46 | + fi |
| 47 | +} |
| 48 | + |
| 49 | +check_dep() |
| 50 | +{ |
| 51 | + DEP=$1 |
| 52 | + if [ -z $(which $DEP) ] ; then |
| 53 | + echo "$DEP is missing. Skipping $0." |
| 54 | + SKIP=1 |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +FILENAME=$(readlink -f "${0}") |
| 59 | + |
| 60 | +echo $FILENAME |
| 61 | + |
| 62 | +if [[ "$FILENAME" != *.wrapper ]] ; then |
| 63 | + echo "$0 is not named correctly. It should be named \$Exec.wrapper" |
| 64 | + SKIP=1 |
| 65 | +fi |
| 66 | + |
| 67 | +BIN=$(echo "$FILENAME" | sed -e 's|.wrapper||g') |
| 68 | +if [[ ! -f $BIN ]] ; then |
| 69 | + echo "$BIN not found" |
| 70 | + SKIP=1 |
| 71 | +fi |
| 72 | + |
| 73 | +DIRNAME=$(dirname $(readlink -f "${0}")) |
| 74 | + |
| 75 | +# Check whether dependencies are present in base system (we do not bundle these) |
| 76 | +# http://cgit.freedesktop.org/xdg/desktop-file-utils/ |
| 77 | +check_dep desktop-file-validate |
| 78 | +check_dep update-desktop-database |
| 79 | +check_dep desktop-file-install |
| 80 | + |
| 81 | +DESKTOPFILE=$(find ../ -name "*.desktop" | head -n 1) |
| 82 | +DESKTOPFILE_NAME=$(basename $DESKTOPFILE) |
| 83 | + |
| 84 | +if [ ! -f "$DESKTOPFILE" ] ; then |
| 85 | + echo "Desktop file is missing. Please run $0 from within an AppImage." |
| 86 | + SKIP=1 |
| 87 | +fi |
| 88 | + |
| 89 | +if [ -z "$APPIMAGE" ] ; then |
| 90 | + echo "\$APPIMAGE is missing. Please run $0 from within an AppImage." |
| 91 | + SKIP=1 |
| 92 | +fi |
| 93 | + |
| 94 | +# Construct path to the icon according to |
| 95 | +# http://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html |
| 96 | +ABS_APPIMAGE=$(readlink -e $APPIMAGE) |
| 97 | +ICONURL="file://$ABS_APPIMAGE" |
| 98 | +MD5=$(echo -n $ICONURL | md5sum | cut -c -32) |
| 99 | +ICONFILE="$HOME/.cache/thumbnails/normal/$MD5.png" |
| 100 | +if [ ! -f "$ICONFILE" ] ; then |
| 101 | + echo "$ICONFILE is missing. Please run $0 from within an AppImage." |
| 102 | + SKIP=1 |
| 103 | +fi |
| 104 | + |
| 105 | +# $XDG_DATA_DIRS contains the default paths /usr/local/share:/usr/share |
| 106 | +# desktop file has to be installed in an applications subdirectory |
| 107 | +# of one of the $XDG_DATA_DIRS components |
| 108 | +if [ -z "$XDG_DATA_DIRS" ] ; then |
| 109 | + echo "\$XDG_DATA_DIRS is missing. Please run $0 from within an AppImage." |
| 110 | + SKIP=1 |
| 111 | +fi |
| 112 | + |
| 113 | +# Determine where the desktop file should be installed |
| 114 | +if [[ $EUID -ne 0 ]]; then |
| 115 | + DESTINATION_DIR_DESKTOP="$HOME/.local/share/applications" |
| 116 | +else |
| 117 | + # TODO: Check $XDG_DATA_DIRS |
| 118 | + DESTINATION_DIR_DESKTOP="/usr/local/share/applications" |
| 119 | +fi |
| 120 | + |
| 121 | +# Check if the desktop file is already there |
| 122 | +# and if so, whether it points to the same AppImage |
| 123 | +if [ -e "$DESTINATION_DIR_DESKTOP/$VENDORPREFIX-$DESKTOPFILE_NAME" ] ; then |
| 124 | + # echo "$DESTINATION_DIR_DESKTOP/$VENDORPREFIX-$DESKTOPFILE_NAME already there" |
| 125 | + EXEC=$(grep "Exec=" "$DESTINATION_DIR_DESKTOP/$VENDORPREFIX-$DESKTOPFILE_NAME" | head -n 1) |
| 126 | + # echo $EXEC |
| 127 | + if [ "Exec=$APPIMAGE" == "$EXEC" ] ; then |
| 128 | + SKIP=1 |
| 129 | + fi |
| 130 | +fi |
| 131 | + |
| 132 | +# We ask the user only if we have found no reason to skip until here |
| 133 | +if [ -z "$SKIP" ] ; then |
| 134 | + yesno "Install" "Should a desktop file for $APPIMAGE be installed?" |
| 135 | +fi |
| 136 | + |
| 137 | +# If the user has agreed, rewrite and install the desktop file |
| 138 | +if [ -z "$SKIP" ] ; then |
| 139 | + # desktop-file-install is supposed to install |
| 140 | + # .desktop files to the user's |
| 141 | + # applications directory when run as a non-root user, |
| 142 | + # and to /usr/share/applications if run as root |
| 143 | + # but that does not really work for me... |
| 144 | + echo desktop-file-install --rebuild-mime-info-cache \ |
| 145 | + --vendor=$VENDORPREFIX --set-key=Exec --set-value=$APPIMAGE \ |
| 146 | + --set-icon=$ICONFILE --set-key=TryExec --set-value=$APPIMAGE $DESKTOPFILE \ |
| 147 | + --dir "$DESTINATION_DIR_DESKTOP" |
| 148 | + desktop-file-install --rebuild-mime-info-cache \ |
| 149 | + --vendor=$VENDORPREFIX --set-key=Exec --set-value=$APPIMAGE \ |
| 150 | + --set-icon=$ICONFILE --set-key=TryExec --set-value=$APPIMAGE $DESKTOPFILE \ |
| 151 | + --dir "$DESTINATION_DIR_DESKTOP" |
| 152 | +fi |
| 153 | + |
| 154 | +xdg-desktop-menu forceupdate |
| 155 | + |
| 156 | +exec $BIN "$@" |
| 157 | +exit $? |
0 commit comments