Skip to content

Commit e22727d

Browse files
author
Andreas Poulsen
committed
Merge branch 'feature/52552_make_scripts_work_with_snaps_particularly_firefox' into 'master'
Make relevant scripts work with snaps, particularly firefox See merge request os2borgerpc/os2borgerpc-scripts!176
2 parents bf5b2c6 + 11a1574 commit e22727d

7 files changed

+103
-59
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/usr/bin/env bash
22

3-
sed -i "s/firefox/google-chrome/" /etc/dconf/db/os2borgerpc.d/02-launcher-favorites
3+
# In 22.04 Firefox is a snap, in 20.04 it's an apt package.
4+
# Once everyone has upgraded, support for the latter can be removed
5+
if [ -d "/snap/firefox" ]; then
6+
sed -i "s/firefox_firefox/google-chrome/" /etc/dconf/db/os2borgerpc.d/02-launcher-favorites
7+
else
8+
sed -i "s/firefox/google-chrome/" /etc/dconf/db/os2borgerpc.d/02-launcher-favorites
9+
fi
410

511
dconf update

os2borgerpc/desktop/desktop_launcher_program_shortcut.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
ADD=$1
1111
PROGRAM=$2
1212

13+
if [ -f "/var/lib/snapd/desktop/applications/${PROGRAM}_$PROGRAM.desktop" ]; then
14+
PROGRAM="${PROGRAM}_$PROGRAM"
15+
fi
16+
1317
if [ "$ADD" = "True" ]; then
1418

1519
# Append the program specified above to the menu/launcher

os2borgerpc/desktop/desktop_print_program_list.sh

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,34 @@
44
55
#
66
# Arguments
7-
# 1: Default is to print programs available. Write 'skrivebord' to print
8-
# programs already on the desktop.
7+
# 1: Default is to print programs available/installed. Write 'skrivebord' to list
8+
# programs already on the desktop, or "menu" to list programs in the launcher.
99

1010
lower() {
1111
echo "$@" | tr '[:upper:]' '[:lower:]'
1212
}
1313

14-
DESKTOP="$(lower "$1")"
14+
find_desktop_files_path() {
15+
PTH_LOCAL=$1
16+
# - shellcheck says find handles non-alphanumeric file names better than ls
17+
find "$PTH_LOCAL" -maxdepth 1 | grep --fixed-strings .desktop | xargs basename --suffix .desktop
18+
}
19+
20+
LOCATION="$(lower "$1")"
1521

16-
USER=user
22+
SHADOW_DESKTOP=/home/.skjult/Skrivebord
23+
SNAP_DESKTOP_FILE_PATH="/var/lib/snapd/desktop/applications"
24+
APT_DESKTOP_FILE_PATH="/usr/share/applications"
1725

18-
if [ "$DESKTOP" = "menu" ];
19-
then
26+
if [ "$LOCATION" = "menu" ]; then
2027
# Print only the last line only and format it a bit more nicely
2128
tail -n 1 /etc/dconf/db/os2borgerpc.d/02-launcher-favorites | sed "s/favorite-apps=\[\|'\|\]\ \|.desktop//g" | tr ',' '\n'
2229
exit
23-
elif [ "$DESKTOP" = "skrivebord" ]
24-
then
25-
PTH="/home/$USER/Skrivebord/"
30+
elif [ "$LOCATION" = "skrivebord" ]; then
31+
PTH="$SHADOW_DESKTOP/"
2632
else
27-
PTH=/usr/share/applications/
33+
PTH=$APT_DESKTOP_FILE_PATH/
34+
find_desktop_files_path $SNAP_DESKTOP_FILE_PATH
2835
fi
2936

30-
# - shellcheck says find handles non-alphanumeric file names better than ls
31-
find $PTH -maxdepth 1 | sed "s,$PTH\|.desktop,,g"
37+
find_desktop_files_path "$PTH"

