Skip to content

Instantly share code, notes, and snippets.

@ruario
Last active December 24, 2024 11:12
Show Gist options
  • Save ruario/a5d161a0c650610750772b655f15a872 to your computer and use it in GitHub Desktop.
Save ruario/a5d161a0c650610750772b655f15a872 to your computer and use it in GitHub Desktop.
A script fetch and convert a Lagrange AppImage to a system installable package
#!/bin/sh -eu
# A script convert a Lagrange AppImage to a system installable package
# Determine if a local package should be used or if one should be fetched
if [ -n "${1:-}" ]; then
APPIMAGE="$1"
else
case "$(uname -m)" in
arm*) APPIMAGE_ARCH=armhf ;;
*) APPIMAGE_ARCH="$(uname -m)" ;;
esac
APPIMAGE_VERSION="$(wget -qO- https://git.skyjake.fi/gemini/lagrange/releases | grep -o '/gemini/lagrange/src/tag/v[0-9\.]\+' | cut -dv -f2 | head -n 1)"
[ -z "$APPIMAGE_VERSION" ] && { echo "Could not work out version"; exit 1 ; }
[ -e "${XDG_DOWNLOAD_DIR:-$HOME/Downloads}/Lagrange-${APPIMAGE_VERSION}-${APPIMAGE_ARCH}.bin.tgz" ] && { echo "${XDG_DOWNLOAD_DIR:-$HOME/Downloads}/Lagrange-${APPIMAGE_VERSION}-${APPIMAGE_ARCH}.bin.tgz is already present"; exit ; }
if ! [ -e "${XDG_DOWNLOAD_DIR:-$HOME/Downloads}/Lagrange-${APPIMAGE_VERSION}-${APPIMAGE_ARCH}.AppImage" ]; then
wget -P "${XDG_DOWNLOAD_DIR:-$HOME/Downloads}" https://git.skyjake.fi/gemini/lagrange/releases/download/v${APPIMAGE_VERSION}/Lagrange-${APPIMAGE_VERSION}-x86_64.AppImage
fi
APPIMAGE="${XDG_DOWNLOAD_DIR:-$HOME/Downloads}/Lagrange-${APPIMAGE_VERSION}-${APPIMAGE_ARCH}.AppImage"
fi
# Check that a valid Lagrange AppImage name was provided and it is present and readable
[ -r "$APPIMAGE" ] || { echo "$APPIMAGE is not present"; exit 1; }
echo "$APPIMAGE" | grep -q 'Lagrange-[0-9\.]\+-[a-z0-9\_]\+\.AppImage$' || { echo "$APPIMAGE is not named like a Lagrange AppImage"; exit 1; }
# Define basic variables, if they are incorrect of not defined
echo "$APPIMAGE" | grep -q '^/' || APPIMAGE="$(realpath "$APPIMAGE")"
[ -n "${APPIMAGE_VERSION:-}" ] || APPIMAGE_VERSION="$(echo "$APPIMAGE" | sed 's,.*/,,' | cut -d- -f2)"
[ -n "${APPIMAGE_ARCH:-}" ] || APPIMAGE_ARCH="$(echo "$APPIMAGE" | sed 's,.*/,,' | cut -d- -f3 | cut -d. -f1)"
# Extract AppImage contents
APPIMAGE_STAGING_DIR="$(mktemp -d)/lagrange"
unsquashfs -n -q -d "$APPIMAGE_STAGING_DIR" -o "$(expr $(grep -Pabom1 '\x00hsqs' "$APPIMAGE" | cut -d: -f1) + 1)" "$APPIMAGE" usr
# Reorganise the contents
cd "$APPIMAGE_STAGING_DIR"
mkdir opt
mv usr opt/Lagrange
mkdir -p usr/local/bin usr/local/share/applications usr/local/share/icons/hicolor/256x256/apps usr/local/share/man/man1
ln -s ../../../opt/Lagrange/bin/lagrange usr/local/bin/lagrange
ln -s ../../../../opt/Lagrange/share/applications/fi.skyjake.Lagrange.desktop usr/local/share/applications/fi.skyjake.Lagrange.desktop
ln -s ../../../../../../../opt/Lagrange/share/icons/hicolor/256x256/apps/fi.skyjake.Lagrange.png usr/local/share/icons/hicolor/256x256/apps/fi.skyjake.Lagrange.png
ln -s ../../../../../opt/Lagrange/share/man/man1/lagrange.1 usr/local/share/man/man1/lagrange.1
# Create a tar package from the AppImage contents
find usr opt ! -type d | tar cafTH "${XDG_DOWNLOAD_DIR:-$HOME/Downloads}/Lagrange-${APPIMAGE_VERSION}-${APPIMAGE_ARCH}.bin.tgz" - ustar --owner=root --group=root
# Cleanup
cd - >/dev/null
rm -r "${APPIMAGE_STAGING_DIR%%/lagrange}"
# Inform the user
cat <<EOF
Created: ${XDG_DOWNLOAD_DIR:-$HOME/Downloads}/Lagrange-${APPIMAGE_VERSION}-${APPIMAGE_ARCH}.bin.tgz
To install:
sudo tar Cfx / Lagrange-${APPIMAGE_VERSION}-${APPIMAGE_ARCH}.bin.tgz
To remove:
sudo rm -r /opt/Lagrange /usr/local/share/{applications,icons/hicolor/256x256/apps,man/man1}/*[Ll]agrange.*
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment