#!/bin/bash
set -eu

# Set HISTCONTROL to ignorespace
HISTCONTROL="ignorespace"

# Define the session name
SESSION_NAME="{{ session_name }}"
LOG_FILE="{{ log_file }}"
# Array of commands to run in separate windows
COMMANDS=(
    {% for command in commands %}
    "{{ command }}"
    {% endfor %}
)

# Start a new screen session (detached)
screen -dmS $SESSION_NAME

# Apply startup message and caption settings
screen -S $SESSION_NAME -X startup_message off
screen -S $SESSION_NAME -X title "$SESSION_NAME"
screen -S $SESSION_NAME -X hardstatus alwayslastline "%{.kW}%-w%{.gK}%n %t%{-}%+w %= %{..B}%H %LD %MM/%d %YY %c"

# Create and rename a window for each command
for i in "${!COMMANDS[@]}"; do
    sleep 0.1
    WINDOW_NAME="window-$i"
    if [ $i -eq 0 ]; then
        # For the first command, send it to the first window and rename it
        screen -S $SESSION_NAME -p 0 -X stuff "${COMMANDS[$i]}"$'\n'
        screen -S $SESSION_NAME -p 0 -X title $WINDOW_NAME
    else
        # For subsequent commands, create new tabs and run the commands
        screen -S $SESSION_NAME -X screen -t $WINDOW_NAME
        screen -S $SESSION_NAME -p $WINDOW_NAME -X stuff "${COMMANDS[$i]}"$'\n'
    fi
done

# Redirect screen log to a specific file
screen -S $SESSION_NAME -X logfile $LOG_FILE
screen -S $SESSION_NAME -X log on
