about summary refs log tree commit diff
path: root/buildtools/debian/mkdeb
blob: 6c475530dac5851236c2e6e1fd1e28371e43cab2 (plain) (blame)
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
#!/usr/bin/perl
###############################################################################
#                                mkdeb
###############################################################################
#
#  This generates a Debian package file (.deb) to install Sourceforge
#  Netpbm on a Debian system.
#
#  This is especially useful because Debian does not have a good Debian
#  package (what Debian contains is derived from Sourceforge Netpbm ca.
#  2002).
#
#  The dependencies this package declares are those that can be satisfied by
#  Debian 8 (Jessie) and Debian 9 (Stretch).  Netpbm works fine on other
#  versions of Debian, but you may have to change the dependencies in this
#  program or ignore dependencies at install time.
###############################################################################

use strict;
use warnings;
use English;
use Getopt::Long;

my $TRUE=1; my $FALSE = 0;



sub parseCommandLine(@) {

    local @ARGV = @_;  # GetOptions takes input from @ARGV only

    my %cmdline;

    my $validOptions = GetOptions(\%cmdline,
                                  "buildtools=s",
                                  "arch=s",
                                  "pkgdir=s",
        );

    if (!$validOptions) {
        print(STDERR "Invalid option syntax.\n");
        exit(1);
    }
    if (@ARGV > 0) {
        print(STDERR "This program takes no non-option arguments.  " .
              "You specified ",
              scalar(@ARGV), "\n");
        exit(1);
    } 

    return(\%cmdline);
}



sub writeFile($$$$) {
    my ($fileLinesR, $fileName, $executable, $errorR) = @_;

    my $success = open(FILE, ">$fileName");
    if ($success) {
        if ($executable eq 'EXECUTABLE') {
            chmod(0755, $fileName);
        } else {
            chmod(0644, $fileName);
        }
        foreach (@{$fileLinesR}) { print FILE; }
        close(FILE);
    } else {
        $$errorR = "Unable to open the file " .
            "'$fileName' for writing.  Errno=$ERRNO\n";
    }
}



sub netpbmVersion($) {
    my ($pkgdir) = @_;

    my $versionFileName = "$pkgdir/VERSION";

    my $versionOpened = open(VERSION, "<$versionFileName");

    my $retval;
    my $error;

    if (!$versionOpened) {
        $error = "Unable to open '$versionFileName' for reading.  " .
            "Errno=$ERRNO\n";
    } else {
        my $version = <VERSION>;
        chomp($version);
        $retval = $version;
        close(VERSION);
    }

    if ($error) {
        print("Failed to determine the version of Netpbm from the package, "
              . "so that will not be correct in netpbm.config and netpbm.pc.  "
              . $error . "\n");
        $retval = "???";
    }
    return $retval;
}



sub control($$) {
    my ($release, $architecture) = @_;
#-----------------------------------------------------------------------------
#  The contents for the package control file, as a hash reference.  In the
#  referenced hash, there is one key for each line of the control file.  The
#  key and value in the hash are the key and value for the line of the control
#  file.
#-----------------------------------------------------------------------------

    # Because developers of some of the dependent libraries frequently switch
    # to distributing versions not backward compatible with what they
    # previously distributed, and Debian always packages the currently
    # distributed version, it is virtually impossible to produce a Netpbm
    # package that works in multiple Debian versions.  This program is coded
    # to create a package that works on the Debian system the Netpbm
    # maintainer currently uses to build the Debian packages he distributes.
    # If you are building for any other version of Debian, you'll have to
    # modify this code.

    # Note that the backward incompatibility is usually only at a binary
    # level, not source level.  And sometimes the only incompatibility for
    # Netpbm purposes is that the soname has changed so that Linux will refuse
    # to run a Netpbm program built for Debian N on Debian N-1.

    # The following is for Debian 9.
    
    my %control;

    my $debianNativeNetpbm = 
        'netpbm, ' .
        'libnetpbm10, ' .
        'libnetpbm10-dev, ' .
        'netpbm-dev, ' .
        'netpbm-nonfree, ' .
        'pbmwbmp, ' .
        'pnmtopng, ' .
        'ucbmpeg';

    $control{'Package'} = 'netpbm-sf';
    $control{'Version'} = $release;
    $control{'Architecture'} = $architecture;
    $control{'Maintainer'} = 'Bryan Henderson <bryanh@giraffe-data.com>';
    $control{'Installed-Size'} = '6164';
    $control{'Depends'} =
        'libc6, ' .
        'libjpeg62, ' .
        'libpng16-16, ' .
        'libtiff5, ' .
        'libx11-6, ' .
        'libxml2, ' .
        'zlib1g, ' .
        'ghostscript, ' .
        'perl, ' .
        'perl-base, ' .
        'bash'
        ;
    # Note: Instead of libjpeg62, Debian 10 has libjpeg62-turbo and Ubuntu 18
    # has libjpeg-turbo8.
    $control{'Conflicts'} = $debianNativeNetpbm;
    $control{'Replaces'} = $debianNativeNetpbm;
    $control{'Provides'} = 
        'netpbm, ' .
        'pbmwbmp, ' .
        'pnmtopng, ' .
        'netpbm-dev, ' .
        'libnetpbm10'
        ;
    $control{'Section'} = 'graphics';
    $control{'Priority'} = 'optional';
    $control{'Section'} = 'graphics';
    $control{'Description'} = 'Graphics conversion tools between image formats
 Netpbm is a toolkit for manipulation of graphic images, including
 conversion of images between a variety of different formats. There
 are over 300 separate tools in the package including converters for
 more than 80 graphics formats.  This is the Super Stable version from
 the Sourceforge Netpbm project, unmodified.';

    return \%control;
}



