2023-07-18 23:35:27 +02:00
|
|
|
#!/usr/bin/env bash
|
2023-07-20 21:01:00 +02:00
|
|
|
#
|
|
|
|
# flatpak-sync is an easy approach for declaring the installed flatpak apps.
|
|
|
|
#
|
|
|
|
# 2023 (c) Marcel Kapfer <opensource@mmk2410.org>
|
|
|
|
# Licensed under the MIT License
|
2023-07-18 23:35:27 +02:00
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
APPS_LIST_PATH="${APPS_LIST_PATH:=$HOME/.config/flatpak-sync/apps.list}"
|
|
|
|
|
2023-07-20 22:03:59 +02:00
|
|
|
APP_NAME="flatpak-sync"
|
|
|
|
APP_COLOR="\033[0;35m"
|
|
|
|
INFO_COLOR="\033[0;32m"
|
|
|
|
ERR_COLOR="\033[1;31m"
|
|
|
|
NO_COLOR="\033[0m"
|
|
|
|
|
2023-07-20 22:12:20 +02:00
|
|
|
function info() {
|
2023-07-20 22:03:59 +02:00
|
|
|
printf "$APP_COLOR[$APP_NAME] $INFO_COLOR$1$NO_COLOR\n"
|
|
|
|
}
|
|
|
|
|
2023-07-20 22:12:20 +02:00
|
|
|
function error() {
|
2023-07-20 22:03:59 +02:00
|
|
|
printf "$APP_COLOR[$APP_NAME] $ERR_COLOR$1$NO_COLOR\n"
|
|
|
|
}
|
|
|
|
|
2023-07-20 22:12:20 +02:00
|
|
|
function usage() {
|
2023-07-20 22:09:33 +02:00
|
|
|
info "Usage: flatpak-sync [sync | add <appId> | remove <appId> | usage]"
|
2023-07-20 22:08:37 +02:00
|
|
|
info "sync: Synchronized installed apps with app list."
|
|
|
|
info "add | a | install | i: Add app with app id to the list and sync."
|
|
|
|
info "remove | r | uninstall | u: Remove app with app id from the list and sync."
|
2023-07-20 22:09:33 +02:00
|
|
|
info "usage: Print this help."
|
2023-07-20 22:08:37 +02:00
|
|
|
info "Without any given argument a sync is executed."
|
2023-07-20 22:03:59 +02:00
|
|
|
}
|
|
|
|
|
2023-07-20 21:06:42 +02:00
|
|
|
function sync() {
|
|
|
|
readarray -t requested_apps < <(cat "$APPS_LIST_PATH")
|
|
|
|
readarray -t installed_apps < <(flatpak list --app --columns app | tail -n +1)
|
2023-07-18 23:35:27 +02:00
|
|
|
|
2023-07-20 21:06:42 +02:00
|
|
|
declare -a requested_apps_cleaned=()
|
2023-07-18 23:35:27 +02:00
|
|
|
|
2023-07-20 22:03:59 +02:00
|
|
|
info "Checking for apps to install."
|
2023-07-20 21:06:42 +02:00
|
|
|
for app in "${requested_apps[@]}"; do
|
|
|
|
if [[ -z "$app" ]] || [[ "$app" =~ ^#.* ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
requested_apps_cleaned+=("$app")
|
2023-07-20 21:07:51 +02:00
|
|
|
# shellcheck disable=SC2076
|
2023-07-20 21:06:42 +02:00
|
|
|
if [[ ! "${installed_apps[*]}" =~ "${app}" ]]; then
|
2023-07-20 22:03:59 +02:00
|
|
|
info "$app not installed. Installing it."
|
2023-07-20 21:06:42 +02:00
|
|
|
flatpak install -y "$app"
|
|
|
|
fi
|
|
|
|
done
|
2023-07-18 23:35:27 +02:00
|
|
|
|
2023-07-20 22:03:59 +02:00
|
|
|
info "Checking for apps to remove."
|
2023-07-20 21:06:42 +02:00
|
|
|
for app in "${installed_apps[@]}"; do
|
2023-07-20 21:07:51 +02:00
|
|
|
# shellcheck disable=SC2076
|
2023-07-20 21:06:42 +02:00
|
|
|
if [[ ! "${requested_apps_cleaned[*]}" =~ "${app}" ]]; then
|
2023-07-20 22:03:59 +02:00
|
|
|
info "$app no found in apps list. Removing it.";
|
2023-07-20 21:06:42 +02:00
|
|
|
flatpak uninstall -y "$app"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-07-20 21:39:43 +02:00
|
|
|
function add() {
|
|
|
|
local appId="${2:-}"
|
|
|
|
|
|
|
|
if [[ -z "$appId" ]]; then
|
2023-07-20 22:03:59 +02:00
|
|
|
error "No application ID given."
|
|
|
|
usage
|
2023-07-20 21:39:43 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-07-20 21:54:09 +02:00
|
|
|
if grep -q "# $appId" "$APPS_LIST_PATH"; then
|
2023-07-20 22:03:59 +02:00
|
|
|
error "App $appId already in apps list but commented out."
|
|
|
|
error "Manual fix of the app list file necessary."
|
2023-07-20 21:39:43 +02:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2023-07-20 21:54:09 +02:00
|
|
|
if grep -q "$appId" "$APPS_LIST_PATH"; then
|
2023-07-20 22:03:59 +02:00
|
|
|
error "App $appId already in apps list."
|
|
|
|
error "Run sync command to install it."
|
|
|
|
usage
|
2023-07-20 21:54:09 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
2023-07-20 22:03:59 +02:00
|
|
|
info "Adding app $appId to apps list and installing it."
|
2023-07-20 21:39:43 +02:00
|
|
|
local appsListFile
|
|
|
|
appsListFile="$(realpath "$APPS_LIST_PATH")"
|
|
|
|
echo "$appId" >> "$appsListFile"
|
|
|
|
}
|
|
|
|
|
2023-07-20 21:48:41 +02:00
|
|
|
function remove() {
|
|
|
|
local appId="${2:-}"
|
|
|
|
|
|
|
|
if [[ -z "$appId" ]]; then
|
2023-07-20 22:03:59 +02:00
|
|
|
error "No application ID given."
|
|
|
|
usage
|
2023-07-20 21:48:41 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-07-20 21:54:09 +02:00
|
|
|
if grep -q "# $appId" "$APPS_LIST_PATH"; then
|
2023-07-20 22:03:59 +02:00
|
|
|
error "App $appId already in apps list but commented out."
|
|
|
|
error "No remove possible."
|
2023-07-20 21:54:09 +02:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! grep -q "$appId" "$APPS_LIST_PATH"; then
|
2023-07-20 22:03:59 +02:00
|
|
|
error "App $appId not in apps list."
|
|
|
|
usage
|
2023-07-20 21:48:41 +02:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2023-07-20 22:03:59 +02:00
|
|
|
info "Removing app $appId from apps list and uninstalling it."
|
2023-07-20 21:48:41 +02:00
|
|
|
local appsListFile
|
|
|
|
appsListFile="$(realpath "$APPS_LIST_PATH")"
|
|
|
|
appsListTmpFile="$(mktemp)"
|
|
|
|
grep -v "$appId" "$APPS_LIST_PATH" >> "$appsListTmpFile"
|
|
|
|
mv "$appsListTmpFile" "$appsListFile"
|
|
|
|
}
|
|
|
|
|
2023-07-20 21:39:43 +02:00
|
|
|
cmd="${1:-}"
|
|
|
|
|
|
|
|
case "$cmd" in
|
|
|
|
a|add|i|install )
|
|
|
|
add "$@"
|
|
|
|
sync
|
|
|
|
;;
|
2023-07-20 21:48:41 +02:00
|
|
|
r|remove|u|uninstall )
|
|
|
|
remove "$@"
|
|
|
|
sync
|
|
|
|
;;
|
2023-07-20 22:09:33 +02:00
|
|
|
usage )
|
|
|
|
usage
|
|
|
|
;;
|
2023-07-20 21:39:43 +02:00
|
|
|
* )
|
|
|
|
sync
|
|
|
|
esac
|