File: access.gi

package info (click to toggle)
gap-atlasrep 2.1.9-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,688 kB
  • sloc: xml: 20,993; javascript: 155; makefile: 113; sh: 1
file content (3309 lines) | stat: -rw-r--r-- 114,810 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
#############################################################################
##
#W  access.gi            GAP 4 package AtlasRep                 Thomas Breuer
##
##  This file contains functions for accessing data from the ATLAS of Group
##  Representations.
##


#############################################################################
##
#F  AGR.InfoRead( <str1>, <str2>, ... )
##
AGR.InfoRead:= function( arg )
    local str;

    if UserPreference( "AtlasRep", "DebugFileLoading" ) = true then
      for str in arg do
        Print( str );
      od;
    fi;
    end;


#############################################################################
##
#F  AGR.StringFile( <filename> )
##
##  In unfortunate cases, files may contain line breaks of the form "\r\n"
##  instead of "\n".
##  'Read' would recognize this situation, and would silently replace these
##  line breaks, but 'StringFile' keeps the file contents.
##  Therefore we remove the '\r' characters.
##
AGR.StringFile:= function( filename )
    local str;

    AGR.InfoRead( "#I  reading `", filename, "' started\n" );
    str:= StringFile( filename );
    AGR.InfoRead( "#I  reading `", filename, "' done\n" );
    if IsString( str ) then
      str:= ReplacedString( str, "\r", "" );
    fi;

    return str;
    end;


#############################################################################
##
#F  AGR.ExtensionInfoCharacterTable
#F  AGR.HasExtensionInfoCharacterTable
#F  AGR.LibInfoCharacterTable
##
##  If the CTblLib package is not available then we cannot use these
##  functions.
##
if IsBound( ExtensionInfoCharacterTable ) then
  AGR.ExtensionInfoCharacterTable:= ExtensionInfoCharacterTable;
  AGR.HasExtensionInfoCharacterTable:= HasExtensionInfoCharacterTable;
  AGR.LibInfoCharacterTable:= LibInfoCharacterTable;
fi;


#############################################################################
##
#F  AGR.IsLowerAlphaOrDigitChar( <char> )
##
AGR.IsLowerAlphaOrDigitChar:= 
    char -> IsLowerAlphaChar( char ) or IsDigitChar( char );


#############################################################################
##
#F  AGR_ChecksumFits( <string>, <checksum> )
##
BindGlobal( "AGR_ChecksumFits", function( string, checksum )
    if checksum = fail then
      # We cannot check anything.
      return true;
    elif IsString( checksum ) then
      # This is a 'SHA256' format string.
      return checksum = ValueGlobal( "HexSHA256" )( string );
    elif IsInt( checksum ) then
      # This is a 'CrcString' value.
      return checksum = CrcString( string );
    else
      Error( "<chcksum> must be a string or an integer" );
    fi;
end );


#############################################################################
##
##  If the IO package is not available then the following assignments have
##  the effect that no warnings about unbound variables are printed when this
##  file gets read.
##
if not IsBound( IO_mkdir ) then
  IO_mkdir:= "dummy";
fi;
if not IsBound( IO_stat ) then
  IO_stat:= "dummy";
fi;
if not IsBound( IO_chmod ) then
  IO_chmod:= "dummy";
fi;


#############################################################################
##
#F  AtlasOfGroupRepresentationsTransferFile( <url>, <localpath>, <crc> )
##
##  This function encapsulates the access to the remote file at the address
##  <url>.
##  <P/>
##  If the access failed then <K>false</K> is returned, otherwise
##  either the data are written to the local file with filename
##  <A>localpath</A> (if this is a string and the user preference
##  <C>AtlasRepDataDirectory</C> is nonempty),
##  or a string with the contents of the file is returned.
##
BindGlobal( "AtlasOfGroupRepresentationsTransferFile",
    function( url, localpath, crc )
    local savetofile, pref, result, str, out;

    # Save the contents of the target file to a local file?
    # (The first two conditions mean that we *want* to avoid saving,
    # the third deals with the situation that the intended path does not
    # exist, probably due to missing write permissions.)
    savetofile:= not ( localpath = fail or
                       IsEmpty( UserPreference( "AtlasRep",
                                "AtlasRepDataDirectory" ) ) or
                       not IsWritableFile( localpath{ [ 1 .. Last( Positions(
                             localpath, '/' ) ) - 1 ] } ) );

    Info( InfoAtlasRep, 2,
          "calling 'Download' with url '", url, "'" );
    if savetofile then
      result:= Download( url, rec( target:= localpath ) );
    elif EndsWith( url, ".gz" ) then
      # We can only download the compressed file and then load it.
      if not IsBound( AGR.TmpDir ) then
        AGR.TmpDir:= DirectoryTemporary();
      fi;
      if AGR.TmpDir = fail then
        return false;
      fi;
      localpath:= Filename( AGR.TmpDir, "currentfile" );
      result:= Download( url, rec( target:= localpath ) );
      if result.success <> true then
        Info( InfoAtlasRep, 2,
              "Download failed" );
        RemoveFile( localpath );
        return false;
      fi;
      # Uncompress and load the contents.
      str:= StringFile( localpath );
      RemoveFile( localpath );
      if not AGR_ChecksumFits( str, crc ) then
        Info( InfoWarning, 1,
              "download of file '", url,
              "' does not yield a string with the expected crc value '",
              crc, "'" );
        return false;
      fi;
      return str;
    else
      # Transfer the file into the GAP session.
      result:= Download( url, rec() );
    fi;

    if result.success <> true then
      Info( InfoAtlasRep, 2,
            "Download failed with message\n#I  ", result.error );
      if savetofile and IsExistingFile( localpath ) then
        # This should not happen, 'Download' should have removed the file.
        if RemoveFile( localpath ) <> true then
          Error( "cannot remove corruped file '", localpath, "'" );
        fi;
      fi;
    elif savetofile and not AGR_ChecksumFits( StringFile( localpath ), crc ) then
      Info( InfoWarning, 1,
            "download of file '", url, "' to '", localpath,
            "' does not yield a file with the expected crc value '",
            crc, "'" );
      if RemoveFile( localpath ) <> true then
        Error( "cannot remove corruped file '", localpath, "'" );
      fi;
    elif not savetofile and not AGR_ChecksumFits( result.result, crc ) then
      Info( InfoWarning, 1,
            "download of file '", url,
            "' does not yield a string with the expected crc value '",
            crc, "'" );
    elif savetofile then
      # The file has been downloaded and stored and seems to be o.k.
      return true;
    else
      # The contents has been downloaded and seems to be o.k.
      return result.result;
    fi;

    return false;
end );


#############################################################################
##
#F  AGR.AccessFilesLocation( <files>, <type>, <replace>, <compressed> )
##
AGR.AccessFilesLocation:= function( files, type, replace, compressed )
#T type is not used at all!
    local names, pref, pair, dirname, filename, datadirs, info, entry,
          prefjson, name, namegz;

    names:= [];
    pref:= UserPreference( "AtlasRep", "AtlasRepDataDirectory" );
    if pref <> "" and not EndsWith( pref, "/" ) then
      pref:= Concatenation( pref, "/" );
    fi;
    for pair in files do
      dirname:= pair[1];
      filename:= pair[2];
      if dirname in [ "datagens", "dataword" ] then
        datadirs:= [ Directory( Concatenation( pref, dirname ) ) ];
      else
        datadirs:= fail;
        for info in AtlasOfGroupRepresentationsInfo.notified do
          if dirname = info.ID then
            if StartsWith( info.DataURL, "http" ) then
              # local directory of a remote data extension
              datadirs:= [ Directory(
                             Concatenation( pref, "dataext/", info.ID ) ) ];
            else
              # local data extension
              datadirs:= [ Directory( info.DataURL ) ];
              entry:= First( AtlasOfGroupRepresentationsInfo.filenames,
                             x -> x[1] = filename );
              if entry = fail then
                Error( "do not know about <filename>" );
              fi;
              filename:= entry[2];
            fi;
            break;
          fi;
        od;
        if datadirs = fail then
          Error( "no data extension with identifier '", dirname, "'" );
        fi;
      fi;

      if replace <> fail then
        filename:= ReplacedString( filename, replace[1], replace[2] );
      fi;

      # Hack/experimental:
      # If wanted then switch to a JSON format alternative of
      # characteristic zero matrices (supported only for "datagens").
      if dirname = "datagens" and
         ( PositionSublist( filename, "-Ar" ) <> fail or
           PositionSublist( filename, "-Zr" ) <> fail ) then
        prefjson:= UserPreference( "AtlasRep", "AtlasRepJsonFilesAddresses" );
        if prefjson <> fail then
          # Use Json format files of characteristic zero
          # matrix representations instead of the GAP format files.
          datadirs:= [ Directory( prefjson[2] ) ];
          filename:= ReplacedString( filename, ".g", ".json" );
        fi;
      fi;

      # There may be an uncompressed or a compressed version.
      # If both are available then prefer the uncompressed version.
      # Take the compressed version only if the program 'gunzip'
      # is available.
      name:= Filename( datadirs, filename );
      if name = fail or not IsReadableFile( name ) then
        if compressed and 
           Filename( DirectoriesSystemPrograms(), "gunzip" ) <> fail then
          namegz:= Filename( datadirs, Concatenation( filename, ".gz" ) );
          if namegz = fail then
            # No version is available yet.
            Add( names, Filename( datadirs[1], filename ) );
          else
            Add( names, namegz );
          fi;
        else
          # No version is available yet.
          Add( names, Filename( datadirs[1], filename ) );
        fi;
      else
        Add( names, name );
      fi;
    od;

    return names;
    end;


#############################################################################
##
#F  AGR.AccessFilesFetch( <filepath>, <filename>, <dirname>
#F                        <type>, <compressed>, <crc> )
##
##  We assume that the local file <filepath> is not yet available,
##  and that we have to download the file.
##
##  <filepath> is the local path where the file shall be stored if local
##  directories are writable (otherwise the content just gets downloaded),
##  <filename> is the name part of the file in question.
##  <dirname> is one of "datagens", "dataword", or a private id.
##  <type> is a type record.
##  <compressed> is 'true' or 'false'.
##  <crc> is either the expected crc value of the file or 'fail'.
##
##  The function returns 'false' if the access failed,
##  'true' if the remote file was copied to a local file,
##  and a string containing the contents of the file otherwise.
##
AGR.AccessFilesFetch:= function( filepath, filename, dirname,
                                 type, compressed, crc )
    local result, iscompressed, info, datadirs, pref, url, pos,
          gzip, gunzip;

    # Try to fetch the remote file.
    result:= fail;
    iscompressed:= false;
    if dirname in [ "datagens", "dataword" ] then
      # This is an 'official' file.
      dirname:= "core";
    fi;

    # The domain is described by the 'notified' list.
    # We are in the case of a remote extension.
    datadirs:= fail;
    for info in AtlasOfGroupRepresentationsInfo.notified do
      if dirname = info.ID then
        if not IsBound( info.data ) then
          # This should happen only for pure local extension, 
          Error( "non-available file <filepath> of a local extension?" );
        fi;

        # Fetch the file if possible.
        if EndsWith( filepath, ".json" ) and EndsWith( filename, ".g" ) then
          # Fetch the file from the address given by the user preference
          # 'AtlasRepJsonFilesAddresses'.
          filename:= filepath{ [ Last( Positions( filepath, '/' ) )+1
                                 .. Length( filepath ) ] };
          crc:= fail;
          pref:= UserPreference( "AtlasRep", "AtlasRepJsonFilesAddresses" );
          url:= pref[1];
        else
          # Use the standard addresses.
          url:= info.DataURL;
        fi;
        if not EndsWith( url, "/" ) then
          url:= Concatenation( url, "/" );
        fi;
        url:= Concatenation( url, filename );

        # First look for an uncompressed file.
        result:= AtlasOfGroupRepresentationsTransferFile( url,
                     filepath, crc );

        # In case of private MeatAxe text files
        # and if 'gunzip' is available,
        # look for a compressed version of the file.
        # (This is not supported for "core".)
        if result = false and compressed and dirname <> "core" then
          gunzip:= Filename( DirectoriesSystemPrograms(), "gunzip" );
          if gunzip <> fail and not IsExecutableFile( gunzip ) then
            gunzip:= fail;
          fi;
          if gunzip <> fail then
            result:= AtlasOfGroupRepresentationsTransferFile(
                         Concatenation( url, ".gz" ),
                         Concatenation( filepath, ".gz" ), fail );
            # If the file has been stored locally then it is compressed.
            # If the contents is stored in 'result' then it is *uncompressed*.
            if result = true then
              iscompressed:= true;
            fi;
          fi;
        fi;

        if result = false then
          Info( InfoAtlasRep, 1,
                "failed to transfer file '", url, "'" );
          return false;
        fi;

        break;
      fi;
    od;
    if dirname <> info.ID then
      Error( "no data extension with identifier '", dirname, "'" );
    fi;

    if result = true then
      # The contents has just been stored in a local file.
      # For MeatAxe text files, perform postprocessing:
      # If wanted and if the file is not yet compressed then compress it.
      if compressed and
         ( iscompressed = false ) and
         type[1] in [ "perm", "matff" ] and
         UserPreference( "AtlasRep", "CompressDownloadedMeatAxeFiles" ) = true
         then
        gzip:= Filename( DirectoriesSystemPrograms(), "gzip" );
        if gzip = fail or not IsExecutableFile( gzip ) then
          Info( InfoAtlasRep, 1, "no 'gzip' executable found" );
        else
          if not IsBound( gunzip ) then
            gunzip:= Filename( DirectoriesSystemPrograms(), "gunzip" );
            if gunzip <> fail and not IsExecutableFile( gunzip ) then
              gunzip:= fail;
            fi;
          fi;
          if gunzip <> fail then
            result:= Process( DirectoryCurrent(), gzip,
                         InputTextNone(), OutputTextNone(), [ filepath ] );
            if result = fail then
              Info( InfoAtlasRep, 2,
                    "impossible to compress file '", filepath, "'" );
            fi;
          fi;
        fi;
      fi;
    fi;

    return result;
    end;


