summaryrefslogtreecommitdiffstats
path: root/scripts/auto-opencode-setup.sh
blob: 0d5403282e10606cca445fdc2249905dbd883572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2026 Open-IOV contributors
# 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 <<EOF
Usage: $0 [enable|disable] [graphical|multi-user] [auto-login] [auto-opencode]

Configure auto-opencode session on login.

Commands:
  enable  - Enable auto-opencode
  disable - Disable auto-opencode
  status  - Show current configuration

Options:
  graphical|multi-user  - Boot target (default: graphical)
  auto-login            - Enable automatic login (graphical: GDM, multi-user: getty)
  auto-opencode         - Start opencode tmux session on login

Examples:
  $0 enable graphical auto-login auto-opencode
  $0 enable multi-user auto-opencode
  $0 disable
  $0 status
EOF
}

create_auto_opencode_script() {
    mkdir -p "$AUTO_OPENCODE_DIR"
    
    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 <<EOF
[daemon]
AutomaticLoginEnable=true
AutomaticLogin=${USER}
WaylandEnable=false

[security]

[xdmcp]

[chooser]

[debug]
EOF
    
    # Add auto-opencode to graphical session startup
    mkdir -p $HOME/.config/autostart
    cat > $HOME/.config/autostart/auto-opencode.desktop <<EOF
[Desktop Entry]
Type=Application
Name=Auto OpenCode
Exec=$HOME/.config/auto-opencode/auto-opencode.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
EOF
    
    log_ok "Graphical auto-login configured"
}

setup_multiuser_autologin() {
    log_info "Setting up multi-user auto-login (getty)..."
    
    # Create systemd override for getty@tty1
    sudo mkdir -p /etc/systemd/system/getty@tty1.service.d
    sudo tee /etc/systemd/system/getty@tty1.service.d/autologin.conf > /dev/null <<EOF
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin rmcore --noclear %I \$TERM
EOF
    
    # Add auto-opencode to .zprofile for multi-user login
    if ! grep -q "auto-opencode.sh" $HOME/.zprofile 2>/dev/null; then
        cat >> $HOME/.zprofile <<'EOF'

# Auto-opencode on login
if [[ -f $HOME/.config/auto-opencode/auto-opencode.sh ]]; then
    source $HOME/.config/auto-opencode/auto-opencode.sh
fi
EOF
    fi
    
    log_ok "Multi-user auto-login configured"
}

disable_autologin() {
    log_info "Disabling auto-login..."
    
    # Remove GDM auto-login
    if [[ -f /etc/gdm3/custom.conf ]]; then
        sudo sed -i '/^AutomaticLoginEnable=/d' /etc/gdm3/custom.conf
        sudo sed -i '/^AutomaticLogin=/d' /etc/gdm3/custom.conf
    fi
    
    # Remove getty auto-login
    sudo rm -f /etc/systemd/system/getty@tty1.service.d/autologin.conf
    
    # Remove autostart desktop entry
    rm -f $HOME/.config/autostart/auto-opencode.desktop
    
    # Remove from .zprofile
    sed -i '/auto-opencode.sh/d' $HOME/.zprofile 2>/dev/null || true
    
    log_ok "Auto-login disabled"
}

enable_auto_opencode() {
    local target="${1:-graphical}"
    local auto_login="${2:-false}"
    local auto_opencode="${3:-false}"
    
    log_info "Enabling auto-opencode (target: $target, auto-login: $auto_login, auto-opencode: $auto_opencode)"
    
    create_auto_opencode_script
    
    # Set target
    echo "$target" > "$TARGET_MARKER"
    
    # Set boot target
    sudo systemctl set-default "$target.target"
    log_ok "Boot target set to $target.target"
    
    # Handle auto-login
    if [[ "$auto_login" == "true" ]]; then
        touch "$AUTO_LOGIN_MARKER"
        if [[ "$target" == "graphical" ]]; then
            setup_graphical_autologin
        else
            setup_multiuser_autologin
        fi
    else
        rm -f "$AUTO_LOGIN_MARKER"
        disable_autologin
    fi
    
    # Handle auto-opencode
    if [[ "$auto_opencode" == "true" ]]; then
        touch "$AUTO_OPENCODE_MARKER"
        log_ok "Auto-opencode enabled"
    else
        rm -f "$AUTO_OPENCODE_MARKER"
        log_ok "Auto-opencode disabled"
    fi
    
    log_ok "Configuration complete"
}

disable_auto_opencode() {
    log_info "Disabling auto-opencode..."
    
    rm -f "$AUTO_OPENCODE_MARKER"
    rm -f "$AUTO_LOGIN_MARKER"
    rm -f "$TARGET_MARKER"
    
    disable_autologin
    
    # Remove auto-opencode script
    rm -f "$AUTO_OPENCODE_SCRIPT"
    
    log_ok "Auto-opencode disabled"
}

show_status() {
    echo "═══════════════════════════════════════════════════════════════"
    echo "  AUTO-OPENCODE STATUS"
    echo "═══════════════════════════════════════════════════════════════"
    
    if [[ -f "$AUTO_OPENCODE_MARKER" ]]; then
        echo "  Auto-opencode: ENABLED"
    else
        echo "  Auto-opencode: DISABLED"
    fi
    
    if [[ -f "$AUTO_LOGIN_MARKER" ]]; then
        echo "  Auto-login:    ENABLED"
    else
        echo "  Auto-login:    DISABLED"
    fi
    
    if [[ -f "$TARGET_MARKER" ]]; then
        echo "  Boot target:   $(cat "$TARGET_MARKER")"
    else
        echo "  Boot target:   $(systemctl get-default | sed 's/.target$//')"
    fi
    
    echo "  Current default: $(systemctl get-default)"
    echo "═══════════════════════════════════════════════════════════════"
}

main() {
    local action="${1:-status}"
    shift || true
    
    local target="graphical"
    local auto_login="false"
    local auto_opencode="false"
    
    # Parse remaining arguments
    for arg in "$@"; do
        case "$arg" in
            graphical|multi-user)
                target="$arg"
                ;;
            auto-login|auto-login=on)
                auto_login="true"
                ;;
            auto-login=off)
                auto_login="false"
                ;;
            auto-opencode|auto-opencode=on)
                auto_opencode="true"
                ;;
            auto-opencode=off)
                auto_opencode="false"
                ;;
            *)
                log_warn "Unknown argument: $arg"
                ;;
        esac
    done
    
    case "$action" in
        enable)
            enable_auto_opencode "$target" "$auto_login" "$auto_opencode"
            ;;
        disable)
            disable_auto_opencode
            ;;
        status)
            show_status
            ;;
        *)
            usage
            exit 1
            ;;
    esac
}

main "$@"