AnyBot/deploy/linux/hands.sh

104 lines
4.3 KiB
Bash
Raw Normal View History

#!/bin/sh
# Make this Linux computer a pair of hands for your AnyBot brain: the brain's bots can see and use
# its screen, files and browser (asking you first, as usual). Works on a desktop and on a computer
# without a screen (a virtual screen is made for it). Run from the app/ folder:
#
# ./deploy/linux/hands.sh --deps also install what a computer without a screen needs (sudo)
# ./deploy/linux/hands.sh set up and start it (asks for your account the first time)
# ./deploy/linux/hands.sh --stop stop it and don't start it with the computer any more
set -e
cd "$(dirname "$0")/../.."
UNIT="$HOME/.config/systemd/user/anybot-hands.service"
ENVF="$HOME/.config/anybot/hands.env"
RUN="$HOME/.local/bin/anybot-hands"
if [ "$1" = "--stop" ]; then
systemctl --user disable --now anybot-hands.service 2>/dev/null || true
rm -f "$UNIT"
systemctl --user daemon-reload
echo "Stopped. (Your account details stay in $ENVF; delete it if you like.)"
exit 0
fi
if [ "$1" = "--deps" ]; then
# A virtual screen, a window manager, the accessibility bus (for reading apps' buttons), a session bus.
if command -v apt-get >/dev/null; then
sudo apt-get update && sudo apt-get install -y xvfb openbox at-spi2-core libatk-adaptor dbus-x11 xauth fonts-noto-cjk
elif command -v dnf >/dev/null; then
sudo dnf install -y xorg-x11-server-Xvfb openbox at-spi2-core at-spi2-atk dbus-x11 xorg-x11-xauth google-noto-sans-cjk-fonts
elif command -v pacman >/dev/null; then
sudo pacman -S --needed xorg-server-xvfb openbox at-spi2-core dbus xorg-xauth noto-fonts-cjk
else
echo "Install with your package manager: Xvfb, openbox, at-spi2-core, dbus, xauth"
fi
fi
command -v systemctl >/dev/null || { echo "This needs systemd (to keep AnyBot running)."; exit 1; }
[ -x "$HOME/.local/bin/anybot" ] || sh deploy/linux/install.sh
if [ ! -f "$ENVF" ]; then
echo "Your AnyBot account (the same one your brain uses):"
printf " Account server address (e.g. https://anybot.example.com): "; read -r URL
printf " Account name: "; read -r USERNAME
printf " Password: "; stty -echo 2>/dev/null; read -r PASSWORD; stty echo 2>/dev/null; echo
printf " Name for this computer [%s]: " "$(hostname)"; read -r NAME
printf " Brain's name (empty = only lend hands, show no bots here): "; read -r BRAIN
mkdir -p "$(dirname "$ENVF")"
umask 077
# Each value in single quotes, so a password with $ or spaces stays as typed.
q() { printf "%s='%s'\n" "$1" "$(printf %s "$2" | sed "s/'/'\\\\''/g")"; }
{
q ANYBOT_ACCOUNT "${URL%/}|$USERNAME"
q ANYBOT_ACCOUNT_PASSWORD "$PASSWORD"
q ANYBOT_HOST_NAME "${NAME:-$(hostname)}"
if [ -n "$BRAIN" ]; then q ANYBOT_BRAIN "$BRAIN"; fi
} > "$ENVF"
chmod 600 "$ENVF"
fi
mkdir -p "$(dirname "$RUN")" "$(dirname "$UNIT")"
cat > "$RUN" <<'SH'
#!/bin/sh
# Started by systemd (anybot-hands.service).
set -a; . "$HOME/.config/anybot/hands.env"; set +a
export ANYBOT_DEVICE_LINK=1
export LANG="${LANG:-C.UTF-8}"
# The accessibility bus, so bots can read apps' buttons instead of only looking at pixels.
export NO_AT_BRIDGE=0 GTK_A11Y=atspi QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
if [ -z "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ]; then
# No screen: a virtual one with a window manager, and a session bus for the accessibility bus.
exec xvfb-run -a -s "-screen 0 1440x900x24" dbus-run-session -- sh -c 'openbox >/dev/null 2>&1 & exec "$HOME/.local/bin/anybot" --serve'
fi
exec "$HOME/.local/bin/anybot" --serve
SH
chmod 755 "$RUN"
cat > "$UNIT" <<UNIT
[Unit]
Description=AnyBot hands (lets your AnyBot brain use this computer)
After=network-online.target graphical-session.target
Wants=network-online.target
[Service]
ExecStart=$RUN
Restart=always
RestartSec=5
[Install]
WantedBy=default.target
UNIT
systemctl --user daemon-reload
systemctl --user enable --now anybot-hands.service
sleep 3
if systemctl --user is-active --quiet anybot-hands.service; then
echo "Running. The brain's bots can now use this computer (they ask you first, as usual)."
echo "Watch it: journalctl --user -u anybot-hands -f"
else
echo "It didn't start. See: journalctl --user -u anybot-hands -n 50"
fi
if ! loginctl show-user "$USER" 2>/dev/null | grep -q "Linger=yes"; then
echo "To keep it running after you log out and after a restart, run once:"
echo " sudo loginctl enable-linger $USER"
fi