#############################################################################
##
#F  AGR.AtlasDataGAPFormatFile2( <filename>[, "string"] )
##
##  This function is used for reading a GAP format file containing
##  a permutation or a matrix over a finite field.
##  The assignment to a global variable is avoided by reading a modified
##  version of the file.
##
AGR.AtlasDataGAPFormatFile2:= function( filename, string... )
    local str, pos, i;

    if Length( string ) = 0 then
      str:= AGR.StringFile( filename );
    else
      str:= filename;
    fi;
    pos:= PositionSublist( str, ":=" );
    if pos <> fail then
      str:= str{ [ pos + 2 .. Length( str ) ] };
    fi;
    i := InputTextString( Concatenation( "return ", str ) );
    i:= ReadAsFunction( i );
    if i <> fail then
      i:= i();
    fi;
    return i;
    end;


#############################################################################
##
#V  AtlasOfGroupRepresentationsAccessFunctionsDefault
##
##  several functions may be provided; return value 'fail' means that
##  the next function is tried, otherwise the result counts
##
InstallValue( AtlasOfGroupRepresentationsAccessFunctionsDefault, [
  rec(
    description:= "download/read MeatAxe text files (default)",

    location:= function( files, type )
      return AGR.AccessFilesLocation( files, type, fail, true );
    end,

    fetch:= function( filepath, filename, dirname, type, crc )
      return AGR.AccessFilesFetch( filepath, filename, dirname, type, true, crc );
    end,

    contents:= function( files, type, filepaths )
      local i;

      if not ( IsExistingFile( filepaths[1] ) or
               IsExistingFile( Concatenation( filepaths[1], ".gz" ) ) ) then
        # We have the file contents.
        return type[2].InterpretDefault( filepaths );
      else
        # We have the local filenames.
        filepaths:= ShallowCopy( filepaths );
        for i in [ 1 .. Length( filepaths ) ] do
          if EndsWith( filepaths[i], ".gz" ) then
            filepaths[i]:= filepaths[i]{ [ 1 .. Length( filepaths[i] )-3 ] };
          fi;
        od;
        return type[2].ReadAndInterpretDefault( filepaths );
      fi;
    end,
  ),

  rec(
    description:= "prefer downloading/reading MeatAxe binary files",

    location:= function( files, type )
      if ( not type[1] in [ "perm", "matff" ] ) or
         IsEmpty( UserPreference( "AtlasRep", "AtlasRepDataDirectory" ) ) then
        return fail;
      fi;

      # A list of file names is given, and the files are not compressed.
      # Replace the text format names by binary format names.
      return AGR.AccessFilesLocation( files, type, [ ".m", ".b" ], false );
    end,

    fetch:= function( filepath, filename, dirname, type, crc )
      # Replace the filename by that of the binary file.
      filename:= ReplacedString( filename, ".m", ".b" );
      filename:= ReplacedString( filename, "/mtx/", "/bin/" );
      return AGR.AccessFilesFetch( filepath, filename, dirname,
                                   type, false, fail );
    end,

    contents:= function( files, type, filepaths )
      # This function is called only for the types "perm" and "matff",
      # binary format files are *not* compressed,
      # and we are sure that we have the filenames not file contents.
      return List( filepaths, FFMatOrPermCMtxBinary );
    end,
  ),

  # GAP format files means:
  # one generator per file,
  # the first line containing an assignment to a global variable,
  # the last character being a semicolon
  rec(
    description:= "prefer downloading/reading GAP format files",

    location:= function( files, type )
      if not type[1] in [ "perm", "matff" ] then
        return fail;
      fi;

      # A list of file names is given, and the files are not compressed.
      # Replace the text format names by GAP format names.
      return AGR.AccessFilesLocation( files, type, [ ".m", ".g" ], false );
    end,

    fetch:= function( filepath, filename, dirname, type, crc )
      # Replace the filename by that of the GAP format file.
      filename:= ReplacedString( filename, ".m", ".g" );
      filename:= ReplacedString( filename, "/mtx/", "/gap/" );
      return AGR.AccessFilesFetch( filepath, filename, dirname,
                                   type, false, fail );
    end,

    contents:= function( files, type, filepaths )
      # This function is called only for the types "perm" and "matff",
      # and GAP format files are *not* compressed.
      if not ( IsExistingFile( filepaths[1] ) or
               IsExistingFile( Concatenation( filepaths[1], ".gz" ) ) ) then
        # We have the file contents.
        return List( filepaths,
                     str -> AGR.AtlasDataGAPFormatFile2( str, "string" ) );
      else
        # We have the local filenames.
        return List( filepaths, AGR.AtlasDataGAPFormatFile2 );
      fi;
    end,
  ),

  rec(
    # This applies only to the "core" data, not to extensions.
    description:= "prefer reading files available from a local server",

    location:= function( files, type )
      local localserverpath, names, pair, filename, info, name;

      # This is meaningful only for official data
      # and if there is a local server.
      localserverpath:= UserPreference( "AtlasRep",
                                        "AtlasRepLocalServerPath" );
      if localserverpath = "" then
        return fail;
      fi;

      names:= [];
      for pair in files do

        # Compose the remote filename.
        if not pair[1] in [ "datagens", "dataword" ] then
          return fail;
        fi;
        filename:= pair[2];
        info:= First( AtlasOfGroupRepresentationsInfo.filenames,
                      x -> x[1] = filename );
        if info = fail then
          Error( "do not know about <filename>" );
        fi;
        filename:= info[2];

        # Check whether the file(s) exist(s).
        name:= Concatenation( localserverpath, filename );
        if IsReadableFile( name ) then
          Add( names, name );
        else
          return fail;
        fi;
      od;

      return names;
    end,

    fetch:= function( filepath, filename, dirname, type, crc )
      # The 'location' function has checked that the file exists.
      return true;
    end,

    contents:= function( files, type, filepaths )
      # We need not care about compressed files,
      # and we know that we get filenames not file contents.
      return type[2].ReadAndInterpretDefault( filepaths );
    end,
  ),
  ] );


#############################################################################
##
#F  AtlasOfGroupRepresentationsLocalFilename( <files>, <type> )
##
InstallGlobalFunction( AtlasOfGroupRepresentationsLocalFilename,
    function( files, type )
    local pref, cand, r, paths;

    pref:= UserPreference( "AtlasRep", "FileAccessFunctions" );
    cand:= [];
    for r in Reversed( AtlasOfGroupRepresentationsInfo.accessFunctions ) do
      if r.description in pref then
        paths:= r.location( files, type );
        if paths <> fail then
          if ForAll( paths, IsReadableFile ) then
            # This has priority, do not consider other sources.
            cand:= [ [ r, List( paths, x -> [ x, true ] ) ] ];
            break;
          else
            Add( cand, [ r, List( paths, x -> [ x, IsReadableFile( x ) ] ) ] );
          fi;
        fi;
      fi;
    od;

    return cand;
end );


#############################################################################
##
#F  AtlasOfGroupRepresentationsLocalFilenameTransfer( <files>, <type> )
##
InstallGlobalFunction( AtlasOfGroupRepresentationsLocalFilenameTransfer,
    function( files, type )
    local cand, list, ok, result, fetchfun, i, filepath, filename, info,
          dirname, crc, res;

    # 1. Determine the local directory where to look for the file,
    #    and the functions that claim to be applicable.
    cand:= AtlasOfGroupRepresentationsLocalFilename( files, type );

    # 2. Check whether the files are already stored.
    #    (If yes then 'cand' has length 1.)
    if Length( cand ) = 1 and ForAll( cand[1][2], x -> x[2] ) then
      # 3. We have the local files.  Return paths and access functions.
      return [ List( cand[1][2], x -> x[1] ), cand[1][1] ];
    elif UserPreference( "AtlasRep", "AtlasRepAccessRemoteFiles" ) = true then
      # Try to fetch the remote files,
      # using the applicable methods.
      for list in cand do
        if Length( list[2] ) = Length( files ) then
          ok:= true;
          result:= [];
          fetchfun:= list[1].fetch;
          for i in [ 1 .. Length( files ) ] do
            if not list[2][i][2] then
              filepath:= list[2][i][1];
              filename:= files[i][2];
              info:= First( AtlasOfGroupRepresentationsInfo.filenames,
#T the list is ssorted; cheaper way!
                            x -> x[1] = filename );
              if info = fail then
                Error( "do not know about <filename>" );
              fi;
              filename:= info[2];
              dirname:= files[i][1];
              if IsBound( info[4] ) then
                crc:= info[4];
              else
                crc:= fail;
              fi;
              res:= fetchfun( filepath, filename, dirname, type, crc );
              if res = false then
                ok:= false;
              fi;
              Add( result, res );
            fi;
          od;
          if ok then
            # 3. We have either the local files or their contents.
            if result[1] = true then
              #  Return paths and the relevant record of access functions.
              return [ List( list[2], x -> x[1] ), list[1] ];
            else
              #  Return contents and the relevant record of access functions.
              return [ result, list[1] ];
            fi;
          fi;
        fi;
      od;
    fi;

    # The file cannot be made available.
    Info( InfoAtlasRep, 1,
          "no files '", files, "' found in the local directories" );
    return fail;
end );


#############################################################################
##
#F  AtlasOfGroupRepresentationsTestTableOfContentsRemoteUpdates()
##
InstallGlobalFunction(
    AtlasOfGroupRepresentationsTestTableOfContentsRemoteUpdates, function()
    local pref, version, inforec, home, result, lines,
          datadirs, line, pos, pos2, filename, localfile, servdate,
          stat;

    if not IsPackageMarkedForLoading( "io", "" ) then
      Info( InfoAtlasRep, 1, "the package IO is not available" );
      return fail;
    fi;

    # If the data directories do not yet exist then nothing is to do.
    pref:= UserPreference( "AtlasRep", "AtlasRepDataDirectory" );
    if not IsDirectoryPath( pref ) then
      return [];
    fi;

    # Download the file that lists the changes.
    version:= InstalledPackageVersion( "atlasrep" );
    inforec:= First( PackageInfo( "atlasrep" ), r -> r.Version = version );
    home:= inforec.PackageWWWHome;
    result:= AtlasOfGroupRepresentationsTransferFile(
                 Concatenation( home, "/htm/data/changes.htm" ), fail, fail );
    if result <> false then
      lines:= SplitString( result, "\n" );
      result:= [];
      lines:= Filtered( lines,
                  x ->     20 < Length( x ) and x{ [ 1 .. 4 ] } = "<tr>"
                       and x{ [ -3 .. 0 ] + Length( x ) } = " -->" );
      if pref <> "" and not EndsWith( pref, "/" ) then
        pref:= Concatenation( pref, "/" );
      fi;
      datadirs:= [ Directory( Concatenation( pref, "datagens" ) ),
                   Directory( Concatenation( pref, "dataword" ) ) ];
      for line in lines do
        pos:= PositionSublist( line, "</td><td>" );
        if pos <> fail then
          pos2:= PositionSublist( line, "</td><td>", pos );
          filename:= line{ [ pos+9 .. pos2-1 ] };
          localfile:= Filename( datadirs, filename );
          if localfile <> fail then
            if not IsReadableFile( localfile ) then
              localfile:= Concatenation( localfile, ".gz" );
            fi;
            if IsReadableFile( localfile ) then
              # There is something to compare.
              pos:= PositionSublist( line, "<!-- " );
              if pos <> fail then
                servdate:= Int( line{ [ pos+5 .. Length( line )-4 ] } );
                stat:= IO_stat( localfile );
                if stat <> fail then
                  if stat.mtime < servdate then
                    Add( result, localfile );
                  fi;
                fi;
              fi;
            fi;
          fi;
        fi;
      od;
      return result;
    fi;

    return [];
    end );


#############################################################################
##
#F  AGR.FileContents( <files>, <type> )
##
##  <files> must be a list of [ <dirname>, <filename> ] pairs,
##  where <dirname> is one of "dataword" or "datagens" or the ID of a
##  data extension.
##
AGR.FileContents:= function( files, type )
    local result;

    if not ( IsList( files ) and
             ForAll( files, l -> IsList( l ) and Length( l ) = 2
                                 and ForAll( l, IsString ) ) ) then
      Error( "<files> must be a list of [ <dirname>, <filename> ] pairs" );
    fi;

    # Perhaps the method in question rearranges the distribution into
    # one or several files.
    result:= AtlasOfGroupRepresentationsLocalFilenameTransfer( files, type );
    if result = fail then
      return fail;
    else
      # We have the local files or the contents of the files.
      # Extract and process the contents.
      return result[2].contents( files, type, result[1] );
    fi;
    end;


#############################################################################
##
#F  AGR.InfoForName( <gapname> )
##
AGR.InfoForName:= function( gapname )
    local pos;

    gapname:= AGR.GAPName( gapname );
    pos:= PositionSorted( AtlasOfGroupRepresentationsInfo.GAPnames,
                          [ gapname ] );
    if pos <= Length( AtlasOfGroupRepresentationsInfo.GAPnames ) and
       AtlasOfGroupRepresentationsInfo.GAPnames[ pos ][1] = gapname then
      return AtlasOfGroupRepresentationsInfo.GAPnames[ pos ];
    else
      return fail;
    fi;
    end;


#############################################################################
##
##  auxiliary function
##
AGR.TST:= function( gapname, value, compname, testfun, msg )
    if not IsBound( AGR.GAPnamesRec.( gapname ) ) then
      Error( "AGR.GAPnamesRec.( \"", gapname, "\" ) is not bound" );
    elif not IsBound( AGR.GAPnamesRec.( gapname )[3] ) then
      Error( "AGR.GAPnamesRec.( \"", gapname, "\" )[3] is not bound" );
    elif IsBound( AGR.GAPnamesRec.( gapname )[3].( compname ) ) then
      Error( "AGR.GAPnamesRec.( \"", gapname, "\" )[3].", compname,
             " is bound" );
    elif not testfun( value ) then
      Error( "<", compname, "> must be a ", msg );
    fi;
    end;


