appdwarf/zzexe

41 lines
812 B
Plaintext
Raw Normal View History

2022-03-25 22:52:40 +00:00
#!/bin/sh
# shellcheck disable=SC2016,SC2129
set -e # exit on failure
IFS=$(printf '\n\t') # smarter ifs
tmp=$(mktemp)
if [ "$1" = "-d" ]; then # decompress
tail -n+11 "$2" | zstd -dof "$tmp" &&
mv "$tmp" "$2"
chmod +x "$2"
2022-03-25 22:52:40 +00:00
exit
fi
# file header
echo '#!/bin/sh' > "$tmp"
2022-03-25 22:52:40 +00:00
# save extension for extracted tmp file
2022-03-25 22:52:40 +00:00
echo 'out=$(mktemp -t .zzXXXX.'"${1##*.}"\) >> "$tmp"
2022-03-25 22:52:40 +00:00
cat >> "$tmp" << 'EOF'
dir=$(dirname "$0")
tail -n+11 "$0" | zstd -cd > "$out"
2022-03-25 22:52:40 +00:00
chmod +x "$out"
ln -s "$out" "$dir"
trap "res=$?" 0 1 2 3 5 10 13 15
EOF
# apply prefix if applicable
[ "$1" = "-p" ] && printf %s "$2 " >> "$tmp" && shift 2
2022-03-25 22:52:40 +00:00
cat >> "$tmp" << 'EOF'
"$dir/$(basename "$out")" "$@"
rm "$out" "$dir/$(basename "$out")"
2022-03-25 22:52:40 +00:00
exit $res
EOF
# compress
zstdmt --ultra -c22 --long "$@" >> "$tmp"
2022-03-25 22:52:40 +00:00
mv "$tmp" "$1"
chmod +x "$1"