#!/bin/sh


for VERSION_PATH in $(find -L . -maxdepth 1 -type d -not -path '.'); do
  echo "Processing $VERSION_PATH"
  #jq -s '{ release: .[0].version_number, profiles: [.[] | .target as $target | .profiles | keys[] as $k | { id: ($k), titles: (.[$k] | .titles), target: $target }] }' $(find -L "$VERSION_PATH" -iname 'profiles.json') > $VERSION_PATH/.overview.json
  # Fix: in some dirs, there is an aggregated json already. Ignore that by setting a minimal depth
  jq -s '{ release: .[0].version_number, profiles: [.[] | .target as $target | .profiles | keys[] as $k | { id: ($k), titles: (.[$k] | .titles), target: $target }] }' $(find -L "$VERSION_PATH" -mindepth 4 -iname 'profiles.json') > $VERSION_PATH/.overview.json

# debugging print: uncomment and comment line above. Run script by piping stdout to /dev/null
#  for file in  $(find -L "$VERSION_PATH" -iname 'profiles.json'); do
#    # jq -s '{ release: .[0].version_number, profiles: [.[] | .target as $target | .profiles | keys[] as $k | { id: ($k), titles: (.[$k] | .titles), target: $target }] }' "$file"
#    # Fix: in some dirs, there is an aggregated json already. Ignore that by setting a minimal depth
#    jq -s '{ release: .[0].version_number, profiles: [.[] | .target as $target | .profiles | keys[] as $k | { id: ($k), titles: (.[$k] | .titles), target: $target }] }' $(find -L "$VERSION_PATH" -mindepth 4 -iname 'profiles.json') > $VERSION_PATH/.overview.json
#  done

done
