PANE=$(flatpak run org.wezfurlong.wezterm cli list | grep syncopotamus | head -n 1 | awk '{print $3}')

CWD=~/repos/syncopotamus

if [ -n "${PANE}" ]; then
  # PANE=$(flatpak run org.wezfurlong.wezterm cli spawn --pane-id $PANE)
  echo "Opening Rails in existing window"
  flatpak run org.wezfurlong.wezterm cli set-tab-title --pane-id $PANE "Rails"
  echo "cd ~/repos/syncopotamus" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $PANE
  echo "clear && bin/dev" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $PANE
  VIM_PANE=$(flatpak run org.wezfurlong.wezterm cli split-pane --top --pane-id $PANE)
else
  # clear
  echo "Opening Rails in this window"
  flatpak run org.wezfurlong.wezterm cli set-tab-title "Rails"
  echo "cd ~/repos/syncopotamus" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste
  echo "clear && bin/dev" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste
  VIM_PANE=$(flatpak run org.wezfurlong.wezterm cli split-pane --top --cwd $CWD)
fi
#
echo "nvim" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $VIM_PANE
flatpak run org.wezfurlong.wezterm cli adjust-pane-size --amount 10 --pane-id $VIM_PANE down

CONSOLE_PANE=$(flatpak run org.wezfurlong.wezterm cli spawn --cwd $CWD --pane-id $VIM_PANE)
flatpak run org.wezfurlong.wezterm cli set-tab-title "Console" --pane-id $CONSOLE_PANE
echo "bin/jobs start" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $CONSOLE_PANE
CONSOLE_PANE=$(flatpak run org.wezfurlong.wezterm cli split-pane --top --cwd $CWD --pane-id $CONSOLE_PANE)
echo "rails c" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $CONSOLE_PANE
# flatpak run org.wezfurlong.wezterm cli adjust-pane-size --amount 10 --pane-id $CONSOLE_PANE down

# echo "clear && rails c" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $CONSOLE_PANE
# flatpak run org.wezfurlong.wezterm cli --activate-pane $VIM_PANE
flatpak run org.wezfurlong.wezterm cli --activate-tab 0

if [ "$1" == "web" ]; then
  firefox https://3.basecamp.com/5456546/buckets/34127086/card_tables/6496384013 &
  firefox --new-window http://localhost:4000 &
fi

# flatpak run org.wezfurlong.wezterm cli activate-tab --tab-index 0
flatpak run org.wezfurlong.wezterm cli activate-pane --pane-id $VIM_PANE