#############################################################################
##
#F  AGR.IsRepNameAvailable( <repname> )
##
##  If 'AtlasOfGroupRepresentationsInfo.checkData' is bound then this
##  function is called when additional data are added that refer to the
##  representation <repname>.
##
##  The data files themselves are *not* read by the function,
##  only the format of the filenames and the access with
##  'AllAtlasGeneratingSetInfos' are checked.
##
AGR.IsRepNameAvailable:= function( repname )
    local filenames, type, parsed, groupname, gapname;

    filenames:= [ Concatenation( repname, ".m1" ),
                  Concatenation( repname, ".g" ) ];
    for type in AGR.DataTypes( "rep" ) do
      parsed:= List( filenames,
          x -> AGR.ParseFilenameFormat( x, type[2].FilenameFormat ) );
      if ForAny( parsed, IsList ) then
        break;
      fi;
    od;
    if ForAll( parsed, IsBool ) then
      Print( "#E  wrong format of '", repname, "'\n" );
      return false;
    fi;
    groupname:= First( parsed, IsList )[1];
    gapname:= First( AtlasOfGroupRepresentationsInfo.GAPnames,
                     pair -> pair[2] = groupname );
    if gapname = fail then
      Print( "#E  no group name '", groupname, "' for '", repname, "'\n" );
      return false;
    elif ForAll( AllAtlasGeneratingSetInfos( gapname[1] ),
                 x -> x.repname <> repname ) then
      Print( "#E  no representation '", repname, "' available\n" );
      return false;
    fi;

    return true;
    end;


#############################################################################
##
#F  AGR.IsPrgNameAvailable( <prgname> )
##
##  If 'AtlasOfGroupRepresentationsInfo.checkData' is bound then this
##  function is called when additional data are added that refer to the
##  program <prgname>.
##
AGR.IsPrgNameAvailable:= function( prgname )
    local type, parsed, groupname;

    for type in AGR.DataTypes( "prg" ) do
      parsed:= AGR.ParseFilenameFormat( prgname, type[2].FilenameFormat );
      if IsList( parsed ) then
        break;
      fi;
    od;
    if parsed = fail then
      Print( "#E  wrong format of '", prgname, "'\n" );
      return false;
    fi;
    groupname:= parsed[1];
    if ForAny( AGR.TablesOfContents( "all" ),
           toc -> IsBound( toc.( groupname ) ) and
                  ForAny( RecNames( toc.( groupname ) ),
                      nam -> ForAny( toc.( groupname ).( nam ),
                                 l -> l[ Length( l ) ] = prgname ) ) ) then
      return true;
    else
      Print( "#E  no program '", prgname, "' available\n" );
      return false;
    fi;
    end;


#############################################################################
##
#V  AGR.MapNameToGAPName
#F  AGR.GAPName( <name> )
##
##  Let <name> be a string.
##  If 'LowercaseString( <name> )' is the lower case version of the GAP name
##  of an ATLAS group then 'AGR.GAPName' returns this GAP name.
##  If <name> is an admissible name of a GAP character table with identifier
##  <id> (this condition is already case insensitive) then 'AGR.GAPName'
##  returns 'AGR.GAPName( <id> )'.
##
##  These two conditions are forced to be consistent, as follows.
##  Whenever a GAP name <nam>, say, of an ATLAS group is notified with
##  'AGR.GNAN', we compute 'LibInfoCharacterTable( <nam> )'.
##  If this is 'fail' then there is no danger of an inconsistency,
##  and if the result is a record <r> then we have the condition
##  'AGR.GAPName( <r>.firstName ) = <nam>'.
##
##  So a case insensitive partial mapping from character table identifiers
##  to GAP names of ATLAS groups is built in 'AGR.GNAN',
##  and is used in 'AGR.GAPName'
##
##  Examples of different names for a group are '"F3+"' vs. '"Fi24'"'
##  and '"S6"' vs. '"A6.2_1"'.
##
AGR.MapNameToGAPName:= [ [], [] ];

AGR.GAPName:= function( name )
    local r, nname, pos;

    # Make sure that the file 'gap/types.g' is already loaded.
    IsRecord( AtlasOfGroupRepresentationsInfo );

    if IsBound( AGR.LibInfoCharacterTable ) then
      r:= AGR.LibInfoCharacterTable( name );
    else
      r:= fail;
    fi;
    if r = fail then
      nname:= LowercaseString( name );
    else
      nname:= r.firstName;
    fi;
    pos:= Position( AGR.MapNameToGAPName[1], nname );
    if pos = fail then
      return name;
    fi;
    return AGR.MapNameToGAPName[2][ pos ];
    end;


#############################################################################
##
#F  AGR.GAPNameAtlasName( <atlasname> )
##
##  Map the Atlas name <atlasname> to the corresponding GAP name.
##
AGR.GAPNameAtlasName:= function( atlasname )
    local entry;

    entry:= First( AtlasOfGroupRepresentationsInfo.GAPnames,
                   x -> x[2] = atlasname );
    if entry = fail then
      return fail;
    fi;
    return entry[1];
    end;


#############################################################################
##
#F  AGR.GNAN( <gapname>, <atlasname>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.GNAN">
##  <Mark><C>AGR.GNAN( </C><M>gapname, atlasname[, dirid]</M><C> )</C></Mark>
##  <Item>
##    Called with two strings <M>gapname</M> (the &GAP; name of the group)
##    and <M>atlasname</M> (the &ATLAS; name of the group),
##    <C>AGR.GNAN</C> stores the information in the list
##    <C>AtlasOfGroupRepresentationsInfo.GAPnames</C>,
##    which defines the name mapping between the &ATLAS;
##    names and &GAP; names of the groups.
##    <P/>
##    An example of a valid call is
##    <C>AGR.GNAN("A5.2","S5")</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.GNAN:= function( gapname, atlasname, dirid... )
    local value, r, pos;

    # Get the arguments.
    if Length( dirid ) = 1 and IsString( dirid[1] ) then
      dirid:= dirid[1];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      if   ForAny( AtlasOfGroupRepresentationsInfo.GAPnames,
                   pair -> gapname = pair[1] ) then
        Error( "cannot notify '", gapname, "' more than once" );
      elif ForAny( AtlasOfGroupRepresentationsInfo.GAPnames,
                   pair -> atlasname = pair[2] ) then
        Error( "ambiguous GAP names for ATLAS name '", atlasname, "'" );
      fi;
    fi;

    # Make the character table names admissible.
    if IsBound( AGR.LibInfoCharacterTable ) then
      r:= AGR.LibInfoCharacterTable( gapname );
    else
      r:= fail;
    fi;
    if r = fail then
      # Store the lowercase name.
      Add( AGR.MapNameToGAPName[1], LowercaseString( gapname ) );
      Add( AGR.MapNameToGAPName[2], gapname );
    elif not r.firstName in AGR.MapNameToGAPName[1] then
      Add( AGR.MapNameToGAPName[1], r.firstName );
      Add( AGR.MapNameToGAPName[2], gapname );
    else
      Error( "<gapname> is not compatible with CTblLib" );
    fi;

    value:= [ gapname, atlasname,
              rec(),
              rec( GNAN:= dirid ) ];
    AddSet( AtlasOfGroupRepresentationsInfo.GAPnames, value );
    AGR.GAPnamesRec.( gapname ):= value;
    end;


#############################################################################
##
#F  AGR.TOC( <typename>, <filename>, <crc>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.TOC">
##  <Mark><C>AGR.TOC( </C><M>typename, filename, crc[, dirid]</M><C> )</C></Mark>
##  <Item>
##    <C>AGR.TOC</C> notifies an entry to the
##    <C>TableOfContents.( </C><M>dirid</M><C> )</C>
##    component of <Ref Var="AtlasOfGroupRepresentationsInfo"/>.
##    The string <M>typename</M> must be the name of the data type
##    to which the entry belongs,
##    the string <M>filename</M> must be the prefix of the data file(s), and
##    <M>crc</M> must be a list that contains the checksums of the data files,
##    which are either integers (see <Ref BookName="ref" Func="CrcFile"/>)
##    or strings (see <C>HexSHA256</C>).
##    In particular, the number of files that are described by the entry
##    equals the length of <M>crc</M>.
##    <P/>
##    The optional argument <M>dirid</M> is equal to the argument with the
##    same name in the corresponding call of
##    <Ref Func="AtlasOfGroupRepresentationsNotifyData"
##    Label="for a local file describing private data"/>.
##    If no <M>dirid</M> argument is given then the current value of
##    <C>AGR.DIRID</C> is taken as the default;
##    this value is set automatically before a <F>toc.json</F> file
##    gets evaluated by
##    <Ref Func="AtlasOfGroupRepresentationsNotifyData"
##    Label="for a local file describing private data"/>,
##    and is reset afterwards.
##    If <C>AGR.DIRID</C> is not bound and <M>dirid</M> is not given then
##    this function has no effect.
##    <P/>
##    An example of a valid call is
##    <C>AGR.TOC("perm","alt/A5/mtx/S5G1-p5B0.m", [-3581724,115937465])</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.TOC:= function( arg )
    local type, crc, n, string, dirid, t, record, filename, pos, entry,
          groupname, added, j, filenamex, stringx;

    # Get the arguments.
    type:= arg[1];
    crc:= arg[3];
    n:= Length( crc );
    string:= arg[2];
    if Length( arg ) = 4 and IsString( arg[4] ) then
      dirid:= arg[4];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    # Parse the filename with the given format info.
    type:= First( AGR.DataTypes( "rep", "prg" ), x -> x[1] = type );
    record:= AtlasTableOfContents( dirid, "all" );

    # Split the name into path and filename.
    filename:= string;
    pos:= Position( filename, '/' );
    while pos <> fail do
      filename:= filename{ [ pos+1 .. Length( filename ) ] };
      pos:= Position( filename, '/' );
    od;

    filenamex:= filename;
    stringx:= string;
    if 1 < n then
      filenamex:= Concatenation( filename, "1" );
      stringx:= Concatenation( string, "1" );
    fi;
    entry:= AGR.ParseFilenameFormat( filenamex, type[2].FilenameFormat );
    if entry = fail then
      Info( InfoAtlasRep, 1, "'", arg, "' is not a valid t.o.c. entry" );
      return;
    fi;
    Add( AtlasOfGroupRepresentationsInfo.newfilenames,
         Immutable( [ filenamex, stringx, dirid, crc[1] ] ) );

    # Get the list for the data in the record for the group name.
    groupname:= entry[1];
    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) and
       ForAll( AtlasOfGroupRepresentationsInfo.GAPnames,
               x -> x[2] <> groupname ) then
      Error( "'", groupname, "' is not a valid group name" );
    fi;
    if not IsBound( record.( groupname ) ) then
      record.( groupname ):= rec();
    fi;
    record:= record.( groupname );
    if not IsBound( record.( type[1] ) ) then
      record.( type[1] ):= [];
    fi;

    # Add the first filename.
    added:= type[2].AddFileInfo( record.( type[1] ), entry, filenamex );

    # Add the other filenames if necessary.
    if added then
      for j in [ 2 .. n ] do
        entry[ Length( entry ) ]:= j;
        filenamex:= Concatenation( filename, String( j ) );
        stringx:= Concatenation( string, String( j ) );
        added:= type[2].AddFileInfo( record.( type[1] ), entry, filenamex )
                and added;
        Add( AtlasOfGroupRepresentationsInfo.newfilenames,
             Immutable( [ filenamex, stringx, dirid, crc[j] ] ) );
      od;
    fi;

    if not added then
      Info( InfoAtlasRep, 1, "'", arg, "' is not a valid t.o.c. entry" );
    fi;
    end;


#############################################################################
##
#F  AGR.GRS( <gapname>, <size>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.GRS">
##  <Mark><C>AGR.GRS( </C><M>gapname, size[, dirid]</M><C> )</C></Mark>
##  <Item>
##    The integer <M>size</M> is stored as the order of the group with
##    &GAP; name <M>gapname</M>,
##    in <C>AtlasOfGroupRepresentationsInfo.GAPnames</C>.
##    <P/>
##    An example of a valid call is
##    <C>AGR.GRS("A5.2",120)</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.GRS:= function( gapname, size, dirid... )

    # Get the arguments.
    if Length( dirid ) = 1 and IsString( dirid[1] ) then
      dirid:= dirid[1];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      AGR.TST( gapname, size, "size", IsPosInt, "positive integer" );
    fi;
    AGR.GAPnamesRec.( gapname )[3].size:= size;
    AGR.GAPnamesRec.( gapname )[4].GRS:= dirid;
    end;


#############################################################################
##
#F  AGR.MXN( <gapname>, <nrMaxes>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.MXN">
##  <Mark><C>AGR.MXN( </C><M>gapname, nrMaxes[, dirid]</M><C> )</C></Mark>
##  <Item>
##    The integer <M>nrMaxes</M> is stored as the number of classes of
##    maximal subgroups of the group with &GAP; name <M>gapname</M>,
##    in <C>AtlasOfGroupRepresentationsInfo.GAPnames</C>.
##    <P/>
##    An example of a valid call is
##    <C>AGR.MXN("A5.2",4)</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.MXN:= function( gapname, nrMaxes, dirid... )

    # Get the arguments.
    if Length( dirid ) = 1 and IsString( dirid[1] ) then
      dirid:= dirid[1];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      AGR.TST( gapname, nrMaxes, "nrMaxes", IsPosInt, "positive integer" );
    fi;
    AGR.GAPnamesRec.( gapname )[3].nrMaxes:= nrMaxes;
    AGR.GAPnamesRec.( gapname )[4].MXN:= dirid;
    end;


