···11+#!/bin/bash
22+33+# Script to generate network_slices_lexicons.txt from lexicon files
44+# This script finds all network.slices.* lexicon JSON files (record types only) and converts them
55+# to the format used in network_slices_lexicons.txt
66+#
77+# Purpose: In case the system lexicons are deleted, copy and paste these one by one into https://pdsls.dev/
88+# This allows restoring the lexicon definitions to the PDS.
99+# Note: This is not a common thing and only slices.network needs to worry about this right now.
1010+1111+set -e
1212+1313+LEXICONS_DIR="./lexicons"
1414+OUTPUT_FILE="./network_slices_lexicons.txt"
1515+SLICES_CONFIG="./slices.json"
1616+1717+# Check if required files/directories exist
1818+if [ ! -d "$LEXICONS_DIR" ]; then
1919+ echo "Error: lexicons directory not found at $LEXICONS_DIR"
2020+ exit 1
2121+fi
2222+2323+if [ ! -f "$SLICES_CONFIG" ]; then
2424+ echo "Error: slices.json not found at $SLICES_CONFIG"
2525+ exit 1
2626+fi
2727+2828+# Extract slice URI from slices.json
2929+SLICE_URI=$(jq -r '.slice' "$SLICES_CONFIG")
3030+if [ "$SLICE_URI" = "null" ] || [ -z "$SLICE_URI" ]; then
3131+ echo "Error: Could not extract slice URI from $SLICES_CONFIG"
3232+ exit 1
3333+fi
3434+3535+# Get current timestamp
3636+CREATED_AT=$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")
3737+3838+# Clear output file
3939+> "$OUTPUT_FILE"
4040+4141+# Counter for generated lexicons
4242+COUNT=0
4343+4444+echo "Generating network.slices lexicons..."
4545+4646+# Find all network.slices lexicon files and process them
4747+find "$LEXICONS_DIR" -path "*/network/slices/*" -name "*.json" -type f | sort | while read -r file; do
4848+ # Extract the lexicon ID from the file content
4949+ LEXICON_ID=$(jq -r '.id // empty' "$file" 2>/dev/null)
5050+5151+ # Skip if not a network.slices lexicon or if ID extraction failed
5252+ if [ -z "$LEXICON_ID" ] || [[ ! "$LEXICON_ID" =~ ^network\.slices\. ]]; then
5353+ continue
5454+ fi
5555+5656+ # Extract definitions
5757+ DEFINITIONS=$(jq -c '.defs' "$file" 2>/dev/null)
5858+5959+ if [ "$DEFINITIONS" = "null" ] || [ -z "$DEFINITIONS" ]; then
6060+ echo "Warning: Could not extract definitions from $file"
6161+ continue
6262+ fi
6363+6464+ # Check if this is a record type (skip procedures and queries)
6565+ MAIN_TYPE=$(jq -r '.defs.main.type // empty' "$file" 2>/dev/null)
6666+ if [ "$MAIN_TYPE" != "record" ]; then
6767+ continue
6868+ fi
6969+7070+ # Create the output JSON object
7171+ OUTPUT_JSON=$(jq -n \
7272+ --arg nsid "$LEXICON_ID" \
7373+ --arg type "network.slices.lexicon" \
7474+ --arg slice "$SLICE_URI" \
7575+ --arg createdAt "$CREATED_AT" \
7676+ --argjson definitions "$DEFINITIONS" \
7777+ '{
7878+ "nsid": $nsid,
7979+ "$type": $type,
8080+ "slice": $slice,
8181+ "createdAt": $createdAt,
8282+ "definitions": ($definitions | tostring)
8383+ }')
8484+8585+ # Append to output file with double newline separator
8686+ if [ $COUNT -gt 0 ]; then
8787+ echo "" >> "$OUTPUT_FILE"
8888+ fi
8989+ echo "$OUTPUT_JSON" >> "$OUTPUT_FILE"
9090+9191+ echo " - $LEXICON_ID"
9292+ COUNT=$((COUNT + 1))
9393+done
9494+9595+echo ""
9696+echo "Generated $COUNT lexicon entries in $OUTPUT_FILE"