#!/bin/bash # auto-opencode-setup.sh - Configure auto-opencode session on login # Usage: sudo ./auto-opencode-setup.sh [enable|disable] [graphical|multi-user] [auto-login] set -euo pipefail AUTO_OPENCODE_DIR="$HOME/.config/auto-opencode" AUTO_OPENCODE_SCRIPT="$AUTO_OPENCODE_DIR/auto-opencode.sh" AUTO_OPENCODE_MARKER="$AUTO_OPENCODE_DIR/enabled" AUTO_LOGIN_MARKER="$AUTO_OPENCODE_DIR/auto-login" TARGET_MARKER="$AUTO_OPENCODE_DIR/target" log_info() { echo -e "\033[0;34m[INFO]\033[0m $1"; } log_ok() { echo -e "\033[0;32m[OK]\033[0m $1"; } log_warn() { echo -e "\033[1;33m[WARN]\033[0m $1"; } log_error() { echo -e "\033[0;31m[ERROR]\033[0m $1"; } usage() { cat < "$AUTO_OPENCODE_SCRIPT" <<'SCRIPTEOF' #!/bin/bash # auto-opencode.sh - Runs on login to start opencode tmux session # This script is sourced from .zprofile (multi-user) or .xprofile (graphical) AUTO_OPENCODE_DIR="$HOME/.config/auto-opencode" AUTO_OPENCODE_MARKER="$AUTO_OPENCODE_DIR/enabled" AUTO_LOGIN_MARKER="$AUTO_OPENCODE_DIR/auto-login" # Check if auto-opencode is enabled [[ -f "$AUTO_OPENCODE_MARKER" ]] || exit 0 # Prevent multiple runs if [[ -n "$AUTO_OPENCODE_STARTED" ]]; then exit 0 fi export AUTO_OPENCODE_STARTED=1 # Only run in interactive shells [[ $- == *i* ]] || exit 0 # Check if tmux is available command -v tmux &>/dev/null || exit 0 command -v opencode &>/dev/null || exit 0 # Check if we're already in tmux [[ -n "$TMUX" ]] && exit 0 # Target directory TARGET_DIR="__OPEN_IOV_PARENT__" # Session name SESSION_NAME="auto-opencode" # Check if session already exists if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then echo "Attaching to existing $SESSION_NAME session..." exec tmux attach-session -t "$SESSION_NAME" fi # Create new tmux session with opencode echo "Starting auto-opencode session..." cd "$TARGET_DIR" # Start tmux session tmux new-session -d -s "$SESSION_NAME" -c "$TARGET_DIR" \ "opencode --dangerously-skip-permissions" # Wait a moment for opencode to start sleep 2 # Send initial commands to read AGENTS.md and handoff tmux send-keys -t "$SESSION_NAME" "cat AGENTS.md" Enter sleep 1 tmux send-keys -t "$SESSION_NAME" "cat Handoff-vGPU-IO.md" Enter sleep 1 # Attach to session exec tmux attach-session -t "$SESSION_NAME" SCRIPTEOF # Bake in the repo parent dir (resolved at setup time) local repo_parent repo_parent="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" sed -i "s|__OPEN_IOV_PARENT__|$repo_parent|g" "$AUTO_OPENCODE_SCRIPT" chmod +x "$AUTO_OPENCODE_SCRIPT" log_ok "Created auto-opencode script: $AUTO_OPENCODE_SCRIPT" } setup_graphical_autologin() { log_info "Setting up graphical auto-login (GDM)..." # Create GDM custom.conf for auto-login sudo mkdir -p /etc/gdm3 sudo tee /etc/gdm3/custom.conf > /dev/null < $HOME/.config/autostart/auto-opencode.desktop <