#!/bin/sh

# Yaroslav de la Peña Smirnov 2021
# 
# A quick script to quickly open several terminal windows, in a layout that I
# often use, all in the specified directory. The layout looks something like
# this:
# 
# ┌──────────────────┬──────────────────────────────────────────────┐
# │                  │                                              │
# │                  │                                              │
# │        lf        │                                              │
# │                  │                                              │
# │                  │                                              │
# ├──────────────────┤                                              │
# │                  │                    shell                     │
# │                  │                                              │
# │                  │                                              │
# │       shell      │                                              │
# │                  │                                              │
# │                  │                                              │
# │                  │                                              │
# └──────────────────┴──────────────────────────────────────────────┘

if [ -z "$1" ]; then
	echo "Specify working directory"
	exit 1
fi

displayw=$(swaymsg -rt get_outputs |\
	jq --raw-output '.[] | select(.focused == true) | .rect.width')
sidebarw=25

if [ $displayw -lt 2560 ]; then
	swaymsg "gaps inner current set 0; gaps outer current set 0"
	sidebarw=27
fi

cd "$1"
setsid $TERMINAL --class "$TERMINAL lf:$1" -e zsh -ic "lfcd" >/dev/null 2>&1 & sleep 0.1
setsid $TERMINAL --class "$TERMINAL edit:$1" >/dev/null 2>&1 & sleep 0.1

sleep 0.2
swaymsg "[app_id=\"$TERMINAL lf:$1\"] focus; resize set width ${sidebarw}ppt; split v"
setsid $TERMINAL >/dev/null 2>&1 & sleep 0.2
swaymsg "resize set height 60ppt" && sleep 0.1
swaymsg "[app_id=\"$TERMINAL edit:$1\"] focus"