This file is indexed.

/usr/share/gnu-smalltalk/scripts/Package.st is in gnu-smalltalk-common 3.2.4-2.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
"======================================================================
|
|   Smalltalk package installer
|
|
 ======================================================================"


"======================================================================
|
| Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
| Written by Paolo Bonzini.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
|
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
| details.
|
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
 ======================================================================"

[PackageLoader fileInPackage: 'NetClients'] on: Error do: [:ex | ex return].


DynamicVariable subclass: CurrentCommand [
]


Package extend [
    srcdir [
	^self baseDirectories last
    ]

    isStarPackageBody [
	^self baseDirectories first isKindOf: VFS.ArchiveFile
    ]

    starFileName [
	| dir |
	self isStarPackageBody ifFalse: [ self halt ].
	^self baseDirectories first asString
    ]

    runCommand: aCommand [
	self isStarPackageBody
	    ifTrue: [ aCommand runOnStar: self ]
	    ifFalse: [ aCommand runOnPackage: self ]
    ]
]

Kernel.PackageContainer subclass: StarPackageFile [
    | name |

    StarPackageFile class >> on: aFile [
	^self new file: aFile; yourself
    ]

    StarPackageFile class >> on: aFile name: aString [
	^self new file: aFile; name: aString; yourself
    ]

    baseDirectoriesFor: aPackage [
	^self file zip
    ]

    name [
	^name
    ]

    name: aString [
	name := aString
    ]

    refresh: loadDate [
	| package |
	package := Kernel.StarPackage file: self file.
	name isNil ifFalse: [ package name: self name ].
        self packages at: package name put: package loadedPackage
    ]
]

Kernel.PackageContainer subclass: RemotePackageFile [
    RemotePackageFile class >> on: aFile [
	^self new file: aFile; yourself
    ]

    testPackageValidity: package [ ]

    refresh: loadDate [
        | file |
	self file withReadStreamDo: [ :fileStream |
	    self parse: fileStream ]
    ]
]

Kernel.PackageContainer subclass: PackageFile [
    | srcdir |

    PackageFile class >> on: aFile [
	^self new file: aFile; yourself
    ]

    srcdir [
	^srcdir
    ]

    srcdir: aString [
	srcdir := aString
    ]

    baseDirectoriesFor: aPackage [
	| srcdirFile builddirPrefix |
	self srcdir isNil ifTrue: [ ^{ file path } ].

	"See if the file is in srcdir or builddir.  In any case, we want to
	 look for files first in the builddir, and secondarily in srcdir."
	srcdirFile := self file pathFrom: self srcdir.
	builddirPrefix := Directory working pathFrom: self srcdir.
	^(srcdirFile startsWith: builddirPrefix, Directory pathSeparatorString)
	    ifFalse: [ {
		"file is in srcdir."
		(File name: srcdirFile) parent.
		self file parent } ]
	    ifTrue: [ {
		"file is in builddir."
		self file parent.
		(self srcdir / (self file pathFrom: Directory working)) parent } ]
    ]

    refresh: loadDate [
        | file |
	self file withReadStreamDo: [ :fileStream |
	    self parse: fileStream ]
    ]
]


DynamicVariable subclass: MainPackage []

Kernel.PackageContainer subclass: PackageCheckout [
    | url checkoutDirectory |

    PackageCheckout class >> cacheRoot [
	^Directory userBase / 'cache'
    ]

    PackageCheckout class >> on: anURL [
	^self new url: anURL; yourself
    ]

    baseDirectoriesFor: aPackage [
	| mainPackage |
	mainPackage := MainPackage value.
	mainPackage isNil ifTrue: [ ^{ self checkoutDirectory } ].
	aPackage = mainPackage ifTrue: [ ^{ self checkoutDirectory } ].

	^{ mainPackage baseDirectories first
	    / (aPackage name copyFrom: mainPackage name size + 2) }
    ]

    checkoutDirectory [
	| dir name |
	checkoutDirectory isNil ifTrue: [
	    name := '%1__%2__%3' % {
		url scheme. url host.
		NetClients.URL encode: url path }.
	    checkoutDirectory := self class cacheRoot / name ].
	^checkoutDirectory
    ]

    checkout [
	self subclassResponsibility
    ]

    url [
	^url
    ]

    url: anURL [
	url := anURL
    ]

    parseFile: aFile [
	aFile withReadStreamDo: [ :fileStream |
	    self parse: fileStream ]
    ]

    parse: aFileStream [
	| addedPackages mainPackage |
	addedPackages := super parse: aFileStream.
	addedPackages size > 1 ifTrue: [ ^addedPackages ].

	mainPackage := addedPackages first.
	MainPackage use: mainPackage during: [
	    mainPackage prerequisites do: [ :each || file |
	        ((each startsWith: mainPackage name, '-') and: [
		    (file := mainPackage baseDirectories first
		        / (each copyFrom: mainPackage name size + 2)
		        / 'package.xml') exists ])
			    ifTrue: [ self parseFile: file ]]].

	^addedPackages
    ]

    refresh: loadDate [
	self checkout.
	(self checkoutDirectory / 'package.xml') exists ifFalse: [
	    Command dryRun ifTrue: [^self].
	    ^self error: 'cannot find package.xml in checked out repository'].

	self file: self checkoutDirectory / 'package.xml'.
	self parseFile: self file.
    ]
]


PackageCheckout subclass: SvnPackageCheckout [
    checkout [
	| realUrl command saveDir |
	self checkoutDirectory exists
	    ifFalse: [
	        self checkoutDirectory emitMkdir.
		realUrl := url copy.
		url scheme = 'svn+http' ifTrue: [ realUrl scheme: 'http' ].
		url host = '' ifTrue: [ realUrl := realUrl path ].
	        command := 'svn checkout %1 .' % {realUrl} ]
	    ifTrue: [
		command := 'svn update' ].

        ('cd %1 && ' % { self checkoutDirectory }, command) displayNl.
        saveDir := Directory working.
	Command
	    execute: [
	        Directory working: self checkoutDirectory.
		Smalltalk system: command ]
	    ensure: [ Directory working: saveDir ]
    ]
]


PackageCheckout subclass: GitPackageCheckout [
    checkout [
	| realUrl command saveDir |
	self checkoutDirectory exists
	    ifFalse: [
	        self checkoutDirectory emitMkdir.
		realUrl := url copy.
		url scheme ~ 'git+(https?|rsync)' ifTrue: [
		     realUrl scheme: (url scheme copyFrom: 5) ].
		url host = '' ifTrue: [ realUrl := realUrl path ].
	        command := 'git clone --depth 1 %1 .' % {realUrl} ]
	    ifTrue: [
		command := 'git pull' ].

        ('cd %1 && ' % { self checkoutDirectory }, command) displayNl.
        saveDir := Directory working.
	Command
	    execute: [
	        Directory working: self checkoutDirectory.
		Smalltalk system: command ]
	    ensure: [ Directory working: saveDir ].

	url fragment isNil ifFalse: [
	    command := 'git checkout origin/%1' % {url fragment}.

            ('cd %1 && ' % { self checkoutDirectory }, command) displayNl.
	    Command
	        execute: [
	            Directory working: self checkoutDirectory.
		    Smalltalk system: command ]
	        ensure: [ Directory working: saveDir ] ]
    ]
]


Kernel.PackageDirectories subclass: PackageFiles [
    | srcdir |

    parseStarFile: file [
	^(StarPackageFile on: file)
	    refresh;
	    yourself
    ]

    parseStarFile: file name: aString [
	^(StarPackageFile on: file name: aString)
	    refresh;
	    yourself
    ]

    parseXMLFile: file [
	^(PackageFile on: file)
	    srcdir: self srcdir;
	    refresh;
	    yourself
    ]

    parse: fileName [
	| file packageFile |
	file := File name: fileName.
	^('*.star' match: fileName)
	    ifFalse: [ self parseXMLFile: file ]
	    ifTrue: [ self parseStarFile: file ]
    ]

    addURL: urlString [
	| url localFile package found |
	url := NetClients.URL fromString: urlString.

	"Remote package.xml: download it to find the `real' URL."
	('*.xml' match: url path)
	    ifTrue: [
		localFile := File name: url entity localFileName.
		package := RemotePackageFile on: localFile.
		package parse: url readStream.
		found := false.
		package packages do: [ :each |
		    (each url notNil and: [each url notEmpty]) ifTrue: [
			found := true.
		        each url = urlString ifTrue: [
			    ^self error: 'infinite loop in package.xml urls' ].
			self addURL: each url ]].
		found ifTrue: [^self].
		^self error: 'url element not found in remote XML file' ].

	"Remote .star file: download it and install."
	('*.star' match: url path)
	    ifTrue: [
		^self add: (self
		    parseStarFile: (File name: url entity localFileName)
		    name: (File stripPathFrom: (File stripExtensionFrom: url path))) ].
	
	(url scheme ~ '^svn(\+|$)')
	    ifTrue: [
		^self add: ((SvnPackageCheckout on: url)
		    refresh;
		    yourself) ].
	
	(url scheme ~ '^git(\+|$)')
	    ifTrue: [
		^self add: ((GitPackageCheckout on: url)
		    refresh;
		    yourself) ].

	(url scheme ~ '^(https?|ftp|file)$')
	    ifTrue: [ self error: 'invalid URL %1' % {url} ]
	    ifFalse: [ self error: 'unknown protocol %1' % {url scheme} ]
    ]

    addFile: fileName [
	self add: (self parse: fileName).
    ]

    filesDo: aBlock [
	(dirs collect: [ :each | each fileName ]) asSet do: aBlock
    ]

    srcdir [
	^srcdir
    ]

    srcdir: aString [
	srcdir := aString
    ]
]

File extend [
    emitZipDir: dir [
	| saveDir |
	self emitRemove.
	('cd %1 && %2 -n .st:.xml -qr %3 .' % { dir. Command zip. self }) displayNl.
        saveDir := Directory working.
	Command
	    execute: [
	        Directory working: dir name.
		Smalltalk system: '%1 -n .st:.xml -qr %2 .' % { Command zip. self }
	    ]
	    ensure: [ Directory working: saveDir ]
    ]

    emitRemove [
	('rm -f %1' % { self }) displayNl.
	Command execute: [
	    self exists ifTrue: [ self remove ] ].
    ]

    emitSymlink: dest [
	| destFile |
	('%1 -f %2 %3' % { Command symLink. self. dest }) displayNl.
	Command execute: [
	    destFile := File name: dest.
	    destFile exists ifTrue: [ destFile remove ].
	    self symlinkAs: dest ].
    ]

    emitInstall: dest [
	| destFile srcStream destStream mode |
	mode := self isExecutable ifTrue: [ 8r755 ] ifFalse: [ 8r644 ].
	destFile := File name: dest.
	('%1 -m %2 %3 %4' % {
	    Command install. mode printString: 8. self. destFile })
		displayNl.
	Command
	    execute: [
	        destFile exists ifTrue: [ destFile remove ].
	        srcStream := self readStream.
		destStream := destFile writeStream.
	        destStream nextPutAll: srcStream.
	    ]
	    ensure: [
		destStream isNil ifFalse: [ destStream close ].
		srcStream isNil ifFalse: [ srcStream close ].
		destFile mode: mode
	    ].
    ]

    emitMkdir [
	| doThat |
	self exists ifTrue: [ ^self ].
	Command execute: [ self parent emitMkdir ].
	('mkdir %1' % { self }) displayNl.
	Command execute: [ Directory create: self name ].
    ]
]

Object subclass: Command [
    | installDir options |

    Command class >> selectionOptions [
	^#()
    ]

    Command class >> current [
	^CurrentCommand value
    ]
    Command class >> execute: aBlock [
	self dryRun ifFalse: [ aBlock value ]
    ]
    Command class >> execute: aBlock ensure: ensureBlock [
	self dryRun ifFalse: [ aBlock ensure: ensureBlock ]
    ]
    Command class >> dryRun [
	^self current isOption: 'dry-run'
    ]

    Command class >> zip [
	^(Smalltalk getenv: 'XZIP') ifNil: [ 'zip' ]
    ]
    Command class >> install [
	^(Smalltalk getenv: 'INSTALL') ifNil: [ 'install' ]
    ]
    Command class >> symLink [
	^(Smalltalk getenv: 'LN_S') ifNil: [ 'ln -s' ]
    ]

    options [
	| optionsCollection |
	optionsCollection := OrderedCollection new.
	options keysDo: [ :opt |
	    (options at: opt) do: [ :arg |
	        optionsCollection add: opt->arg ]].
	^optionsCollection
    ]

    options: aCollection [
	options := Dictionary new.
	aCollection do: [ :assoc |
	    (options at: assoc key ifAbsentPut: [ OrderedCollection new ])
	        addLast: assoc value.
	    (self isValidOption: assoc key) ifFalse: [
	        self error: ('--%1 invalid for this mode' % {assoc key}) ] ]
    ]

    isValidOption: aString [
	^#('srcdir' 'target-directory' 'destdir' 'image-file'
	   'dry-run' 'kernel-directory' 'verbose') includes: aString
    ]

    isOption: aString [ ^options includesKey: aString ]
    optionsAt: aString [ ^options at: aString ]
    optionAt: aString [ ^self optionAt: aString ifAbsent: [ nil ] ]

    optionAt: aString ifAbsent: aBlock [
	| value |
	value := options at: aString ifAbsent: [ ^aBlock value ].
	value size > 1 ifTrue: [ self error: '--%1 given multiple times' % {aString} ].
	^value first
    ]

    validateDestDir: destdir installDir: instDir [ 
	instDir isNil ifTrue: [ ^self ].
	(instDir asFile isAbsolute not and: [ destdir notEmpty ])
            ifTrue: [
		self error: '--destdir used with relative target directory' ]
    ]

    prolog [
	| destdir instDir |
	destdir := self optionAt: 'destdir' ifAbsent: [ '' ].
	instDir := self optionAt: 'target-directory' ifAbsent: [ nil ].

	self validateDestDir: destdir installDir: instDir.
	installDir :=
	    File name: destdir, (instDir ifNil: [ self defaultInstallDir asString ])
    ]

    defaultInstallDir [ ^Directory image ]
    installDir [ ^installDir ]

    defaultFiles [ ^#() ]
    runOnAll: args [ self subclassResponsibility ]

    executeOnAll: args [
	CurrentCommand use: self during: [
	    | packageArgs |
	    self prolog.
	    packageArgs := args.
            packageArgs isEmpty ifTrue: [ packageArgs := self defaultFiles ].
            packageArgs isEmpty ifTrue: [ self error: 'no packages given' ].
	    self runOnAll: packageArgs]
    ]
]


Command subclass: PkgDownload [
    PkgDownload class >> selectionOptions [
	^#('download')
    ]

    PkgDownload class >> urlBase [
	^'http://smalltalk.gnu.org/project'
    ]

    isValidOption: aString [
	"Let PkgInstall decide later."
	^true
    ]

    prolog []

    runOnAll: args [
	| urls |
	urls := args collect: [ :each || pkg url |
	    pkg := PackageLoader packageAt: each ifAbsent: [ nil ].
	    url := pkg ifNotNil: [ pkg url ].
	    url ifNil: [ '%1/%2/package.xml' % { self class urlBase. each } ] ].
		
	PkgInstall new
            options: self options;
            executeOnAll: urls
    ]
]

Command subclass: PackageCommand [
    | packages |

    buildPackages [
	| srcdir |
	packages := PackageFiles new.
	srcdir := self optionAt: 'srcdir' ifAbsent: [ '.' ].
	(srcdir = '.' or: [ (File fullNameFor: srcdir) = Directory working ])
	    ifTrue: [ srcdir := nil ].

	packages srcdir: srcdir.
	^packages
    ]

    packages [
	packages isNil ifTrue: [ self buildPackages ].
	^packages 
    ]

    srcdir [
	^self packages srcdir ifNil: [ '.' ]
    ]

    addFile: aString [
	aString ~ '^[a-z+]*://'
	    ifTrue: [
		self handleURLs
		    ifFalse: [ self error: 'URLs are not supported by this mode' ]
		    ifTrue: [ self packages addURL: aString ] ]
	    ifFalse: [ self packages addFile: aString ]
    ]

    handleURLs [ ^false ]

    run [ self packages do: [ :pkg | pkg runCommand: self ] ]
    runOnStar: aStarPackage [ self runOnPackage: aStarPackage ]
    runOnPackage: aPackage [ ]

    runOnAll: args [
	args do: [ :each | self addFile: each ].
	self run
    ]
]

PackageCommand subclass: PkgDist [
    PkgDist class >> selectionOptions [
	^#('dist')
    ]

    validateDestDir: destdir installDir: instDir [ 
	(destdir isEmpty and: [ instDir isNil ]) ifTrue: [
	    self error: 'using --dist without specifying --distdir' ].
    ]

    defaultInstallDir [ ^'' ]
    run [
	super run.

	"Distribute package files, unless they are automatically generated
	 from autoconf."
	packages filesDo: [ :each |
	    | destName autoconfName srcdir |
	    destName := File stripPathFrom: each.
	    srcdir := self srcdir / (File pathFor: each).
	    autoconfName := destName, '.in'.
	    (srcdir includes: autoconfName)
		ifFalse: [
		    self distribute: (File name: each)
			as: destName
			in: nil ] ]
    ]

    isValidOption: aString [
	(#('all-files' 'copy') includes: aString) ifTrue: [^true].
	^super isValidOption: aString
    ]

    distribute: srcFile as: file in: dir [
	| destName baseDir |
	baseDir := self installDir.
	dir isNil ifFalse: [ baseDir := baseDir / dir ].
	destName := baseDir nameAt: file.
	(self isOption: 'copy')
	    ifTrue: [ srcFile emitInstall: destName ]
	    ifFalse: [ srcFile emitSymlink: destName ]
    ]

    runOnPackage: aPackage [
	| dirs files baseDir |
	files := (self isOption: 'all-files')
	    ifTrue: [ aPackage allFiles ]
	    ifFalse: [ aPackage allDistFiles ].

        dirs := files collect: [ :file | File pathFor: file ].
	dirs := dirs asSet remove: '' ifAbsent: [ ]; asSortedCollection.

	baseDir := self installDir.
	aPackage relativeDirectory isNil ifFalse: [
	    baseDir := baseDir / aPackage relativeDirectory ].

	baseDir emitMkdir.
        dirs do: [ :dir | (baseDir / dir) emitMkdir ].

        files do: [ :file || srcFile destName |
	    srcFile := aPackage fullPathOf: file.
	    self distribute: srcFile as: file in: aPackage relativeDirectory ]
    ]
    runOnStar: aPackage [
	self error: 'cannot distribute sources from .star file'
    ]
]

PackageCommand subclass: PkgInstall [
    | tmpDir |
    isValidOption: aString [
	(#('load' 'test') includes: aString) ifTrue: [^true].
	^super isValidOption: aString
    ]

    handleURLs [ ^true ]

    run [
        "Create the installation directory."
        self installDir emitMkdir.
	[ super run ] ensure: [
	    tmpDir isNil ifFalse: [ tmpDir all remove ] ].

	(Command dryRun not and: [ self isOption: 'load' ])
	    ifTrue: [ ^self loadPackages ].

	(self isOption: 'test') ifTrue: [ self runTests ]
    ]

    runTests [
	"Run SUnit tests, used unless --load is given too."
	| script result |
	script := ''.
	self packages do: [ :each || pkg |
	    pkg := each.
            script := script, ' ', pkg sunitScript.
            pkg test notNil ifTrue: [
                pkg := pkg test.
                script := script, ' ', pkg sunitScript ].
            pkg fileIn ].

	(PackageLoader packageAt: #SUnit) loaded ifFalse: [ ^self ].
	script isEmpty ifTrue: [ ^self ].

	result := TestSuitesScripter run: script quiet: false verbose: false.
	result runCount = result passedCount
	    ifFalse: [ ObjectMemory quit: 1 ]
    ]

    loadPackages [
	"Call gst-load, needed because we added our code to the image."
	| gstPackage execDir gstLoad pat packageList |
	gstPackage := File executable.
	gstPackage stripPath = 'gst-tool'
	    ifTrue: [
		gstLoad := gstPackage.
		pat := '%1 gst-load -I %2 --kernel-directory %3 %4 %5' ]
	    ifFalse: [
		gstLoad := gstPackage directory / 'gst-load'.
		pat := '%1 -I %2 --kernel-directory %3 %4 %5' ].

	packageList := ''.
	self packages
	    do: [ :each | packageList := packageList, ' ', each name ].

	Smalltalk system: (pat % {
	    gstLoad.
	    File image.
	    Directory kernel.
	    (self isOption: 'test') ifTrue: [ '--test' ] ifFalse: [ '' ].
	    packageList })
    ]

    tmpDir [
	tmpDir isNil ifTrue: [
            tmpDir := Directory createTemporary: Directory temporary / 'gstar-'.
            ('mkdir %1' % { tmpDir }) displayNl ].
	^tmpDir
    ]

    runOnPackage: aPackage [
	| pkg destFile dirs files baseDir |
	baseDir := self tmpDir / aPackage name.
	pkg := aPackage copy.
	pkg relativeDirectory: nil.

	baseDir emitMkdir.
	Command
	    execute: [
	        (baseDir / 'package.xml') withWriteStreamDo: [ :s |
	            pkg printOn: s ].

	        files := pkg allFiles.
                dirs := files collect: [ :file | File pathFor: file ].
	        (dirs asSet remove: '' ifAbsent: []; asSortedCollection)
		    do: [ :dir | (baseDir / dir) emitMkdir ].

                files do: [ :file || srcFile |
	            srcFile := (aPackage fullPathOf: file).
	            srcFile emitSymlink: (baseDir nameAt: file) ].

	        (self installDir / aPackage name, '.star')
		    emitZipDir: baseDir
	    ]
	    ensure: [ baseDir all remove ].
    ]

    runOnStar: aPackage [
	| destFile |
	destFile := self installDir nameAt: aPackage name, '.star'.
	(File name: aPackage starFileName) emitInstall: destFile.
    ]
]

PackageCommand subclass: PkgUninstall [
    PkgUninstall class >> selectionOptions [
	^#('uninstall')
    ]

    run [
        super run.
        packages filesDo: [ :each | (File name: each) emitRemove ]
    ]

    runOnPackage: aPackage [
	| baseDir |
	baseDir := self installDir.
	aPackage relativeDirectory isNil ifFalse: [
	    baseDir := baseDir / aPackage relativeDirectory ].

	aPackage allFiles do: [ :file |
	    (baseDir / file) emitRemove ]
    ]

    runOnStar: aPackage [ ]
]

PackageCommand subclass: ListCommand [
    validateDestDir: destdir installDir: installDir [
	destdir = ''
	    ifFalse: [ self error: '--destdir not needed with --list-files' ].
	installDir isNil
	    ifFalse: [ self error: '--target-directory not needed with --list-files' ]
    ]
    defaultInstallDir [ ^'.' ]
]

ListCommand subclass: PkgList [
    PkgList class >> selectionOptions [
	^#('list-files' 'no-install')
    ]

    isValidOption: aString [
	(#('list-files' 'vpath' 'load') includes: aString) ifTrue: [^true].
	^super isValidOption: aString
    ]

    run [
	| base vpathBase vpath source test listFiles |
	super run.
	listFiles := self optionsAt: 'list-files'.
	vpath := self isOption: 'vpath'.
	source := self isOption: 'load'.
	test := self isOption: 'test'.
	base := self installDir.
	vpathBase := File name: self srcdir.

        listFiles do: [ :each || package files |
	    package := self packages at: each.
	    files := source
		ifFalse: [ package allFiles ]
		ifTrue: [ package fileIns ].

	    (test and: [ source and: [ package test notNil ]])
		ifTrue: [ files := files, package test fileIns ].

	    files do: [ :file |
		| path relativePath |
		path := package fullPathOf: file.
		relativePath := base pathTo: path.
		(vpath and: [ (relativePath indexOfSubCollection: '../') > 0 ])
		    ifTrue: [ relativePath := vpathBase pathTo: path ].

		relativePath displayNl ] ]
    ]
]

ListCommand subclass: PkgPackageList [
    PkgPackageList class >> selectionOptions [
	^#('list-packages')
    ]

    runOnPackage: aPackage [ aPackage name displayNl ]
]

PackageCommand subclass: PkgPrepare [
    | srcFile |
    PkgPrepare class >> selectionOptions [
	^#('prepare')
    ]

    validateDestDir: destdir installDir: installDir [
	destdir = ''
	    ifFalse: [ self error: '--destdir not needed with --prepare' ].
	installDir isNil
	    ifFalse: [ self error: '--target-directory not needed with --prepare' ]
    ]

    defaultFiles [
	(File exists: self srcdir, '/package.xml')
	    ifTrue: [ srcFile := 'package.xml' ].
	(File exists: self srcdir, '/package.xml.in')
	    ifTrue: [ srcFile := 'package.xml.in' ].
	^{ srcFile }
    ]

    addFile: aString [
	| f |
	srcFile isNil ifTrue: [
	    f := self srcdir / aString.
            f exists
	        ifTrue: [ srcFile := (File name: self srcdir) pathTo: f ].

	    f := f, '.in'.
            f exists
	        ifTrue: [ srcFile := (File name: self srcdir) pathTo: f ].

            (File exists: aString)
		ifTrue: [ srcFile := File name: aString ].

	    srcFile isNil ifTrue: [ self error: '%1 not found' % {srcFile} ]
	].

	super addFile: aString.
    ]

    run [
        | base configureAC makefileAM gstIN |
	base := File name: self srcdir.
	configureAC := base at: 'configure.ac'.
	makefileAM := base at: 'Makefile.am'.
	gstIN := base at: 'gst.in'.

	configureAC exists ifFalse: [
	    'creating configure.ac' displayNl.
	    Command dryRun ifFalse: [
	        configureAC withWriteStreamDo: [ :ws | self writeConfigure: ws ] ] ].
	gstIN exists ifFalse: [
	    'creating gst.in' displayNl.
	    Command dryRun ifFalse: [
	        gstIN withWriteStreamDo: [ :ws | self writeGstIn: ws ] ] ].
	makefileAM exists ifFalse: [
	    'creating Makefile.am' displayNl.
	    Command dryRun ifFalse: [
	        makefileAM withWriteStreamDo: [ :ws | self writeMakefile: ws ] ] ]
    ]

    writeGstIn: ws [
	ws nextPutAll:
'#! /bin/sh
abs_top_builddir=@abs_top_builddir@
: ${LIBTOOL=$abs_top_builddir/libtool}

exec $LIBTOOL --mode=execute @PACKAGE_DLOPEN_FLAGS@ @GST@ ${1+"$@"}
'
    ]

    writeConfigure: ws [
	| numPackages pkgName tarName |
	numPackages := 0.
	self packages do: [ :each |
	    pkgName := each name.
	    numPackages := numPackages + 1 ].

	numPackages = 1 ifFalse: [ pkgName := 'XYZ' ].

        tarName := 'gst-',
	    (pkgName asLowercase copyReplacingAllRegex: '[-_ ]+' with: '-').

	ws nextPutAll: ('AC_PREREQ(2.59)
AC_INIT([GNU Smalltalk package %1], [0.0], , %2)
AC_CONFIG_SRCDIR([%3])

AM_INIT_AUTOMAKE

AM_PATH_GST([2.95c], , [AC_MSG_ERROR([GNU Smalltalk not found])])
' % { pkgName. tarName. srcFile }).

	packages filesDo: [ :each |
	    self writeConfigureEntry: each to: ws ].

	ws nextPutAll: '
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([gst], [chmod +x gst])
AC_OUTPUT
'
    ]

    writeConfigureEntry: each to: ws [
	| pkgName buildPath srcPath pkgSrcDir relPkgSrcDir generated |
	buildPath := Directory working pathTo: each.
	srcPath := (File name: self srcdir) pathTo: each.

	pkgSrcDir := srcPath size < buildPath size
	    ifTrue: [ File pathFor: srcPath ifNone: [ self srcdir ] ]
	    ifFalse: [ File append: (File pathFor: buildPath) to: self srcdir ].

	relPkgSrcDir := (File name: self srcdir) pathTo: pkgSrcDir.

	('*.in' match: each)
	    ifTrue: [
		srcPath := srcPath allButLast: 3.
		buildPath := buildPath allButLast: 3.
		generated := true ]
	    ifFalse: [
		generated := (File name: srcPath, '.in') exists ].

	(File name: each) withReadStreamDo: [ :rs |
	    | pkg |
	    [ pkg := Package parse: rs ]
	        on: Kernel.PackageNotAvailable
	        do: [ :ex | ex resume ].
	    pkgName := pkg name ].

	ws nextPutAll: ('GST_PACKAGE_ENABLE([%1], [%2]' % {
	    pkgName. relPkgSrcDir }).

	generated ifTrue: [
	    ws nextPutAll: (', , , [%1]' % {
		(File name: relPkgSrcDir) pathTo: srcPath }) ].

	ws nextPutAll: ')'; nl.
    ]

    writeMakefile: ws [
	ws nextPutAll:
'AUTOMAKE_OPTIONS = foreign
AM_CPPFLAGS = $(GST_CFLAGS)

## Example:
##
## gst_module_ldflags = -rpath $(gstlibdir) -module \
##        -no-undefined -export-symbols-regex gst_initModule
##
## noinst_HEADERS = md5.h sha1.h
## gstlib_LTLIBRARIES = digest.la
## digest_la_SOURCES = digest.c md5.c sha1.c
## digest_la_LDFLAGS = $(gst_module_ldflags)


### -------------------------------------- ###
### Rules completed by GST_PACKAGE_ENABLE. ###
### -------------------------------------- ###

DISTCLEANFILES = pkgrules.tmp
all-local:
clean-local::
install-data-hook::
dist-hook::
uninstall-local::

@PACKAGE_RULES@
'
    ]
]


Object subclass: PackageManager [
    | mode args options |

    ModeClasses := nil.
    PackageManager class >> classForMode: aString [
	^self modeClasses at: aString ifAbsent: [ nil ]
    ]

    PackageManager class >> modeClasses [
	ModeClasses isNil ifTrue: [
	    ModeClasses := Dictionary new.
	    Command allSubclassesDo: [ :each |
	        each selectionOptions do: [ :opt |
		    ModeClasses at: opt put: each ] ] ].

	^ModeClasses
    ]

    mode [
	mode isNil ifTrue: [ mode := PkgInstall ].
	^mode
    ]

    mode: aClass [
	(mode notNil and: [ mode ~~ aClass ])
	    ifTrue: [ self error: 'multiple modes specified' ].
	mode := aClass
    ]

    options [
	options isNil ifTrue: [ options := OrderedCollection new ].
	^options
    ]

    addOption: opt argument: arg [
	self options add: opt->arg
    ]

    addArgument: arg [
	args isNil ifTrue: [ args := OrderedCollection new ].
	args add: arg
    ]

    helpString [
	^'Usage:
    gst-package [OPTION]... ARGS...

Operation modes:
        --install               make or install STAR packages (default)
        --uninstall             remove the packages mentioned in the FILES
        --dist                  copy files instead of creating STAR files.
        --prepare               create configure.ac or Makefile.am
        --list-files PKG        just output the list of files in the package
        --list-packages         just output the list of packages in the files

        --download, --update    download package from smalltalk.gnu.org or
                                from its specified URL

	--help                  display this message and exit
	--version               print version information and exit

Common suboptions:
    -n, --dry-run               print commands without running them
	--srcdir DIR            look for non-built files in directory DIR
	--distdir DIR	        for --dist, place files in directory DIR
	--destdir DIR	        prefix the destination directory with DIR
        --target-directory DIR  install the files in DIR (unused for --dist)
    -I, --image-file=FILE       load into the specified image
        --kernel-dir=PATH       use the specified kernel directory
    -v, --verbose               print extra information while processing

--install suboptions:
	--test                  run unit tests after merging
	--load                  also load the Smalltalk files in the image

--list-files suboptions:
	--load                  only list files that are filed in when loading
	--test                  with --load, also include unit test files
	--vpath			Omit path to srcdir for files that are there

--dist suboptions:
        --all-files             Process all files, not just non-built ones
        --copy                  Do not create symbolic links

All operation modes except --download (or its synonym --update) accept
paths to package.xml files or .star files, including remote URLs.
--download and --update accept names of packages, which will be searched
in the current system or on smalltalk.gnu.org)
or URLs to package.xml or .star files.

Except in uninstall and list files mode, gst-package requires write
access to the GNU Smalltalk image directory, and merges the XML package
files on the command line with that file.

The default target directory is ', Directory image name, '

'
    ]

    displayHelpAndQuit: anInteger [
	<category: 'help printing'>

        self helpString displayOn:
            (anInteger = 1 ifTrue: [stderr] ifFalse: [stdout]).
        ObjectMemory quit: anInteger
    ]

    parseArguments: args [
        Getopt
            "--kernel-directory and --image-file are processed by gst-tool.
	     --no-load present for backwards compatibility, it is now the
	     default. --no-install is also present for backwards compatibility."
	     parse: args
	     with: '-h|--help --no-load --test --load --no-install --uninstall
                --dist -t|--target-directory: --list-files: --list-packages
                --prepare --srcdir: --distdir|--destdir: -n|--dry-run
		--all-files --vpath --copy -I|--image-file: --kernel-directory:
		--update|--download --version -v|-V|--verbose'

            do: [ :opt :arg || modeClass |
	        opt = 'help' ifTrue: [
		    self displayHelpAndQuit: 0 ].

	        opt = 'version' ifTrue: [
		    ('gst-package - %1' % {Smalltalk version}) displayNl.
		    ObjectMemory quit: 0 ].

		opt = 'verbose' ifTrue: [
		    OutputVerbosity := 1.
		    FileStream verbose: true ].

		modeClass := self class classForMode: opt.
		modeClass notNil ifTrue: [ self mode: modeClass ].

		(modeClass isNil or: [ arg notNil ])
		    ifTrue: [
			opt isNil
			    ifFalse: [ self addOption: opt argument: arg. ]
			    ifTrue: [ self addArgument: arg ]]]
            ifError: [
                self displayHelpAndQuit: 1 ].
    ]

    run [
	| command |
	(args isNil or: [args isEmpty]) ifTrue: [ self displayHelpAndQuit: 1 ].
        command := self mode new.
        command options: self options.
        command executeOnAll: args.
    ]
]


[ PackageManager new
	parseArguments: Smalltalk arguments;
	run
]
    on: Error
    do: [ :ex |
	('gst-package: ', ex messageText, '
') displayOn: stderr.
	ex pass. ObjectMemory quit: 1 ].