Skip to content

Instantly share code, notes, and snippets.

@syldrathecat
Last active November 23, 2021 21:29
Show Gist options
  • Save syldrathecat/2ecbd8aa15b3e92ebdcf85d2fcbdc383 to your computer and use it in GitHub Desktop.
Save syldrathecat/2ecbd8aa15b3e92ebdcf85d2fcbdc383 to your computer and use it in GitHub Desktop.
#!/bin/bash
SNAP_NAME=rocketchat-desktop
SNAP_ID=G4dFdVrFArll5teSvqpgWfRXIMGjME0l
SNAP_SEARCH_URL=https://search.apps.ubuntu.com/api/v1/package/$SNAP_NAME
X_UBUNTU_DEVICE_CHANNEL=edge # stable, candidate, beta, or edge
OUTPUT_DIR=$PWD
TMP_DIR=/tmp/snap2app_$PPID
echo " * Creating $TMP_DIR"
mkdir -p "$TMP_DIR" || { echo " ! Failed to create directory $TMPDIR"; exit 1; }
cd "$TMP_DIR"
cleanup()
{
echo " * Cleaning up $TMP_DIR"
cd -
rm -r "$TMP_DIR"
}
trap cleanup EXIT
find_commands()
{
for name in $@; do
local varname=${name^^}
declare -g $varname=$(command -v "$name")
[ -n "${!varname}" ] || { echo " ! Missing required command: $name"; exit 1; }
done
}
extract_vars()
{
local response=$1
shift
local vars=$@
qlist=.${vars// /,.}
{
for var in $vars; do
IFS= read $var
done
} <<< $($JQ -r "$qlist" <<< $response)
}
echo " * Searching for required commands"
find_commands cat cp rm chmod curl sed jq unsquashfs sha512sum appimagetool
echo " * Searching for package on snap store"
headers="-H X-Ubuntu-Device-Channel:$X_UBUNTU_DEVICE_CHANNEL"
response=$($CURL -L $headers -s "$SNAP_SEARCH_URL") || { echo " ! Failed to download $SNAP_SEARCH_URL"; }
extract_vars "$response" download_sha512 download_url snap_id title version revision developer_name
[ -n "$download_url" ] || { echo " ! Response missing field: download_url"; exit 1; }
echo " * Found snap package"
echo " - $title $version (revision $revision) by $developer_name"
echo " - URL: $download_url"
echo " * Downloading snap"
filename="${SNAP_ID}_${revision}.snap"
$CURL -L "$download_url" -o "$filename" || { echo " ! Failed to download $download_url"; exit 1; }
printf " * Checking SHA512 sum ... ";
read -d " " sha512 <<< $($SHA512SUM "$filename")
if [ "$sha512" = "$download_sha512" ]; then
echo " OK"
else
echo " Failed!"
echo " - Expected: $download_sha512"
echo " - Got: $sha512"
exit 1
fi
echo " * Un-squashing package"
appdir="$SNAP_NAME-$version.AppDir"
$UNSQUASHFS -d "$appdir" "$filename" > /dev/null || { echo " ! Failed to unsquash package"; exit 1; }
[ -d "$appdir" ] || { echo " ! Missing extracted directory directory"; exit 1; }
echo " * Generating AppRun"
AppRun="$appdir/AppRun"
# This script is tailored specifically for rocketchat
# It is not a general purpose snap->appimage conversion
$CAT <<EOF > "$AppRun"
#!/bin/sh
HERE="\$(dirname "\$(readlink -f "\${0}")")"
export LD_LIBRARY_PATH=\$HERE/lib/x86_64-linux-gnu:\$HERE/usr/lib/x86_64-linux-gnu:\$HERE:\$LD_LIBRARY_PATH
export DISABLE_WAYLAND=1
cd "\$HERE"
exec ./rocketchat-desktop --no-sandbox "\$@"
EOF
$CHMOD +x "$AppRun"
echo " * Copying .desktop file"
old_desktop_file="$appdir/meta/gui/rocketchat-desktop.desktop"
old_icon_file="$appdir/meta/gui/icon.png"
desktop_file="$appdir/rocketchat-desktop.desktop"
icon_file="$appdir/rocketchat-desktop.png"
[ -n "$old_desktop_file" ] || { echo " ! Could not find .desktop file in the package"; exit 1; }
$SED 's/Icon=.*/Icon=rocketchat-desktop/' "$old_desktop_file" > "$desktop_file"
$CP "$old_icon_file" "$icon_file"
echo " * Removing snap files"
$RM "$appdir/desktop-init.sh" "$appdir/desktop-common.sh" \
"$appdir/desktop-gnome-specific.sh" "$appdir/command.sh"
$RM -r "$appdir/meta" "$appdir/gnome-platform"
appimg="$OUTPUT_DIR/$SNAP_NAME-$version.AppImage"
echo " * Creating AppImage ($appimg)"
$APPIMAGETOOL -v "$appdir" "$appimg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment