#!/usr/bin/env bash
source $DOTFILES_PATH/bin/helpers/functions.sh

# Define associative array with app_name => command mapping
declare -A apps=(
  ["vesktop"]="vesktop"
  ["bitwarden"]="bitwarden"
  ["ktailctl"]="flatpak run org.fkoehler.KTailctl"
  ["ulauncher"]="ulauncher --no-window-shadow --hide-window"
)

# check if screen has any dead sessions
if screen -list | grep -q "Dead"; then
  screen -wipe
fi

echo "Starting auto-start applications..."
for app_name in "${!apps[@]}"; do
  command="${apps[$app_name]}"
  command_binary=$(echo $command | awk '{print $1}')

  if [ -x "$(command -v $command_binary)" ]; then
    if screen -list | grep -q $app_name; then
      printfe "%s\n" "yellow" "$app_name is already running. Skipping..."
      continue
    fi

    printfe "%s\n" "green" "Starting $app_name with command: $command..."
    screen -dmS $app_name $command
    sleep 1
  fi
done

screen -ls
