appdwarf/appdwarf

106 lines
3.5 KiB
Plaintext
Raw Normal View History

#!/bin/sh
#shellcheck disable=SC2086,SC2155
set -e # exit on failure
IFS=$(printf '\n\t') # smarter ifs
header() {
APPDIR="/tmp/dwarf_$(basename "$0")$(echo "$0"|md5sum|head -c5)"
export APPDIR APPIMAGE="$(realpath "$0")" ARGV0="$0" OWD="$PWD"
args="-o offset=auto -o tidy_strategy=swap -o workers=4"
[ ! -d "$APPDIR" ] && mkdir "$APPDIR" && dwarfs $args "$0" "$APPDIR" 2>/dev/null
"$APPDIR/AppRun" "$@"
res=$?
fusermount -quz "$APPDIR"
rmdir "$APPDIR" 2>/dev/null
exit $res
}
zzexe() {
[ "$1" = "-d" ] && awk 'f;/^exit \$res$/{f=1}' "$2" | head -c-1 | zstd -cd | ifne sponge "$2" && exit
[ "$1" = "-p" ] && p="$2" && shift 2 # save prefix if present
zstdmt -cq19 "$@" | ifne sponge "$1"
type zzexe_header | sed 's/^ *//' | tail -n+4 | head -n-1 | cat - "$1" | ifne sponge "$1"
sed -i -e '1i #!/bin/sh' -e "1s/);$/${1##*[./]})/" -e "6s/^/$p /" "$1"
exit
}
zzexe_header() {
dir=$(dirname "$0") out=$(mktemp -t .zzXXXX.)
awk "f;/^exit \\$res$/{f=1}" "$0" | head -c-1 | zstd -cd > "$out"
chmod +x "$out"
ln -s "$out" "$dir"
trap : 0 1 2 3 6 14 15
"$dir/$(basename "$out")" "$@"
res=$?
rm "$out" "$dir/$(basename "$out")"
exit $res
}
2021-12-31 19:24:04 +00:00
case "$1" in
-d | -p )
zzexe "$@" ;;
--version | -v )
tput setaf 2; echo appdwarf 2022.06.25
tput setaf 6; echo Built by July 🏳️‍🌈; exit ;;
-* | '' )
tput setaf 6
echo "Usage: $(basename "$0") [APP/FILE/FOLDER/URL] [compression options]"
tput setaf 3
echo " -d Decompress a zzexe'd file"
echo " -h, --help Print this help text"
echo " -v, --version Print the appdwarf version"; exit ;;
esac
[ "$(basename "$0")" = zzexe ] && zzexe "$@"
if [ ! -d "$1" ]; then # directory doesn't exist, see if this is an appimage
if [ ! -f "$1" ]; then # file doesn't exist, see if this is a url
if ! echo "$1" | grep / ; then # AppImageHub
echo "Checking AppImageHub for this program..."
app=https://github.com/AppImage/appimage.github.io/raw/master/apps/$1.md
shift
set -- "$(curl -L "$app" | grep -o https.\*releases | sed 's|/releases$||')" "$@"
elif ! echo "$1" | grep https ; then # GitHub in Author/Repo format
echo "Assuming this is a GitHub repo..."
app=$1
set -- "https://github.com/$1" "$@"
fi
if echo "$1" | grep 'https://github.com/[^/]*/[^/]*/*$'; then # GitHub url
echo "Assuming this is a GitHub repo..."
app="$(echo "${1%/}"/releases | sed 's|github.com|api.github.com/repos|')"
shift
set -- "$(curl "$app" | grep -v arm | grep -om1 https.\*AppImage)" "$@"
fi
app=$(basename "$1") # actually try to get the appimage
if aria2c -x16 -s16 "$1" -o "$app" || wget "$1" -O "$app"; then
shift
set -- "$app" "$@"
else
tput setaf 1; echo "Unable to find valid AppImage or AppDir" >&2
rm "$app" 2> /dev/null; exit 1
fi
fi
chmod +x "$1"
rm -rf squashfs-root
if ! "$(realpath "$1")" --appimage-extract 2> /dev/null; then
tput setaf 4; echo "$1 is not an AppImage, it will be zzexe'd"
zzexe "$@"
fi
app="$(basename "$1" .AppImage)" shift
set -- "$app" "$@"
mv squashfs-root "$1"
fi
head="$(mktemp)"
echo '#!/bin/sh' > $head
type header | tail -n+4 | head -n-1 | sed 's/^ *//' >> $head
mkdwarfs -o "$(realpath "$1").sh" -B5 --header $head -i "$@"
rm $head
chmod +x "$(realpath "$1").sh"