#############################################################################
##
#F  AGR.MXO( <gapname>, <sizesMaxes>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.MXO">
##  <Mark><C>AGR.MXO( </C><M>gapname, sizesMaxes[, dirid]</M><C> )</C></Mark>
##  <Item>
##    The list <M>sizesMaxes</M> of subgroup orders of the classes of
##    maximal subgroups of the group with &GAP; name <M>gapname</M>
##    (not necessarily dense, in non-increasing order) is stored
##    in <C>AtlasOfGroupRepresentationsInfo.GAPnames</C>.
##    <P/>
##    An example of a valid call is
##    <C>AGR.MXO("A5.2",[60,24,20,12])</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.MXO:= function( gapname, sizesMaxes, dirid... )
    local i;

    # Get the arguments.
    if Length( dirid ) = 1 and IsString( dirid[1] ) then
      dirid:= dirid[1];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    # Entries of the form '0' mean unknown values.
    for i in [ 1 .. Length( sizesMaxes ) ] do
      if IsBound( sizesMaxes[i] ) and sizesMaxes[i] = 0 then
        Unbind( sizesMaxes[i] );
      fi;
    od;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      AGR.TST( gapname, sizesMaxes, "sizesMaxes",
          x -> IsList( x ) and ForAll( x, IsPosInt )
                           and IsSortedList( Reversed( Compacted( x ) ) ),
          "list of non-increasing pos. integers" );
    fi;
    AGR.GAPnamesRec.( gapname )[3].sizesMaxes:= sizesMaxes;
    AGR.GAPnamesRec.( gapname )[4].MXO:= dirid;
    end;


#############################################################################
##
#F  AGR.MXS( <gapname>, <structureMaxes>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.MXS">
##  <Mark><C>AGR.MXS( </C><M>gapname, structureMaxes[, dirid]</M><C> )</C></Mark>
##  <Item>
##    The list <M>structureMaxes</M> of strings describing the
##    structures of the maximal subgroups of the group with &GAP; name
##    <M>gapname</M> (not necessarily dense), is stored
##    in <C>AtlasOfGroupRepresentationsInfo.GAPnames</C>.
##    <P/>
##    An example of a valid call is
##    <C>AGR.MXS("A5.2",["A5","S4","5:4","S3x2"])</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.MXS:= function( gapname, structureMaxes, dirid... )
    local i;

    # Get the arguments.
    if Length( dirid ) = 1 and IsString( dirid[1] ) then
      dirid:= dirid[1];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    # Entries of the form '""' mean unknown values.
    for i in [ 1 .. Length( structureMaxes ) ] do
      if IsBound( structureMaxes[i] ) and structureMaxes[i] = "" then
        Unbind( structureMaxes[i] );
      fi;
    od;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      AGR.TST( gapname, structureMaxes, "structureMaxes",
          x -> IsList( x ) and ForAll( x, IsString ),
          "list of strings" );
    fi;
    AGR.GAPnamesRec.( gapname )[3].structureMaxes:= structureMaxes;
    AGR.GAPnamesRec.( gapname )[4].MXS:= dirid;
    end;


#############################################################################
##
#F  AGR.STDCOMP( <gapname>, <factorCompatibility>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.STDCOMP">
##  <Mark><C>AGR.STDCOMP( </C><M>gapname, factorCompatibility[, dirid]</M><C> )</C></Mark>
##  <Item>
##    The list <M>factorCompatibility</M> (with entries
##    the standardization of the group with &GAP; name <M>gapname</M> ,
##    the &GAP; name of a factor group,
##    the standardization of this factor group, and
##    <K>true</K> or <K>false</K>, indicating whether mapping the standard
##    generators for <M>gapname</M> to those of <M>factgapname</M> defines an
##    epimorphism) is stored
##    in <C>AtlasOfGroupRepresentationsInfo.GAPnames</C>.
##    <P/>
##    An example of a valid call is
##    <C>AGR.STDCOMP("2.A5.2",[1,"A5.2",1,true])</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.STDCOMP:= function( gapname, factorCompatibility, dirid... )

    # Get the arguments.
    if Length( dirid ) = 1 and IsString( dirid[1] ) then
      dirid:= dirid[1];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) and
       not ( IsList( factorCompatibility ) and
             Length( factorCompatibility ) = 4 and
             IsPosInt( factorCompatibility[1] ) and
             IsString( factorCompatibility[2] ) and
             IsPosInt( factorCompatibility[3] ) and
             IsBool( factorCompatibility[4] ) ) then
      Error( "<factorCompatibility> must be a suitable list" );
    fi;
    if not IsBound( AGR.GAPnamesRec.( gapname )[3].factorCompatibility ) then
      AGR.GAPnamesRec.( gapname )[3].factorCompatibility:= [];
    fi;
    Add( AGR.GAPnamesRec.( gapname )[3].factorCompatibility,
         Concatenation( factorCompatibility, [ dirid ] ) );
    end;


#############################################################################
##
#F  AGR.RNG( <repname>, <descr>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.RNG">
##  <Mark><C>AGR.RNG( </C><M>repname, descr[, dirid]</M><C> )</C></Mark>
##  <Item>
##    Called with two strings <M>repname</M> (denoting the name
##    of a file containing the generators of a matrix representation over a
##    ring that is not determined by the filename)
##    and <M>descr</M> (describing this ring <M>R</M>, say),
##    <C>AGR.RNG</C> adds the triple
##    <M>[ repname, descr, R ]</M>
##    to the list stored in the <C>ringinfo</C> component of
##    <Ref Var="AtlasOfGroupRepresentationsInfo"/>.
##    <P/>
##    An example of a valid call is
##    <C>AGR.RNG("A5G1-Ar3aB0","Field([Sqrt(5)])")</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.RNG:= function( repname, descr, args... )
    local len, dirid, data;

    # Get the arguments.
    len:= Length( args );
    if len in [ 1, 3 ] and IsString( args[ len ] ) then
      dirid:= args[ len ];
      args:= args{ [ 1 .. len-1 ] };
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      # Check that this representation really exists.
      if not AGR.IsRepNameAvailable( repname ) then
        return;
      fi;
    fi;

    data:= [ repname, descr, EvalString( descr ) ];
    Append( data, args );
    Add( data, dirid );

    if ForAny( AtlasOfGroupRepresentationsInfo.ringinfo,
               entry -> repname = entry[1] ) then
      Info( InfoAtlasRep, 1,
            "data '", data, "' cannot be notified more than once" );
    else
      Add( AtlasOfGroupRepresentationsInfo.ringinfo, data );
    fi;
    end;


#############################################################################
##
#F  AGR.TOCEXT( <atlasname>, <std>, <maxnr>, <files>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.TOCEXT">
##  <Mark><C>AGR.TOCEXT( </C><M>atlasname, std, maxnr, files[, dirid]</M><C> )</C></Mark>
##  <Item>
##    Called with <M>atlasname</M>,
##    the positive integers <M>std</M> (the standardization) and
##    <M>maxnr</M> (the number of the class of maximal subgroups), and
##    the list <M>files</M> (of filenames of straight line programs for
##    computing generators of the <M>maxnr</M>-th maximal subgroup, using
##    a straight line program for a factor group plus perhaps some straight
##    line program for computing kernel generators),
##    <C>AGR.TOCEXT</C> stores the information in
##    <C>AtlasOfGroupRepresentationsInfo.GAPnames</C>.
##    <P/>
##    An example of a valid call is
##    <C>AGR.TOCEXT("2A5",1,3,["A5G1-max3W1"])</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.TOCEXT:= function( groupname, std, maxnr, files, dirid... )
    local info;

    # Get the arguments.
    if Length( dirid ) = 1 and IsString( dirid[1] ) then
      dirid:= dirid[1];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      if not ( IsString( groupname ) and IsPosInt( std )
                                     and IsPosInt( maxnr )
                                     and IsList( files )
                                     and ForAll( files, IsString ) ) then
        Error( "not a valid t.o.c.ext entry" );
      elif ForAll( AtlasOfGroupRepresentationsInfo.GAPnames,
                   x -> x[2] <> groupname )  then
        Error( "'", groupname, "' is not a valid group name" );
      fi;

      # Check that the required programs really exist.
      # (We cannot check the availability of the required program for
      # computing kernel generators, since these programs will be notified
      # *after* the current call, in another directory.)
      if not AGR.IsPrgNameAvailable( files[1] ) then
        # The program for the max. subgroup of the factor is not available.
        Print( "#E  factor program required by '", groupname, "' and '",
               files, "' not available\n" );
        return;
      fi;
    fi;

    info:= First( AtlasOfGroupRepresentationsInfo.GAPnames,
                      x -> x[2] = groupname );
    if not IsBound( info[3].maxext ) then
      info[3].maxext:= [];
    fi;
    Add( info[3].maxext, [ std, maxnr, files, dirid ] );
    end;


#############################################################################
##
#F  AGR.API( <repname>, <info>[, <dirid>] )
##
##  <#GAPDoc Label="AGR.API">
##  <Mark><C>AGR.API( </C><M>repname, info[, dirid]</M><C> )</C></Mark>
##  <Item>
##    Called with the string <M>repname</M> (denoting the name of a
##    permutation representation)
##    and the list <M>info</M> (describing the point stabilizer of this
##    representation),
##    <C>AGR.API</C> binds the component <M>repname</M> of the record
##    <C>AtlasOfGroupRepresentationsInfo.permrepinfo</C> to a record that
##    describes the contents of <M>info</M>.
##    <P/>
##    <M>info</M> has the following entries.
##    <List>
##    <Item>
##      At position <M>1</M>, the transitivity is stored.
##    </Item>
##    <Item>
##      If the transitivity is zero then <M>info</M> has length two,
##      and the second entry is the list of orbit lengths.
##    </Item>
##    <Item>
##      If the transitivity is positive then <M>info</M> has length
##      four or five, and the second entry is the rank of the action.
##    </Item>
##    <Item>
##      If the transitivity is positive then the third entry is one of the
##      strings <C>"prim"</C>, <C>"imprim"</C>, denoting primitivity or not.
##    </Item>
##    <Item>
##      If the transitivity is positive then the fourth entry is either
##      the string <C>"???"</C> or a string that describes the structure of
##      the point stabilizer.
##      If the third entry is <C>"imprim"</C> then this description consists
##      of a subgroup part and a maximal subgroup part, separated by
##      <C>" &lt; "</C>.
##    </Item>
##    <Item>
##      If the third entry is <C>"prim"</C> then the fifth entry is either
##      the string <C>"???"</C>
##      or the number of the class of maximal subgroups
##      that are the point stabilizers.
##    </Item>
##    </List>
##    <P/>
##    An example of a valid call is
##    <C>AGR.API("A5G1-p5B0",[3,2,"prim","A4",1])</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.API:= function( repname, info, dirid... )
    local r;

    # Get the arguments.
    if Length( dirid ) = 1 and IsString( dirid[1] ) then
      dirid:= dirid[1];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      if IsBound( AtlasOfGroupRepresentationsInfo.permrepinfo.( repname ) ) then
        Error( "cannot notify '", repname, "' more than once" );
      fi;

      # Check that this representation really exists.
      if not AGR.IsRepNameAvailable( repname ) then
        return;
      fi;
    fi;

    # The component 'dirid' is used in 'StringOfAtlasTableOfContents'.
    r:= rec( transitivity:= info[1], dirid:= dirid );
    if info[1] = 0 then
      r.orbits:= info[2];
      r.isPrimitive:= false;
    else
      r.rankAction:= info[2];
      r.isPrimitive:= ( info[3] = "prim" );
      r.stabilizer:= info[4];
      if r.isPrimitive then
        r.maxnr:= info[5];
      fi;
    fi;

    AtlasOfGroupRepresentationsInfo.permrepinfo.( repname ):= r;
    end;


#############################################################################
##
#F  AGR.CHAR( <gapname>, <repname>, <char>, <pos>[, <charname>][, <dirid>] )
##
##  <#GAPDoc Label="AGR.CHAR">
##  <Mark><C>AGR.CHAR( </C><M>gapname, repname, char, pos[, charname[, dirid]]</M><C> )</C></Mark>
##  <Item>
##    Called with the strings <M>gapname</M>
##    and <M>repname</M> (denoting the name of the representation),
##    the integer <M>char</M> (the characteristic of the representation),
##    and <M>pos</M> (the position or list of positions of the irreducible
##    constituent(s)),
##    <C>AGR.CHAR</C> stores the information in
##    <C>AtlasOfGroupRepresentationsInfo.characterinfo</C>.
##    <P/>
##    A string describing the character can be entered as <M>charname</M>.
##    <P/>
##    If <M>dirid</M> is given but no <M>charname</M> is known then one can
##    enter <K>fail</K> as the fifth argument.
##    <P/>
##    An example of a valid call is
##    <C>AGR.CHAR("M11","M11G1-p11B0",0,[1,2],"1a+10a")</C>.
##  </Item>
##  <#/GAPDoc>
##
AGR.CHAR:= function( groupname, repname, char, pos, arg... )
    local charname, dirid, map;

    # Get the arguments.
    if Length( arg ) >= 1 then
      charname:= arg[1];
    else
      charname:= fail;
    fi;
    if Length( arg ) = 2 then
      dirid:= arg[2];
    elif IsBound( AGR.DIRID ) then
      dirid:= AGR.DIRID;
    else
      Info( InfoAtlasRep, 1, "'AGR.DIRID' is not bound" );
      return;
    fi;

    map:= AtlasOfGroupRepresentationsInfo.characterinfo;
    if not IsBound( map.( groupname ) ) then
      map.( groupname ):= [];
    fi;
    map:= map.( groupname );
    if char = 0 then
      char:= 1;
    fi;
    if not IsBound( map[ char ] ) then
      map[ char ]:= [ [], [], [], [] ];
    fi;
    map:= map[ char ];

    if IsBound( AtlasOfGroupRepresentationsInfo.checkData ) then
      # Check whether we have already a character for this representation.
      # (Two different representations with the same character are allowed.)
      if arg[2] in map[2] and map[1][ Position( map[2], repname ) ] <> pos then
        Error( "attempt to enter two different characters for ", arg[2] );
      fi;

      # Check that this representation really exists.
      if not AGR.IsRepNameAvailable( repname ) then
        return;
      fi;
    fi;

    Add( map[1], pos );
    Add( map[2], repname );
    Add( map[3], charname );
    Add( map[4], dirid );

    # The character information forms one global object.
    # It may belong to any t.o.c., and we would not consider the new entry
    # if the cached t.o.c. would be taken.
    AtlasOfGroupRepresentationsInfo.TOC_Cache:= rec();
    AtlasOfGroupRepresentationsInfo.TableOfContents.merged:= rec();
    end;


#############################################################################
##
#F  AGR.CompareAsNumbersAndNonnumbers( <nam1>, <nam2> )
##
##  This function is available as 'BrowseData.CompareAsNumbersAndNonnumbers'
##  if the Browse package is available.
##  But we must deal also with the case that this package is not available.
##
AGR.CompareAsNumbersAndNonnumbers:= function( nam1, nam2 )
    local len1, len2, len, digit, comparenumber, i;

    len1:= Length( nam1 );
    len2:= Length( nam2 );
    len:= len1;
    if len2 < len then
      len:= len2;
    fi;
    digit:= false;
    comparenumber:= 0;
    for i in [ 1 .. len ] do
      if nam1[i] in DIGITS then
        if nam2[i] in DIGITS then
          digit:= true;
          if comparenumber = 0 then
            # first digit of a number, or previous digits were equal
            if nam1[i] < nam2[i] then
              comparenumber:= 1;
            elif nam1[i] <> nam2[i] then
              comparenumber:= -1;
            fi;
          fi;
        else
          # if digit then the current number in 'nam2' is shorter,
          # so 'nam2' is smaller;
          # if not digit then a number starts in 'nam1' but not in 'nam2',
          # so 'nam1' is smaller
          return not digit;
        fi;
      elif nam2[i] in DIGITS then
        # if digit then the current number in 'nam1' is shorter,
        # so 'nam1' is smaller;
        # if not digit then a number starts in 'nam2' but not in 'nam1',
        # so 'nam2' is smaller
        return digit;
      else
        # both characters are non-digits
        if digit then
          # first evaluate the current numbers (which have the same length)
          if comparenumber = 1 then
            # nam1 is smaller
            return true;
          elif comparenumber = -1 then
            # nam2 is smaller
            return false;
          fi;
          digit:= false;
        fi;
        # now compare the non-digits
        if nam1[i] <> nam2[i] then
          return nam1[i] < nam2[i];
        fi;
      fi;
    od;

    if digit then
      # The suffix of the shorter string is a number.
      # If the longer string continues with a digit then it is larger,
      # otherwise the first digits of the number decide.
      if len < len1 and nam1[ len+1 ] in DIGITS then
        # nam2 is smaller
        return false;
      elif len < len2 and nam2[ len+1 ] in DIGITS then
        # nam1 is smaller
        return true;
      elif comparenumber = 1 then
        # nam1 is smaller
        return true;
      elif comparenumber = -1 then
        # nam2 is smaller
        return false;
      fi;
    fi;

    # Now the longer string is larger.
    return len1 < len2;
    end;


#############################################################################
##
#F  AGR.SetGAPnamesSortDisp()
##
##  Bind the component 'AtlasOfGroupRepresentationsInfo.GAPnamesSortDisp'.
##
AGR.SetGAPnamesSortDisp:= function()
    local list;

    list:= ShallowCopy( AtlasOfGroupRepresentationsInfo.GAPnames );
    SortParallel( List( list, x -> x[1] ), list,
                  AGR.CompareAsNumbersAndNonnumbers );
    AtlasOfGroupRepresentationsInfo.GAPnamesSortDisp:= list;
    end;


#############################################################################
##
#F  AGR.ParseFilenameFormat( <string>, <format> )
##
AGR.ParseFilenameFormat:= function( string, format )
    local result, i, res;

    string:= SplitString( string, "-" );
    if Length( string ) <> Length( format[1] ) then
      return fail;
    fi;
    result:= [];
    for i in [ 1 .. Length( string ) ] do

      # Loop over the '-' separated components.
      res:= format[2][i]( string[i], format[1][i] );
      if res = fail then
        return fail;
      fi;
      Append( result, res );

    od;
    return result;
    end;


#############################################################################
##
#F  AtlasDataGAPFormatFile( <filename>[, "string"] )
##
##  <ManSection>
##  <Func Name="AtlasDataGAPFormatFile" Arg='filename[, "string"]'/>
##
##  <Description>
##  Let <A>filename</A> be the name of a file containing the generators of a
##  representation in characteristic zero such that reading the file via
##  <Ref Func="ReadAsFunction" BookName="ref"</C> yields a record
##  containing the list of the generators and additional information.
##  Then <Ref Func="AtlasDataGAPFormatFile"/> returns this record.
##  </Description>
##  </ManSection>
##
BindGlobal( "AtlasDataGAPFormatFile", function( filename, string... )
    local fun;

    AGR.InfoRead( "#I  reading '", filename, "' started\n" );
    if Length( string ) = 0 then
      fun:= ReadAsFunction( filename );
    else
      fun:= ReadAsFunction( InputTextString( filename ) );
    fi;
    AGR.InfoRead( "#I  reading '", filename, "' done\n" );
    if fun = fail then
      Info( InfoAtlasRep, 1,
            "problem reading '", filename, "' as function\n" );
    else
      fun:= fun();
    fi;
    return fun;
end );


#############################################################################
##
#F  AtlasDataJsonFormatFile( <filename>[, "string"] )
##
##  Evaluate the contents of a Json format file that describes matrices
##  in characteristic zero.
##
##  Admit a prescribed ring for the matrix entries,
##  given by the 'givenRing' component of the info record stored in the
##  global option 'inforecord'.
##
BindGlobal( "AtlasDataJsonFormatFile", function( filename, string... )
    local obj, arec, givenF, info, F, pol, facts, gen, roots, ecoeffs, pos,
          found, N, B, nrows, ncols, mats, d, mat, i, j;

    if Length( string ) = 0 then
      obj:= AGR.GapObjectOfJsonText( StringFile( filename ) );
    else
      obj:= AGR.GapObjectOfJsonText( filename );
    fi;
    if obj.status = false then
      if Length( string ) = 0 then
        Info( InfoAtlasRep, 1,
              "file ", filename, " does not contain valid Json" );
      else
        Info( InfoAtlasRep, 1,
              "string <filename> does not contain valid Json" );
      fi;
      return fail;
    fi;
    arec:= obj.value;

    givenF:= ValueOption( "inforecord" );
    if givenF <> fail then
      if IsBound( givenF.givenRing ) then
        givenF:= givenF.givenRing;
      else
        givenF:= fail;
      fi;
    fi;

    info:= arec.ringinfo;

    if givenF = fail or IsCyclotomicCollection( givenF ) then
      # Create the default field extension in GAP.
      if Length( info ) = 1 and info[1] = "IntegerRing" then
        F:= Integers;
        gen:= 1;
      elif Length( info ) = 2 and info[1] = "QuadraticField" then
        F:= Field( Rationals, [ Sqrt( info[2] ) ] );
        pol:= UnivariatePolynomial( F, arec.polynomial );
        gen:= - Value( Factors( PolynomialRing( F ), pol )[1], 0 );
      elif Length( info ) = 2 and info[1] = "CyclotomicField" then
        F:= CyclotomicField( Rationals, info[2] );
        gen:= E( info[2] );
      elif Length( info ) = 2 and info[1] = "AbelianNumberField" then
        # Choose the unique root of the polynomial
        # that involves E(N) with the correct sign.
        # (This is of course a hack, but it makes the generators in GAP
        # backwards compatible with the old Magma and GAP format files.)
        F:= CyclotomicField( Rationals, info[2] );
        pol:= UnivariatePolynomial( F, arec.polynomial );
        roots:= List( Factors( PolynomialRing( F ), pol ),
                      x -> - Value( x, 0 ) );
        ecoeffs:= COEFFS_CYC( E( info[2] ) );
        pos:= PositionNonZero( ecoeffs );
        gen:= First( roots, x -> COEFFS_CYC( x )[ pos ] = ecoeffs[ pos ] );
        F:= Field( Rationals, [ gen ] );
      elif Length( info ) = 1 and info[1] = "NumberField" then
        # We have no good idea to guess the conductor.
        found:= false;
        pol:= UnivariatePolynomial( Rationals, arec.polynomial );
        for N in [ 3 .. 100 ] do
          F:= CyclotomicField( Rationals, N );
          facts:= Factors( PolynomialRing( F ), pol );
          if Length( facts ) = Degree( pol ) then
            roots:= List( facts, x -> - Value( x, 0 ) );
            ecoeffs:= COEFFS_CYC( E( info[2] ) );
            pos:= PositionNonZero( ecoeffs );
            gen:= First( roots, x -> COEFFS_CYC( x )[ pos ] = ecoeffs[ pos ] );
            F:= Field( Rationals, [ gen ] );
            found:= true;
            break;
          fi;
        od;
        if not found then
          Error( "did not find conductor" );
        fi;
      else
        Error( "invalid 'ringinfo'" );
      fi;
    else
      # Take a root of the given defining polynomial
      # over the given field extension as the primitive element.
      F:= givenF;
      if Length( arec.polynomial ) = 2 then
        # no proper extension, usually 'Integers'
        gen:= One( F );
      else
        pol:= UnivariatePolynomial( F, arec.polynomial * One( F ) );
        facts:= Filtered( Factors( PolynomialRing( F ), pol ),
                          x -> Degree( x ) = 1 );
        if Length( facts ) = 0 then
          Error( "the polynomial <pol> has no root in <F>" );
        fi;
        gen:= - Value( facts[1], 0 );
      fi;
    fi;

    B:= List( [ 0 .. Length( arec.polynomial )-2 ], i -> gen^i );

    nrows:= arec.dimensions[1];
    ncols:= arec.dimensions[2];

    mats:= [];
    d:= arec.denominator;
    for mat in arec.generators do
      pos:= 0;
      for i in [ 1 .. nrows ] do
        for j in [ 1 .. ncols ] do
          pos:= pos + 1;
          if IsList( mat[ pos ] ) then
            mat[ pos ]:= mat[ pos ] * B;
          fi;
        od;
      od;
      if givenF <> fail then
        mat:= mat * One( givenF );
      fi;
      if d <> 1 then
        mat:= mat / d;
      fi;
#T eventually support 'Matrix( F, mat, ncols )'
#T and given ConstructingFilter!
      Add( mats, List( [ 1 .. nrows ],
                       i -> mat{ [ (i-1)*ncols+1 .. i*ncols ] } ) );
    od;

    return rec( generators:= mats );
end );


#############################################################################
##
#F  AtlasStringOfFieldOfMatrixEntries( <mats> )
#F  AtlasStringOfFieldOfMatrixEntries( <filename> )
##
InstallGlobalFunction( AtlasStringOfFieldOfMatrixEntries, function( mats )
    local F, n, str;

    if IsString( mats ) then
      mats:= AtlasDataGAPFormatFile( mats ).generators;
    fi;

    if   IsCyclotomicCollCollColl( mats ) then
      F:= Field( Rationals, Flat( mats ) );
    elif ForAll( mats, IsQuaternionCollColl ) then
      F:= Field( Flat( List( Flat( mats ), ExtRepOfObj ) ) );
    else
      Error( "<mats> must be a matrix list of cyclotomics or quaternions" );
    fi;

    n:= Conductor( F );

    if DegreeOverPrimeField( F ) = 2 then

      # The field is quadratic,
      # so it is generated by 'Sqrt(n)' if $'n' \equiv 1 \pmod{4}$,
      # by 'Sqrt(-n)' if $'n' \equiv 3 \pmod{4}$,
      # and by one of 'Sqrt(n/4)', 'Sqrt(-n/4)' otherwise.
      if   n mod 4 = 1 then
        str:= Concatenation( "[Sqrt(", String( n ), ")]" );
      elif n mod 4 = 3 then
        str:= Concatenation( "[Sqrt(-", String( n ), ")]" );
      elif Sqrt( -n/4 ) in F then
        str:= Concatenation( "[Sqrt(-", String( n/4 ), ")]" );
      else
        str:= Concatenation( "[Sqrt(", String( n/4 ), ")]" );
      fi;

    elif IsCyclotomicField( F ) then

      # The field is not quadratic but cyclotomic.
      str:= Concatenation( "[E(", String( n ), ")]" );

    else
      str:= "";
    fi;

    if IsCyclotomicCollCollColl( mats ) then
      if str = "" then
        str:= String( F );
      else
        str:= Concatenation( "Field(", str, ")" );
      fi;
    elif F = Rationals then
      str:= "QuaternionAlgebra(Rationals)";
    elif str = "" then
      str:= Concatenation( "QuaternionAlgebra(", String( F ), ")" );
    else
      str:= Concatenation( "QuaternionAlgebra(", str, ")" );
    fi;

    return [ F, str ];
end );


#############################################################################
##
#F  AtlasOfGroupRepresentationsScanFilename( <name>, <result>, <dirid> )
##
BindGlobal( "AtlasOfGroupRepresentationsScanFilename",
    function( name, result, dirid )
    local filename, pos, type, format, groupname;

    # Replace the name of binary files by the corresponding MeatAxe filename.
    pos:= PositionSublist( name, ".b" );
    if pos <> fail then
      name:= ReplacedString( name, ".b", ".m" );
    fi;

    # Split the name into path and filename.
    filename:= name;
    pos:= Position( filename, '/' );
    while pos <> fail do
      filename:= filename{ [ pos+1 .. Length( filename ) ] };
      pos:= Position( filename, '/' );
    od;

    for type in AGR.DataTypes( "rep", "prg" ) do
      format:= AGR.ParseFilenameFormat( filename, type[2].FilenameFormat );
      if format <> fail then
        groupname:= format[1];
        if not IsBound( result.( groupname ) ) then
          result.( groupname ):= rec();
        fi;
        if not IsBound( result.( groupname ).( type[1] ) ) then
          result.( groupname ).( type[1] ):= [];
        fi;
        if type[2].AddFileInfo( result.( groupname ).( type[1] ),
                                format, filename ) then
          Add( AtlasOfGroupRepresentationsInfo.newfilenames,
               Immutable( [ filename, name, dirid ] ) );
          return true;
        else
          return false;
        fi;
      fi;
    od;

    # No type matches.
    return false;
end );


