Skip to content

Instantly share code, notes, and snippets.

@ruario
Last active September 26, 2023 10:49
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ruario/d44cb29feb83c9191b1adfdfd318c617 to your computer and use it in GitHub Desktop.
This script allows you to extract and register a Vivaldi Linux deb or rpm package with your Desktop Environment using a unique keyword, to assist with multiple side-by-side "installs".
#!/bin/sh -eu
a=off
if [ "x${1:-}" = "x-n" ] && [ -n "${2:-}" ]; then
n="${2#vivaldi-}"
c="Vivaldi-$n"
a=on
echo "Using the name: vivaldi-$n"
shift 2
fi
if [ -n "${1:-}" ]; then
if echo "$1" | grep -qE "vivaldi-(stable|snapshot)[_-]([0-9]+\.){3}[0-9]+-[0-9]+[_\.][a-z0-9_]+\.(rpm|deb)" && [ -e "$1" ]; then
v="${1##*/}"
else
echo "$1 is not a Vivaldi package" >&2
exit 1
fi
else
echo -e "Supply the name of the Vivaldi .deb or .rpm package you wish to register like so:\n\n $0 vivaldi-stable-6.1.3035.84-1.x86_64.rpm\n" >&2
exit 1
fi
cd "$(dirname "$1")"
[ "$a" = on ] && d="${v%.*}-$n" || d="${v%.*}"
[ -d "$d" ] && rm -r "$d"
mkdir "$d"
cd "$d"
echo "Extracting: $v" >&2
if [ "${v##*.}" = deb ]; then
ar p "../$v" data.tar.xz | tar Jx ./opt ./usr/share/applications --strip 3
else
tail -c+"$(grep -Fabom1 7zXZ "../$v" | cut -d: -f1)" "../$v" | xz -d | cpio --quiet -id ./opt\* ./usr/share/applications\*
mv opt/vivaldi*/* .
mv usr/share/applications .
rm -r opt usr
fi
w="$(find . -maxdepth 1 -type f \( -name vivaldi -o -name vivaldi-snapshot \))"
x="$(echo applications/*.desktop)"
chmod 755 $x
if [ "$a" = on ]; then
f="vivaldi-$n"
sed -i "s,/${w#*/}/,/$f/,g;/^exec -a/s,\"\$@\", --user-data-dir=\"\${XDG_CONFIG_HOME:-\$HOME/.config}/$f\" --class=\"$c\" \"\$@\"," $w
sed -i "s,^Icon=${w#*/},Icon=$f,;0,/^Name=/s,^Name=.*,Name=Vivaldi ($n),;/^StartupNotify/ i StartupWMClass=$c" $x
if [ -x update-ffmpeg ]; then
sed -i "s,/${w#*/}/,/$f/,g" update-ffmpeg
fi
else
f="${w#*/}"
fi
cat <<REGISTER> register
#!/bin/sh -eu
update_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
}
if [ "x\${1:-}" = "x-d" ]; then
rm -fv "\${XDG_DATA_HOME:-\$HOME/.local/share}/applications/$f.desktop" "\${XDG_DATA_HOME:-\$HOME/.local/share}/icons/hicolor"/*/apps/$f.png
if [ -h "\$HOME/bin/$f" ]; then
rm -v "\$HOME/bin/$f"
elif [ -h "\${XDG_DATA_HOME:-\$HOME/.local/share}/../bin/$f" ]; then
rm -v "\$(cd "\${XDG_DATA_HOME:-\$HOME/.local/share}/../bin"; pwd)/$f"
fi
update_dbs
exit
fi
cd "\${0%/*}"
for p in product_logo_*.png; do
s="\$(echo \$p | grep -o '[0-9]\+')"
l="\${XDG_DATA_HOME:-\$HOME/.local/share}/icons/hicolor/\${s}x\$s/apps"
mkdir -p "\$l"
ln -vfs "\$PWD/\$p" "\$l/$f.png"
done
sed -i "s,^Exec=.*vivaldi\(-stable\|-snapshot\)* ,Exec=\$PWD/${w#*/} ," $x
mkdir -p "\${XDG_DATA_HOME:-\$HOME/.local/share}/applications"
ln -vfs "\$PWD/$x" "\${XDG_DATA_HOME:-\$HOME/.local/share}/applications/$f.desktop"
if [ -d "\$HOME/bin" ]; then
ln -vfs "\$PWD/${w#*/}" "\$HOME/bin/$f"
elif [ -d "\${XDG_DATA_HOME:-\$HOME/.local/share}/../bin" ]; then
ln -vfs "\$PWD/${w#*/}" "\$(cd "\${XDG_DATA_HOME:-\$HOME/.local/share}/../bin"; pwd)/$f"
fi
update_dbs
REGISTER
chmod 755 "register"
printf "Creating symlinks:\n\n" >&2
cd - >/dev/null
if [ ! -e "$f" ] || [ -h "$f" ]; then
ln -fsT "$d" "$f"
d="$f"
fi
"$d/register"
cat <<HELP
NOTE: You may need to re-login before $f shows up!
***
To remove symlinks and de-register $f issue:
'$PWD/$d/register' -d
HELP
@ruario
Copy link
Author

