#!/bin/bash # Installation script for TeamSpeak Echoion systemd service set -e RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" USER_NAME="${USER_NAME:-bot}" INSTALL_TYPE="${1:-user}" # 'user' or 'system' echo -e "${GREEN}TeamSpeak Echoion Bot - Service Installer${NC}" echo "==========================================" echo "" # Show usage for user without proper session if [ "$INSTALL_TYPE" == "user" ] && [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then echo -e "${YELLOW}Note: Running without a full user session${NC}" echo "This is OK - the script will install the service files." echo "You may need to manually reload systemd after installation." echo "" fi # Check if running as root for system install if [ "$INSTALL_TYPE" == "system" ] && [ "$EUID" -ne 0 ]; then echo -e "${RED}Error: System installation requires root privileges${NC}" echo "Run: sudo $0 system" exit 1 fi # Check if user is correct if [ "$INSTALL_TYPE" == "user" ] && [ "$(whoami)" != "$USER_NAME" ]; then echo -e "${YELLOW}Warning: You are running as '$(whoami)' but USER_NAME is set to '$USER_NAME'${NC}" echo "This is OK if you're installing for another user." echo "To install for current user, run: USER_NAME=$(whoami) $0" echo "" read -p "Continue anyway? (y/N) " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1 fi fi # Verify bot user exists if ! id "$USER_NAME" &>/dev/null; then echo -e "${RED}Error: User '$USER_NAME' does not exist${NC}" echo "Create the user first: sudo useradd -m -s /bin/bash $USER_NAME" exit 1 fi echo "Installation type: $INSTALL_TYPE" echo "Bot user: $USER_NAME" echo "" # Check prerequisites echo -e "${YELLOW}Checking prerequisites...${NC}" # Check if weston is installed if ! command -v weston &> /dev/null; then echo -e "${RED}Error: weston is not installed${NC}" echo "Install it with: sudo pacman -S weston" exit 1 fi # Check if dbus-run-session is available if ! command -v dbus-run-session &> /dev/null; then echo -e "${RED}Error: dbus-run-session is not available${NC}" echo "Install it with: sudo pacman -S dbus" exit 1 fi # Check TeamSpeak directory TS3_DIR="/home/$USER_NAME/TeamSpeak3-Client-linux_amd64" if [ ! -d "$TS3_DIR" ]; then echo -e "${YELLOW}Warning: TeamSpeak directory not found at: $TS3_DIR${NC}" echo "Make sure to install TeamSpeak client there before starting the service" fi # Check certificates CERT_FILE="/home/$USER_NAME/.config/EchoionTsBot.crt" KEY_FILE="/home/$USER_NAME/.config/EchoionTsBot.key" if [ ! -f "$CERT_FILE" ] || [ ! -f "$KEY_FILE" ]; then echo -e "${YELLOW}Warning: TLS certificates not found${NC}" echo " Cert: $CERT_FILE" echo " Key: $KEY_FILE" echo "You can generate self-signed certificates with:" echo " openssl req -x509 -newkey rsa:4096 -keyout $KEY_FILE -out $CERT_FILE -days 365 -nodes" fi echo -e "${GREEN}Prerequisites check passed!${NC}" echo "" # Copy wrapper script echo -e "${YELLOW}Installing wrapper script...${NC}" mkdir -p "/home/$USER_NAME/echoion/scripts" cp "$SCRIPT_DIR/../scripts/run-teamspeak-headless.sh" "/home/$USER_NAME/echoion/scripts/" chown -R "$USER_NAME:$USER_NAME" "/home/$USER_NAME/echoion" chmod +x "/home/$USER_NAME/echoion/scripts/run-teamspeak-headless.sh" echo -e "${GREEN}Wrapper script installed${NC}" echo "" # Function to check if we have a working user systemd session has_user_systemd_session() { local user="$1" # Check if we can communicate with the user systemd if sudo -u "$user" systemctl --user --quiet is-system-running 2>/dev/null || \ sudo -u "$user" systemctl --user daemon-reload 2>/dev/null; then return 0 fi return 1 } # Install systemd service if [ "$INSTALL_TYPE" == "system" ]; then echo -e "${YELLOW}Installing system service...${NC}" cp "$SCRIPT_DIR/../systemd/teamspeak-echoion.service" /etc/systemd/system/ # Update user in service file sed -i "s/User=bot/User=$USER_NAME/g" /etc/systemd/system/teamspeak-echoion.service sed -i "s/Group=bot/Group=$USER_NAME/g" /etc/systemd/system/teamspeak-echoion.service sed -i "s|/home/bot|/home/$USER_NAME|g" /etc/systemd/system/teamspeak-echoion.service systemctl daemon-reload echo -e "${GREEN}System service installed${NC}" echo "" echo "To start the service:" echo " sudo systemctl start teamspeak-echoion" echo "" echo "To enable on boot:" echo " sudo systemctl enable teamspeak-echoion" echo "" echo "To check status:" echo " sudo systemctl status teamspeak-echoion" else echo -e "${YELLOW}Installing user service...${NC}" # Create user systemd directory sudo -u "$USER_NAME" mkdir -p "/home/$USER_NAME/.config/systemd/user" # Copy service file cp "$SCRIPT_DIR/../systemd/teamspeak-echoion-user.service" "/home/$USER_NAME/.config/systemd/user/teamspeak-echoion.service" # Update paths in service file sed -i "s|/home/bot|/home/$USER_NAME|g" "/home/$USER_NAME/.config/systemd/user/teamspeak-echoion.service" chown "$USER_NAME:$USER_NAME" "/home/$USER_NAME/.config/systemd/user/teamspeak-echoion.service" # Try to reload user systemd, but don't fail if not in a session echo -e "${YELLOW}Attempting to reload systemd user daemon...${NC}" if has_user_systemd_session "$USER_NAME"; then echo -e "${GREEN}Systemd user session found, reloading daemon${NC}" sudo -u "$USER_NAME" systemctl --user daemon-reload echo "" echo "To start the service:" echo " systemctl --user start teamspeak-echoion" echo "" echo "To enable on login:" echo " systemctl --user enable teamspeak-echoion" echo "" echo "To check status:" echo " systemctl --user status teamspeak-echoion" else echo -e "${YELLOW}Note: No active systemd user session detected${NC}" echo "This is normal when running via SSH without X11 or from a minimal shell" echo "" echo "The service file has been installed. To complete setup, run these commands" echo "from a proper user session (e.g., after logging in via SSH with a PTY):" echo "" echo " systemctl --user daemon-reload" echo " systemctl --user start teamspeak-echoion" echo " systemctl --user enable teamspeak-echoion" echo "" echo "Or start it manually with:" echo " ~/echoion/scripts/run-teamspeak-headless.sh" fi echo "" echo "To enable lingering (start on boot without login):" echo " sudo loginctl enable-linger $USER_NAME" fi echo "" echo -e "${GREEN}Installation complete!${NC}" echo "" echo "View logs with:" if [ "$INSTALL_TYPE" == "system" ]; then echo " sudo journalctl -u teamspeak-echoion -f" else echo " sudo -u $USER_NAME journalctl --user -u teamspeak-echoion -f" fi