#!/bin/bash # # install.sh — install the KDE virtual-display auto-scaler. # # Copies the script into ~/.local/bin and installs the udev rule that fires it # on the DRM "change" event (the resize that spice-vdagent performs on # dock/undock). Re-running it is safe: it overwrites the files in place. # # Requires sudo for the udev-rule install. set -eu SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_NAME="auto-scale-virtual.sh" RULE_NAME="99-auto-scale-virtual.rules" BIN_DIR="${HOME}/.local/bin" RULE_DIR="/etc/udev/rules.d" # --- sanity: we're next to the files we install ----------------------------- if [ ! -f "${SCRIPT_DIR}/${SCRIPT_NAME}" ] || [ ! -f "${SCRIPT_DIR}/${RULE_NAME}" ]; then echo "install.sh must sit next to ${SCRIPT_NAME} and ${RULE_NAME}." >&2 exit 1 fi # --- the udev rule pins the user's UID/GID; patch it to match $UID ----------- # Keeps the repo version generic while installing a working rule for the # current user. Falls back to 1001 if id is unavailable (matches the default # baked into the shipped rule). UID_NUM="${UID:-$(id -u 2>/dev/null || echo 1001)}" GID_NUM="$(id -g 2>/dev/null || echo 1001)" # --- 1) the script into ~/.local/bin ---------------------------------------- mkdir -p "${BIN_DIR}" install -m 755 "${SCRIPT_DIR}/${SCRIPT_NAME}" "${BIN_DIR}/${SCRIPT_NAME}" echo "installed ${SCRIPT_NAME} -> ${BIN_DIR}/${SCRIPT_NAME}" # --- 2) the udev rule, rewritten for the current user, then installed ------- TMP_RULE="$(mktemp)" trap 'rm -f "${TMP_RULE}"' EXIT sed -E "s/--uid=[0-9]+/--uid=${UID_NUM}/; s/--gid=[0-9]+/--gid=${GID_NUM}/" \ "${SCRIPT_DIR}/${RULE_NAME}" > "${TMP_RULE}" if [ "${EUID}" -eq 0 ]; then install -m 644 "${TMP_RULE}" "${RULE_DIR}/${RULE_NAME}" udevadm control --reload-rules else if command -v sudo >/dev/null 2>&1; then install_sudo() { sudo install -m 644 "$@"; } sudo udevadm control --reload-rules else echo "Need root to install the udev rule to ${RULE_DIR}." >&2 echo "The rule has been generated at: ${TMP_RULE}" >&2 echo "Run: sudo install -m 644 ${TMP_RULE} ${RULE_DIR}/${RULE_NAME} && sudo udevadm control --reload-rules" >&2 exit 1 fi install_sudo "${TMP_RULE}" "${RULE_DIR}/${RULE_NAME}" fi echo "installed ${RULE_NAME} -> ${RULE_DIR}/${RULE_NAME}" echo "udev rules reloaded" echo echo "Done. Dock/undock (or resize the SPICE client) and the scale is applied" echo "automatically. Run ${SCRIPT_NAME} manually at any time to fix the scale."