1
+ #! /bin/bash
2
+
3
+ # Check if at least one input file is provided
4
+ if [ " $# " -lt 1 ]; then
5
+ echo " Usage: $0 <input_policies_file1> [input_policies_file2 ...]"
6
+ exit 1
7
+ fi
8
+
9
+ # Process each input file
10
+ for INPUT_FILE in " $@ " ; do
11
+ # Validate file extension
12
+ if [[ ! " $INPUT_FILE " =~ \. json$ ]]; then
13
+ echo " Error: Input file $INPUT_FILE must have a .json extension"
14
+ continue
15
+ fi
16
+
17
+ echo " Processing $INPUT_FILE ..."
18
+
19
+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " > /dev/null 2>&1 && pwd ) "
20
+ INPUT_FILE_WITHOUT_EXTENSION=" ${INPUT_FILE% .json} "
21
+ POLICIES_DIR=" $SCRIPT_DIR /../$INPUT_FILE_WITHOUT_EXTENSION "
22
+ HASHES_FILE=$( mktemp)
23
+
24
+ echo " Creating $POLICIES_DIR directory..."
25
+ mkdir -p $POLICIES_DIR
26
+
27
+ # Step 1: Create individual policy files and collect their hashes
28
+ echo " Creating individual policy files..."
29
+ echo " {" > " $HASHES_FILE "
30
+ first=true
31
+
32
+ jq -c ' .[]' " $INPUT_FILE " | while read -r policy; do
33
+ name=$( echo " $policy " | jq -r ' .name' | tr ' ' ' -' )
34
+ court=$( echo " $policy " | jq -r ' .court' )
35
+ policy_filepath=" $POLICIES_DIR /${name} -Policy.json"
36
+
37
+ # Remove the uri field if it exists and save to a temporary file
38
+ echo " $policy " | jq ' del(.uri)' > " $policy_filepath "
39
+
40
+ # Get IPFS hash
41
+ ipfs_hash=$( ipfs add -Q " $policy_filepath " )
42
+ if [ -n " $ipfs_hash " ]; then
43
+ echo " Preparing $name Court ($court ): ${name} -Policy.json"
44
+ # Add comma for all but the first entry
45
+ if [ " $first " = true ]; then
46
+ first=false
47
+ else
48
+ echo " ," >> " $HASHES_FILE "
49
+ fi
50
+ # Store the hash with court as key
51
+ echo " \" $court \" : \" $ipfs_hash \" " >> " $HASHES_FILE "
52
+ else
53
+ echo " Failed to get IPFS hash for ${name} -Policy.json"
54
+ rm " $HASHES_FILE "
55
+ continue 2
56
+ fi
57
+ done
58
+
59
+ echo " }" >> " $HASHES_FILE "
60
+
61
+ # Step 2: Update the input file with URIs
62
+ echo " Updating URIs in $INPUT_FILE ..."
63
+ jq --slurpfile hashes " $HASHES_FILE " '
64
+ map(. + {uri: ("/ipfs/" + ($hashes[0][.court | tostring]))})
65
+ ' " $INPUT_FILE " > " ${INPUT_FILE} .tmp" && mv " ${INPUT_FILE} .tmp" " $INPUT_FILE "
66
+
67
+ rm " $HASHES_FILE "
68
+ echo " Done! URIs updated in $INPUT_FILE "
69
+ echo " ----------------------------------------"
70
+ done
0 commit comments