#############################################################################
##
#F  AtlasOfGroupRepresentationsComposeTableOfContents( <filelist>,
#F                                                     <gapnames>, <dirid> )
##
##  This code is used by 'AtlasTableOfContents'.
##
BindGlobal( "AtlasOfGroupRepresentationsComposeTableOfContents",
    function( filelist, gapnames, dirid )
    local result, name, len, groupname, record, type, listtosort;

    # Initialize the result record.
    result:= rec( otherfiles:= [] );

    # Deal with the case of 'gzip'ped files, and omit obvious garbage.
    for name in Set( filelist ) do
      len:= Length( name );
      if 3 <= len and name{ [ len-2 .. len ] } = ".gz" then
        name:= name{ [ 1 .. len-3 ] };
      fi;
      if AtlasOfGroupRepresentationsScanFilename( name, result, dirid )
         = false then
        if not ( name in [ "dummy", ".", "..", ".svn", "toc.g", "toc.json",
                           ".svnignore" ] or
                 name[ Length( name ) ] = '%' or
                 ( 3 <= Length( name )
                   and name{ Length( name ) + [ - 2 .. 0 ] } = "BAK" ) ) then
          Info( InfoAtlasRep, 3,
                "t.o.c. construction: ignoring name '", name, "'" );
          AddSet( result.otherfiles, name );
        fi;
      fi;
    od;

    # Postprocessing,
    # and *sort* the representations as given in the type definition.
    for groupname in List( gapnames, x -> x[2] ) do
      if IsBound( result.( groupname ) ) then

        record:= result.( groupname );
        for type in AGR.DataTypes( "rep", "prg" ) do
          if IsBound( record.( type[1] ) ) then

            type[2].PostprocessFileInfo( result, record );

            # Sort the data of the given type as defined.
            if IsBound( type[2].SortTOCEntries ) then
              listtosort:= List( record.( type[1] ), type[2].SortTOCEntries );
              SortParallel( listtosort, record.( type[1] ) );
            fi;

          fi;
        od;

      fi;
    od;

    # Store the current date in Coordinated Universal Time
    # (Greenwich Mean Time).
    result.lastupdated:= CurrentDateTimeString();

    return result;
end );


#############################################################################
##
#F  AtlasTableOfContents( <tocid>, <allorlocal>[, <body>] )
##
InstallGlobalFunction( AtlasTableOfContents,
    function( tocid, allorlocal, body... )
    local toc, id, groupnames, prefix, filenames, dirinfo, dstdir, dstfile,
          tocremote, typeinfo, groupname, r, pair, type, entry, fileinfo,
          filename, loc, dirname, privdir, f, comp, dir, result;

    # Take the stored version if it is already available.
    toc:= AtlasOfGroupRepresentationsInfo.TableOfContents;
    id:= Concatenation( tocid, "|", allorlocal );
    if IsBound( toc.( id ) ) then
      return toc.( id );
    fi;

    if not allorlocal in [ "all", "local" ] then
      Error( "<allorlocal> must be \"all\" or \"local\"" );
    fi;

    groupnames:= AtlasOfGroupRepresentationsInfo.GAPnames;
    prefix:= "";
    filenames:= [];

    # Fetch the information that was set during the notification.
    dirinfo:= First( AtlasOfGroupRepresentationsInfo.notified,
                     entry -> entry.ID = tocid );
    if dirinfo = fail then
      return fail;
    fi;

    if allorlocal = "all" then

      if IsBound( dirinfo.data ) then
        # This is a remote part of the database.
        # The contents are described by the record stored as the third entry.
        # (We need also the 'AGR.TOC' calls.)
        toc.( id ):= rec();
        AGR.DIRID:= tocid;
        for entry in dirinfo.data.Data do
          CallFuncList( AGR.( entry[1] ), entry[2] );
        od;
        Unbind( AGR.DIRID );
        toc.( id ).TocID:= tocid;
        return toc.( id );
      else
        # This is a local part of the database,
        # there may be a 'toc.json' file (evaluate it if available)
        # or 'body' contains its contents.
        if Length( body ) = 1 then
          body:= body[1];
        elif IsExistingFile( dirinfo.DataURL ) then
          privdir:= Directory( dirinfo.DataURL );
          filename:= Filename( privdir, "toc.json" );
          if not IsReadableFile( filename ) = true then
            return fail;
          fi;
          body:= AGR.StringFile( filename );
        else
          return fail;
        fi;

        # Store the hint for calls of 'AGR.API', 'AGR.CHR', 'AGR.TOC'.
        f:= AGR.GapObjectOfJsonText( body );
        if f.status = false then
          Error( "file <filename> does not contain valid JSON" );
        fi;
        f:= f.value;
        for comp in [ "Version", "SelfURL", "DataURL", "LocalDirectory" ] do
          if IsBound( f.( comp ) ) then
            dirinfo.( comp ):= f.( comp );
          fi;
        od;
        dirname:= dirinfo.DataURL;
        toc.( id ):= rec();
        AGR.DIRID:= tocid;
        if IsBound( f.DataURL ) then
          # Notify all files, not just the locally available ones.
          for entry in f.Data do
            CallFuncList( AGR.( entry[1] ), entry[2] );
          od;
          dirinfo.data:= f;
          Unbind( AGR.DIRID );
          toc.( id ).TocID:= tocid;
          return toc.( id );
        else
          # This is *only* locally available.
          # Rely on the locally available files.
          for entry in f.Data do
            # Omit 'AGR.TOC' lines that may be present.
            # Note that we are going to list the local directory contents.
            if entry[1] <> "TOC" then
              CallFuncList( AGR.( entry[1] ), entry[2] );
            fi;
          od;
          Unbind( AGR.DIRID );
        fi;

        if IsExistingFile( dirname ) then
          # List the information available in the given local directory.
          # (Up to one directory layer above the data files is supported.)
          for dir in Difference( DirectoryContents( dirname ),
                                 [ ".", "..", ".svn" ] ) do
            dstfile:= Filename( privdir, dir );
            if IsDirectoryPath( dstfile ) then
              Append( filenames,
                      List( Difference( DirectoryContents( dstfile ),
                                        [ ".", "..", ".svn" ] ),
                            x -> Concatenation( dir, "/", x ) ) );
            else
              Add( filenames, dir );
            fi;
          od;

          # Compose the result record.
          result:= AtlasOfGroupRepresentationsComposeTableOfContents( filenames,
                       groupnames, tocid );
          result.TocID:= tocid;

          # Store the newly computed table of contents.
          toc.( id ):= result;
          return result;

        fi;
      fi;

    else
      # First create the "all" variant, then filter it.
      tocremote:= AtlasTableOfContents( tocid, "all" );
      if tocid = "core" then
        typeinfo:= Concatenation(
                     List( AGR.DataTypes( "rep" ), x -> [ "datagens", x ] ),
                     List( AGR.DataTypes( "prg" ), x -> [ "dataword", x ] ) );
      else
        typeinfo:= List( AGR.DataTypes( "rep", "prg" ), x -> [ tocid, x ] );
      fi;

      for groupname in RecNames( tocremote ) do
        r:= tocremote.( groupname );
        if IsRecord( r ) then
          for pair in typeinfo do
            type:= pair[2];
            if IsBound( r.( type[1] ) ) then
              for entry in r.( type[1] ) do
                fileinfo:= entry[ Length( entry ) ];
                if IsString( fileinfo ) then
                  fileinfo:= [ fileinfo ];
                fi;
                loc:= AtlasOfGroupRepresentationsLocalFilename(
                          List( fileinfo, x -> [ pair[1], x ] ), type );
                if not IsEmpty( loc ) and ForAll( loc[1][2], x -> x[2] ) then
                  Append( filenames, fileinfo );
                fi;
              od;
            fi;
          od;
        fi;
      od;

      # Compose the result record.
      result:= AtlasOfGroupRepresentationsComposeTableOfContents( filenames,
                   groupnames, tocid );

      result.TocID:= tocid;

      # Store the newly computed table of contents.
      toc.( id ):= result;

      # Return the result record.
      return result;
    fi;

    return fail;
end );


#############################################################################
##
#F  StringOfAtlasTableOfContents( <inforec> )
##
InstallGlobalFunction( StringOfAtlasTableOfContents, function( inforec )
    local dirid, remote, prefix, open, close, toc, data, entry, name, val,
          entry2, reps, type, nam, r, line, map, i, j, list, comp, str,
          parskip;

    if IsString( inforec ) then
      r:= First( AtlasOfGroupRepresentationsInfo.notified,
                 x -> x.ID = inforec );
      if r = fail then
        Error( "if the argument is a string then it must be \"core\" ",
               "or the ID of a data extension" );
      elif IsBound( r.data ) then
        r:= r.data;
      fi;
      inforec:= rec( ID:= inforec );
      if IsBound( r.DataURL ) and StartsWith( r.DataURL, "http" ) then
        inforec.DataURL:= r.DataURL;
      fi;
      for comp in [ "Version", "SelfURL", "LocalDirectory" ] do
        if IsBound( r.( comp ) ) then
          inforec.( comp ):= r.( comp );
        fi;
      od;
    fi;

    if not IsRecord( inforec ) then
      Error( "<inforec> must be an ID string or a record" );
    elif not IsBound( inforec.ID ) then
      Error( "<inforec>.ID must be bound" );
    fi;

    dirid:= inforec.ID;
    if ForAll( AtlasOfGroupRepresentationsInfo.notified,
               x -> x.ID <> dirid ) then
      Error( "<inforec>.ID must be \"core\" ",
             "or the ID of a data extension" );
    fi;

    remote:= IsBound( inforec.DataURL ) and
             StartsWith( inforec.DataURL, "http" );

    prefix:= "[\"";
    open:= "\",[";
    close:= "]]";

    toc:= AGR.TablesOfContents( dirid )[1];

    data:= rec( GNAN:= [],
                GRS:= [],
                MXN:= [],
                MXO:= [],
                MXS:= [],
                TOC:= [],
                STDCOMP:= [],
                RNG:= [],
                TOCEXT:= [],
                API:= [],
                CHAR:= [],
              );

    # Collect the available information for this group.
    for entry in AtlasOfGroupRepresentationsInfo.GAPnames do

      if entry[4].GNAN = dirid then
        Add( data.GNAN, [ entry[1], Concatenation( "\"", entry[2], "\"" ) ] );
      fi;
      if IsBound( entry[4].GRS ) and entry[4].GRS = dirid then
        Add( data.GRS, [ entry[1], String( entry[3].size ) ] );
      fi;
      if IsBound( entry[4].MXN ) and entry[4].MXN = dirid then
        Add( data.MXN, [ entry[1], String( entry[3].nrMaxes ) ] );
      fi;
      if IsBound( entry[4].MXO ) and entry[4].MXO = dirid then
        # For the JSON format, replace each hole in the list by '0'.
        val:= ReplacedString( String( entry[3].sizesMaxes ), " ", "" );
        val:= ReplacedString( val, ",,", ",0," );
        val:= ReplacedString( val, ",,", ",0," );
        val:= ReplacedString( val, "[,", "[0," );
        Add( data.MXO, [ entry[1], val ] );
      fi;
      if IsBound( entry[4].MXS ) and entry[4].MXS = dirid then
        # For the JSON format, replace each hole in the list by '""'.
        val:= ReplacedString( String( entry[3].structureMaxes ), " ", "" );
        val:= ReplacedString( val, ",,", ",\"\"," );
        val:= ReplacedString( val, ",,", ",\"\"," );
        val:= ReplacedString( val, "[,", "[\"\"," );
        Add( data.MXS, [ entry[1], val ] );
      fi;
      if IsBound( entry[3].factorCompatibility ) then
        for entry2 in entry[3].factorCompatibility do
          if entry2[5] = dirid then
            Add( data.STDCOMP, [ entry[1],
              ReplacedString( String( entry2{ [ 1 .. 4 ] } ), " ", "" ) ] );
          fi;
        od;
      fi;
      if IsBound( entry[3].maxext ) then
        for entry2 in entry[3].maxext do
          if entry2[4] = dirid then
            Add( data.TOCEXT, [ entry[2],
              Concatenation(
                String( entry2[1] ), ",", String( entry2[2] ), ",",
                 ReplacedString( String( entry2[3] ), " ", "" ) ) ] );
          fi;
        od;
      fi;

      if remote and IsBound( toc.( entry[2] ) ) then
        # Collect the representations and straight line programs.
        reps:= toc.( entry[2] );
        for type in AGR.DataTypes( "rep", "prg" ) do
          if IsBound( reps.( type[1] ) ) then
            for entry in reps.( type[1] ) do
              if IsBound( type[2].TOCEntryString ) then
                val:= type[2].TOCEntryString( type[1], entry );
                if val = fail then
                  Info( InfoAtlasRep, 1,
                        "'TOCEntryString' for data type '", type[1], "'",
                        " returns 'fail'" );
                else
                  Add( data.TOC, type[2].TOCEntryString( type[1], entry ) );
                fi;
              else
                Info( InfoAtlasRep, 1,
                      "no component 'TOCEntryString' for data type '",
                      type[1], "'" );
              fi;
            od;
          fi;
        od;
      fi;

    od;

    for entry in AtlasOfGroupRepresentationsInfo.ringinfo do
      if entry[ Length( entry ) ] = dirid then
        if Length( entry ) = 4 then
          Add( data.RNG, [ entry[1], Concatenation( "\"", entry[2], "\"" ) ] );
        else
          # The length is 6.
          Add( data.RNG, [ entry[1], Concatenation( "\"", entry[2], "\"" ),
                           entry[4], entry[5] ] );
        fi;
      fi;
    od;

    for nam in RecNames( AtlasOfGroupRepresentationsInfo.permrepinfo ) do
      r:= AtlasOfGroupRepresentationsInfo.permrepinfo.( nam );
      if r.dirid = dirid then
        line:= "[";
        Append( line, String( r.transitivity ) );
        Append( line, "," );
        if r.transitivity = 0 then
          Append( line, ReplacedString( String( r.orbits ), " ", "" ) );
        else
          Append( line, String( r.rankAction ) );
          Append( line, "," );
          if r.isPrimitive then
            Append( line, "\"prim\"" );
          else
            Append( line, "\"imprim\"" );
          fi;
          Append( line, ",\"" );
          Append( line, r.stabilizer );
          Append( line, "\"" );
          if r.isPrimitive then
            Append( line, "," );
            if IsInt( r.maxnr ) then
              Append( line, String( r.maxnr ) );
            else
              Append( line, "\"" );
              Append( line, String( r.maxnr ) );
              Append( line, "\"" );
            fi;
          fi;
        fi;
        Append( line, "]" );

        Add( data.API, [ nam, line ] );
      fi;
    od;

    for nam in RecNames( AtlasOfGroupRepresentationsInfo.characterinfo ) do
      map:= AtlasOfGroupRepresentationsInfo.characterinfo.( nam );
      for i in [ 1 .. Length( map ) ] do
        if IsBound( map[i] ) then
          for j in [ 1 .. Length( map[i][1] ) ] do
            if map[i][4][j] = dirid then
              # This information belongs to the current t.o.c.
              line:= Concatenation( "\"", map[i][2][j], "\"," );
              if i = 1 then
                Append( line, "0" );
              else
                Append( line, String( i ) );
              fi;
              Append( line, "," );
              Append( line,
                      ReplacedString( String( map[i][1][j] ), " ", "" ) );
              if map[i][3][j] <> fail then
                Append( line, ",\"" );
                Append( line, map[i][3][j] );
                Append( line, "\"" );
              fi;
              Add( data.CHAR, [ nam, line ] );
            fi;
          od;
        fi;
      od;
    od;

    list:= [];
    for comp in [ "GNAN", "GRS", "MXN", "MXO", "MXS" ] do
      parskip:= "\n";
      Sort( data.( comp ) );
      for entry in data.( comp ) do
        Add( list, Concatenation( parskip, prefix, comp, open,
                       "\"", entry[1], "\",", entry[2], close ) );
        parskip:= "";
      od;
    od;

    # Sort the TOC entries by filename and type.
    SortParallel( List( data.TOC,
        x -> SplitString( x, "\"" ){ [ 4, 2 ] } ), data.TOC );
    parskip:= "\n";
    for entry in data.TOC do
      Add( list, Concatenation( parskip, prefix, "TOC", open, entry, close ) );
      parskip:= "";
    od;

    for comp in [ "STDCOMP", "RNG", "TOCEXT", "API", "CHAR" ] do
      parskip:= "\n";
      Sort( data.( comp ) );
      for entry in data.( comp ) do
        if Length( entry ) = 2 then
          Add( list, Concatenation( parskip, prefix, comp, open,
                         "\"", entry[1], "\",", entry[2], close ) );
        else
          Add( list, Concatenation( parskip, prefix, comp, open,
                         "\"", entry[1], "\",", entry[2], ",",
                         ReplacedString( String( entry[3] ), " ", "" ), ",",
                         ReplacedString( String( entry[4] ), " ", "" ),
                         close ) );
        fi;
        parskip:= "";
      od;
    od;

    str:= "{\n";
    Append( str, "\"ID\":\"" );
    Append( str, dirid );
    Append( str, "\",\n" );
    for comp in [ "Version", "DataURL", "SelfURL", "LocalDirectory" ] do
      if IsBound( inforec.( comp ) ) then
        Append( str, "\"" );
        Append( str, comp );
        Append( str, "\":\"" );
        Append( str, inforec.( comp ) );
        Append( str, "\",\n" );
      fi;
    od;
    Append( str, "\"Data\":[" );
    Append( str, JoinStringsWithSeparator( list, ",\n" ) );
    Append( str, "\n]\n}\n" );

    return str;
end );


