Wanting to avoid running unnecessary setup in logs as much as possible. Unfortunately, `flatpak remote-add` still runs even if repo exists, as it apparently modifies the URL, no matter if it's the same. I tried to extract & compare input URL & flatpak URL to mitigate this as an condition, but it doesn't work, as flatpak lists repo URL, while we input flatpakref URLs, which are not the same.
150 lines
7.5 KiB
Bash
Executable file
150 lines
7.5 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
# Opt out of and remove Fedora's flatpak repo
|
|
if grep -qz 'fedora' <<< "$(flatpak remotes)"; then
|
|
/usr/bin/gnome-software --quit
|
|
/usr/lib/fedora-third-party/fedora-third-party-opt-out
|
|
/usr/bin/fedora-third-party disable
|
|
flatpak remote-delete fedora --force
|
|
flatpak remote-delete fedora-testing --force
|
|
|
|
# Remove flatpak apps from origin fedora
|
|
FEDORA_FLATPAKS=$(flatpak list --app --columns=application,origin | grep -w 'fedora' | awk '{print $1}')
|
|
flatpak remove --system --noninteractive ${FEDORA_FLATPAKS[@]}
|
|
|
|
# Remove flatpak runtimes from origin fedora
|
|
FEDORA_FLATPAKS=$(flatpak list --runtime --columns=application,arch,branch,origin | grep -w 'fedora' | awk '{print $1"/"$2"/"$3}')
|
|
flatpak remove --system --noninteractive ${FEDORA_FLATPAKS[@]}
|
|
fi
|
|
|
|
REPO_INFO="/usr/share/bluebuild/default-flatpaks/system/repo-info.yml"
|
|
REPO_URL=$(yq '.repo-url' $REPO_INFO)
|
|
REPO_NAME=$(yq '.repo-name' $REPO_INFO)
|
|
REPO_TITLE=$(yq '.repo-title' $REPO_INFO)
|
|
|
|
# General conditions for not running the unnecessary flatpak setup
|
|
# Currently, we don't modify remote title if it's already modified
|
|
# Flatpak add remote is ran for some reason, even with --if-not-exists flag, apparently, it modifies the URL
|
|
# We cannot compare repo URLs properly
|
|
# Flatpak outputs repo URL, while we have flatpakref URL, which is not the same
|
|
readarray -t CURRENT_REPO_INFO < <(flatpak remotes --system --columns=name,url,title)
|
|
for index in "${CURRENT_REPO_INFO[@]}"; do
|
|
CURRENT_REPO_NAMES+=("$(echo "${index}" | awk '{print $1}')")
|
|
CURRENT_REPO_TITLES+=("$(echo "${index}" | awk '{ for(i=3;i<NF;i++) printf "%s ", $i; print $NF }')")
|
|
for name in "${CURRENT_REPO_NAMES[@]}"; do
|
|
for title in "${CURRENT_REPO_TITLES[@]}"; do
|
|
if [[ "${name}" == "${REPO_NAME}" ]] && [[ "${title}" == "${REPO_TITLE}" ]]; then
|
|
no_title_modify=true
|
|
fi
|
|
done
|
|
done
|
|
done
|
|
|
|
# Set up system-wide Flatpak repository
|
|
if [[ $REPO_URL != "null" && $REPO_NAME != "null" ]]; then
|
|
echo "Adding system-wide remote $REPO_NAME from $REPO_URL"
|
|
flatpak remote-add --if-not-exists --system "$REPO_NAME" "$REPO_URL"
|
|
fi
|
|
|
|
# If configured remote is flathub, enable it here.
|
|
# Flathub is already installed on Fedora, but not enabled by default,
|
|
# so the above command won't add it again
|
|
if [[ $REPO_NAME == "flathub" ]]; then
|
|
flatpak remote-modify --system "$REPO_NAME" --enable
|
|
fi
|
|
|
|
# Change repository title to configured title, if not null
|
|
if [[ $REPO_TITLE != "null" ]] && ! ${no_title_modify}; then
|
|
flatpak remote-modify --system "$REPO_NAME" --title="$REPO_TITLE"
|
|
echo "Setting title $REPO_TITLE for remote $REPO_NAME"
|
|
fi
|
|
|
|
# Notifications config
|
|
NOTIFICATIONS_FILE="/usr/share/bluebuild/default-flatpaks/notifications"
|
|
USER_NOTIFICATIONS_FILE="/etc/bluebuild/default-flatpaks/notifications"
|
|
# Ignore words starting with # symbol, whitelines & duplicate entries for notifications config
|
|
MAINTAINER_NOTIFICATIONS=$(cat "$NOTIFICATIONS_FILE" | grep -v -E '^#|^$' | awk '!seen[$0]++')
|
|
USER_NOTIFICATIONS=$(cat "$USER_NOTIFICATIONS_FILE" | grep -v -E '^#|^$' | awk '!seen[$0]++')
|
|
|
|
# If user modified notifications config, utilize user's configuration, otherwise maintainer's
|
|
if [[ -n $USER_NOTIFICATIONS ]]; then
|
|
NOTIFICATIONS="$USER_NOTIFICATIONS"
|
|
else
|
|
NOTIFICATIONS="$MAINTAINER_NOTIFICATIONS"
|
|
fi
|
|
|
|
# Installed flatpaks
|
|
FLATPAK_LIST=$(flatpak list --system --app --columns=application)
|
|
|
|
# Flatpak list files
|
|
INSTALL_LIST_FILE="/usr/share/bluebuild/default-flatpaks/system/install"
|
|
REMOVE_LIST_FILE="/usr/share/bluebuild/default-flatpaks/system/remove"
|
|
USER_INSTALL_LIST_FILE="/etc/bluebuild/default-flatpaks/system/install"
|
|
USER_REMOVE_LIST_FILE="/etc/bluebuild/default-flatpaks/system/remove"
|
|
# Prefer user's install + remove list over maintainer's, in case when same flatpak ID is present in maintainer's install list + user's remove list & vice-versa
|
|
# Also ignores words starting with # symbol, whitelines & duplicate entries
|
|
MAINTAINER_INSTALL_LIST=$(comm -23 <(sort "$INSTALL_LIST_FILE") <(sort "$USER_REMOVE_LIST_FILE") | grep -v -E '^#|^$' | awk '!seen[$0]++')
|
|
MAINTAINER_REMOVE_LIST=$(comm -23 <(sort "$REMOVE_LIST_FILE") <(sort "$USER_INSTALL_LIST_FILE") | grep -v -E '^#|^$' | awk '!seen[$0]++')
|
|
# Combine maintainer & user list. Ignore words starting with # symbol, whitelines & duplicate entries
|
|
COMBINED_INSTALL_LIST=$(cat <(echo "$MAINTAINER_INSTALL_LIST") "$USER_INSTALL_LIST_FILE" | grep -v -E '^#|^$' | awk '!seen[$0]++')
|
|
COMBINED_REMOVE_LIST=$(cat <(echo "$MAINTAINER_REMOVE_LIST") "$USER_REMOVE_LIST_FILE" | grep -v -E '^#|^$' | awk '!seen[$0]++')
|
|
|
|
function notify-send-pre-install {
|
|
user_name=$(loginctl list-sessions --output=json | jq -r '.[].user')
|
|
uid=$(loginctl list-sessions --output=json | jq -r '.[].uid')
|
|
xdg_runtime_path="/run/user/$uid"
|
|
display_var=$(printenv DISPLAY)
|
|
sudo -u "$user_name" DBUS_SESSION_BUS_ADDRESS=unix:path="$xdg_runtime_path"/bus DISPLAY="$display_var" notify-send "Flatpak Installer" "Started install of system flatpaks" --app-name="Flatpak Installer" -u NORMAL
|
|
}
|
|
|
|
function notify-send-install {
|
|
user_name=$(loginctl list-sessions --output=json | jq -r '.[].user')
|
|
uid=$(loginctl list-sessions --output=json | jq -r '.[].uid')
|
|
xdg_runtime_path="/run/user/$uid"
|
|
display_var=$(printenv DISPLAY)
|
|
sudo -u "$user_name" DBUS_SESSION_BUS_ADDRESS=unix:path="$xdg_runtime_path"/bus DISPLAY="$display_var" notify-send "Flatpak Installer" "Finished install of system flatpaks:\n$INSTALL_LIST" --app-name="Flatpak Installer" -u NORMAL
|
|
}
|
|
|
|
function notify-send-pre-uninstall {
|
|
user_name=$(loginctl list-sessions --output=json | jq -r '.[].user')
|
|
uid=$(loginctl list-sessions --output=json | jq -r '.[].uid')
|
|
xdg_runtime_path="/run/user/$uid"
|
|
display_var=$(printenv DISPLAY)
|
|
sudo -u "$user_name" DBUS_SESSION_BUS_ADDRESS=unix:path="$xdg_runtime_path"/bus DISPLAY="$display_var" notify-send "Flatpak Installer" "Started uninstall of some system flatpaks" --app-name="Flatpak Installer" -u NORMAL
|
|
}
|
|
|
|
function notify-send-uninstall {
|
|
user_name=$(loginctl list-sessions --output=json | jq -r '.[].user')
|
|
uid=$(loginctl list-sessions --output=json | jq -r '.[].uid')
|
|
xdg_runtime_path="/run/user/$uid"
|
|
display_var=$(printenv DISPLAY)
|
|
sudo -u "$user_name" DBUS_SESSION_BUS_ADDRESS=unix:path="$xdg_runtime_path"/bus DISPLAY="$display_var" notify-send "Flatpak Installer" "Finished uninstall of system flatpaks:\n$REMOVE_LIST" --app-name="Flatpak Installer" -u NORMAL
|
|
}
|
|
|
|
# Install flatpaks in list
|
|
if [[ -f $INSTALL_LIST_FILE ]] || [[ -f $USER_INSTALL_LIST_FILE ]]; then
|
|
if [[ -n $FLATPAK_LIST ]]; then
|
|
INSTALL_LIST=$(comm -23 <(echo "$COMBINED_INSTALL_LIST" | sort) <(echo "$FLATPAK_LIST" | sort))
|
|
else
|
|
INSTALL_LIST="$COMBINED_INSTALL_LIST"
|
|
fi
|
|
if [[ -n $INSTALL_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then
|
|
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
|
|
elif [[ -n $INSTALL_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then
|
|
notify-send-pre-install
|
|
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
|
|
notify-send-install
|
|
fi
|
|
fi
|
|
|
|
# Remove flatpaks in list
|
|
if [[ -f $REMOVE_LIST_FILE ]] || [[ -f $USER_REMOVE_LIST_FILE ]]; then
|
|
REMOVE_LIST=$(comm -12 <(echo "$COMBINED_REMOVE_LIST" | sort) <(echo "$FLATPAK_LIST" | sort))
|
|
if [[ -n $REMOVE_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then
|
|
flatpak uninstall --system --noninteractive ${REMOVE_LIST[@]}
|
|
elif [[ -n $REMOVE_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then
|
|
notify-send-pre-uninstall
|
|
flatpak uninstall --system --noninteractive ${REMOVE_LIST[@]}
|
|
notify-send-uninstall
|
|
fi
|
|
fi
|