This file is indexed.

/usr/share/tcltk/critcl-app3.1.8/critcl.tcl is in critcl 3.1.9-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#!/bin/sh
# -*- tcl -*-
# # ## ### ##### ######## ############# #####################

# Critcl Application.

# # ## ### ##### ######## ############# #####################

#   Prebuild shared libraries using the Critcl package.
#
#   Based originally on critbind by Jean-Claude Wippler
#   Transmogrified into critcl   by Steve Landers
#
#   $Id: critcl.tcl 4717 2009-11-02 01:24:42Z stevel $
#
# \
    exec tclkit $0 ${1+"$@"}

# # ## ### ##### ######## ############# #####################
## Requirements

package provide critcl::app [package require critcl]
package require cmdline

# It is expected here that critcl already imported platform, or an
# equivalent package, i.e. the critcl::platform fallback. No need to
# do it again.
#package require platform

# Note: We can assume here that the commands lassign and dict are
# available. The critcl package has made sure of that.

namespace eval ::critcl::app {}

# # ## ### ##### ######## ############# #####################

# Intercept 'package' calls.

# This code is present to handle the possibility of building multiple
# different versions of the same package, or of different packages
# having dependencies on different versions of a 3rd party
# package. Each will 'package provide' its version to our Tcl, and
# thus normally be reported as a conflict. To prevent that the
# intercepted command checks for this situation, and forces Tcl to
# forget the previously registered package.

rename package ::critcl::app::__package
proc package {option args} {
    if {$option eq "provide"} {
        if {![catch {
	    set v [::critcl::app::__package present [lindex $args 0]]
	}] &&
	    ([llength $args] > 1) &&
	    ($v ne [lindex $args 1])
	} {
	    # A package is provided which is already present in
	    # memory, the number of arguments is ok, and the version
	    # of the new package is different from what is
	    # known. Force Tcl to forget the previous package, this is
	    # not truly a conflict.
            ::critcl::app::__package forget [lindex $args 0]
        }
    }

    return [eval [linsert $args 0 ::critcl::app::__package $option]]
}

# # ## ### ##### ######## ############# #####################
## Override the default of the critcl package for errors and
## message. Write them to the terminal (and, for errors, abort the
## application instead of throwing them up the stack to an uncertain
## catch).

proc ::critcl::error {msg} {
    global argv0
    puts stderr "$argv0 error: $msg"
    flush stderr
    exit 1
}

proc ::critcl::msg {args} {
    switch -exact -- [llength $args] {
	1 {
	    puts stdout [lindex $args 0]
	    flush stdout
	}
	2 {
	    lassign $args o m
	    if {$o ne "-nonewline"} {
		return -code error "wrong\#args, expected: ?-nonewline? msg"
	    }
	    puts -nonewline stdout $m
	    flush stdout
	}
	default {
	    return -code error "wrong\#args, expected: ?-nonewline? msg"
	}
    }
    return
}

# # ## ### ##### ######## ############# #####################

proc ::critcl::app::main {argv} {
    Cmdline $argv

    # When creating a package use a transient cache which is not in
    # conflict with "compile & run", or other instances of the critcl
    # application.

    if {$v::mode eq "pkg"} {
	set pkgcache [PackageCache]
	critcl::cache $pkgcache
    }

    ProcessInput
    StopOnFailed

    # All input files have been processed and their data saved. Now
    # generate the boilerplate bracketing all the sub-ordinate
    # Foo_Init() functions, i.e. the code which provides a single
    # initialization function for the whole set of input files.

    if {$v::mode eq "pkg"} {
	# Create a merged shared library and put a proper Tcl package
	# around it.

	BuildBracket
	StopOnFailed
	AssemblePackage

	if {!$v::keep} {
	    file delete -force $pkgcache
	}
    } elseif {$v::mode eq "tea"} {
	AssembleTEA
    }

    StopOnFailed

    if {$v::keep} {
	::critcl::print stderr "Files left in [critcl::cache]"
    }
    return
}

proc ::critcl::app::PackageCache {} {
    if {$v::cache ne {}} {
	return $v::cache
    }
    return [file join ~ .critcl pkg[pid].[clock seconds]]
}

proc ::critcl::app::StopOnFailed {} {
    if {!$v::failed} return
    ::critcl::print stderr "Files left in [critcl::cache]"
    ::critcl::print stderr "FAILURES $v::failed"
    ::critcl::print stderr "FAILED:  [join $v::borken "\nFAILED:  "]"
    ::critcl::print stderr "FAILED   [join [split [join $v::log \n\n] \n] "\nFAILED   "]"
    exit 1 ; #return -code return
}

proc ::critcl::app::Cmdline {argv} {
    variable options

    # Rationalized application name. Direct user is the intercepted
    # '::critcl::error' command.
    set ::argv0 [cmdline::getArgv0]

    # Semi-global application configuration.
    set v::verbose  0      ; # Default, no logging.
    set v::src     {}      ; # No files to process.
    set v::mode    cache   ; # Fill cache. Alternatively build a
			     # package, or TEA hierarchy.
    set v::shlname ""      ; # Name of shlib to build.
    set v::outname ""      ; # Name of shlib dir to create.
    set v::libdir  lib     ; # Directory to put the -pkg or -tea
			     # directory into.
    set v::incdir  include ; # Directory to put the -pkg include files into (stubs export),
                             # and search in (stubs import)
    set v::keep    0       ; # Default: Do not keep generated .c files.

    # Local actions.
    set selftest   0 ;# Invoke the application selftest, which simply
                      # runs whatever test/*.tst files are found in the
                      # starkit or starpack. IOW, this functionality is
                      # usable only for a wrapped critcl application.
    set cleaning   0 ;# Clean the critcl cache. Default: no.
    set showall    0 ;# Show all configurations in full. Default: no.
    set show       0 ;# Show the chosen build configuration. Default: no.
    set showtarget 0 ;# Show the chosen build target only. Default: no.
    set targets    0 ;# Show the available targets.
    set help       0 ;# Show the application's help text.

    # Local configuration. Seen outside of this procedure only
    # directly, through the chosen build configuration.

    set target     "" ;# The user-specified build target, if any.
    set configfile "" ;# The user-specified custom configuration file,
		       # if any

    # Process the command line...

    while {[set result [cmdline::getopt argv $options opt arg]] != 0} {
	if {$result == -1} {
	    switch -glob -- $opt {
		with-* {
		    set argv [lassign $argv opt arg]
		    regsub {^-with-} $opt {} opt
		    lappend v::uc $opt $arg
		    continue
		}
		default {
		    Usage "Unknown option \"$opt\""
		}
	    }
	}
	switch -exact -- $opt {
	    v - -version {
		::critcl::print [package present critcl]
		::exit 0
	    }
	    I          { AddIncludePath $arg }
	    L          { AddLibraryPath $arg }
	    cache      { set v::cache $arg }
	    clean      { incr cleaning }
	    config     { set configfile $arg }
	    debug      {
		lappend v::debug $arg
		#critcl::config lines 0
	    }
	    force      {
		critcl::config force 1
		::critcl::print stderr "Compilation forced"
	    }
	    keep       {
		critcl::config keepsrc 1
		#critcl::config lines 0
		set v::keep 1
	    }
	    help       { incr help }
	    libdir     {
		set v::libdir $arg

		# In case critcl is wrapped Tcl must be told about the
		# outside location for packages.

		lappend ::auto_path $arg
		lappend ::auto_path [file dirname $arg]
		AddLibraryPath $arg
	    }
	    includedir {
		set v::incdir  $arg
		AddIncludePath $arg
	    }
	    enable     { lappend v::uc $arg 1 }
	    disable    { lappend v::uc $arg 0 }
	    pkg        { set v::mode pkg ; incr v::verbose }
	    tea        { set v::mode tea ; incr v::verbose }
	    show       { incr show }
	    showall    { incr showall }
	    showtarget { incr showtarget }
	    target     { set target $arg }
	    targets    { incr targets }
	    test       { set selftest 1 }
	    default {
		Usage "Unknown option \"$opt\""
	    }
	}
    }

    # ... validate the settings, and act on them.

    if {$help} {
	Help
	exit
    }

    # Parse the user-specified configuration file, if any. This
    # overrides the default configuration file read by the critcl
    # package when it was loaded. It does keep the default platform
    # from that active.

    if {$configfile ne ""} {
	if {$argv eq "" && [file extension $configfile] eq ".tcl"} {
	    # probably means the user has omitted the config file and we've
	    # picked up the source file name
	    Usage "-config is missing file argument"
	}
	if {![file exists $configfile]} {
	    Usate "Can't read configuration file $configfile"
	}
	critcl::readconfig $configfile
    }

    # And switch to the user-provided target platform.

    if {$target ne ""} {
	if {($argv eq "") && [file extension $target] eq ".tcl"} {
	    # probably means the user has omitted the config file and we've
	    # picked up the source file name
	    Usage "-target is missing file argument"
	}

	set match [critcl::chooseconfig $target 1]

	if {[llength $match] == 1} {
	    critcl::setconfig [lindex $match 0]
	} else {
	    Usage "multiple targets matched : $match"
	}
    }

    if {($v::mode eq "pkg") || $show} {
	critcl::crosscheck
    }

    if {$cleaning} {
	critcl::clean_cache
    }

    if {$show} {
	if {$v::mode eq "pkg"} {
	    critcl::cache [PackageCache]
	}
	critcl::showconfig stdout
    }

    if {$showall} {
	critcl::showallconfig stdout
    }

    if {$showtarget} {
	::critcl::print [critcl::targetplatform]
    }

    if {$targets} {
	::critcl::print [critcl::knowntargets]
    }

    if {$show || $showall || $targets || $showtarget} {
	exit
    }

    if {$selftest} {
	Selftest
	exit
    }

    # Invoking the application without input files is an error, except
    # if it was to just clean the local critcl cache.

    if {[llength $argv] < 1} {
	if {!$cleaning} Usage
	exit
    }

    # The remainder of the arguments are the files to process, except
    # for lib and pkg modes where they can be prefixed with the name
    # of the output file, i.e. shared library. If however no such
    # output file is present the name of the first input file will be
    # used as name of the library.

    set v::src $argv

    # (%) Determine the name of the shared library to generate from
    # the input files. This location is referenced by (=).

    if {$v::mode ne "cache"} {
	set name [lindex $argv 0]
	set addext 0

	# Split a version number off the package name.
	set ver {}
	if {[regexp {^([^0-9]+)([0-9][.0-9]*)$} $name -> base ver]} {
	    set name $base
	}

	switch [file extension $name] {
	    .dll   -
	    .dylib -
	    .sl    -
	    .so {
		# The name of the result shlib is prefixed, take it as
		# package name, and strip it off the list of input
		# files.
		set v::outname [file rootname $name]
		set v::src     [lrange $v::src 1 end]
		set addext 1
	    }
	    .tcl {
		# We have no discernible result shlib, take
		# the stem of the first input file as package
		# name

		set v::outname [file rootname $name]
		set addext 1
	    }
	    "" {
		# See above for .tcl, except that there is no stem to
		# take. And if this is the only argument we also have
		# to derive the full name of the expected input file
		# from it.
		set v::outname $name
		if {[llength $argv] == 1} {
		    set v::src [list $v::outname.tcl]
		} else {
		    set v::src [lrange $v::src 1 end]
		}
	    }
	    default {
		Usage "Not sure how to handle \"$name\""
	    }
	}

	# Put the version number back. We have to distinguish package
	# library file name and package directory name. Only the
	# latter should have the version number.
	set v::shlname $v::outname
	if {$ver ne {}} {
	    append v::outname $ver
	}

	if {$addext || ([file extension $v::shlname] eq "")} {
	    append v::shlname [critcl::sharedlibext]
	}

	critcl::config combine dynamic

	if {![llength $v::src]} {
	    Usage "No input files"
	}
    }

    # Determine the platform to use by the build backend, based on
    # actual platform we are on and the user's chosen target, if any.

    set v::actualplatform [::critcl::actualtarget]
    return
}

proc ::critcl::app::AddIncludePath {path} {
    set dirs [critcl::config I]
    lappend dirs [file normalize $path]
    critcl::config I $dirs
    return
}

proc ::critcl::app::AddLibraryPath {path} {
    set dirs [critcl::config L]
    lappend dirs [file normalize $path]
    critcl::config L $dirs
    return
}

proc ::critcl::app::Log {text} {
    if {!$v::verbose} return
    ::critcl::print -nonewline $text
    flush stdout
    return
}

proc ::critcl::app::LogLn {text} {
    if {!$v::verbose} return
    ::critcl::print $text
    flush stdout
    return
}

proc ::critcl::app::Usage {args} {
    global argv0
    if {[llength $args]} {
	::critcl::print stderr "$argv0 error: [join $args]"
    }

    ::critcl::print stderr [string map [list @ $argv0] {To compile and run a tcl script
	@ [-force] [-keep] [-cache dir] file[.tcl]

To compile and build a package
    @ options -pkg ?name? [files...]

To repackage for TEA
    @ options -tea ?name? [files...]

Options include:
    -debug [symbols|memory|all] enable debugging
    -force          force compilation of C files
    -show           show the configuration options being used
    -target target  generate binary for specified target platform/architecture

Other options that may be useful:
    -I dir          adds dir to the include search path when compiling.
    -L dir          adds dir to the library search path when linking.
    -cache dir      sets the Critcl cache directory to dir.
    -keep           keep intermediate C files in the Critcl cache
    -config file    read the Critcl configuration options from file
    -libdir dir     location of generated library/package
    -includedir dir location of generated package headers (stubs)
    -showall        show configuration for all supported platforms
    -targets        show all available target platforms

You can display the built-in help wiki on most platforms using:
    @ -help }]
    exit 1
    return
}