sub writeControlFile($$) {
    my ($controlR, $fileName) = @_;

    open(CTL, '>', $fileName)
        or die "Can't open '$fileName': $ERRNO";

    while (my ($key, $value) = each %{$controlR}) {
        print CTL ("$key: $value\n");
    }
    
    close(CTL);
}



sub createScripts($$) {
    my ($dpkgDirName, $buildToolsDir) = @_;

    my @scriptList = ('postinst', 'postrm');

    my @scriptFileList = map("$buildToolsDir/debian/$_", @scriptList);

    system('cp', @scriptFileList, "$dpkgDirName/DEBIAN/") &&
        die("Failed to copy postinst, etc. to '$dpkgDirName/DEBIAN'.");

    my @createdFileList = map("$dpkgDirName/DEBIAN/$_", @scriptList);

    chmod(0755, @createdFileList);
}



sub createDirOrDie($) {
    my ($newDirName) = @_;

    mkdir($newDirName)
        or die("Couldn't create directory '$newDirName'.  $ERRNO");

    chmod(0755, $newDirName);
}



sub 
processTemplate($$$) {
    my ($templateR, $infoR, $outputR) = @_;

    my @output;

    foreach (@{$templateR}) {
        if (m{^@}) {
            # Comment -- ignore it.
        } else {
            if (defined($infoR->{VERSION})) {
                s/\@VERSION\@/$infoR->{VERSION}/;
            }
            if (defined($infoR->{BINDIR})) {
                s/\@BINDIR@/$infoR->{BINDIR}/;
            }
            if (defined($infoR->{LIBDIR})) {
                s/\@LIBDIR@/$infoR-.{LIBDIR}/;
            }
            if (defined($infoR->{LINKDIR})) {
                s/\@LINKDIR@/$infoR->{LINKDIR}/;
            }
            if (defined($infoR->{DATADIR})) {
                s/\@DATADIR@/$infoR->{DATADIR}/;
            }
            if (defined($infoR->{INCLUDEDIR})) {
                s/\@INCLUDEDIR@/$infoR->{INCLUDEDIR}/;
            }
            if (defined($infoR->{MANDIR})) {
                s/\@MANDIR@/$infoR->{MANDIR}/;
            }
            push(@output, $_);
        }
    }
    $$outputR = \@output;
}





sub makeConfig($$$) {
    my ($fileName, $templateSubsR, $netpbmPkgDir) = @_;
#-----------------------------------------------------------------------------
# Install 'netpbm-config' -- a program you run to tell you things about
# how Netpbm is installed.
#-----------------------------------------------------------------------------
    my $error;

    my $configTemplateFilename = $netpbmPkgDir . "/config_template";

    my $templateOpened = open(TEMPLATE, "<$configTemplateFilename");
    if (!$templateOpened) {
        $error = "Can't open template file '$configTemplateFilename'.\n";
    } else {
        my @template = <TEMPLATE>;

        close(TEMPLATE);

        processTemplate(\@template, $templateSubsR, \my $fileContentsR);

        writeFile($fileContentsR, $fileName, 'EXECUTABLE', \$error);
    }
    if ($error) {
        print(STDERR "Failed to create the Netpbm configuration program.  " .
              "$error\n");
    }
}



sub makePkgConfig($$$) {
    my ($fileName, $templateSubsR, $netpbmPkgDir) = @_;
#-----------------------------------------------------------------------------
# Install a pkg-config file (netpbm.pc) - used by the 'pkg-config' program to
# find out various things about how Netpbm is installed.
#-----------------------------------------------------------------------------
    my $error;

    my $pcTemplateFilename = "$netpbmPkgDir/pkgconfig_template";

    my $templateOpened = open(TEMPLATE, "<$pcTemplateFilename");
    if (!$templateOpened) {
        $error = "Can't open template file '$pcTemplateFilename'.\n";
    } else {
        my @template = <TEMPLATE>;

        close(TEMPLATE);

        processTemplate(\@template, $templateSubsR,
                        \my $fileContentsR);

        writeFile($fileContentsR, $fileName, 'NOTEXECUTABLE', \$error);
    }
    if ($error) {
        print(STDERR "Failed to create the Netpbm Pkg-config file.  " .
              "$error\n");
    }
}



sub makeManweb($$) {
    my ($dpkgDirName, $netpbmPkgDir) = @_;
#-----------------------------------------------------------------------------
#  Set up things so one can read the manual with e.g.
#
#    $ manweb pnmtojpeg
#-----------------------------------------------------------------------------
    my @manwebConfContents;

    push(@manwebConfContents, "#Configuration file for Manweb\n");
    push(@manwebConfContents, "webdir=/usr/man/web\n");

    createDirOrDie("$dpkgDirName/etc");

    my $manwebConfFileName = "$dpkgDirName/etc/manweb.conf";

    writeFile(\@manwebConfContents, $manwebConfFileName,
        'NOTEXECUTABLE', \my $error);

    if ($error) {
        die("Failed to create Manweb configuration file $manwebConfFileName");
    }
    createDirOrDie("$dpkgDirName/usr/man");

    system('cp', '--archive',
           "$netpbmPkgDir/man/web", "$dpkgDirName/usr/man/web") &&
               die("Failed to copy executables from '$netpbmPkgDir/bin' " .
                   "to '$dpkgDirName/usr/bin'");
}



sub buildDpkgBuildTree($$$$$) {
    my ($dpkgDirName, $release, $netpbmPkgDir, $architecture,
        $buildToolsDir) = @_;
#-----------------------------------------------------------------------------
#  Create the directory tree that is the input to Dpkg-deb --build.
#  This tree contains all the files to be installed, _plus_ the control
#  subdirectory named DEBIAN.
#-----------------------------------------------------------------------------
    print("Creating file tree for input to dpkg-deb --build as " .
          "'$dpkgDirName'\n");

    createDirOrDie($dpkgDirName);

    createDirOrDie("$dpkgDirName/DEBIAN");

    my $controlR = control($release, $architecture);

    writeControlFile($controlR, "$dpkgDirName/DEBIAN/control");

    createScripts($dpkgDirName, $buildToolsDir);

    createDirOrDie("$dpkgDirName/usr");

    system('cp', '--archive',
           "$netpbmPkgDir/bin", "$dpkgDirName/usr/bin") &&
               die("Failed to copy executables from '$netpbmPkgDir/bin' " .
                   "to '$dpkgDirName/usr/bin'");

    # doc.url is inappropriate with the program installed into the global
    # /usr/bin .
    unlink("$dpkgDirName/usr/bin/doc.url");

    system("cp", "--archive", "$netpbmPkgDir/include",
           "$dpkgDirName/usr/include") &&
               die("Failed to copy header files from " .
                   "'$netpbmPkgDir/include' " .
                   "to '$dpkgDirName/usr/include'");

    system("cp", "--archive",
           "$netpbmPkgDir/lib", "$dpkgDirName/usr/lib") &&
               die("Failed to copy libraries from '$netpbmPkgDir/lib' " .
                   "to '$dpkgDirName/usr/lib'");

    my @linkFileList = glob("$netpbmPkgDir/link/*");

    if (@linkFileList > 0) {
        system("cp", "--archive",
               @linkFileList, "$dpkgDirName/usr/lib/") &&
                   die("Failed to copy libraries from '$netpbmPkgDir/link' " .
                       "to '$dpkgDirName/usr/lib'");
    }
    createDirOrDie("$dpkgDirName/usr/share");

    system("cp", "--archive",
           "$netpbmPkgDir/misc", "$dpkgDirName/usr/share/netpbm") &&
               die("Failed to copy files from '$netpbmPkgDir/misc' " .
                   "to '$dpkgDirName/usr/share/netpbm'");

    # We install Netpbm in the default search path, so most of the values
    # 'netpbm-config' returns are null strings.
    my $templateSubsR =
    {VERSION    => $release,
     BINDIR     => '',
     LIBDIR     => '',
     LINKDIR    => '',
     DATADIR    => '/usr/share/netpbm',
     INCLUDEDIR => '',
     MANDIR     => ''};

    makeConfig("$dpkgDirName/usr/bin/netpbm-config", $templateSubsR,
               $netpbmPkgDir);

    createDirOrDie("$dpkgDirName/usr/lib/pkgconfig");

    makePkgConfig("$dpkgDirName/usr/lib/pkgconfig/netpbm.pc", $templateSubsR,
                  $netpbmPkgDir);

    # Beginning in Netpbm 10.78 (March 2017_, 'make package' doesn't package
    # the manweb stuff, so we no longer put it in the Debian package.
    #makeManweb($dpkgDirName, $netpbmPkgDir);
}