#############################################################################
##
#F  AGR.TablesOfContents( <descr> )
##
##  Admissible arguments are
##  1 the string "all",
##  2 the string "local",
##  3 a string describing a table of contents
##    which occurs as the 'ID' component of an entry in
##    'AtlasOfGroupRepresentationsInfo.notified', or
##  4 a list of conditions such as [ <std>, "contents", <...> ]
##  5 a list of strings as 1-3.
##
AGR.TablesOfContents:= function( descr )
    local pos, tocid, allorlocal, label, tocs, entry;

    if descr = [] then
      descr:= [ "all" ];
    elif IsString( descr ) then
      descr:= [ descr ];
    elif not IsList( descr ) then
      Error( "<descr> must be a string or a list of strings/conditions" );
    fi;

    pos:= Position( descr, "contents" );
    if pos <> fail then
      # 'descr' is a list of conditions.
      # Evaluate only its "contents" part,
      # i. e., restrict the tables of contents, and remove this condition.
      # (Do not make a shallow copy!)
      tocid:= descr[ pos+1 ];
      Remove( descr, pos );
      Remove( descr, pos );
      if IsString( tocid ) then
        descr:= [ tocid ];
      else
        descr:= tocid;
      fi;
    elif ForAny( descr,
                 x -> x <> "all" and x <> "local"
                      and ForAll( AtlasOfGroupRepresentationsInfo.notified,
                                  entry -> x <> entry.ID ) ) then
      # 'descr' is a list of conditions that do not restrict the
      # table of contents.
      descr:= [ "all" ];
    fi;
    pos:= Position( descr, "local" );
    if pos <> fail then
      allorlocal:= "local";
      Remove( descr, pos );
      if descr = [] then
        descr:= [ "all" ];
      fi;
    else
      allorlocal:= "all";
    fi;

    # Now 'descr' is a list of identifiers of tables of contents.
    label:= JoinStringsWithSeparator(
                SortedList( Concatenation( [ allorlocal ], descr ) ), "|" );
    if not IsBound( AtlasOfGroupRepresentationsInfo.TOC_Cache.( label ) ) then
      tocs:= [];
      for entry in AtlasOfGroupRepresentationsInfo.notified do
        if "all" in descr or entry.ID in descr then
          Add( tocs, AtlasTableOfContents( entry.ID, allorlocal ) );
        fi;
      od;
      AtlasOfGroupRepresentationsInfo.TOC_Cache.( label ):= tocs;
    fi;
    return AtlasOfGroupRepresentationsInfo.TOC_Cache.( label );
end;


#############################################################################
##
#F  AGR.CreateLocalJSONFile( <dirid>, <body> )
##
##  Return either 'fail' or the path of the newly created file.
##
AGR.CreateLocalJSONFile:= function( dirid, body )
    local pref, datadir, filename;

    pref:= UserPreference( "AtlasRep", "AtlasRepDataDirectory" );
    if pref = "" then
      # We cannot (or do not want to) store local files.
      return fail;
    elif pref <> "" and not EndsWith( pref, "/" ) then
      pref:= Concatenation( pref, "/" );
    fi;
    datadir:= Concatenation( pref, "dataext/", dirid );
    if not IsDirectoryPath( datadir ) then
      # Create the subdirectory 'dirid' and set the mode 1023.
      # (Note that 'mkdir' does not guarantee the required mode,
      # so we call 'chmod' afterwards.)
      if not IsPackageMarkedForLoading( "IO", "" ) or
         IO_mkdir( datadir, 1023 ) <> true then
        Info( InfoAtlasRep, 1,
              "AtlasOfGroupRepresentationsNotifyData:\n",
              "#I  'IO_mkdir' cannot create the local directory\n",
              "#I  ", datadir );
        return fail;
      elif IO_chmod( datadir, 1023 ) <> true then
        Info( InfoAtlasRep, 1,
              "AtlasOfGroupRepresentationsNotifyData:\n",
              "#I  'IO_chmod' cannot set the mode of \n",
              "#I  ", datadir, " to 1023" );
        return fail;
      fi;
    fi;

    filename:= Filename( Directory( datadir ), "toc.json" );
    if IsReadableFile( filename ) then
      # A perhaps outdated version of the file is stored;
      # we did not know this in advance because 'dirid' was not given.
      # Compare the current version with the available one,
      # and update the local version if necessary.
      if AGR.StringFile( filename ) <> body then
        if FileString( filename, body ) = fail then
          Info( InfoAtlasRep, 1,
                "AtlasOfGroupRepresentationsNotifyData:\n",
                "#I  could not replace file\n",
                "#I  '", filename, "'" );
          return fail;
        else
          Info( InfoAtlasRep, 1,
                "AtlasOfGroupRepresentationsNotifyData:\n",
                "#I  replaced file\n",
                "#I  '", filename, "'" );
        fi;
      fi;
    elif FileString( filename, body ) = fail then
      Info( InfoAtlasRep, 1,
            "AtlasOfGroupRepresentationsNotifyData:\n",
            "#I  could not write local file\n",
            "#I  '", filename, "'" );
      return fail;
    else
      Info( InfoAtlasRep, 1,
            "AtlasOfGroupRepresentationsNotifyData:\n",
            "#I  wrote new file\n",
            "#I  '", filename, "'" );
    fi;

    return filename;
end;