proc ::critcl::app::Help {} {
    if {[catch {package require Mk4tcl} msg] ||
	[catch {package require Wikit} msg]} {
	::critcl::print $msg
        set txt "Couldn't load the Critcl help Wiki\n"
        append txt "To display the Critcl help wiki run \"critcl\" "
        append txt "without any options.\n"
        ::critcl::print $txt
        exit
    } else {
        Wikit::init [file join $::starkit::topdir doc critcl.tkd]
    }
}

proc ::critcl::app::Selftest {} {
    foreach t [glob -directory [file join $starkit::topdir test] *.tst] {
        source $t
    }
    return
}

proc ::critcl::app::ProcessInput {} {
    # Main loop. This processes the input files, one by one.

    set v::debug [lsort -unique $v::debug]

    # NOTE that this effectively executes them (source!) in the
    # context of this application. The files are trusted to not
    # contain malicious side-effects, etc.

    # Initialize the accumulator variables for various per-file
    # information which will be needed later when building the
    # over-arching initialization code.

    set v::clibraries {}  ;# External libraries used. To link the final shlib against.
    set v::ldflags    {}  ;# Linker flags.
    set v::objects    {}  ;# The object files to link.
    set v::edecls     {}  ;# Initialization function decls for the pieces.
    set v::initnames  {}  ;# Initialization function calls for the pieces.
    set v::tsources   {}  ;# Tcl companion sources.
    set v::mintcl     8.4 ;# Minimum version of Tcl required to run the package.
    set v::tk         0   ;# Boolean flag. Set if any sub-package needs Tk, forcing it on the collection as well.
    set v::preload    {}  ;# List of libraries declared for preload.
    set v::license    {}  ;# Accumulated licenses, if any.
    set v::failed      0  ;# Number of build failures encountered.
    set v::borken     {}  ;# List of files which failed to build.
    set v::log        {}  ;# List of log messages for the failed files
    set v::headers    {}  ;# List of header directories (in the result cache)
                           # to export.
    set v::pkgs       {}  ;# List of package names for the pieces.
    set v::inits      {}  ;# Init function names for the pieces, list.
    set v::meta       {}  ;# All meta data declared by the input files.

    # Other loop status information.

    set first  1  ;# Flag, reset after first round, helps with output formatting.
    set missing 0

    if {$v::mode eq "tea"} {
	LogLn "Config:   TEA Generation"
	Log   "Source:   "

	# Initialize the accumulator variables for various per-file
	# information.

	set v::org      {} ; # Organization the package is licensed by.
	set v::ver      {} ; # Version of the package.
	set v::cfiles   {} ; # Companion files (.tcl, .c, .h, etc).
	set v::teasrc   {} ; # Input file(s) transformed for use in the Makefile.in. 
	set v::imported {} ; # List of stubs APIs imported from elsewhere.
	set v::config   {} ; # List of user-specified configuration settings.

    } elseif {[llength $v::src]} {
	LogLn "Config:   [::critcl::targetconfig]"
	LogLn "Build:    [::critcl::buildplatform]"

	set t [::critcl::targetplatform]
	if {$v::actualplatform ne $t} {
	    LogLn "Target:   $v::actualplatform (by $t)"
	} else {
	    LogLn "Target:   $v::actualplatform"
	}
	Log   "Source:   "
    }

    foreach f $v::src {
	# Avoid reloading itself.
	if {[file rootname [file tail $f]] eq "critcl"} continue

	if {$v::mode eq "tea"} {
	    lappend v::teasrc "\${srcdir}/src/[file tail $f]"
	}

	# Canonicalize input argument, and search in a few places.
	set fn [file normalize $f]

	set found [file exists $fn]
	if {!$found} {
	    if {[file extension $fn] ne ".tcl"} {
		append fn .tcl
		set found [file exists $fn]
	    }
	    if {!$found} {
		if {!$first} { ::critcl::print stderr "" }
		::critcl::print stderr "$f doesn't exist"
		incr missing
		continue
	    }
	}

	set first 0
	Log "[file tail $fn] "
	set dir [file dirname $fn]

	if {$v::mode eq "tea"} {
	    # In TEA mode we are not building anything at all. We only
	    # wish to know and scan for the declarations of companion
	    # files, so that we know what to put these into the TEA
	    # directory hierarchy. This also provides us with the
	    # version number to use.

	    LogLn ""
	    array set r [critcl::scan $fn]
	    lappend v::cfiles $f $r(files)
	    if {$r(org) ne {}} {
		lappend v::org $r(org)
	    }
	    if {$r(version) ne {}} {
		lappend v::ver $r(version)
	    }
	    if {$r(imported) ne {}} {
		critcl::lappendlist v::imported $r(imported)
	    }
	    if {$r(config) ne {}} {
		critcl::lappendlist v::config $r(config)
	    }
	    if {$r(meta) ne {}} {
		lappend v::meta $r(meta)
	    }
	    continue
	}

	# Execute the input file and collect all the crit(i)c(a)l :)
	# information. Depending on the use of 'critcl::failed' this
	# may or may not have generated the internal object file.

	if {$v::mode eq "pkg"} {
	    critcl::buildforpackage
	}

	if {[llength $v::debug]} {
	    # As the debug settings are stored per file we now take
	    # the information from the application's commandline and
	    # force things here, faking the proper path information.

	    set save [info script]
	    info script $fn
	    eval [linsert $v::debug 0 critcl::debug]
	    info script $save
	}

	#puts ||$v::uc||
	if {[llength $v::uc]} {
	    # As the user-config settings are stored per file we now
	    # take the information from the application's commandline
	    # and force things here, faking the proper path information.
	    # Full checking of the data happens only if the setting is
	    # actually used by the file.

	    set save [info script]
	    info script $fn

	    foreach {k v} $v::uc {
		#puts UC($k)=|$v|
		critcl::userconfig set $k $v
	    }
	    info script $save
	}

	# Ensure that critcl's namespace introspection is done
	# correctly, and not thinking that 'critcl::app' is the
	# namespace to use for the user's commands.

	uplevel #0 [list source $fn]

	if {[critcl::cnothingtodo $fn]} {
	    ::critcl::print stderr "nothing to build for $f"
	    continue
	}

	# Force build. Our 'buildforpackage' call above disabled
	# 'critcl::failed' and 'critcl::load' (Causing them to return
	# OK, and bypassing anything conditional on their failure). If
	# there is a failure we want to know it correctly, here.
	#
	# Regardless, we have to force (and later restore) the proper
	# script location, something the 'source' comand above did
	# automatically.

	set save [info script]
	info script $fn
	set failed [critcl::cbuild $fn 0]
	incr v::failed $failed
	info script $save

	# We can skip the part where we collect the build results for
	# use by the overarching code if either no overall shlib is
	# generated from the input, or any of the builds made so far
	# failed.

	# NOTE that we were NOT skipping the build step for any of the
	# packages, even if previous packages failed. We want the
	# maximum information about problems from a single run, not
	# fix things one by one.

	set results [critcl::cresults $fn]
	if {$failed} {
	    lappend v::borken $f
	    lappend v::log    [dict get $results log]
	    Log "(FAILED) "
	} elseif {[dict exists $results warnings]} {
	    # There might be warnings to print even if the build did
	    # not fail.
	    set warnings [dict get $results warnings]
	    if {[llength $warnings]} {
		::critcl::print stderr "\n\nWarning  [join $warnings "\nWarning  "]"
	    }
	}
	if {$v::failed || ($v::mode ne "pkg")} continue

	array set r $results

	append v::edecls    "extern Tcl_AppInitProc $r(initname)_Init;\n"
	append v::initnames "    if ($r(initname)_Init(ip) != TCL_OK) return TCL_ERROR;\n"
	append v::license   [License $f $r(license)]

	lappend v::pkgs  $r(pkgname)
	lappend v::inits $r(initname)
	lappend v::meta  $r(meta)

	# The overall minimum version of Tcl required by the combined
	# packages is the maximum over all of their minima.
	set v::mintcl [Vmax $v::mintcl $r(mintcl)]
	set v::tk     [Max $v::tk $r(tk)]
	critcl::lappendlist v::objects    $r(objects)
	critcl::lappendlist v::tsources   $r(tsources)
	critcl::lappendlist v::clibraries $r(clibraries)
	critcl::lappendlist v::ldflags    $r(ldflags)
	critcl::lappendlist v::preload    $r(preload)

	if {[info exists r(apiheader)]} {
	    critcl::lappendlist v::headers $r(apiheader)
	}
    }

    if {$missing} {
	critcl::error  "Missing files: $missing, aborting"
    }

    # Reduce package and init function to the first pieces. Easier to
    # do it this way than having a conditional set in the loop.

    set v::pkgs  [lindex $v::pkgs  0]
    set v::inits [lindex $v::inits 0]
    # Strip the prefix used by the foundation package. Keep in sync.
    regsub {^ns_} $v::inits {} v::inits

    return
}

proc ::critcl::app::Vmax {a b} {
    if {[package vcompare $a $b] >= 0} {
	return $a
    } else {
	return $b
    }
}

proc ::critcl::app::Max {a b} {
    if {$a >= $b} {
	return $a
    } else {
	return $b
    }
}

proc ::critcl::app::License {file text} {
    if {$text eq "<<Undefined>>"} { return {} }
    return "\n\[\[ [file tail $file] \]\] __________________\n$text"
}

proc ::critcl::app::BuildBracket {} {
    ::critcl::print "\nLibrary:  [file tail $v::shlname]"

    # The overarching initialization code, the bracket, has no real
    # file behind it. Fake it based on the destination shlib, this
    # ensures that the generated _Init function has the proper name
    # without having to redefine things through C macros, as was done
    # before.
    info script $v::shlname

    critcl::config combine ""

    # Inject the information collected from the input files, making
    # them part of the final result.
    critcl::tcl $v::mintcl
    if {$v::tk} { critcl::tk }

    set                 lib critcl::cobjects
    critcl::lappendlist lib $v::objects
    eval $lib

    set                 lib critcl::clibraries
    critcl::lappendlist lib [lsort -unique $v::clibraries]
    eval $lib

    eval [linsert [lsort -unique $v::ldflags] 0 critcl::ldflags]
    eval [linsert [lsort -unique $v::preload] 0 critcl::preload]

    critcl::cinit $v::initnames $v::edecls

    # And build everything.
    critcl::buildforpackage 0
    set failed [critcl::cbuild "" 0]

    incr v::failed $failed
    if {$failed} {
	lappend v::borken <<Bracket>>
	Log "(FAILED) "
    }
    return
}

proc ::critcl::app::PlaceShlib {} {
    # Copy the generated shlib from the cache to its final resting
    # place. For -pkg this was set be inside the directory hierarchy
    # of the newly-minted package. To prevent hassle a previously
    # existing file gets deleted.

    if {[file exists $v::shlname]} {
	file delete -force $v::shlname
    }

    # NOTE that the fake 'info script location' set by 'BuildBracket'
    # is still in effect, making access to the build results easy.
    set shlib [dict get [critcl::cresults] shlib]
    file copy $shlib $v::shlname

    # For MSVC debug builds we get a separate debug info file.
    set pdb [file root $shlib].pdb
    if {[file exists $pdb]} {
	file copy -force $pdb [file root $v::shlname].pdb
    }

    # Record shlib in the meta data, list of package files.
    set d [file tail [file dirname $v::shlname]]
    set f [file tail $v::shlname]
    lappend v::meta [list included [file join $d $f]]
    return
}

proc ::critcl::app::ExportHeaders {} {
    set incdir [CreateIncludeDirectory]

    foreach dir $v::headers {
	set stem [file tail $dir]
	set dst  [file join $incdir $stem]

	::critcl::print "Headers:  $v::incdir/$stem"

	file mkdir $dst
	foreach f [glob -nocomplain -directory $dir *] {
	    file copy -force $f $dst
	}
    }
    return
}

proc ::critcl::app::AssemblePackage {} {

    # Validate and/or create the main destination directory L. The
    # package will become a subdirectory of L. See (x). And a platform
    # specific directory inside of that will hold the shared
    # library. This allows us to later merge the packages for
    # different platforms into a single multi-platform package.

    set libdir [CreateLibDirectory]

    set libname  [file tail $v::outname]
    set pkgdir   [file join $libdir $libname]
    set shlibdir [file join $pkgdir $v::actualplatform]

    # XXX fileutil::stripPwd ...
    if {[string first [pwd] $pkgdir] != -1} {
	set first [string length [pwd]]
	set dir [string range $pkgdir [incr first] end]
    } else {
	set dir $pkgdir
    }
    ::critcl::print "Package:  $dir"

    file mkdir             $pkgdir
    file mkdir             $shlibdir

    set shl [file tail $v::shlname]

    CreatePackageIndex     $shlibdir [file rootname $shl] \
	[PlaceTclCompanionFiles $pkgdir]
    CreateLicenseTerms     $pkgdir
    CreateRuntimeSupport   $pkgdir

    # Place the shlib generated by BuildBracket into its final resting
    # place, in the directory hierarchy of the just-assembled package.

    set v::shlname [file join $shlibdir $shl]
    PlaceShlib

    # At last we can generate and write the meta data. Many of the
    # commands before added application-level information (like
    # included files, entrytclcommand, ...) to the information
    # collected from the input files

    CreateTeapotMetadata $pkgdir
    ExportHeaders
    return
}

proc ::critcl::app::CreatePackageIndex {shlibdir libname tsources} {
    # Build pkgIndex.tcl

    set version [package present $v::pkgs]

    # (=) The 'package present' works because (a) 'ProcessInput'
    # sources the package files in its own context, this process, and
    # (b) the package files (are expected to) contain the proper
    # 'package provide' commands (for compile & run mode), and we
    # expect that at least one of the input files specifies the
    # overall package built from all the inputs. See also (%) in
    # Cmdline, where the application determines shlib name and package
    # name, often from the first input file, and/or working backwards
    # from package name to input file.

    set    index [open [file join [file dirname $shlibdir] pkgIndex.tcl] w]
    puts  $index [PackageGuard $v::mintcl]
    puts  $index [IndexCommand $version $libname $tsources $shlibdir]
    close $index
    return
}

proc ::critcl::app::Mapping {} {
    # Create the platform mapping for each of the platforms listed on
    # the Config platform line

    set map    [critcl::getconfigvalue platform]
    set minver [lindex $map 1]

    set plats  [list]
    foreach plat [lrange $map 2 end] {
	set mapping($plat) [list [critcl::actualtarget] $minver]
	lappend plats $plat
    }

    if {[llength $plats]} {
	::critcl::print "Platform: [join $plats {, }] $minver and later"
    }

    set map {}
    foreach plat [lsort [array names mapping]] {
	lappend map $plat $mapping($plat)
    }
    return $map
}

proc ::critcl::app::Preload {shlibdir} {
    if {![llength $v::preload]} { return {} }

    # Locate the external libraries declared for preloading and put
    # them into the package. Put the shared library of the internal
    # preload support pseudo-package into it as well. This will all be
    # picked up by the 'package ifneeded' script.

    # First handle the declared libraries. Any problem there throws an
    # error, or aborts.

    set preload {}
    foreach shlib $v::preload {
	file copy -force [PreloadLocation $shlib] $shlibdir
	lappend preload [file tail $shlib]
    }

    # Everything was ok, now place the supporting shlib into the
    # package as well.

    file copy -force \
	[file join [critcl::cache] preload[critcl::sharedlibext]] \
	$shlibdir

    ::critcl::print "Preload:  [join $preload {, }]"
    return $preload
}

proc ::critcl::app::PreloadLocation {shlib} {
    set searchpath [PreloadSearchPath $shlib]

    foreach path $searchpath {
	if {![file exists $path]} continue
	return $path
    }

    set    msg "can't find preload library $shlib"
    append msg " for target platform \"$v::actualplatform\";"
    append msg " searched for "
    append msg [linsert [join $searchpath {, }] end-1 and]
    critcl::error $msg
    return
}

proc ::critcl::app::PreloadSearchPath {shlib} {

    # Look for lib FOO as follows:
    # (1) FOO.so
    # (2) FOO/FOO.so
    # (3) FOO/<platform>/FOO.so
    #
    # Look for lib BAR/FOO as follows:
    # (1) FOO.so
    #
    # Then, if BAR/FOO doesn't exist as directory:
    # (2) BAR/FOO.so
    # (3) BAR/<platform>/FOO.so
    #
    # Conversely, if BAR/FOO does exist as directory:
    # (2) BAR/FOO/FOO.so
    # (3) BAR/FOO/<platform>/FOO.so

    #   - lib.so
    #   - dir/lib.so
    #   - dir/plat/lib.so

    set tail [file tail $shlib]

    if {[file isdirectory $shlib]} {
	set dir $shlib
    } else {
	set dir [file dirname $shlib]
	if {$dir eq "."} {
	    set dir $tail
	}
    }

    set ext [critcl::sharedlibext]	    
    return [list \
		$tail$ext \
		[file join $dir $tail$ext] \
		[file join $dir $v::actualplatform $tail$ext]]
}

proc ::critcl::app::PackageGuard {v} {
    return [string map [list @ $v] \
	{if {![package vsatisfies [package provide Tcl] @]} {return}}]
}

proc ::critcl::app::IndexCommand {version libname tsources shlibdir} {
    # We precompute as much as possible instead of wholesale defering
    # to the runtime and dynamic code. See ticket (38bf01b26e). That
    # makes it easier to debug the index command, as it is immediately
    # visible in the pkgIndex.tcl file. And supports placement into
    # the meta data.

    set loadcmd [LoadCommand $version $libname $tsources $shlibdir]
    return "package ifneeded [list $v::pkgs $version] $loadcmd"
}

proc ::critcl::app::LoadCommand {version libname tsources shlibdir} {
    # New style. Precompute as much as possible.

    set map [Mapping]
    if {$map ne {}} { set map " [list $map]" }
    set platform "\[::critcl::runtime::MapPlatform$map\]"

    set     loadcmd {}
    lappend loadcmd {source [file join $dir critcl-rt.tcl]}
    lappend loadcmd "set path \[file join \$dir $platform\]"
    lappend loadcmd "set ext \[info sharedlibextension\]"
    lappend loadcmd "set lib \[file join \$path \"$libname\$ext\"\]"

    foreach p [Preload $shlibdir] {
	lappend loadcmd "::critcl::runtime::preFetch \$path \$ext [list $p]"
    }

    lappend loadcmd "load \$lib [list $v::inits]"

    foreach t $tsources {
	lappend loadcmd "::critcl::runtime::Fetch \$dir [list $t]"
    }

    lappend loadcmd [list package provide $v::pkgs $version]

    # Wrap the load command for use by the index command.
    # First make it a proper script, indented, i.e. proc body.

    set loadcmd "\n    [join $loadcmd "\n    "]"

    if {[package vsatisfies $v::mintcl 8.5]} {
	# 8.5+: Put the load command into an ::apply, i.e. make it
	# an anonymous procedure.

	set loadcmd "\[list ::apply \{dir \{$loadcmd\n\}\} \$dir\]"
    } else {
	# 8.4: Use a named, transient procedure. Name is chosen
	# for low probability of collision with anything else.
	# NOTE: We have to catch the auto-delete command because
	# the procedure may have been redefined and destroyed by
	# recursive calls to 'package require' of more critcl-based
	# packages.
	set n __critcl_load__
	append loadcmd "\n    catch \{rename $n {}\}";# auto delete
	set loadcmd "\"\[list proc $n \{dir\} \{[string map [list \n { ; }] $loadcmd]\}\] ; \[list $n \$dir\]\""
    }

    lappend v::meta [list entrytclcommand [list "eval $loadcmd"]]

    return $loadcmd
}

proc ::critcl::app::IndexCommandXXXXX {version libname tsources shlibdir} {
    # Old style critcl. Ifneeded and loading is entirely and
    # dynamically handled in the runtime support code.

    set map       [Mapping]
    set preload   [Preload $shlibdir]
    set arguments [list $v::pkgs $version $libname $v::inits $tsources $map]
    return "source \[file join \$dir critcl-rt.tcl\]\n::critcl::runtime::loadlib \$dir $arguments $preload"
}

proc ::critcl::app::CreateLicenseTerms {pkgdir} {
    # Create a license.terms file.

    if {$v::license eq ""} {
	set v::license <<Undefined>>
    } else {
	set v::license [string trimleft $v::license]
    }
    set    license [open [file join $pkgdir license.terms] w]
    puts  $license $v::license
    close $license
    return
}

proc ::critcl::app::CreateTeapotMetadata {pkgdir} {
    if {![llength $v::meta]} {
	critcl::error "Meta data missing"
	return
    }

    # Merge the data from all input files, creating a list of words
    # per key. Note: Data from later input files does not replace
    # previous words, they get added instead.

    set umd {}
    foreach md $v::meta {
	foreach {k vlist} $md {
	    foreach v $vlist {
		dict lappend umd $k $v
	    }
	}
    }

    # Check the identifying keys, i.e. package name, version, and
    # platform for existence.

    foreach k {name version platform} {
	if {![dict exists $umd $k]} {
	    critcl::error "Package $k missing in meta data"
	}
    }


    # Collapse the data of various keys which must have only one,
    # unique, element.

    foreach k {name version platform build::date generated::date} {
	if {![dict exists $umd $k]} continue
	dict set umd $k [lindex [dict get $umd $k] 0]
    }

    # Add the entity information, and format the data for writing,
    # using the "external" format for TEApot meta data. This writer
    # limits lines to 72 characters, roughly. Beyond that nothing is
    # done to make the output look pretty.

    set md {}
    lappend md "Package [dict get $umd name] [dict get $umd version]"
    dict unset umd name
    dict unset umd version

    dict for {k vlist} $umd {
	set init 1
	foreach v $vlist {
	    if {$init} {
		# The first element of the value list is always added,
		# regardless of length, to avoid infinite looping
		# without progress.
		set line {}
		lappend line Meta $k $v
		set init 0
		continue
	    }
	    if {[string length [linsert $line end $v]] > 72} {
		# If the next element brings us beyond the limit we
		# flush the current state and re-initialize.
		lappend md $line
		set line {}
		lappend line Meta $k $v
		set init 0
		continue
	    }
	    # Add the current element, extending the line.
	    lappend line $v
	}

	# Flush the last line.
	lappend md $line
    }

    # Last step, write the formatted meta data to the associated file.

    set    teapot [open [file join $pkgdir teapot.txt] w]
    puts  $teapot [join $md \n]
    close $teapot
    return
}

proc ::critcl::app::PlaceTclCompanionFiles {pkgdir} {
    # Arrange for the companion Tcl source files (as specified by
    # critcl::tsources) to be copied into the Tcl subdirectory (in
    # accordance with TIP 55)

    if {![llength $v::tsources]} { return {} }

    set tcldir [file join $pkgdir tcl]
    file mkdir $tcldir
    set files {}
    foreach t $v::tsources {
	file copy -force $t $tcldir

	set t [file tail $t]
	lappend files $t

	# Metadata management
	lappend v::meta [list included tcl/$t]
    }
    return $files
}

proc ::critcl::app::CreateRuntimeSupport {pkgdir} {
    # Create the critcl-rt.tcl file in the generated package. This
    # provides the code which dynamically assembles at runtime the
    # package loading code, i.e. the 'package ifneeded' command
    # expected by Tcl package management.

    variable mydir
    set runtime [file join $mydir runtime.tcl]

    if {![file exists $runtime]} {
	critcl::error "can't find Critcl's package runtime support file \"runtime.tcl\""
    }

    set fd [open $runtime]
    set txt [read $fd]
    close $fd

    append txt [DummyCritclPackage]
    append txt [PlatformGeneric]

    set    fd [open [file join $pkgdir critcl-rt.tcl] w]
    puts  $fd $txt
    close $fd

    lappend v::meta [list included critcl-rt.tcl]
    return
}

proc ::critcl::app::DummyCritclPackage {} {
    # This command provides conditional no-ops for any of the critcl
    # procedures exported by the regular package, so that a .tcl file
    # with embedded C can also be its own companion file declaring Tcl
    # procedures etc. These dummy procedures are defined if and only
    # if their regular counterpart is not present.

    # Note: We are generating code checking each and every relevant
    # command individually to avoid trouble with different versions of
    # critcl which may export a differing set of procedures. This way
    # we will not miss anything just because we assumed that the
    # presence of critcl::FOO also implies having critcl::BAR, or not.

    # Append dummy Critcl procs
    # XXX This should be made conditional on the .tcl actually using itself as companion.
    append txt "\n\# Dummy implementation of the critcl package, if not present\n"

    foreach name [lsort [namespace eval ::critcl {namespace export}]] {
	switch $name {
	    compiled  { set result 1 }
	    compiling { set result 0 }
	    done      { set result 1 }
	    check     { set result 0 }
	    failed    { set result 0 }
	    load      { set result 1 }
	    Ignore    { append txt [DummyCritclCommand $name {
		namespace eval ::critcl::v {}
		set ::critcl::v::ignore([file normalize [lindex $args 0]]) .
	    }]
		continue
	    }
	    default   {
		append txt [DummyCritclCommand $name {}]
		continue
	    }
	}
	append txt [DummyCritclCommand $name "return $result"]
    }

    return $txt
}

proc ::critcl::app::DummyCritclCommand {name result} {
    append txt "if \{!\[llength \[info commands ::critcl::$name\]\]\} \{\n"
    append txt "    namespace eval ::critcl \{\}\n"
    append txt "    proc ::critcl::$name \{args\} \{$result\}\n"
    append txt "\}\n"
    return $txt
}

proc ::critcl::app::PlatformGeneric {} {
    # Return a clone of the platform::generic command, from the
    # currently loaded platform package. The generated package cannot
    # assume that the deployment environment contains this package. To
    # avoid trouble if the DP has the package the definition is made
    # conditional, i.e. the clone is skipped if the command is already
    # present.

    set body [info body ::platform::generic]

    append txt "\n# Define a clone of platform::generic, if needed\n"
    append txt "if \{!\[llength \[info commands ::platform::generic\]\]\} \{\n"
    append txt "    namespace eval ::platform \{\}\n"
    append txt "    proc ::platform::generic \{\} \{"
    append txt [join [split $body \n] "\n    "]
    append txt "\}\n"
    append txt "\}\n\n"

    return $txt
}

proc ::critcl::app::AssembleTEA {} {
    LogLn {Assembling TEA hierarchy...}

    set libdir  [CreateLibDirectory]
    set libname [file rootname [file tail $v::outname]]
    set pkgdir  [file join $libdir $libname]

    LogLn "\tPackage: $pkgdir"

    file mkdir $pkgdir

    # Get a proper version number
    set ver 0.0
    if {[llength $v::ver]} {
	set ver [lindex $v::ver 0] 
    }
    # Get a proper organization this is licensed by
    set org Unknown
    if {[llength $v::org]} {
	set org [lindex $v::org 0] 
    }

    PlaceTEASupport    $pkgdir $libname $ver $org
    PlaceCritclSupport $pkgdir
    PlaceInputFiles    $pkgdir

    # Last, meta data for the TEA setup.
    CreateTeapotMetadata $pkgdir
    return
}

proc ::critcl::app::CreateLibDirectory {} {
    set libdir [file normalize $v::libdir]
    if {[file isfile $libdir]} {
	critcl::error "can't package $v::shlname - $libdir is not a directory"
    } elseif {![file isdirectory $libdir]} {
	file mkdir $libdir
    }

    return $libdir
}

proc ::critcl::app::CreateIncludeDirectory {} {
    set incdir [file normalize $v::incdir]
    if {[file isfile $incdir]} {
	::critcl::error "can't package $v::shlname headers - $incdir is not a directory"
    } elseif {![file isdirectory $incdir]} {
	file mkdir $incdir
    }

    return $incdir
}

proc ::critcl::app::PlaceTEASupport {pkgdir pkgname pversion porg} {
    # Create the configure.in file in the generated TEA
    # hierarchy.

    LogLn "\tPlacing TEA support..."

    foreach {pmajor pminor} [split $pversion .] break
    if {$pminor eq {}} { set pminor 0 }
    if {$pmajor eq {}} { set pmajor 0 }

    variable mydir
    set tea [file join $mydir tea]

    if {![file exists $tea]} {
	critcl::error "can't find Critcl's TEA support files"
    }

    # Copy the raw support files over.
    foreach f [glob -directory $tea *] {
	file copy $f $pkgdir

	if {[file tail $f] eq "tclconfig"} {
	    foreach f [glob -directory $tea/tclconfig *] {
		lappend v::meta [list included tclconfig/[file tail $f]]
	    }
	} else {
	    lappend v::meta [list included [file tail $f]]
	}
    }

    # Basic map for the placeholders in the templates

    set now  [clock seconds]
    set year [clock format $now -format {%Y}]
    set now  [clock format $now]
    set map  [list \
		 @@CRITCL@@     "\"$::argv0 $::argv\"" \
		 @@PNAME@@   $pkgname \
		 @@PMAJORV@@ $pmajor  \
		 @@PMINORV@@ $pminor  \
		 @@PFILES@@  "\\\n\t[join $v::teasrc " \\\n\t"]" \
		 @@PORG@@    $porg \
		 @@YEAR@@    $year \
		 @@NOW@@     $now]
    set cmap $map
    set mmap $map

    # Extend map with stubs API data

    if {![llength $v::imported]} {
	lappend cmap @@API@@ {}
	lappend mmap @@API@@ {} @@APIUSE@@ {}
    } else {
	set macros {}
	# Creating the --with-foo-include options for imported APIs.

	lappend macros "#-----------------------------------------------------------------------"
	lappend macros "## TEA stubs header setup"
	lappend macros ""
	foreach api $v::imported {
	    set capi [string map {:: _} $api]

	    lappend macros  "CRITCL_TEA_PUBLIC_PACKAGE_HEADERS(\[$capi\])"
	    lappend mvardef "CRITCL_API_${capi}_INCLUDE = @CRITCL_API_${capi}_INCLUDE@"
	    lappend mvaruse "-I \$(CRITCL_API_${capi}_INCLUDE)"
	}
	lappend cmap @@API@@    \n[join $macros \n]\n
	lappend mmap @@API@@    \n[join $mvardef \n]\n
	lappend mmap @@APIUSE@@ " \\\n\t\t[join $mvaruse " \\\n\t\t"]"
    }

    # Extend map with custom user configuration data.

    if {![llength $v::config]} {
	lappend cmap @@UCONFIG@@ {}
	lappend mmap @@UCONFIG@@ {} @@UCONFIGUSE@@ {}
    } else {

	# Note: While we could assume that the user-specified
	# configuration options of a single file are consistent with
	# each other here we have a union of options from multiple
	# files. No such assumption can be made. Thus, we unique the
	# list, and then check that each option name left has a unique
	# definition as well.

	set ok 1
	array set udef {}
	set uclist [lsort -unique $v::config]
	foreach uc $uclist {
	    set oname [lindex $uc 0]
	    if {[info exists udef($oname)]} {
		LogLn "\t    Inconsistent definition for $oname"
		LogLn "\t    (1) $uc"
		LogLn "\t    (2) $udef($oname)"
		set ok 0
		continue
	    }
	    set udef($oname) $uc
	}
	if {!$ok} {
	    ::critcl::error "Conflicting user-specified configuration settings."
	}

	# Creating the --(with,enable,disable)-foo options for
	# user-specified configuration options.

	lappend macros "#-----------------------------------------------------------------------"
	lappend macros "## TEA user option setup"
	lappend macros ""
	foreach uc $uclist {
	    lassign $uc oname odesc otype odefault

	    if {$otype eq "bool"} {
		set odefault [expr {$odefault ? "yes" : "no"}]
		if {$odesc eq {}} {
		    set odesc "--enable-$oname"
		}
		append odesc " (default: $odefault)"

		lappend macros  "CRITCL_TEA_BOOL_CONFIG(\[$oname\],\n\t\[$odefault\],\n\t\[$odesc\])"
	    } else {
		if {$odesc eq {}} {
		    set odesc "--with-$oname"
		}
		append odesc " (default: $odefault, of [join $otype {, }])"

		lappend macros  "CRITCL_TEA_WITH_CONFIG(\[$oname\],\n\t\[[join $otype { }]\],\n\t\[$odefault\],\n\t\[$odesc\])"
	    }

	    lappend mvardef "CRITCL_UCONFIG_${oname} = @CRITCL_UCONFIG_${oname}@"
	    lappend mvaruse "\$(CRITCL_UCONFIG_${oname})"
	}
	lappend cmap @@UCONFIG@@    \n[join $macros \n]\n
	lappend mmap @@UCONFIG@@    \n[join $mvardef \n]\n
	lappend mmap @@UCONFIGUSE@@ " \\\n\t\t[join $mvaruse " \\\n\t\t"]"
    }

    # Postprocess a few files (configure.in, Makefile.in).

    Map  [file join $pkgdir configure.in] $cmap
    Map  [file join $pkgdir Makefile.in]  $mmap
    Map  [file join $pkgdir Config.in]    $map

    # At last locate a suitable autoconf (2.59+), and generate
    # configure from the configure.in.

    set here [pwd]
    cd $pkgdir
    if {$::tcl_platform(platform) eq "windows"} {
	# msys/mingw, cygwin, or other unix emulation on windows.
	exec sh [LocateAutoconf 1]
    } else {
	exec [LocateAutoconf 0]
    }
    file delete -force autom4te.cache

    lappend v::meta [list included configure]

    cd $here

    return
}

proc ::critcl::app::Map {path map} {
    set fd  [open $path r]
    set txt [read $fd]
    close $fd

    set txt [string map $map $txt]

    set    fd  [open $path w]
    puts -nonewline $fd $txt
    close $fd

    return
}

proc ::critcl::app::PlaceCritclSupport {pkgdir} {
    LogLn "\tPlacing Critcl support..."

    set c [file join $pkgdir critcl]
    set l [file join $c lib]
    file mkdir $l

    # Locate the critcl packages, and their forward compatibility
    # support packages, and copy them into the TEA hierarchy for use
    # by the generated Makefile.
    foreach {pkg dir} {
	critcl           critcl
	critcl::app      app-critcl
	critcl::util     critcl-util
	dict84           util84
	stubs::container stubs
    } {
	set cmd      [package ifneeded $pkg [package require $pkg]]
	set location [file dirname [lindex $cmd end]]

	# Squash any soft-links, which Tcl would copy as links.
	set location [file dirname [file normalize $location/__]]
	file copy $location $l/$dir
    }

    # Generate a suitable main.tcl. Note that this main file sources
    # the critcl packages directly, to ensure that the build uses the
    # code put into the generated TEA hierarchy, and is not influenced
    # by whatever is installed outside.

    set     pfiles {}
    lappend pfiles util84/lassign util84/dict
    lappend pfiles stubs/container  stubs/reader    stubs/genframe
    lappend pfiles stubs/gen_decl   stubs/gen_macro stubs/gen_slot
    lappend pfiles stubs/gen_header stubs/gen_init  stubs/gen_lib
    lappend pfiles stubs/writer
    lappend pfiles critcl/critcl app-critcl/critcl critcl-util/util

    set fd [open [file join $pkgdir critcl main.tcl] w]
    puts $fd [join \
		  [list \
		       "# Required packages: cmdline, md5" \
		       "# Optional: tcllibc, Trf, md5c, cryptkit (md5 acceleration)" \
		       "# Enforce usage of the local critcl packages." \
		       "foreach p \{\n\t[join $pfiles \n\t]\n\} \{" \
		       {    source [file dirname [info script]]/lib/$p.tcl} \
		       "\}" \
		       {critcl::app::main $argv}] \n]
    close $fd

    # Add to set of included files.
    lappend v::meta [list included critcl/main.tcl]
    foreach p $pfiles {
	lappend v::meta [list included critcl/lib/$p.tcl]
    }
    return
}

proc ::critcl::app::PlaceInputFiles {pkgdir} {
    LogLn "\tPlacing input files..."

    # Main critcl source file(s), plus companions

    foreach f $v::src {
	#LogLn "\tB   $f"

	set dst [file join src [file tail $f]]
	lappend v::meta [list included $dst]

	set dst [file join $pkgdir $dst]
	file mkdir [file dirname $dst]
	file copy $f $dst
    }

    foreach {f cf} $v::cfiles {
	set base [file dirname $f]
	foreach f [lsort -unique $cf] {
	    set fs [file join $base $f]

	    #LogLn "\tC   $fs"

	    set dst [file join src $f]
	    lappend v::meta [list included $dst]

	    set dst [file join $pkgdir $dst]

	    file mkdir [file dirname $dst]
	    file copy $fs $dst
	}
    }
    return
}

proc ::critcl::app::LocateAutoconf {iswin} {
    set ac [auto_execok autoconf2.50]

    if {$ac eq {}} {
	return -code error "autoconf 2.59 or higher required, not found"
    }

    if {$iswin} {
	# msys/mingw, cygwin, or other unix emulation on windows.
	set cmd [linsert $ac 0 exec sh]
    } else {
	set cmd [linsert $ac 0 exec]
    }

    set v [lindex [split [eval [linsert $cmd end --version]] \n] 0 end]

    if {![package vsatisfies $v 2.59]} {
	return -code error "$ac $v is not 2.59 or higher, as required"
    }

    return $ac
}

# # ## ### ##### ######## ############# #####################

namespace eval ::critcl::app {
    # Path of the application package directory.
    variable myself [file normalize [info script]]
    variable mydir [file dirname $myself]

    variable options {
	I.arg L.arg cache.arg clean config.arg debug.arg force help
	keep libdir.arg pkg show showall target.arg targets
	test tea showtarget includedir.arg enable.arg disable.arg
	v -version
    }

    # Application state
    namespace eval v {
	# - -- --- ----- -------- ------------- ---------------------
	# Data collected from the command line.

	variable verbose    0 ;# Level of chattering written during a run.
	variable src       {} ;# List of files to process.

	variable actualplatform {} ;# Target platform, with x-compile information resolved.

	variable shlname   "" ;# Name of the shlib to generate (-pkg, -tea).
	variable outname   "" ;# Name of the shlib dir to use (-pkg, -tea).
	variable libdir   lib ;# Place for the package (-pkg, -tea).
	variable incdir   include ; # Directory to put the -pkg include files into (stubs export),
                                    # and search in (stubs import)
	variable keep       0 ;# Boolean flag. Default: Do not keep generated .c files.
	variable debug     {} ;# List of debug modes to activate.
	variable cache     {} ;# User specified path to the directory for the result cache.
	variable uc        {} ;# List. User specified configuration data.

	# Build mode. Default is to fill the result
	# cache. Alternatives are building a package (-pkg), or
	# assembling/repackaging for TEWA (-tea).

	variable mode cache ;# pkg, tea

	# - -- --- ----- -------- ------------- ---------------------
	# Data accumulated while processing the input files.

	variable failed      0  ;# Number of build failures encountered.
	variable clibraries {}  ;# External libraries used. To link the final shlib against.
	variable ldflags    {}  ;# Linker flags.
	variable objects    {}  ;# The object files to link.
	variable edecls     {}  ;# Initialization function decls for the pieces (C code block).
	variable initnames  {}  ;# Initialization function calls for the pieces (C code block).
	variable tsources   {}  ;# Tcl companion sources.
	variable mintcl     8.4 ;# Minimum version of Tcl required to run the package.
	variable preload    {}  ;# List of libraries declared for preload.
	variable license    {}  ;# Accumulated licenses, if any.
	variable pkgs       {}  ;# List of package names for the pieces.
	variable inits      {}  ;# Init function names for the pieces, list.
	variable meta       {}  ;# All meta data declared by the input files.

	# critcl::scan results
	variable org        {}  ;# Organization package is licensed by
	variable ver        {}  ;# Version of the package.
	variable cfiles     {}  ;# Companion files (.tcl, .c, .h, etc).
	variable teasrc     {}  ;# Input file(s) transformed for use in the Makefile.in.
	variable imported   {}  ;# List of stubs APIs imported from elsewhere.
	variable config     {}  ;# List of user-specified configuration settings.
	# variable meta         ;# See above.
    }
}

# # ## ### ##### ######## ############# #####################
return