#!/bin/bash
#
# shellcheck shell=bash disable=SC2312
#
# ---
# see: https://gist.github.com/rtrouton/e698369d71bd3867db73b2d7a25c0d74
# also: https://gist.github.com/rtrouton/e698369d71bd3867db73b2d7a25c0d74

log_service_filename="$(date '+%Y%m%d%H%M%S')-logging-subsystem-report"
log_service_csv="$(mktemp -t "${log_service_filename}" | sed 's![^.]*$!!')csv"

# Directory containing the .plist files
log_service_config_dir="/System/Library/Preferences/Logging/Subsystems"

# First column for CSV with subsystem name
first_csv_col=$(ls "${log_service_config_dir}" \
  | sed -e 's/^[^\.]*\.[^\.]*\.//' -e 's/\.plist//')

# Second column for CSV with subsystem identifier
second_csv_col=$(ls "${log_service_config_dir}" | sed 's/\.plist//')

# Third column for CSV with subsystem configuration file location
third_csv_col=$(ls -d "${log_service_config_dir}"/*)

# Combine into CSV file
touch "${log_service_csv}"
printf "Logging Subsystem Name,Logging Subsystem Identifier,Configuration File Location\n" >>"${log_service_csv}"
/usr/bin/paste -d, <(echo "${first_csv_col}") <(echo "${second_csv_col}") <(echo "${third_csv_col}") >>"${log_service_csv}"

echo "Logging Subsystem Information CSV file available from: ${log_service_csv}"
