Skip to content

Instantly share code, notes, and snippets.

@ruario
Last active September 30, 2024 18:44
Show Gist options
  • Save ruario/a98005997f7c111ba44d908594f028dd to your computer and use it in GitHub Desktop.
Save ruario/a98005997f7c111ba44d908594f028dd to your computer and use it in GitHub Desktop.
A script to convert a Vivaldi rpm or deb into Appimage format.
#!/bin/sh
#
# appimage-vivaldi.sh (version 0.9)
#
# A script to convert a Vivaldi rpm or deb into AppImage format.
# Before you use this script, you may need to adjust the following
# variable, based on the name and location of AppImageTool on your
# system.
#
# You can get the latest AppImageTool from here
# <https://github.com/AppImage/appimagetool/releases>
APPIMAGETOOL="$PWD/appimagetool.AppImage"
# Use the most recent Vivaldi package in the current working
# directory or whatever the user has defined as the first option
# to this script.
if [ -n "${1:-}" ]; then
VIVALDI_PKG="$1"
else
VIVALDI_PKG="$(ls -t vivaldi-*.rpm vivaldi-*.deb 2>/dev/null | head -n 1)"
fi
# Exit as soon as an error is encountered
set -e
# Check that a suitable Vivaldi package has been specified
VIVALDI_PKG_INFO="$(echo "$VIVALDI_PKG" | sed -n 's/.*\(vivaldi-[a-z]\+\)[_-]\(\([0-9]\+\.\)\{3\}[0-9]\+\)-[0-9]\+[_\.]\([a-z0-9_]\+\)\.\(deb\|rpm\)$/\1:\2:\4/p')"
if [ -z "$VIVALDI_PKG_INFO" ]; then
echo 'You must specify the path to a locally stored Vivaldi Linux package.' >&2
echo "Example usage: $0 vivaldi-stable-1.4.589.29-1.x86_64.rpm" >&2
exit 1
fi
if [ ! -r "$VIVALDI_PKG" ]; then
echo "$VIVALDI_PKG is either not present or cannot be read" >&2
exit 1
fi
# Obtain variables from package name
VIVALDI_STREAM="$(echo "$VIVALDI_PKG_INFO" | cut -d: -f1)"
VIVALDI_VERSION="$(echo "$VIVALDI_PKG_INFO" | cut -d: -f2)"
VIVALDI_ARCH="$(echo "$VIVALDI_PKG_INFO" | cut -d: -f3)"
case "$VIVALDI_ARCH" in
amd64|x86_64) VIVALDI_ARCH=x86_64; DEBIAN_ARCHITECTURE=amd64 ;;
arm64|aarch64) VIVALDI_ARCH=aarch64; DEBIAN_ARCHITECTURE=arm64 ;;
armhf|armv7hl) VIVALDI_ARCH=armhf; DEBIAN_ARCHITECTURE=armhf ;;
esac
APPDIR="$VIVALDI_STREAM-$VIVALDI_VERSION-$VIVALDI_ARCH.AppDir"
APPIMAGE="$VIVALDI_STREAM-$VIVALDI_VERSION-$VIVALDI_ARCH.AppImage"
# Check for a previous package and stop if found
if [ -e "$APPIMAGE" ]; then
echo "$APPIMAGE already exists" >&2
exit 1
fi
# Extract package contents
available () {
command -v "$1" >/dev/null 2>&1
}
extract_rpm () {
if available bsdtar; then
bsdtar Cxf "$APPDIR" "$1" ./opt ./usr/share
elif available cpio; then
tail -c+"$(grep -abom1 7zXZ "$1" | cut -d: -f1)" "$1" | xz -d | (cd "$APPDIR" && cpio --quiet -id ./opt\* ./usr/share\*)
else
echo 'You must install BSD tar or GNU cpio to use this script' >&2
exit 1
fi
}
extract_deb () {
if available bsdtar; then
DEB_EXTRACT_COMMAND='bsdtar xOf'
elif available ar; then
DEB_EXTRACT_COMMAND='ar p'
else
echo 'You must install BSD tar or GNU binutils to use this script' >&2
exit 1
fi
$DEB_EXTRACT_COMMAND "$1" data.tar.xz | tar CxJ "$APPDIR" ./opt ./usr/share
}
[ -d "$APPDIR" ] && rm -r "$APPDIR"
mkdir "$APPDIR"
case "$VIVALDI_PKG" in
*deb) extract_deb "$VIVALDI_PKG" ;;
*rpm) extract_rpm "$VIVALDI_PKG" ;;
esac
# Create a script to allow registering Vivaldi with Desktop Environment
sed -i '2i . "$APPDIR/opt/'${VIVALDI_STREAM%-stable}'/register"' "$APPDIR/opt/${VIVALDI_STREAM%-stable}/${VIVALDI_STREAM%-stable}"
cat <<REGISTER_VIVALDI > "$APPDIR/opt/${VIVALDI_STREAM%-stable}/register"
update_desktop_dbs () {
touch -c "\${XDG_DATA_HOME:-\$HOME/.local/share}/icons/hicolor" 2>/dev/null ||:
if command -v gtk-update-icon-cache >/dev/null 2>&1; then
gtk-update-icon-cache -tq "\${XDG_DATA_HOME:-\$HOME/.local/share}/icons/hicolor" 2>/dev/null ||:
fi
if command -v update-desktop-database >/dev/null 2>&1; then
update-desktop-database -q "\${XDG_DATA_HOME:-\$HOME/.local/share}/applications" 2>/dev/null ||:
fi
exit
}
if [ "x\${1:-}" = "x--unregister-vivaldi" ]; then
rm -v "\${XDG_DATA_HOME:-\$HOME/.local/share}/applications/$VIVALDI_STREAM.desktop" "\${XDG_DATA_HOME:-\$HOME/.local/share}/icons/hicolor/"*"/apps/${VIVALDI_STREAM%-stable}.png"
update_desktop_dbs
elif [ "x\${1:-}" = "x--register-vivaldi" ]; then
cd "\$APPDIR"
for p in opt/${VIVALDI_STREAM%-stable}/product_logo_*.png; do
s="\$(echo \$p | grep -o '[0-9]\+')"
d="\${XDG_DATA_HOME:-\$HOME/.local/share}/icons/hicolor/\${s}x\$s/apps"
mkdir -p "\$d"
install "\$PWD/\$p" "\$d/${VIVALDI_STREAM%-stable}.png"
done
mkdir -p "\${XDG_DATA_HOME:-\$HOME/.local/share}/applications"
sed "s,^Exec=.*vivaldi\(-stable\|-snapshot\)* ,Exec=\"\$APPIMAGE\" ," usr/share/applications/$VIVALDI_STREAM.desktop > "\${XDG_DATA_HOME:-\$HOME/.local/share}/applications/$VIVALDI_STREAM.desktop"
update_desktop_dbs
fi
REGISTER_VIVALDI
# Add version checking script to warn on old versions
sed -i '/^exec -a/ i . "$HERE/check-version"' "$APPDIR/opt/${VIVALDI_STREAM%-stable}/${VIVALDI_STREAM%-stable}"
sed -i "/^exec -a/s/$/ \"\${VIVALDI_UPDATE_NOTE:-}\"/" "$APPDIR/opt/${VIVALDI_STREAM%-stable}/${VIVALDI_STREAM%-stable}"
cat <<CHECK_VERSION > "$APPDIR/opt/${VIVALDI_STREAM%-stable}/check-version"
DWLD=''
if command -v wget >/dev/null 2>&1; then
DWLD='wget -qO-'
elif command -v curl >/dev/null 2>&1; then
DWLD='curl -s'
fi
if [ -n "\$DWLD" ] && [ -z "\${DISABLE_VIVALDI_APPIMAGE_UPDATE_NOTE:-}" ]; then
VIVALDI_NEW_VERSION="\$(timeout 2s \$DWLD "https://repo.vivaldi.com/archive/deb/dists/stable/main/binary-$DEBIAN_ARCHITECTURE/Packages.gz" | gzip -d | grep -A6 -x "Package: $VIVALDI_STREAM" | sed -n 's/^Version: \(\([0-9]\+\.\)\{3\}[0-9]\+\).*/\1/p' | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n | tail -n 1)"
if [ -z "VIVALDI_NEW_VERSION" ]; then
echo "Could not work out the latest public version of $VIVALDI_STREAM" >&2
else
if [ $VIVALDI_VERSION != "\$VIVALDI_NEW_VERSION" ]; then
VIVALDI_UPDATE_NOTE="\$(mktemp -td vivaldi-update-checker.XXXXXX)/update-vivaldi.html"
if [ $VIVALDI_STREAM = vivaldi-stable ]; then
cat <<EOF > "\$VIVALDI_UPDATE_NOTE"
<title>Update Vivaldi</title>
<p>The current, public version of Vivaldi (stable) is \$VIVALDI_NEW_VERSION.</p>
<p>Check which version you have installed via "[Main Menu] Help → About".</p>
<p>To keep track of updates, subscribe to the following two feeds:</p>
<ul>
<li><a href="https://vivaldi.com/category/desktop-releases/feed/">Major desktop releases</a></li>
<li><a href="https://vivaldi.com/category/desktop-updates/feed/">Critical desktop updates</a> (including security fixes)</li>
</ul>
EOF
elif [ $VIVALDI_STREAM = vivaldi-snapshot ]; then
cat <<EOF > "\$VIVALDI_UPDATE_NOTE"
<title>Update Vivaldi (Snapshot)</title>
<p>The current, public version of Vivaldi Snapshot is \$VIVALDI_NEW_VERSION.</p>
<p>Check which version you have installed via "[Main Menu] Help → About".</p>
<p>To keep track of updates, subscribe to the <a href="https://vivaldi.com/category/desktop-snapshots/feed/">desktop snapshots feed</a>.</p>
EOF
fi
VIVALDI_UPDATE_NOTE="file://\$VIVALDI_UPDATE_NOTE"
fi
fi
fi
CHECK_VERSION
# Configure package contents for AppImage
ln -s "opt/${VIVALDI_STREAM%-stable}/${VIVALDI_STREAM%-stable}" "$APPDIR/AppRun"
ln -s "opt/${VIVALDI_STREAM%-stable}/product_logo_256.png" "$APPDIR/${VIVALDI_STREAM%-stable}.png"
ln -s "usr/share/applications/$VIVALDI_STREAM.desktop" "$APPDIR/$VIVALDI_STREAM.desktop"
grep -q '^StartupWMClass' "$APPDIR/$VIVALDI_STREAM.desktop" || sed -i "/^Terminal=false/ i StartupWMClass=$VIVALDI_STREAM" "$APPDIR/$VIVALDI_STREAM.desktop"
# Create the AppImage
$APPIMAGETOOL "$APPDIR" "$APPIMAGE"
# Remove staging directory
rm -r "$APPDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment