#!/usr/bin/env bash

[[ -d "./docs" ]] || (echo "Folder ./docs not found" && exit 1)

DOCS_FILE="./docs/SCRIPTS.md"
SCRIPTS_FOLDER="./home/scripts"

# search for every directory in the scripts folder:
# The docs for each scripts are in the beginning of the file prefixed by "#-" or "# -" for the title
# Inside the folders, the scripts are stored in default.nix files
echo "[//]: # (This file is autogenerated)" >"$DOCS_FILE"
echo "# Scripts" >>"$DOCS_FILE"
echo "" >>"$DOCS_FILE"
echo "Scripts are located in the 'home/scripts' folder. Home-manager add those in the user's path." >>"$DOCS_FILE"
echo "" >>"$DOCS_FILE"

for folder in "$SCRIPTS_FOLDER"/*; do
  script="$folder/default.nix"
  [[ -f "$script" ]] || continue

  content=$(cat "$script")
  content=$(echo "$content" | grep -E "^#-|^# -" | sed 's/^#- //;s/^# - //;s/#-//')

  echo "$content" >>"$DOCS_FILE"
  echo "" >>"$DOCS_FILE"
done
