#!/usr/bin/env fish
# SPDX-License-Identifier: MIT
# Copyright (C) 2022-2023 Nathan Chancellor

function wezterm_open_remotes -d "Open a new wezterm tab for each remote machine I regularly use"
    set fish_path (command -v fish)
    set rg_path (command -v rg)
    set wezterm_path (command -v wezterm)

    if test ($wezterm_path cli list | count) != 2
        __print_error "wezterm appears to have more than one tab open?"
        return 1
    end

    # Local machines
    set hosts \
        hetzner:"🌎 Hetzner" \
        aadp:"🟪 AADP" \
        amd-desktop-8745HS:"🟥 AMD mini desktop" \
        intel-desktop-11700:"🟦 Intel i7-11700" \
        intel-laptop:"🟦 Intel laptop" \
        honeycomb:"🟪 Honeycomb" \
        intel-desktop-n100:"🟦 Intel mini desktop" \
        chromebox:"🟦 Chromebox"

    set msh_args $argv

    for item in $hosts
        set host (string split -f 1 : $item)
        set title (string split -f 2 : $item)
        $wezterm_path cli spawn -- $fish_path -c "__wezterm_title $title; msh $msh_args $host; exec $fish_path -l"
    end

    set mac_model (sysctl hw.model | string split -f2 ': ')
    switch $mac_model
        case Mac13,1
            set icon 🖥️
        case Mac14,2
            set icon 💻
    end
    $wezterm_path cli spawn -- $fish_path -c "__wezterm_title '$icon macOS'; exec $fish_path -l"
    if command -q orb; and orb list | $rg_path -q fedora
        $wezterm_path cli spawn -- $fish_path -c "__wezterm_title '$icon Fedora'; ssh orb; exec $fish_path -l"
    end
end
