pytrainer: unpin python 3.10
[NixPkgs.git] / pkgs / build-support / appimage / appimage-exec.sh
blob975dfb4c3d9375af3b6a16b16c10ceacc5368841
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
13 export APPIMAGE
15 # src : AppImage
16 # dest : let's unpack() create the directory
17 unpack() {
18 local src="$1"
19 local out="$2"
21 # https://github.com/AppImage/libappimage/blob/ca8d4b53bed5cbc0f3d0398e30806e0d3adeaaab/src/libappimage/utils/MagicBytesChecker.cpp#L45-L63
22 local appimageSignature;
23 appimageSignature="$(LC_ALL=C readelf -h "$src" | awk 'NR==2{print $10$11;}')"
24 local appimageType;
25 appimageType="$(LC_ALL=C readelf -h "$src" | awk 'NR==2{print $12;}')"
27 # check AppImage signature
28 if [ "$appimageSignature" != "4149" ]; then
29 echo "Not an AppImage file"
30 exit
33 case "$appimageType" in
34 "01")
35 echo "Uncompress $(basename "$src") of type $appimageType"
36 mkdir "$out"
37 pv "$src" | bsdtar -x -C "$out" -f -
40 "02")
41 # This method avoid issues with non executable appimages,
42 # non-native packer, packer patching and squashfs-root destination prefix.
44 # multiarch offset one-liner using same method as AppImage
45 # see https://gist.github.com/probonopd/a490ba3401b5ef7b881d5e603fa20c93
46 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}')
47 echo "Uncompress $(basename "$src") of type $appimageType @ offset $offset"
48 unsquashfs -q -d "$out" -o "$offset" "$src"
49 chmod go-w "$out"
52 # "03")
53 # get ready, https://github.com/TheAssassin/type3-runtime
56 echo Unsupported AppImage Type: "$appimageType"
57 exit
59 esac
60 echo "$(basename "$src") is now installed in $out"
63 apprun() {
65 SHA256=$(sha256sum "$APPIMAGE" | awk '{print $1}')
66 export APPDIR="${XDG_CACHE_HOME:-$HOME/.cache}/appimage-run/$SHA256"
68 #compatibility
69 if [ -x "$APPDIR/squashfs-root" ]; then APPDIR="$APPDIR/squashfs-root"; fi
71 if [ ! -x "$APPDIR" ]; then
72 mkdir -p "$(dirname "$APPDIR")"
73 unpack "$APPIMAGE" "$APPDIR"
74 else echo "$(basename "$APPIMAGE")" installed in "$APPDIR"
77 export PATH="$PATH:$PWD/usr/bin"
80 wrap() {
82 # quite same in appimageTools
83 export APPIMAGE_SILENT_INSTALL=1
85 if [ -n "$APPIMAGE_DEBUG_EXEC" ]; then
86 cd "$APPDIR" || true
87 exec "$APPIMAGE_DEBUG_EXEC"
90 exec "$APPDIR/AppRun" "$@"
93 usage() {
94 cat <<EOF
95 Usage: appimage-run [appimage-run options] <AppImage> [AppImage options]
97 -h show this message
98 -d debug mode
99 -x <directory> : extract appimage in the directory then exit.
100 -w <directory> : run uncompressed appimage directory (used in appimageTools)
102 [AppImage options]: Options are passed on to the appimage.
103 If you want to execute a custom command in the appimage's environment, set the APPIMAGE_DEBUG_EXEC environment variable.
106 exit 1
109 while getopts "x:w:dh" option; do
110 case "${option}" in
111 d) set -x
113 x) # eXtract
114 unpack_opt=true
115 APPDIR=${OPTARG}
117 w) # WrapAppImage
118 export APPDIR=${OPTARG}
119 wrap_opt=true
121 h) usage
123 *) usage
125 esac
126 done
127 shift "$((OPTIND-1))"
129 if [ -n "$wrap_opt" ] && [ -d "$APPDIR" ]; then
130 wrap "$@"
131 exit
132 else
133 APPIMAGE="$(realpath "$1")" || usage
134 shift
137 if [ -n "$unpack_opt" ] && [ -f "$APPIMAGE" ]; then
138 unpack "$APPIMAGE" "$APPDIR"
139 exit
142 if [ -n "$apprun_opt" ] && [ -f "$APPIMAGE" ]; then
143 apprun
144 wrap "$@"
145 exit