#!/bin/bash

## Copyright (C) 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

#set -x

set -e
shopt -s nullglob

error_handler() {
   local exit_code="$?"
   echo "ERROR: exit_code: $exit_code | BASH_COMMAND: $BASH_COMMAND"
   exit 1
}

trap error_handler ERR

SCRIPTNAME="$(basename "$BASH_SOURCE")"

print_profile(){
  if [ "$show" = "--show" ]; then
    echo "===================================================================="
    echo "BEGIN PROFILE: ${profile}"
    echo "--------------------------------------------------------------------"
    cat "${profile}"
    echo "--------------------------------------------------------------------"
    echo "END PROFILE: ${profile}"
    echo "===================================================================="
  else
    echo "${profile}"
  fi
}

usage(){
  echo "usage: ${0##*/}: --available|--used [--show]"
  exit 0
}

option="$1"
show="$2"

if [ "$option" = "--used" ]; then
  for profile in \
    /etc/onion-grater-merger.d/*.yml \
    /usr/local/etc/onion-grater-merger.d/*.yml
  do
    echo "${profile}" | grep -F "*" && continue
    print_profile
  done
elif [ "$option" = "--available" ]; then
  for profile in \
    /usr/share/doc/onion-grater-merger/examples/*.yml
  do
    echo "${profile}" | grep -F "*" && continue
    print_profile
  done
else
  usage
fi

