#!/usr/bin/env bash
# ssh-open — mở cửa sổ iTerm2 mới với 6 tab, mỗi tab ssh vào 1 host theo thứ tự.
# Màu tab do ~/.zshrc (hàm ssh() override) tự set khi ssh chạy trong tab.
# delay giữa các tab để iTerm kịp xử lý OSC đổi màu (tránh mất màu khi tạo tab liên tục).
set -euo pipefail

# Danh sách host theo thứ tự (alias từ ~/.ssh/config).
HOSTS=(ta vrc aws-vanhoa toc oc tk)

# Sinh AppleScript list: {"ta","vrc",...}
alist="$(printf '"%s",' "${HOSTS[@]}")"
alist="{${alist%,}}"

osascript <<APPLESCRIPT
tell application "iTerm"
    activate
    set theHosts to $alist
    set newWindow to (create window with default profile)
    delay 0.5
    tell newWindow
        -- Tab đầu: dùng tab gốc của cửa sổ mới.
        tell current session of current tab
            write text ("ssh " & item 1 of theHosts)
        end tell
        -- Các host còn lại: mỗi host 1 tab mới.
        repeat with i from 2 to (count of theHosts)
            set t to (create tab with default profile)
            delay 0.5
            tell current session of t
                write text ("ssh " & item i of theHosts)
            end tell
        end repeat
    end tell
end tell
APPLESCRIPT
