sdrangel: fix build on x86_64-darwin
[NixPkgs.git] / pkgs / build-support / appimage / appimage-exec.sh
blobb91d13c4a235603f74eba4ef2db301fe0d9cea5b
1 #!@shell@
2 # shellcheck shell=bash
4 if [ -n "$DEBUG" ] ; then
5 set -x
6 fi
8 PATH="@path@:$PATH"
9 apprun_opt=true
10 OWD=$(readlink -f .)
11 # can be read by appimages: https://docs.appimage.org/packaging-guide/environment-variables.html
12 export OWD
14 # src : AppImage
15 # dest : let's unpack() create the directory
16 unpack() {
17 local src="$1"
18 local out="$2"
20 # https://github.com/AppImage/libappimage/blob/ca8d4b53bed5cbc0f3d0398e30806e0d3adeaaab/src/libappimage/utils/MagicBytesChecker.cpp#L45-L63
21 local appimageSignature;
22 appimageSignature="$(LC_ALL=C readelf -h "$src" | awk 'NR==2{print $10$11;}')"
23 local appimageType;
24 appimageType="$(LC_ALL=C readelf -h "$src" | awk 'NR==2{print $12;}')"
26 # check AppImage signature
27 if [ "$appimageSignature" != "4149" ]; then
28 echo "Not an AppImage file"
29 exit
32 case "$appimageType" in
33 "01")
34 echo "Uncompress $(basename "$src") of type $appimageType"
35 mkdir "$out"
36 pv "$src" | bsdtar -x -C "$out" -f -
39 "02")
40 # This method avoid issues with non executable appimages,
41 # non-native packer, packer patching and squashfs-root destination prefix.
43 # multiarch offset one-liner using same method as AppImage
44 # see https://gist.github.com/probonopd/a490ba3401b5ef7b881d5e603fa20c93
45 offset=$(LC_ALL=C readelf -h "$src" | awk 'NR==13{e_shoff=$5} NR==18{e_shentsize=$5} NR==19{e_shnum=$5} END{print e_shoff+e_shentsize*e_shnum}')
46 echo "Uncompress $(basename "$src") of type $appimageType @ offset $offset"
47 unsquashfs -q -d "$out" -o "$offset" "$src"
48 chmod go-w "$out"
51 # "03")
52 # get ready, https://github.com/TheAssassin/type3-runtime
55 echo Unsupported AppImage Type: "$appimageType"
56 exit
58 esac
59 echo "$(basename "$src") is now installed in $out"
62 apprun() {
64 SHA256=$(sha256sum "$APPIMAGE" | awk '{print $1}')
65 export APPDIR="${XDG_CACHE_HOME:-$HOME/.cache}/appimage-run/$SHA256"
67 #compatibility
68 if [ -x "$APPDIR/squashfs-root" ]; then APPDIR="$APPDIR/squashfs-root"; fi
70 if [ ! -x "$APPDIR" ]; then
71 mkdir -p "$(dirname "$APPDIR")"
72 unpack "$APPIMAGE" "$APPDIR"
73 else echo "$(basename "$APPIMAGE")" installed in "$APPDIR"
76 export PATH="$PATH:$PWD/usr/bin"
79 wrap() {
81 # quite same in appimageTools
82 export APPIMAGE_SILENT_INSTALL=1
84 if [ -n "$APPIMAGE_DEBUG_EXEC" ]; then
85 cd "$APPDIR" || true
86 exec "$APPIMAGE_DEBUG_EXEC"
89 exec "$APPDIR/AppRun" "$@"
92 usage() {
93 cat <<EOF
94 Usage: appimage-run [appimage-run options] <AppImage> [AppImage options]
96 -h show this message
97 -d debug mode
98 -x <directory> : extract appimage in the directory then exit.
99 -w <directory> : run uncompressed appimage directory (used in appimageTools)
101 [AppImage options]: Options are passed on to the appimage.
102 If you want to execute a custom command in the appimage's environment, set the APPIMAGE_DEBUG_EXEC environment variable.
105 exit 1
108 while getopts "x:w:dh" option; do
109 case "${option}" in
110 d) set -x
112 x) # eXtract
113 unpack_opt=true
114 APPDIR=${OPTARG}
116 w) # WrapAppImage
117 export APPDIR=${OPTARG}
118 wrap_opt=true
120 h) usage
122 *) usage
124 esac
125 done
126 shift "$((OPTIND-1))"
128 if [ -n "$wrap_opt" ] && [ -d "$APPDIR" ]; then
129 wrap "$@"
130 exit
131 else
132 APPIMAGE="$(realpath "$1")" || usage
133 shift
136 if [ -n "$unpack_opt" ] && [ -f "$APPIMAGE" ]; then
137 unpack "$APPIMAGE" "$APPDIR"
138 exit
141 if [ -n "$apprun_opt" ] && [ -f "$APPIMAGE" ]; then
142 apprun
143 wrap "$@"
144 exit