#!/usr/bin/env bash

SRC_BASE="/home/nate/source"

DIRECTORIES=$(fd -a -L -d 1 -t d . $SRC_BASE -x basename {};)
DIR=$(echo "$DIRECTORIES" | wofi --style="$HOME"/.config/wofi/style.widgets.css --conf="$HOME"/.config/wofi/config.screenshot 2>/dev/null)
FULL_DIR=$(eval echo "$SRC_BASE/$DIR")

# Check if a directory is non empty
if [[ $(basename "$FULL_DIR") == $(basename "$SRC_BASE") ]]; then
  exit 0
fi

foot --working-directory="$FULL_DIR" >/dev/null 2>&1 &
sleep 0.15

FFLINKS_FILE="$FULL_DIR/.fflinks"

# If there is an .fflinks file
if [[ -f "$FFLINKS_FILE" ]]; then
  swaymsg "splitv"
  URLS=$(awk '{ printf "\"%s\" ", $0 }' $FFLINKS_FILE)
  echo "$URLS" | xargs firefox &
  sleep 0.15
  # firefox $URLS
fi

swaymsg "focus parent; splith"
foot --working-directory="$FULL_DIR" hx >/dev/null 2>&1 &
sleep 0.15

swaymsg "focus left; focus parent; resize shrink width 400px; focus right"