#############################################################################
##
#F  AtlasOfGroupRepresentationsNotifyData( <dir>, <id>[, <test>] )
#F  AtlasOfGroupRepresentationsNotifyData( <filename>[, <id>][, <test>] )
#F  AtlasOfGroupRepresentationsNotifyData( <url>[, <id>][, <test>] )
#F  AtlasOfGroupRepresentationsNotifyData( <filecontents>[, <id>][, <test>] )
##
InstallGlobalFunction( AtlasOfGroupRepresentationsNotifyData,
    function( arg )
    local usage, test, dir, filename, url, firstarg, dirname, dirid, body, f,
          known, r, comp, localdir, pos, package, path, localdirs, pref,
          datadirs, p, datadir, oldtest, olddata, nam, entry,
          toc, allfilenames, RemovedDirectories, groupname, record, type,
          name, tocs, ok, oldtoc, list, i, names, unknown, value;

    # Get and check the arguments.
    usage:= Concatenation(
      "usage:\n",
      "AtlasOfGroupRepresentationsNotifyData( <dir>, <id>[, <test>] ),\n",
      "AtlasOfGroupRepresentationsNotifyData( <filename>[, <id>][, <test>] ),\n",
      "AtlasOfGroupRepresentationsNotifyData( <url>[, <id>][, <test>] ),\n",
      "AtlasOfGroupRepresentationsNotifyData( <filecontents>[, <id>][, <test>] )" );

    test:= false;
    if Length( arg ) = 0 then
      Error( usage );
    elif IsBool( arg[ Length( arg ) ] ) then
      test:= ( arg[ Length( arg ) ] = true );
      Remove( arg );
      if Length( arg ) = 0 then
        Error( usage );
      fi;
    fi;

    dir:= fail;
    filename:= fail;
    url:= fail;
    body:= fail;
    firstarg:= arg[1];
    if IsDirectory( firstarg ) then
      # first form, with directory
      dir:= firstarg;
      dirname:= ShallowCopy( dir![1] );
    elif IsString( firstarg ) then
      if StartsWith( firstarg, "~" ) then
        firstarg:= UserHomeExpand( firstarg );
      fi;
      if IsDirectoryPath( firstarg ) then
        # first form, with directory path
        dirname:= ShallowCopy( firstarg );
        dir:= Directory( dirname );
      elif IsReadableFile( firstarg ) then
        # second form
        filename:= firstarg;
      elif not StartsWith( firstarg, "{" ) then
        # third form
        url:= firstarg;
      else
        # fourth form
        body:= firstarg;
      fi;
    else
      Error( usage );
    fi;

    if Length( arg ) = 2 and IsString( arg[2] ) then
      dirid:= arg[2];
      if dirid = "local" then
        # "local" is reserved to restrict overviews.
        Error( "<dirid> must not be the string \"local\"" );
      elif ( dir <> fail or filename <> fail ) and
         ForAny( AtlasOfGroupRepresentationsInfo.notified,
                 entry -> entry.ID = dirid and
                          not StartsWith( entry.DataURL, "http" ) ) then
        # We have already notified this extension,
        # and it is a local one, thus we can ignore this notification.
        # (If we know the extension just as a remote one,
        # we can try to upgrade it to a local one.)
        return true;
      fi;
    elif Length( arg ) <> 1 then
      Error( usage );
    fi;

    if dir <> fail then

      # This is the first form:
      # A local directory contains some data files.

      if IsEmpty( dirname ) then
        Error( "<dirname> must not be empty" );
      elif dirname[ Length( dirname ) ] <> '/' then
        Add( dirname, '/' );
      fi;
      Add( AtlasOfGroupRepresentationsInfo.notified,
           rec( ID:= dirid, DataURL:= dirname ) );

    elif filename <> fail then

      # This is the second form:
      # A local JSON format file describes remote and/or local data.
      body:= AGR.StringFile( filename );
      if body = fail then
        Info( InfoAtlasRep, 1,
              "AtlasOfGroupRepresentationsNotifyData:\n",
              "#I  cannot read '", filename, "'" );
        return false;
      fi;
      f:= AGR.GapObjectOfJsonText( body );
      if f.status = false then
        Error( "file <filename> does not contain valid JSON" );
      fi;
      f:= f.value;
      if IsBound( dirid ) and dirid <> f.ID then
        Error( "'", dirid, "' differs from ID in file: '", filename, "'" );
      fi;
      dirid:= f.ID;
      known:= First( AtlasOfGroupRepresentationsInfo.notified,
                     entry -> entry.ID = dirid );
      if known <> fail then
        if known.ID = dirid and not StartsWith( known.DataURL, "http" ) then
          # We have already notified this extension as a local one.
          return true;
        elif dirid = "core" then
          return true;
        fi;
      fi;

      # The extension is either new or has been notified as a remote one
      # which may later be upgraded to a local one.
      if dirid = "core" then
        r:= rec( ID:= dirid, data:= f );
        for comp in [ "Version", "DataURL", "SelfURL", "LocalDirectory" ] do
          if IsBound( f.( comp ) ) then
            r.( comp ):= f.( comp );
          fi;
        od;
        Add( AtlasOfGroupRepresentationsInfo.notified, r );
        AtlasTableOfContents( dirid, "all" );
        return true;
      elif IsBound( f.LocalDirectory ) then
        # If the local directory is available then delegate to the first form.
        # The filename is interpreted relative to GAP's 'pkg' directory,
        # so we assume that the relevant package is already loaded.
        localdir:= f.LocalDirectory;
        pos:= Position( f.LocalDirectory, '/' );
        if pos = fail then
          Error( "local directory ", f.LocalDirectory,
                 " does not have the form <pkgname>/<path>" );
        fi;
        package:= f.LocalDirectory{ [ 1 .. pos-1 ] };
        if IsPackageMarkedForLoading( package, "" ) then
          path:= f.LocalDirectory{ [ pos+1 .. Length ( f.LocalDirectory ) ] };
          localdirs:= DirectoriesPackageLibrary( package, path );
          if not IsEmpty( localdirs ) and IsDirectory( localdirs[1] ) then
            # Do not delegate to the first form
            # because we need the components of 'f'.
            Info( InfoAtlasRep, 2,
                  "AtlasOfGroupRepresentationsNotifyData:\n",
                  "#I  use the local directory instead of the remote one ",
                  "for '", dirid, "'" );
            if known <> fail then
              # Remove the remote notification before we can upgrade to local.
              Info( InfoAtlasRep, 2,
                    "AtlasOfGroupRepresentationsNotifyData:\n",
                    "#I  first remove the remote notification" );
              AtlasOfGroupRepresentationsForgetData( dirid );
            fi;
            r:= rec( ID:= dirid, DataURL:= ShallowCopy( localdirs[1]![1] ) );
          fi;
        fi;
      fi;

      if not IsBound( r ) then
        # If we arrive here then 'LocalDirectory' cannot be used.
        # Create a copy of the JSON file in the local data directory
        # if possible.
        AGR.CreateLocalJSONFile( dirid, body );
        r:= rec( ID:= dirid, DataURL:= f.DataURL );
      fi;
      r.data:= f;
      for comp in [ "Version", "SelfURL", "LocalDirectory" ] do
        if IsBound( f.( comp ) ) then
          r.( comp ):= f.( comp );
        fi;
      od;
      Add( AtlasOfGroupRepresentationsInfo.notified, r );

    elif url <> fail then

      # This is the third form:
      # We know a URL where a JSON format file can be found
      # that describes remote and/or local data.
      # - If 'dirid' is given and we have already a local copy of this file
      #   then we delegate to the second form.
      # - Otherwise, if we are in online mode and can download this file
      #   then do this and then delegate to the fourth form.
      # - Otherwise we give up.

      pref:= UserPreference( "AtlasRep", "AtlasRepDataDirectory" );
      if pref <> "" and not EndsWith( pref, "/" ) then
        pref:= Concatenation( pref, "/" );
      fi;
      datadirs:= Directory( Concatenation( pref, "dataext" ) );
      if IsBound( dirid ) then
        datadirs:= Filename( datadirs, dirid );
        if IsReadableFile( datadirs ) and IsDirectoryPath( datadirs ) then
          filename:= Filename( Directory( datadirs ), "toc.json" );
          if IsReadableFile( filename ) then
            Info( InfoAtlasRep, 1,
                  "AtlasOfGroupRepresentationsNotifyData:\n",
                  "#I  use the locally available '", dirid, "/toc.json'\n",
                  "#I  instead of '", url, "'" );
            return AtlasOfGroupRepresentationsNotifyData(
                       filename, dirid, test );
          fi;
        fi;
      fi;

      if UserPreference( "AtlasRep", "AtlasRepAccessRemoteFiles" ) = true then
        # Try to fetch the file.
        body:= AtlasOfGroupRepresentationsTransferFile( url, fail, fail );
        if body = false then
          Info( InfoAtlasRep, 1,
                "AtlasOfGroupRepresentationsNotifyData:\n",
                "#I  cannot read '", url, "'" );
          return false;
        fi;

        f:= AGR.GapObjectOfJsonText( body );
        if f.status = false then
          Error( "file does not contain valid JSON: ", url );
        fi;
        f:= f.value;
        if not IsBound( f.ID ) then
          Error( "file does not contain ID: ", url );
        elif IsBound( dirid ) and dirid <> f.ID then
          Error( "'", dirid, "' differs from ID in file: ", url );
        elif not IsBound( f.DataURL ) then
          Error( "file does not contain DataURL: ", url );
        fi;
        dirid:= f.ID;
        if ForAny( AtlasOfGroupRepresentationsInfo.notified,
                   entry -> entry.ID = dirid and
                            not StartsWith( entry.DataURL, "http" ) ) then
          # We have already notified this extension as a local one.
          return true;
        fi;

        # Store a local copy of the file if it is not yet available
        # and if we are allowed/able to write local files;
        # it can then be used next time in offline mode.
        # If necessary then create a subdirectory in 'dataext'.
        filename:= AGR.CreateLocalJSONFile( dirid, body );

        # Delegate to the fourth form.
        return AtlasOfGroupRepresentationsNotifyData( body, dirid, test );

      elif IsBound( dirid ) then
        Info( InfoAtlasRep, 2,
              "AtlasOfGroupRepresentationsNotifyData:\n",
              "#I  cannot notify new data extension in offline mode,\n",
              "#I  local directory '", dirid, "'\n",
              "#I  is not available" );
      else
        Info( InfoAtlasRep, 2,
              "AtlasOfGroupRepresentationsNotifyData:\n",
              "#I  cannot notify new data extension via\n",
              "#I  '", url, "'\n",
              "#I  in offline mode" );
      fi;

      # Give up.
      return false;

    fi;

    # Now we are in the first, second, or fourth form.
    # It remains to evaluate the JSON file.
    # The file may contain calls of 'AGR.GNAN' (which must come *before* the
    # data for the groups in question can be notified) and of 'AGR.API' and
    # 'AGR.CHAR' (which must come *afterwards*).
    # So we must postpone the calls of 'AGR.IsRepNameAvailable'.
    # If the private files are not stored locally then the file must contain
    # also the relevant 'AGR.TOC' calls.
    oldtest:= IsBound( AtlasOfGroupRepresentationsInfo.checkData );
    Unbind( AtlasOfGroupRepresentationsInfo.checkData );
    olddata:= rec(
      permrepinfo:= RecNames( AtlasOfGroupRepresentationsInfo.permrepinfo ),
      charrepinfo:= [],
      );
    for nam in RecNames( AtlasOfGroupRepresentationsInfo.characterinfo ) do
      for entry in AtlasOfGroupRepresentationsInfo.characterinfo.( nam ) do
        Append( olddata.charrepinfo, entry[2] );
      od;
    od;

    # Set up the table of contents for the extension.
    # The following call does the work.
    if body <> fail then
      if ForAll( AtlasOfGroupRepresentationsInfo.notified,
                 r -> r.ID <> dirid ) then
        # 'AtlasTableOfContents' sets the necessary components.
        Add( AtlasOfGroupRepresentationsInfo.notified, rec( ID:= dirid ) );
      fi;
      toc:= AtlasTableOfContents( dirid, "all", body );
    else
      toc:= AtlasTableOfContents( dirid, "all" );
    fi;

    # In test mode, make the ignored filenames visible.
    if test and IsBound( toc.otherfiles )
            and not IsEmpty( toc.otherfiles ) then
      Print( "#I  AtlasOfGroupRepresentationsNotifyData:\n",
             "#I  ignored files in ", dirname, ":\n",
             "#I  ", toc.otherfiles, "\n" );
    fi;
    Unbind( toc.otherfiles );

    # Check that no filename of this table of contents exists already in
    # another table of contents.
    Sort( AtlasOfGroupRepresentationsInfo.filenames );
    ok:= true;
    for entry in AtlasOfGroupRepresentationsInfo.newfilenames do
      pos:= PositionSorted( AtlasOfGroupRepresentationsInfo.filenames,
                            [ entry[1] ] );
      if pos <= Length( AtlasOfGroupRepresentationsInfo.filenames )
         and AtlasOfGroupRepresentationsInfo.filenames[ pos ][1] = entry[1] then
        Info( InfoAtlasRep, 1,
              "file '", entry[1], "' was already in another t.o.c." );
        ok:= false;
      fi;
    od;
      
    if not ok then
      AtlasOfGroupRepresentationsForgetData( dirid );
      return false;
    fi;

    Append( AtlasOfGroupRepresentationsInfo.filenames,
            AtlasOfGroupRepresentationsInfo.newfilenames );
    AtlasOfGroupRepresentationsInfo.newfilenames:= [];
    Sort( AtlasOfGroupRepresentationsInfo.filenames );

    # Add group names that were not notified.
    unknown:= Set( Filtered( RecNames( toc ),
                       x -> ForAll( AtlasOfGroupRepresentationsInfo.GAPnames,
                                    pair -> x <> pair[2] ) ) );
    RemoveSet( unknown, "TocID" );
    RemoveSet( unknown, "lastupdated" );
    if not IsEmpty( unknown ) then
      ok:= false;
      Info( InfoAtlasRep, 1,
            "no GAP names defined for ", unknown );
      for name in unknown do
        value:= [ name, name, rec() ];
        AddSet( AtlasOfGroupRepresentationsInfo.GAPnames, value );
        AGR.GAPnamesRec.( name ):= value;
      od;
    fi;

    # Clear the caches.
    AGR.SetGAPnamesSortDisp();
    AtlasOfGroupRepresentationsInfo.TOC_Cache:= rec();
    AtlasOfGroupRepresentationsInfo.TableOfContents.merged:= rec();

    # Run the postponed tests.
    if test then
      AtlasOfGroupRepresentationsInfo.checkData:= true;
      olddata.repinfo:= Difference(
                   RecNames( AtlasOfGroupRepresentationsInfo.permrepinfo ),
                   olddata.permrepinfo );
      olddata.charrepinfonew:= [];
      for nam in RecNames( AtlasOfGroupRepresentationsInfo.characterinfo ) do
        for entry in AtlasOfGroupRepresentationsInfo.characterinfo.( nam ) do
          Append( olddata.charrepinfonew, entry[2] );
        od;
      od;
      UniteSet( olddata.repinfo, Difference( olddata.charrepinfonew,
                                             olddata.charrepinfo ) );
      for nam in olddata.repinfo do
        ok:= AGR.IsRepNameAvailable( nam ) and ok;
      od;
    fi;

    # Restore the original flag.
    if not oldtest then
      Unbind( AtlasOfGroupRepresentationsInfo.checkData );
    fi;

    # Return the flag.
    return ok;
    end );


#############################################################################
##
#F  AtlasOfGroupRepresentationsForgetData( <dirid> )
##
InstallGlobalFunction( AtlasOfGroupRepresentationsForgetData,
    function( dirid )
    local GAPnames, notified, i, j, entry, gapname, pos, list, info;

    GAPnames:= AtlasOfGroupRepresentationsInfo.GAPnames;
    notified:= AtlasOfGroupRepresentationsInfo.notified;

    for i in [ 1 .. Length( notified ) ] do
      if notified[i].ID = dirid then

        # Remove the group related information that belongs to 'dirid'.
        for j in [ 1 .. Length( GAPnames ) ] do
          entry:= GAPnames[j];
          if entry[4].GNAN = dirid then
            gapname:= entry[1];
            pos:= Position( AGR.MapNameToGAPName[2], gapname );
            if pos <> fail then
              Remove( AGR.MapNameToGAPName[1], pos );
              Remove( AGR.MapNameToGAPName[2], pos );
            fi;
            Unbind( GAPnames[j] );
            # This throws away information which was perhaps added
            # in another extension.
            Unbind( AGR.GAPnamesRec.( gapname ) );
          fi;
          if IsBound( entry[4].GRS ) and entry[4].GRS = dirid then
            Unbind( entry[4].GRS );
            Unbind( entry[3].size );
          fi;
          if IsBound( entry[4].MXN ) and entry[4].MXN = dirid then
            Unbind( entry[4].MXN );
            Unbind( entry[3].nrMaxes );
          fi;
          if IsBound( entry[4].MXO ) and entry[4].MXO = dirid then
            Unbind( entry[4].MXO );
            Unbind( entry[3].sizesMaxes );
          fi;
          if IsBound( entry[4].MXS ) and entry[4].MXS = dirid then
            Unbind( entry[4].MXS );
            Unbind( entry[3].structureMaxes );
          fi;
        od;
        AtlasOfGroupRepresentationsInfo.GAPnames:= Compacted( GAPnames );

        # Remove information about representations.
        AtlasOfGroupRepresentationsInfo.ringinfo:= Filtered(
            AtlasOfGroupRepresentationsInfo.ringinfo,
            x -> x[ Length(x) ] <> dirid );
        for gapname in RecNames(
                         AtlasOfGroupRepresentationsInfo.characterinfo ) do
          for list in AtlasOfGroupRepresentationsInfo.characterinfo.(
                          gapname ) do
            for pos in Reversed( Positions( list[4], dirid ) ) do
              for j in [ 1 .. 4 ] do
                Remove( list[j], pos );
              od;
            od;
          od;
        od;
        info:= AtlasOfGroupRepresentationsInfo.permrepinfo;
        for entry in RecNames( info ) do
          if info.( entry ).dirid = dirid then
            Unbind( info.( entry ) );
          fi;
        od;

        AtlasOfGroupRepresentationsInfo.filenames:= Filtered(
            AtlasOfGroupRepresentationsInfo.filenames,
            x -> x[3] <> dirid );
        AtlasOfGroupRepresentationsInfo.newfilenames:= [];

        # Remove the information concerning the data extension.
        Remove( notified, i );

        # Discard all caches.
        Unbind( AtlasOfGroupRepresentationsInfo.TableOfContents.(
                    Concatenation( dirid, "|all" ) ) );
        Unbind( AtlasOfGroupRepresentationsInfo.TableOfContents.(
                    Concatenation( dirid, "|local" ) ) );
        AtlasOfGroupRepresentationsInfo.TableOfContents.merged:= rec();
        AGR.SetGAPnamesSortDisp();
        AtlasOfGroupRepresentationsInfo.TOC_Cache:= rec();

        return;

      fi;
    od;

    Print( "#I  AtlasOfGroupRepresentationsForgetData:\n",
           "#I  There seems to be no data extension with identifier '",
           dirid, "'\n" );
    end );


if IsString( IO_mkdir ) then
  Unbind( IO_mkdir );
fi;
if IsString( IO_stat ) then
  Unbind( IO_stat );
fi;
if IsString( IO_chmod ) then
  Unbind( IO_chmod );
fi;


#############################################################################
##
#E