steam-deck-syncthing/syncthing-deck-setup.sh
2022-07-09 23:16:04 -04:00

149 lines
3.2 KiB
Bash

#!/bin/bash
SDS_VERSION="0.0.1"
DEFAULT_VERSION="latest"
DEFAULT_PLATFORM="linux"
DEFAULT_ARCH="amd64"
SYNC_VERSION=$DEFAULT_VERSION
SYNC_PLATFORM=$DEFAULT_PLATFORM
SYNC_ARCH=$DEFAULT_ARCH
PKG_NAME="syncthing-$SYNC_PLATFORM-$SYNC_ARCH-v$SYNC_VERSION"
SDS_DIR_TMP="$HOME/.cache/steamdecksync"
SDS_DIR_BIN="$HOME/.local/bin"
SDS_DIR_SYS="$HOME/.config/systemd/user"
sds_extract_local_files () {
tar -xf $SDS_DIR_TMP/syncthing-files.tar.gz -C $SDS_DIR_TMP
}
sds_fetch_remote_files () {
echo "Pulling Syncthing package $PKG_NAME"
local REPO_URL="https://github.com/syncthing/syncthing/"
local RELEASE_URL="$REPO_URL/releases/download/v$SYNC_VERSION/$PKG_NAME.tar.gz"
curl --silent -L $RELEASE_URL > $SDS_DIR_TMP/syncthing-files.tar.gz
}
sds_install () {
cp $SDS_DIR_TMP/$PKB_NAME/syncthing $SDS_DIR_BIN/syncthing
systemctl --user daemon-reload
systemctl --user enable syncthing.service
systemctl --user start syncthing.service
}
sds_make_syncthing_service () {
cat > $HOME/.config/systemd/user/syncthing.service <<- EOM
[Unit]
Description=Syncthing - Open Source Continuous File Synchronization
Documentation=man:syncthing(1)
StartLimitIntervalSec=60
StartLimitBurst=4
[Service]
ExecStart=/home/deck/.local/bin/syncthing serve --no-browser --no-restart --logflags=0
Restart=on-failure
RestartSec=1
SuccessExitStatus=3 4
RestartForceExitStatus=3 4
# Hardening
SystemCallArchitectures=native
MemoryDenyWriteExecute=true
NoNewPrivileges=true
[Install]
WantedBy=default.target
EOM
}
sds_set_package_name () {
if [ $SYNC_VERSION == "latest" ]; then
local TAG_URL="https://api.github.com/repos/syncthing/syncthing/tags?per_page=1"
local SYNC_VERSION=$(\
curl --silent $TAG_URL | \
grep '"name":' | \
sed -r 's/.*v(.*)\".*/\1/'
)
fi
PKG_NAME="syncthing-$SYNC_PLATFORM-$SYNC_ARCH-v$SYNC_VERSION"
}
sds_setup () {
mkdir -p $SDS_DIR_TMP
mkdir -p $SDS_DIR_BIN
mkdir -p $SDS_DIR_SYS
}
sds_uninstall () {
echo "Stopping/Disabling Systemd User Service"
systemctl --user disable syncthing.service
systemctl --user stop syncthing.service
echo "Removing SDS directories and files"
rm -rf $SDS_DIR_TMP
rm $SDS_DIR_BIN/syntching
rm $SDS_DIR_SYS/syncthing.service
echo "SDS uninstalled"
}
sds_usage () {
echo -e "Usage: syncthing-deck-setup [<options>]"
printf "\n%17s %s" "-V, --version" "Specify a Syncthing Version. Default is latest."
printf "\n%17s %s" "-P, --platform" "Specify an OS/Platform. Default is linux."
printf "\n%17s %s" "-A, --arch" "Specify an architecture. Default is amd64."
printf "\n%17s %s" "-U, --uninstall" "Remove files setup with this script."
printf "\n%17s %s" "-v" "Show version"
printf "\n%17s %s" "-h" "Show this usage"
echo -e "\n"
}
while [[ $# -gt 0 ]]; do
case $1 in
-V|--version)
SYNC_VERSION="$2"
shift
shift
;;
-P|--platform)
SYNC_PLATFORM="$2"
shift
shift
;;
-A|--arch)
SYNC_ARCH="$2"
shift
shift
;;
-U|--uninstall)
sds_uninstall
exit 0
;;
-v)
echo "Syncthing Deck Setup version $SDS_VERSION"
exit 0
;;
*)
sds_usage
exit 0
;;
esac
done
sds_setup
sds_set_package_name
sds_fetch_remote_files
sds_make_syncthing_service
sds_extract_local_files
sds_install