os2borgerpc/desktop/desktop_program_shortcut.sh

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,41 @@
33
# Adds/Removes programs from the desktop in Ubuntu 20.04
44
55
#
6-
# Note that the program assumes danish locale, where the 'Desktop' directory
6+
# Note that this script currently assumes danish locale, where the 'Desktop' directory
77
# is instead named 'Skrivebord'.
88
#
99
# Arguments:
1010
# 1: Use a boolean to decide whether to add or remove the program shortcut
1111
# 2: This argument should specify the name of a program (.desktop-file)
12-
# under /usr/share/applications/
12+
# under /usr/share/applications/ or /var/lib/snapd/desktop/applications/
13+
# This parameter IS case-sensitive as some applications have
14+
# capitalized characters in their filename.
1315

14-
lower() {
15-
echo "$@" | tr '[:upper:]' '[:lower:]'
16-
}
16+
ADD="$1"
17+
PROGRAM="$2"
1718

18-
ADD=$1
19-
PROGRAM="$(lower "$2")"
19+
SHADOW_DESKTOP="/home/.skjult/Skrivebord"
20+
SNAP_DESKTOP_FILE_PATH="/var/lib/snapd/desktop/applications"
21+
APT_DESKTOP_FILE_PATH="/usr/share/applications"
2022

21-
SHADOW=.skjult
23+
# TODO?: Make it replace all desktop icons which are copies with symlinks?
24+
25+
mkdir --parents $SHADOW_DESKTOP
2226

2327
if [ "$ADD" = 'True' ]; then
24-
mkdir --parents /home/$SHADOW/Skrivebord
25-
cp "/usr/share/applications/$PROGRAM.desktop" /home/$SHADOW/Skrivebord/
28+
if [ -f "$SNAP_DESKTOP_FILE_PATH/${PROGRAM}_$PROGRAM.desktop" ]; then
29+
DESKTOP_FILE=$SNAP_DESKTOP_FILE_PATH/${PROGRAM}_$PROGRAM.desktop
30+
else
31+
DESKTOP_FILE=$APT_DESKTOP_FILE_PATH/$PROGRAM.desktop
32+
fi
33+
34+
# Remove it first as it may be a copy and not symlink (ln --force can't overwrite regular files)
35+
rm --force "$SHADOW_DESKTOP/$PROGRAM.desktop"
36+
37+
ln --symbolic --force "$DESKTOP_FILE" $SHADOW_DESKTOP/
2638
else
27-
echo "Forsøger at slette programmet $PROGRAM"
28-
rm "/home/$SHADOW/Skrivebord/$PROGRAM.desktop"
39+
if [ -f "$SHADOW_DESKTOP/${PROGRAM}_$PROGRAM.desktop" ]; then
40+
PROGRAM=${PROGRAM}_$PROGRAM
41+
fi
42+
rm --force "$SHADOW_DESKTOP/$PROGRAM.desktop"
2943
fi

os2borgerpc/desktop/desktop_shortcut_to_directory.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
# Places a shortcut on the Desktop to any directory on the file system
44
#
55
# Parameters:
6-
# 1: The path to the directory you want a shortcut to
7-
# 2: The name of the shortcut on the Desktop
6+
# 1: Whether to add or remove the shortcut
7+
# 2: The path to the directory you want a shortcut to
8+
# 3: The name of the shortcut on the Desktop
89

910
set -ex
1011

11-
DIRECTORY="$1"
12-
SHORTCUT_NAME="$2"
12+
ADD="$1"
13+
DIRECTORY="$2"
14+
SHORTCUT_NAME="$3"
1315

1416
SHADOW_DESKTOP="/home/.skjult/Skrivebord"
1517

1618
mkdir --parents $SHADOW_DESKTOP
1719

18-
# Note: "ln" doesn't care if the destination ($DIRECTORY) exists
19-
ln --symbolic --force "$DIRECTORY" "$SHADOW_DESKTOP/$SHORTCUT_NAME"
20+
if [ "$ADD" = "True" ]; then
21+
# Note: "ln" doesn't care if the destination ($DIRECTORY) exists
22+
ln --symbolic --force "$DIRECTORY" "$SHADOW_DESKTOP/$SHORTCUT_NAME"
23+
else
24+
rm "$SHADOW_DESKTOP/$SHORTCUT_NAME"
25+
fi
Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
1-
#!/bin/bash
1+
#!/usr/bin/env sh
22