sub debianArchOfThisMachine() {

    # A lazy implementation that handles only the most common cases

    my $retval;

    my $arch = qx{'arch'};
    chomp($arch);

    if ($arch eq 'x86_64') {
        $retval = 'amd64';
    } elsif ($arch =~ m{i.86}) {
        $retval = 'i386';
    } else {
        die("Can't determine the Debian architecture classification of this " .
            "system.  You'll have to give a -arch option");
    }
    return $retval;
}



sub buildToolsDir($) {
    my ($cmdlineR) = @_;

    my $retval;

    if (exists($cmdlineR->{'buildtools'})) {
        $retval = $cmdlineR->{'buildtools'};
    } else {
        if (-f('./debian/mkdeb')) {
            $retval = '.';
        } else {
            die("The current directory does not appear to be 'buildtools' " .
                "subdirectory of a Netpbm source tree, so you will have " .
                "to use the -buildtools option to identify it");
        }
    }
    return $retval;
}



sub netpbmPkgDir($) {
    my ($cmdlineR) = @_;

    my $retval;

    if (exists($cmdlineR->{'pkgdir'})) {
        $retval = $cmdlineR->{'pkgdir'};
    } else {
        my $tmpdir = $ENV{TMPDIR} || "/tmp";

        my $defaultPkgDir = "$tmpdir/netpbm";

        if (-d($defaultPkgDir)) {
            $retval = $defaultPkgDir;
        } else {
            die("No directory '$defaultPkgDir' exists.  " .
                "You can specify the Netpbm package directory " .
                "(what 'make package' created), with a -pkgdir option");
        }
    }
    return $retval;
}



sub arch($) {
    my ($cmdlineR) = @_;

    my $retval;

    if (exists($cmdlineR->{'arch'})) {
        $retval = $cmdlineR->{'arch'};
    } else {
        $retval = debianArchOfThisMachine();
    }

    return $retval;
}



###############################################################################
#                               MAIN PROGRAM
###############################################################################

my $cmdlineR = parseCommandLine(@ARGV);

my $buildTools = buildToolsDir($cmdlineR);

my $netpbmPkgDir = netpbmPkgDir($cmdlineR);

my $arch = arch($cmdlineR);

my $release = netpbmVersion($netpbmPkgDir);

my $dpkgDirName = "/tmp/netpbm-sf-$release";
 
buildDpkgBuildTree($dpkgDirName, $release, $netpbmPkgDir, $arch,
                   $buildTools);

my $debFileName = 'netpbm-sf-' . $release . '_' . $arch . '.deb';

system('dpkg-deb', '--build', $dpkgDirName, $debFileName) &&
    die("dpgk-deb --build with input '$dpkgDirName' failed.");

system('rm', '--recursive', $dpkgDirName);