···1+#!/bin/bash
2+3+# Script to generate network_slices_lexicons.txt from lexicon files
4+# This script finds all network.slices.* lexicon JSON files (record types only) and converts them
5+# to the format used in network_slices_lexicons.txt
6+#
7+# Purpose: In case the system lexicons are deleted, copy and paste these one by one into https://pdsls.dev/
8+# This allows restoring the lexicon definitions to the PDS.
9+# Note: This is not a common thing and only slices.network needs to worry about this right now.
10+11+set -e
12+13+LEXICONS_DIR="./lexicons"
14+OUTPUT_FILE="./network_slices_lexicons.txt"
15+SLICES_CONFIG="./slices.json"
16+17+# Check if required files/directories exist
18+if [ ! -d "$LEXICONS_DIR" ]; then
19+ echo "Error: lexicons directory not found at $LEXICONS_DIR"
20+ exit 1
21+fi
22+23+if [ ! -f "$SLICES_CONFIG" ]; then
24+ echo "Error: slices.json not found at $SLICES_CONFIG"
25+ exit 1
26+fi
27+28+# Extract slice URI from slices.json
29+SLICE_URI=$(jq -r '.slice' "$SLICES_CONFIG")
30+if [ "$SLICE_URI" = "null" ] || [ -z "$SLICE_URI" ]; then
31+ echo "Error: Could not extract slice URI from $SLICES_CONFIG"
32+ exit 1
33+fi
34+35+# Get current timestamp
36+CREATED_AT=$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")
37+38+# Clear output file
39+> "$OUTPUT_FILE"
40+41+# Counter for generated lexicons
42+COUNT=0
43+44+echo "Generating network.slices lexicons..."
45+46+# Find all network.slices lexicon files and process them
47+find "$LEXICONS_DIR" -path "*/network/slices/*" -name "*.json" -type f | sort | while read -r file; do
48+ # Extract the lexicon ID from the file content
49+ LEXICON_ID=$(jq -r '.id // empty' "$file" 2>/dev/null)
50+51+ # Skip if not a network.slices lexicon or if ID extraction failed
52+ if [ -z "$LEXICON_ID" ] || [[ ! "$LEXICON_ID" =~ ^network\.slices\. ]]; then
53+ continue
54+ fi
55+56+ # Extract definitions
57+ DEFINITIONS=$(jq -c '.defs' "$file" 2>/dev/null)
58+59+ if [ "$DEFINITIONS" = "null" ] || [ -z "$DEFINITIONS" ]; then
60+ echo "Warning: Could not extract definitions from $file"
61+ continue
62+ fi
63+64+ # Check if this is a record type (skip procedures and queries)
65+ MAIN_TYPE=$(jq -r '.defs.main.type // empty' "$file" 2>/dev/null)
66+ if [ "$MAIN_TYPE" != "record" ]; then
67+ continue
68+ fi
69+70+ # Create the output JSON object
71+ OUTPUT_JSON=$(jq -n \
72+ --arg nsid "$LEXICON_ID" \
73+ --arg type "network.slices.lexicon" \
74+ --arg slice "$SLICE_URI" \
75+ --arg createdAt "$CREATED_AT" \
76+ --argjson definitions "$DEFINITIONS" \
77+ '{
78+ "nsid": $nsid,
79+ "$type": $type,
80+ "slice": $slice,
81+ "createdAt": $createdAt,
82+ "definitions": ($definitions | tostring)
83+ }')
84+85+ # Append to output file with double newline separator
86+ if [ $COUNT -gt 0 ]; then
87+ echo "" >> "$OUTPUT_FILE"
88+ fi
89+ echo "$OUTPUT_JSON" >> "$OUTPUT_FILE"
90+91+ echo " - $LEXICON_ID"
92+ COUNT=$((COUNT + 1))
93+done
94+95+echo ""
96+echo "Generated $COUNT lexicon entries in $OUTPUT_FILE"