3-
# DESCRIPTION
3+
# DESCRIPTION
44
# This script either copies a given installed .desktop file to the autostart directory
5-
# or removes a given file from the autostart directory.
5+
# or removes a given file from the autostart directory.
66
#
7-
# To check which scripts are installed on a machine run the script
8-
# "desktop_print_program_list.sh" AKA "Desktop - Vis programliste" with paremeter
7+
# To check which scripts are installed on a machine run the script
8+
# "desktop_print_program_list.sh" AKA "Desktop - Vis programliste" with paremeter
99
# "mulige" to print a full list of eligible files to add to autostart.
1010
#
1111
# PARAMENTERS
12-
# 1. String. The given file's name, either with no extension or with .desktop
13-
# eg. "firefox" or "firefox.desktop". This parameter IS case-sensitive
14-
# as some applications have capitalized characters in their filename.
12+
# 1. String. The given file's name, e.g. firefox, without the .desktop extension.
13+
# This parameter IS case-sensitive as some applications have
14+
# capitalized characters in their filename.
1515
# 2. Checkbox. Check this box to delete a file from the autostart folder instead.
1616

17-
SELECTED=$1
18-
DELETE=$2
17+
set -x
1918

20-
if [[ $SELECTED != *.desktop ]]; then
21-
SELECTED="$SELECTED.desktop"
22-
fi
19+
PROGRAM="$1"
20+
DELETE="$2"
2321

2422
AUTOSTART_DIR="/home/.skjult/.config/autostart"
25-
AUTOSTART_FILE="$AUTOSTART_DIR/$SELECTED"
26-
INSTALLED_APP_FILE="/usr/share/applications/$SELECTED"
2723

28-
if [ "$DELETE" = "True" ]; then
29-
echo "Removing $SELECTED from autostart directory"
30-
31-
rm "$AUTOSTART_FILE"
24+
if [ -f "/var/lib/snapd/desktop/applications/${PROGRAM}_$PROGRAM.desktop" ]; then
3225

33-
exit "$?"
26+
INSTALLED_APP_FILE="/var/lib/snapd/desktop/applications/${PROGRAM}_$PROGRAM.desktop"
27+
AUTOSTART_FILE="$AUTOSTART_DIR/${PROGRAM}_$PROGRAM.desktop"
28+
else
29+
INSTALLED_APP_FILE="/usr/share/applications/$PROGRAM.desktop"
30+
AUTOSTART_FILE="$AUTOSTART_DIR/$PROGRAM.desktop"
3431
fi
3532

36-
mkdir -p $AUTOSTART_DIR
33+
mkdir --parents $AUTOSTART_DIR
34+
35+
# Remove it first, partially because ln even with --force cannot replace it if it's a regular file
36+
rm --force "$AUTOSTART_FILE"
3737

38-
echo "Adding $SELECTED to autostart directory"
38+
if [ "$DELETE" != "True" ]; then
3939

40-
cp "$INSTALLED_APP_FILE" "$AUTOSTART_FILE"
40+
echo "Adding $PROGRAM to autostart directory"
4141

42-
exit "$?"
42+
ln --symbolic --force "$INSTALLED_APP_FILE" "$AUTOSTART_FILE"
43+
44+
exit "$?"
45+
fi

os2borgerpc/os2borgerpc/browser_set_default.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#
55
# Arguments:
66
# 1: Which browser to set as default.
7-
# Options are: 'firefox' or 'chrome'
87

98
set -ex
109

@@ -16,10 +15,16 @@ BROWSER="$(lower "$1")"
1615

1716
FILE="/usr/share/applications/defaults.list"
1817

19-
if [ "$BROWSER" = "firefox" ]; then
20-
DESKTOP_FILE=firefox.desktop
18+
# They can type in "chrome" but the desktop file is called google-chrome
19+
if [ "$BROWSER" = "chrome" ]; then
20+
BROWSER=google-chrome
21+
fi
22+
23+
# Handle snaps, which have names like firefox_firefox.desktop
24+
if [ -d "/snap/$BROWSER" ]; then
25+
DESKTOP_FILE=${BROWSER}_$BROWSER.desktop
2126
else
22-
DESKTOP_FILE=google-chrome.desktop
27+
DESKTOP_FILE=${BROWSER}.desktop
2328
fi
2429

2530
# We cleanup the defaults.list as sometimes it seems to be populated

0 commit comments

Comments
 (0)