ruario commented Jun 22, 2023

Running it with -n followed by your chosed name/keyword/stream (e.g. -n alt) and then the Vivaldi package would result in something like the following:

$ ./register-vivaldi.sh -n alt vivaldi-stable-6.1.3035.100-1.x86_64.rpm
Using the name: vivaldi-alt
Extracting: vivaldi-stable-6.1.3035.100-1.x86_64.rpm
Creating symlinks:

'/home/ruario/.local/share/icons/hicolor/128x128/apps/vivaldi-alt.png' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/product_logo_128.png'
'/home/ruario/.local/share/icons/hicolor/16x16/apps/vivaldi-alt.png' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/product_logo_16.png'
'/home/ruario/.local/share/icons/hicolor/22x22/apps/vivaldi-alt.png' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/product_logo_22.png'
'/home/ruario/.local/share/icons/hicolor/24x24/apps/vivaldi-alt.png' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/product_logo_24.png'
'/home/ruario/.local/share/icons/hicolor/256x256/apps/vivaldi-alt.png' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/product_logo_256.png'
'/home/ruario/.local/share/icons/hicolor/32x32/apps/vivaldi-alt.png' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/product_logo_32.png'
'/home/ruario/.local/share/icons/hicolor/48x48/apps/vivaldi-alt.png' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/product_logo_48.png'
'/home/ruario/.local/share/icons/hicolor/64x64/apps/vivaldi-alt.png' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/product_logo_64.png'
'/home/ruario/.local/share/applications/vivaldi-alt.desktop' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/applications/vivaldi-stable.desktop'
'/home/ruario/bin/vivaldi-alt' -> '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/vivaldi'

        NOTE: You may need to re-login before vivaldi-alt shows up!

                                  ***

        To remove symlinks and de-register vivaldi-alt issue:

  '/home/ruario/Downloads/vivaldi-stable-6.1.3035.100-1.x86_64-alt/register' -d

This version of Vivaldi would now be available from Gnome, KDE Plasma, Xfce, etc. menus and launcher as "Vivaldi (alt)". It will use its own settings and will not be touched when you upgrade other streams such as stable or snapshot.

Alternatively you can skip -n keyword and the script will just register the build as is, without changing the stream.

It does not really matter so much if you choose an rpm or deb package, since an install is not taking place and thus neither the rpm nor `dpkg commands need to be present.

It may be slightly more reliable to use the rpm because the deb package needs ar for extraction and some distros do not install this by default. While the rpm package does rely on cpio as part of its extraction, this is (almost always) preinstalled.

In either case, if you found that ar or cpio were not present, just install the "binutils" or "cpio" packages (respectively) via your package manager, before attempting to run the script again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment