-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapt-buildrepo
executable file
·938 lines (770 loc) · 19.7 KB
/
apt-buildrepo
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
#! /usr/bin/perl -w
#
# apt-buildrepo: Generate an apt package repository
#
# Copyright (c) 2024 Network RADIUS
#
# This program 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 3 of
# the License, or (at your option) any later version.
#
# This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
#
# Matthew Newton
# 20 May 2024
#
use strict;
use vars qw($VERSION);
$VERSION = "0.01";
our $DEBUG = 0;
use Getopt::Long qw(GetOptions);
use POSIX qw/strftime/;
use File::Temp;
#
# Options passed in on the command line
#
my %opts = (
"root" => undef,
"pkgdir" => undef,
"conffile" => undef,
"suite" => undef,
"codename" => undef,
"origin" => undef,
"label" => undef,
"signby" => undef,
"gpgsecringfile" => undef,
"gpgpassfile" => undef,
);
#
# Print out the syntex/help text
#
sub syntax
{
my $rc = shift;
my $errmsg = shift;
select STDERR if $rc > 0;
if ($errmsg) {
select STDERR;
print "error: $errmsg\n\n";
}
print <<"EOF";
Syntax: $0 [options] <dir>
<dir> is the top-level repository directory.
Options are:
-p <pkgdir> - directory containing packages, relative to <dir>
-c <codename> - e.g. "buster"
-s <suite> - e.g. "testing"
-O <origin> - optional origin, e.g. "mycompany", added to the repo
-L <label> - optional label, e.g. "development packages"
-r <keyname> - name of gpg key to use for signing, repo will
be unsigned if not set
-k <keyring> - secret keyring file that contains the signing key
-P <gpgpass> - file containing plaintext password for secret key
-h - this help
EOF
exit $rc;
}
#
# Package to handle file checksums
#
# This currently calls out to other utilities (e.g. sha256sum)
# but could be updated to use perl modules instead.
#
# We cache any checksums to save having to calculate them
# multiple times.
#
package FileChecksum;
use strict;
use Carp;
sub new
{
my ($class, %data) = @_;
my $self = {
"cache" => {}
};
bless $self, $class;
$self->init(\%data);
}
sub init
{
my $self = shift;
my $data = shift;
$self->{"filename"} = $$data{"filename"};
return $self;
}
#
# Hacky way to calculate file checksum using CLI utils...
#
sub _read_sum
{
my $self = shift;
my $utility = shift;
my $cs = "";
my $fn = $self->{"filename"};
my $FH;
my $cmd = "$utility '$fn'";
open $FH, "$cmd |" or die "unable to run '$cmd'";
my $cksum = <$FH>;
close $FH;
if ($cksum =~ /^([^\s]+)\s+(.*)$/) {
die "unknown output running $cmd" unless $2 eq $fn;
$cs = $1;
} else {
die "failure getting checksum using '$cmd'";
}
return $cs;
}
#
# Private method to do common checks before calculating a
# checksum
#
sub _checks
{
my $self = shift;
croak "no filename set" unless defined $self->{"filename"};
croak "file " . $self->{"filename"} . " not readable" unless -r $self->{"filename"};
}
#
# Private methods to get and set checksum cache entries
#
sub _cache_get
{
my $self = shift;
my $type = shift;
return undef unless defined $self->{"cache"}{$type};
return $self->{"cache"}{$type};
}
sub _cache_set
{
my $self = shift;
my $type = shift;
my $value = shift;
$self->{"cache"}{$type} = $value;
return $value;
}
#
# Public methods to calculate different checksum types of a file
#
sub md5
{
my $self = shift;
$self->_checks();
my $cache = $self->_cache_get("md5");
return $cache if defined $cache;
my $sum = $self->_read_sum("md5sum");
return $self->_cache_set("md5", $sum);
}
sub sha1
{
my $self = shift;
$self->_checks();
my $cache = $self->_cache_get("sha1");
return $cache if defined $cache;
my $sum = $self->_read_sum("sha1sum");
return $self->_cache_set("sha1", $sum);
}
sub sha256
{
my $self = shift;
$self->_checks();
my $cache = $self->_cache_get("sha256");
return $cache if defined $cache;
my $sum = $self->_read_sum("sha256sum");
return $self->_cache_set("sha256", $sum);
}
sub sha512
{
my $self = shift;
$self->_checks();
my $cache = $self->_cache_get("sha512");
return $cache if defined $cache;
my $sum = $self->_read_sum("sha512sum");
return $self->_cache_set("sha512", $sum);
}
#
# Package to handle tasks related to a Debian .deb package file
#
package DebPackage;
use strict;
use Carp;
sub new
{
my ($class, %data) = @_;
my $self = {};
bless $self, $class;
$self->init(\%data);
}
sub init
{
my $self = shift;
my $data = shift;
$self->{"filename"} = $$data{"filename"} || undef;
$self->readpkg() if defined $self->{"filename"};
return $self;
}
#
# Public method (usually just called from init) to read all
# information about a Debian package file
#
sub readpkg
{
my $self = shift;
$self->{"pkgdata"} = {};
croak "no package filename set" unless defined $self->{"filename"};
croak "cannot read package '" . $self->{"filename"} . "'" unless -r $self->{"filename"};
if ($DEBUG) {
print "Reading package: ";
print $self->{"filename"} . "\n";
}
my $sum = FileChecksum->new("filename" => $self->{"filename"});
$self->{"pkgdata"}{"MD5sum"} = $sum->md5();
$self->{"pkgdata"}{"SHA1"} = $sum->sha1();
$self->{"pkgdata"}{"SHA256"} = $sum->sha256();
$self->{"pkgdata"}{"SHA512"} = $sum->sha512();
$self->read_deb_fields();
$self->read_deb_contents();
# Filename here will be incorrect for the repo, but is
# updated to the correct relative path elsewhere.
$self->{"pkgdata"}{"Filename"} = $self->{"filename"};
$self->{"pkgdata"}{"Size"} = (stat($self->{"filename"}))[7];
}
#
# Read control fields from package file
#
sub read_deb_fields
{
my $self = shift;
my $fn = $self->{"filename"};
my $field;
my $data = $self->{"pkgdata"};
my $FH;
open $FH, "dpkg-deb -I '$fn' |" or die "unable to run dpkg-deb";
while (my $l = <$FH>) {
next if $l =~ /^ new Debian/;
next if $l =~ /^ size \d+ bytes/;
next if $l =~ /^\s+\d+ bytes/;
if ($l =~ /^ ([A-Za-z-]+):\s(.*)$/) {
$field = $1;
$data->{$field} = $2;
}
elsif ($field && $l =~ /^\s\s(.*)$/) {
$data->{$field} .= "\n$1";
}
else {
die "unknown dpkg output '$l'";
}
}
close $FH;
}
#
# Read contents of package file
#
# Use `dpkg-deb` to do this for now as it's simplest.
#
sub read_deb_contents
{
my $self = shift;
my $fn = $self->{"filename"};
my $field;
my @contents;
my $FH;
open $FH, "dpkg-deb -c '$fn' |" or die "unable to run dpkg-deb";
while (my $l = <$FH>) {
if ($l =~ /^
[^\s]{10}
\s [^\s\/]+\/[^\s\/]+
\s+ \d+
\s \d{4}-\d\d-\d\d
\s+ \d\d:\d\d
\s+ .\/([^\s]+)
(?: \s+ -> .* )?
$/x) {
push @contents, $1 unless $1 =~ /\/$/;
}
}
close $FH;
$self->{"contents"} = \@contents;
}
#
# Store an attribute about this package
#
sub set_pkg_attr
{
my $self = shift;
my $attr = shift;
my $value = shift;
$self->{"pkgdata"}{$attr} = $value;
return $value;
}
#
# Get a package attribute we already stored
#
sub get_pkg_attr
{
my $self = shift;
my $attr = shift;
return $self->{"pkgdata"}{$attr};
}
#
# Get a list of all package attributes
#
sub get_all_attrs
{
my $self = shift;
my @attrnames = keys %{$self->{"pkgdata"}};
return @attrnames;
}
#
# Get the list of all files contained in this package
#
sub get_pkg_contents
{
my $self = shift;
return $self->{"contents"};
}
#
# The main program
#
package main;
#
# Search the archive root for all debian packages (except in the
# "dists" directory, which is where the repo control files go.
#
sub search_package_files
{
my $dir = shift;
my @pkgs = ();
my @dirs = ();
$dir =~ s/\/+$//;
if ($dir eq $opts{"root"}."/dists") {
print "skipping dists dir\n" if $DEBUG > 2;
return ();
}
print "> entering $dir\n" if $DEBUG > 2;
my $D;
opendir($D, $dir) or die "can't open directory $dir";
while (my $name = readdir($D)) {
next if $name =~ /^\.\.?$/;
if (-d "$dir/$name") {
push @dirs, $name;
}
elsif ($name =~ /\.deb$/) {
push @pkgs, "$dir/$name";
}
}
closedir($D);
# Recurse subdirectories
foreach my $subdir (@dirs) {
push @pkgs, search_package_files("$dir/$subdir");
}
print "< done with $dir\n" if $DEBUG > 2;
return @pkgs;
}
#
# Given an array of .deb filenames, create a new DebPackage
# object for each one containing all the package attributes,
# checksums, contents, etc.
#
sub scan_packages
{
my $pkgfiles = shift;
my $aroot = $opts{"root"};
my @packages = ();
foreach my $pkgfile (@$pkgfiles) {
print "adding package $pkgfile\n" if $DEBUG > 1;
my $pkg = new DebPackage(filename => $pkgfile);
push @packages, $pkg;
#
# Set correct filename for package relative to archive root
#
my $relfile = $pkgfile;
$relfile =~ s+^$aroot/++;
$pkg->set_pkg_attr("Filename", $relfile);
}
return \@packages;
}
#
# Get unique list of all architectures from list of package objects
#
sub find_archs
{
my $packages = shift;
my %archs = ();
foreach my $pkg (@$packages) {
my $pkgarch = $pkg->get_pkg_attr("Architecture");
$archs{$pkgarch} = 1;
}
my @a = sort keys %archs;
return \@a;
}
#
# Generate all repos for all packages
#
sub make_repo
{
my $packages = shift;
#
# Get an array of all used architectures
#
my $archs = find_archs($packages);
#
# Only support main for now, this script is supposed to be simple
#
my $component = "main";
#
# Possibly write to tmpdir and rename after all files
# created? For now just write directly to dists
#
my $distsdirname = "dists";
#
# Some common paths
#
my $distsdir = $opts{"root"} . "/$distsdirname";
my $suitedir = $distsdir . "/" . $opts{"suite"};
my $codedir = $distsdir . "/" . $opts{"codename"};
my $componentdir = $codedir . "/" . $component;
#
# Make required directories. Should we wipe the dists dir
# here too?
#
mkdir($distsdir);
die "unable to make '$distsdir'" unless -d $distsdir;
mkdir($codedir);
die "unable to mkdir '$codedir'" unless -d $codedir;
mkdir($componentdir);
die "unable to mkdir '$componentdir'" unless -d $componentdir;
my @indexfiles = ();
#
# Create the "binary-N" directory for each architecture found
#
foreach my $arch (@$archs) {
# There shouldn't be a "binary-all" directory in the repo.
next if $arch eq "all";
my $binarydir = $componentdir . "/binary-" . $arch;
my $packagesfile = $binarydir . "/Packages";
my $contentsfile = $componentdir . "/Contents-" . $arch;
my $reldir = $component . "/binary-" . $arch;
my $archreleasefile = $binarydir . "/Release";
mkdir($binarydir);
write_arch_release_file($archreleasefile, $arch, $component);
push @indexfiles, $reldir . "/Release";
write_packages_file($packagesfile, $packages, $arch);
do_compress("gzip", $packagesfile, $packagesfile.".gz");
do_compress("bzip2", $packagesfile, $packagesfile.".bz2");
push @indexfiles, $reldir . "/Packages";
push @indexfiles, $reldir . "/Packages.gz";
push @indexfiles, $reldir . "/Packages.bz2";
write_contents_file($contentsfile, $packages, $arch);
do_compress("gzip", $contentsfile, $contentsfile.".gz");
unlink($contentsfile);
push @indexfiles, $component . "/Contents-" . $arch . ".gz";
}
#
# Write and sign the top-level Release file
#
write_release_file(
"topdir" => $codedir,
"archs" => $archs,
"components" => [$component],
"indexes" => \@indexfiles,
);
sign_release_file($codedir);
}
#
# Write the Release file for a given architecture
#
sub write_arch_release_file
{
my $file = shift;
my $arch = shift;
my $component = shift;
my $FH;
open $FH, ">", $file or die "unable to open '$file' for writing";
print $FH "Origin: " . $opts{"origin"} . "\n" if defined $opts{"origin"};
print $FH "Label: " . $opts{"label"} . "\n" if defined $opts{"label"};
print $FH "Archive: " . $opts{"codename"} . "\n";
print $FH "Architecture: " . $arch . "\n";
print $FH "Component: " . $component . "\n";
close $FH;
}
#
# Write the Release file for the whole repository
#
sub write_release_file
{
my %relopt = @_;
my $top = $relopt{"topdir"} . "/";
my %indexsums;
my %filesizes;
my $maxlen = 1;
foreach my $indexfile (@{$relopt{"indexes"}}) {
my $size = (stat($top . $indexfile))[7];
$filesizes{$indexfile} = $size;
if (length($size) > $maxlen) {
$maxlen = length($size);
}
my $sum = FileChecksum->new("filename" => $top . $indexfile);
if (my $s = $sum->md5()) {
$indexsums{"MD5Sum"}{$indexfile} = $s;
}
if (my $s = $sum->sha1()) {
$indexsums{"SHA1"}{$indexfile} = $s;
}
if (my $s = $sum->sha256()) {
$indexsums{"SHA256"}{$indexfile} = $s;
}
if (my $s = $sum->sha512()) {
$indexsums{"SHA512"}{$indexfile} = $s;
}
}
my $FH;
open $FH, ">", $top . "Release";
print $FH "Origin: " . $opts{"origin"} . "\n" if defined $opts{"origin"};
print $FH "Label: " . $opts{"label"} . "\n" if defined $opts{"label"};
print $FH "Suite: " . $opts{"suite"} . "\n";
print $FH "Codename: " . $opts{"codename"} . "\n";
print $FH "Date: " . strftime("%a, %d %b %Y %T +0000", gmtime()) . "\n";
print $FH "Architectures: " . join(" ", @{$relopt{"archs"}}) . "\n";
print $FH "Components: " . join(" ", @{$relopt{"components"}}) . "\n";
print $FH "Description: Generated by apt-buildrepo\n";
foreach my $sum (sort keys %indexsums) {
print $FH "$sum:\n";
my @files = sort keys %{$indexsums{$sum}};
foreach my $f (@files) {
my $filesize = $filesizes{$f};
my $size = (" " x ($maxlen - length($filesize))) . $filesize;
print $FH " " . $indexsums{$sum}{$f} .
" " . $size .
" " . $f . "\n";
}
}
close $FH;
}
#
# The top-level Release file should be signed, both inline
# (InRelease) and separate (Release.gpg). gnupg is awful, but we
# do our best...
#
sub sign_release_file
{
my $dir = shift;
my $release = $dir . "/Release";
my $inrelease = $dir . "/InRelease";
my $signed = $dir . "/Release.gpg";
my $gpgcmd = "gpg --pinentry-mode=loopback --batch";
#
# Even if we can't write new signed files, always remove the old ones
# as they'll be invalid after creating a new Release file.
#
unlink $inrelease if -e $inrelease;
unlink $signed if -e $signed;
#
# We need a signing key otherwise we can't do anything at all
#
return unless defined $opts{"signby"};
if (defined $opts{"gpgpassfile"}) {
$gpgcmd .= " --passphrase-file '" . $opts{"gpgpassfile"} . "'";
}
#
# If a GPG secret keyring is provided then we need to import it. We
# also need a temporary key dir to import to. Otherwise we rely on the
# caller having set $GPGHOME if necessary.
#
my $gpghome = undef;
my $tmpdir = undef;
if (defined $opts{"gpgsecringfile"}) {
$tmpdir = File::Temp->newdir();
$gpghome = $tmpdir->dirname() . "/gpg";
mkdir $gpghome;
chmod 0700, $gpghome;
$gpgcmd .= " --homedir '$gpghome'";
system("$gpgcmd --import '" . $opts{"gpgsecringfile"} . "'");
}
#
# We should now be able to sign the Release files
#
system("$gpgcmd --armor --sign --clearsign --output '$inrelease' '$release'");
system("$gpgcmd --armor --sign --detach-sign --output '$signed' '$release'");
#
# When tmpdir goes out of scope, File::Temp will wipe the
# contents. undef just to make sure.
#
undef $tmpdir;
}
#
# Write the architecture Packages file, which contains details of
# all the packages in that architecture (or "all")
#
sub write_packages_file
{
my $file = shift;
my $packages = shift;
my $arch = shift;
my @attr_order = qw/
Package Priority Section Installed-Size Maintainer
Architecture Source Version Provides Depends Breaks
Recommends Suggests Filename Size MD5sum SHA1 SHA256
SHA512 Description Homepage
/;
my $FH;
open $FH, ">", $file or die "unable to open '$file' for writing";
foreach my $package (@$packages) {
my $pa = $package->get_pkg_attr("Architecture");
next unless $pa eq $arch or $pa eq "all";
my @allattrs = $package->get_all_attrs();
my %allattrs = map { $_ => 1 } @allattrs;
# write all standard keys in order
foreach my $attr (@attr_order) {
write_packages_field($FH, $package, $attr);
delete $allattrs{$attr};
}
# add any extra keys from the package that we missed above
foreach my $attr (sort keys %allattrs) {
write_packages_field($FH, $package, $attr);
}
print $FH "\n";
}
close $FH;
}
#
# Write an individual field - needs to handle multi-line fields
#
sub write_packages_field
{
my $fh = shift;
my $pkg = shift;
my $field = shift;
my $value = $pkg->get_pkg_attr($field);
return 0 unless defined $value;
$value =~ s/\n/\n /gs;
print $fh "$field: $value\n";
return 1;
}
#
# Write the Contents file for an architecture, which lists the
# file contents of all available packages
#
sub write_contents_file
{
my $file = shift;
my $packages = shift;
my $arch = shift;
my %contents = ();
my $FH;
foreach my $package (@$packages) {
my $pa = $package->get_pkg_attr("Architecture");
next unless $pa eq $arch or $pa eq "all";
# We just hope that the section is the same in multiple
# versions of the same package. If not, ....?
my $section = $package->get_pkg_attr("Section");
my $pkgname = $package->get_pkg_attr("Package");
my $location = $section . "/" . $pkgname;
my $pkgcontents = $package->get_pkg_contents();
foreach my $pkgfile (@$pkgcontents) {
$contents{$pkgfile} = $location;
}
}
open $FH, ">", $file or die "unable to open '$file' for writing";
print $FH "FILE LOCATION\n";
foreach my $pkgfile (sort keys %contents) {
print $FH $pkgfile . " " . $contents{$pkgfile} . "\n";
}
close $FH;
}
#
# Compress a file. Currently by calling an external utility
#
sub do_compress
{
my $compress = shift;
my $source = shift;
my $dest = shift;
system("$compress -c '$source' > '$dest'");
}
#
# Get command-line arguments
#
Getopt::Long::Configure qw(gnu_getopt);
GetOptions(
'pkgdir|p=s' => \$opts{"pkgdir"},
'codename|c=s' => \$opts{"codename"},
'suite|s=s' => \$opts{"suite"},
'origin|O=s' => \$opts{"origin"},
'label|L=s' => \$opts{"label"},
'keyname|r=s' => \$opts{"signby"},
'keyring|k=s' => \$opts{"gpgsecringfile"},
'passfile|P=s' => \$opts{"gpgpassfile"},
'help|h' => \$opts{"help"},
) or syntax(1, "bad options");
#
# Process args and do sanity checks
#
# This is the time to read a config file, if that ability is
# added in future
#
syntax(0) if $opts{"help"};
$opts{"root"} = $ARGV[0];
syntax(1, "no root directory given") unless defined $opts{"root"};
#
# Check root exists
#
$opts{"root"} =~ s/\/+$//;
die "root directory '".$opts{"root"}."' does not exist"
unless -d $opts{"root"};
#
# Check pkgdir exists
#
$opts{"pkgdir"} = "" unless defined $opts{"pkgdir"};
#
# Check pkgdir under root
#
die "package directory '".$opts{"root"}."/".$opts{"pkgdir"}."' does not exist"
unless -d $opts{"root"};
#
# Check codename
#
die "no codename given (-c)" unless defined $opts{"codename"};
$opts{"suite"} = $opts{"suite"} || $opts{"codename"};
#
# Check packages dir has been set (must be under the root)
#
syntax(1, "need packages dir") unless defined $opts{"pkgdir"};
#
# Sanity check the signing options
#
if (defined $opts{"gpgpassfile"} and ! -r $opts{"gpgpassfile"}) {
die "gpg password file '" . $opts{"gpgpassfile"} . "' not readable";
}
if (defined $opts{"gpgsecringfile"} and ! -r $opts{"gpgsecringfile"}) {
die "gpg secret keyring file '" . $opts{"gpgpassfile"} . "' not readable";
}
#
# On to the real stuff - search the packages directory and get a
# list of all .deb package files found
#
my @p = search_package_files($opts{"root"} . "/" . $opts{"pkgdir"});
if ($DEBUG) {
print "Found packages:\n";
foreach my $pkg (sort @p) {
print " - $pkg\n";
}
}
#
# Read information about all the packages
#
my $packages = scan_packages(\@p);
#
# TODO Check for duplicates (package name, architecture, version)
#
#check_package_duplicates(\@p);
#
# Create the repository files
#
make_repo($packages);