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

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-rails" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $PANE
  echo "rvm use 3.3.1" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $PANE
  echo "rails s" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $PANE
  PANE=$(flatpak run org.wezfurlong.wezterm cli split-pane --left --pane-id $PANE)
else
  echo "Opening Rails in this window"
  flatpak run org.wezfurlong.wezterm cli set-tab-title "Rails"
  echo "cd ~/repos/syncopotamus-rails" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste
  echo "clear" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste
  echo "rvm use 3.3.1" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste
  echo "rails s" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste
  PANE=$(flatpak run org.wezfurlong.wezterm cli split-pane --left)
fi

echo "cd ~/repos/syncopotamus-rails" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $PANE
echo "nvim" | flatpak run org.wezfurlong.wezterm cli send-text --no-paste --pane-id $PANE
flatpak run org.wezfurlong.wezterm cli adjust-pane-size --amount 50 --pane-id $PANE right

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