This file is indexed.

/usr/bin/emgrip is in emdebian-grip 3.1.0.

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

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
#!/usr/bin/perl

=head1 NAME

emgrip - converts a .deb to Emdebian Grip on-the-fly

=cut

=head1 Synopsis

 DEB_BUILD_OPTIONS='usegrip' emgrip [-v|--verbose] [-q|--quiet]
    [--vendor VENDOR] [-o|--outdir DIRECTORY] FILENAME

where FILENAME is a .dsc, a .changes or a .deb

 emgrip -?|-h|--help|--version

=cut

=head1 Description

Converts a .deb package to Emdebian Grip by unpacking, pruning
particular classes of files (determined by DEB_BUILD_OPTIONS or
the dpkg vendor support) and repacking with a shortened
description and the em1 version suffix.

For the Debian buildd, the unstable-grip vendor omits generation of
Emdebian TDebs and changes the suite name to use the -grip suffix.

When creating a public Emdebian Grip repository, it is strongly
recommended to specify .changes or .dsc files to allow the generation
of Emdebian TDebs from the source package as well as including the
source package itself in the repository.

In .dsc mode, C<emgrip> only processes the source package, as of
version 3.0.0

If a file exists with the same name in the output directory, it
will be overwritten.

=cut

=head1 Copyright and Licence

 Copyright (C) 1997-2000  Roman Hodek <roman@hodek.net>
 Copyright (C) 2000-2002  Colin Watson <cjwatson@debian.org>
 Copyright (C) 2002-2004  David Schleef <ds@schleef.org>
 Copyright (C) 2004  Nikita Youshchenko <yoush@cs.msu.su>
 Copyright (C) 2004  Raphael Bossek <bossekr@debian.org>
 Copyright (C) 2007-2011  Neil Williams <codehelp@debian.org>
 Copyright (C) 2005 Joey Hess <joeyh@debian.org>

 This package 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 <http://www.gnu.org/licenses/>.

=cut

use Carp;
use Cwd;
use Cwd 'realpath';
use POSIX;
use POSIX qw(:errno_h :signal_h);
use POSIX qw(locale_h);
use Locale::gettext;
use IO::Handle;
use Debian::Debhelper::Dh_Lib;
use File::Basename;
use File::Temp qw/tempfile tempdir/;
use Dpkg::Deps;
use strict;
use warnings;
use vars qw/ $progname $verbose $our_version $tmpdir $package
$nofailmsg $debname $mode %builds $deb $retval $outdir $nolintian
%options $essential $docs %files $help @filelist %origdirs $vendor
@src @debs @gripped $changes $arch $symbols $locale $noperl $vopts 
$vname $vshort $noscripts $base_arch $distchange /;

setlocale(LC_MESSAGES, "");
textdomain("emdebian-grip");

$progname = basename ($0);
$verbose = 1;
$our_version = &scripts_version();
$vendor = $ENV{"DEB_VENDOR"} if (defined $ENV{"DEB_VENDOR"});
$base_arch = `LC_ALL=C dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null`;
chomp $base_arch;
@src=();
%origdirs = ();

=head1 Emdebian Grip

The idea here is based on dpkg-cross:

 1. receive .deb or .changes as a command-line option
 2. parse DEB_BUILD_OPTIONS
 3. unpack the .deb as dpkg-cross does
 4. remove files as per DEB_BUILD_OPTIONS
 5. repack the .deb with an Emdebian version suffix.
 6. build new .deb and revise .changes so that the
     original source can be retained.
 7. Include the gripped packages into a local mirror
     to reduce the size of Packages.gz and the size of
     the mirror itself.

As few dependencies as possible - this script should
become a new package and have a new set of lintian
checks to allow perl etc.

 NO FUNCTIONAL CHANGES IN THE FINAL .DEB !

Conversion of Debian translations to Emdebian TDebs (where
supported by the build options) needs to be done inside the
source package so a .changes file is needed for this step.
If the .changes file references a Debian TDeb, emgrip will
run 'em_installtdeb --no-sign' inside the source package.

Individual packages containing translation files will have the
translation files removed and Debian TDebs are skipped.

Future versions may also understand Dpkg Classes
but until those are implemented, this uses brute force.

Set the build options by placing a file in
F</etc/dpkg/origins/> containing the build options in a
field called C<Grip-Build-Option> and call C<emgrip> with the
C<--vendor> option or C<DEB_VENDOR> environment variable.

dpkg-vendor fields used by emgrip with default values:

 Vendor: emdebian-grip
 Vendor-Name: Emdebian Grip
 Grip-Build-Option: usegrip
 Short-Desc-Suffix: gripped

The C<Short-Desc-Suffix> is wrapped in parentheses and
appended to the short description [default is 'gripped'].

=cut

=head1 DEB_BUILD_OPTIONS

If none of these options are set, emgrip does nothing.

 nodocs

Remove all content in /usr/share/doc/*/ except the copyright
file and compress the copyright file itself. Remove manpages
and info pages (translated or not). Also remove doc-base files
that describe the documentation that no longer exists.

 nohelp

Remove even help files from all packages, including -doc packages.
Includes removing /usr/share/gtk-doc/html and
/usr/share/gnome/help/\$package
Despite removing the help files, dependencies on help file utilities
like scrollkeeper will remain, at least for Lenny.

 noessential

Remove the 'Essential: yes' tag from DEBIAN/control - this
is a brute force removal, no additional dependencies are to
be generated (no patches) so care is needed here.

 nolintian

Remove all files from /usr/share/lintian/overrides and
/usr/share/linda/overrides - saves up to 1Mb.

 usegrip

Combination flag that sets all DEB_BUILD_OPTIONS for Emdebian
Grip - noessential, nohelp, nodocs, nolintian

 debiangrip

Debian-only flag which omits processing of TDebs, even for .changes
files then changes the distribution to match the Emdebian suites in
Debian. Other processing remains the same as the usegrip option.

 nosymbols

Currently being tested - the idea is to drop DEBIAN/symbols files
but this could have unpredictable effects on packages built on a
system lacking these symbols. The space saved by dropping these files
might not be worth the cost, so 'usegrip' does not yet activate
'nosymbols'. This might be suitable for Crush, maybe not Grip.

 noperl

Remove any file that is found to be a perl script text executable
by 'file' - used by Crush.

 usecrush

Combination flag that adds nosymbols and noperl to usegrip.

 noscripts

Drop all maintainer scripts from all packages irrespective of the
type of script. Includes removing debconf templates and config scripts.
md5sums files and triggers are also removed. Scripts are moved to a
F<scripts/> subdirectory of the output directory used by C<emgrip>.

 usebaked

Combination flag that adds noscripts and nosymbols to usegrip. Add
C<noperl> to use Baked for Crush.

=cut

=head1 Field removal

Functional control fields are retained but the question arises:

 Who defines functional?

Well, for the current purposes of Emdebian Grip, functional includes
only the following control fields:

 Package
 Source
 Version
 Architecture
 Maintainer
 Installed-Size
 Depends
 Pre-Depends
 Multi-Arch
 Conflicts
 Replaces
 Section
 Priority
 Description
 Task
 XS-Python-Version

Recommends is explicitly rejected, as is Suggests - the expectation
is that Emdebian Grip will disable Install-Recommends in apt anyway,
so that Gripped packages can be mixed with Debian or locally-built
packages.

Note also that 'Essential' is not preserved.

Homepage, VCS* and all X- control fields are dropped.

The net result is a (potentially) much cleaner dpkg database.

The description is trimmed to four lines (or three if the fourth line
is empty).

=cut

=head1 Automation

Relatively simple, but a few checks are needed in case your list of
possible packages includes packages already processed by dpkg-cross.

 #!/bin/sh
 set -e

 DIR=/tmp/myrepo/
 for deb in `ls /var/cache/apt/archives/*.deb`; do
    cross=`echo $deb | grep "\-.*-cross" || true`
    if [ ! -z "$cross" ]; then
        echo "skipping dpkg-cross package: $deb"
        continue
    fi
    DEB_BUILD_OPTIONS="nodocs nohelp usegrip" ./emgrip -o $DIR $deb
 done

With C<dpkg-vendor> support, DEB_BUILD_OPTIONS can be replaced by
setting the vendor.

A similar script should be able to cope with obtaining .changes files
to retain the source packages.

Despite using dcut internally, emgrip isn't set up to obtain the
.changes files directly from http://incoming.debian.org, a wrapper
script would be needed.

An additional wrapper is likely to be needed to allow an Emdebian Grip
repository to "catch-up" with the existing Debian mirrors, e.g. if the
first upload to the Grip repository is a binary-only upload without a
referenced .orig.tar.gz.

emgrip does ensure that the .tar.gz is copied into the output directory
and reprepro can locate the relevant files if the --ignore=missingfile
option is passed. Sadly, this functionality is not preserved in the
reprepro processincoming mode.

To ease the process of gripping an existing mirror, the
emdebian-grip-server package depends on 'reprepro' to create a filtered
Debian mirror that can be kept up to date, generates meta-data for
Emdebian Grip, builds gripped packages in an 'incoming/' directory and
includes the packages into the final Emdebian Grip repository.

Once a package has been "gripped", the binary packages could be
removed from the filtered mirror - the source packages (particularly
the .orig.tar.gz) are probably best retained in the filtered mirror.

=cut

while( @ARGV ) {
	$_= shift( @ARGV );
	last if m/^--$/;
	if (!/^-/) {
		unshift(@ARGV,$_);
		last;
	} elsif (/^(-\?|-h|--help|--version)$/) {
		&usageversion();
		exit (0);
	} elsif (/^(--vendor)$/) {
		$vendor = shift;
		if (not defined $vendor) {
			warn _g("Undefined vendor string.\n");
			next;
		}
		my $chk_vndr = `LC_ALL=C dpkg-vendor --vendor $vendor --query Grip-Build-Option 2>/dev/null`;
		if (($? != 0) or ($chk_vndr eq "\n")) {
			warn sprintf(_g("Unrecognised vendor: '%s'"), $vendor);
			undef ($vendor);
		} else {
			chomp ($chk_vndr);
			$vopts = $chk_vndr;
			$chk_vndr = `LC_ALL=C dpkg-vendor --vendor $vendor --query Vendor-Name 2>/dev/null`;
			chomp ($chk_vndr);
			$vname = $chk_vndr;
			$chk_vndr = `LC_ALL=C dpkg-vendor --vendor $vendor --query Short-Desc-Suffix 2>/dev/null`;
			chomp ($chk_vndr);
			$vshort = $chk_vndr;
		}
	} elsif (/^(-v|--verbose)$/) {
		$verbose++;
	} elsif (/^(-q|--quiet)$/) {
		$verbose--;
	} elsif (/^(-o|--outdir)$/) {
		$outdir = shift;
		die sprintf(_g("'%s' is not a directory.\n"), $outdir)
			if (not -d $outdir);
		$outdir =~ s:/$::;
		$outdir = realpath ($outdir);
		chomp ($outdir);
	} else {
		my $fail = $_;
		die sprintf(_g("%s: Unknown option: %s\n"), $progname, $fail);
	}
}

my $varg = (defined $vendor) ? "--vendor $vendor" : "";
my $chk_vndr = `LC_ALL=C dpkg-vendor $varg --query Grip-Build-Option 2>/dev/null`;
if (($? != 0) or ($chk_vndr eq "\n")) {
	undef $vendor;
} else {
	chomp ($chk_vndr);
	$vopts = $chk_vndr;
	$chk_vndr = `LC_ALL=C dpkg-vendor $varg --query Vendor-Name 2>/dev/null`;
	chomp ($chk_vndr);
	$vname = $chk_vndr;
	$chk_vndr = `LC_ALL=C dpkg-vendor $varg --query Short-Desc-Suffix 2>/dev/null`;
	chomp ($chk_vndr);
	$vshort = $chk_vndr;
}

if (defined $vopts) {
	my @opt = split (" ", $vopts);
	foreach my $o (@opt) {
		$options{$o}++;
	}
}

if (defined $ENV{"DEB_BUILD_OPTIONS"}) {
	my @opt = split (" ", $ENV{"DEB_BUILD_OPTIONS"});
	foreach my $o (@opt) {
		$options{$o}++;
	}
}

die sprintf(_g("%s: Please specify some packages to take into a grip.\n"),
	$progname) if (not defined @ARGV);
$retval = 0;
$outdir = ($ENV{'TMPDIR'} and -d $ENV{'TMPDIR'}) ?
	$ENV{'TMPDIR'} : '/tmp' if (not defined $outdir);

opendir (OUT, $outdir) or die ("Cannot open $outdir: $!\n");
my @list=grep(!/^\.\.?$/, readdir(OUT));
closedir (OUT);
foreach my $f (@list) {
	$files{$f}++;
}

# convert DEB_BUILD_OPTIONS into simple values.
$options{'usegrip'}++ if (defined $options{'debiangrip'});
$options{'usegrip'}++ if (defined $options{'usebaked'});
$options{'usegrip'}++ if (defined $options{'usecrush'});
$essential++ if ((defined $options{'noessential'}) or
	(defined $options{'usegrip'}));
$nolintian++ if ((defined $options{'nolintian'}) or
	(defined $options{'usegrip'}));
$docs++ if ((defined $options{'nodocs'}) or
	(defined $options{'usegrip'}));
$help++ if ((defined $options{'nohelp'}) or
	(defined $options{'usegrip'}));
$symbols++ if ((defined $options{'nosymbols'}) or
	(defined $options{'usecrush'}) or
	(defined $options{'usebaked'}));
$locale++ if ((defined $options{'usegrip'}) or
	(defined $options{'usecrush'}));
$noperl++ if ((defined $options{'noperl'}) or
	(defined $options{'usecrush'}));
$noscripts++ if ((defined $options{'nosymbols'}) or
	(defined $options{'usebaked'}));
if (defined $options{'usebaked'}) {
	system ("mkdir -p $outdir/scripts");
}

if (defined $options{'usecrush'}) {
	$chk_vndr = `LC_ALL=C dpkg-vendor --vendor emdebian-crush --query Vendor-Name 2>/dev/null`;
	chomp ($chk_vndr);
	$vname = $chk_vndr;
	$chk_vndr = `LC_ALL=C dpkg-vendor --vendor emdebian-crush --query Short-Desc-Suffix 2>/dev/null`;
	chomp ($chk_vndr);
	$vshort = $chk_vndr;
}

if (defined $options{'usebaked'}) {
	$chk_vndr = `LC_ALL=C dpkg-vendor --vendor emdebian-baked --query Vendor-Name 2>/dev/null`;
	chomp ($chk_vndr);
	$vname = $chk_vndr if (defined $chk_vndr);
	$chk_vndr = `LC_ALL=C dpkg-vendor --vendor emdebian-baked --query Short-Desc-Suffix 2>/dev/null`;
	chomp ($chk_vndr);
	$vshort = $chk_vndr if (defined $chk_vndr);
}

# nothing to do if no options set.
exit 0 if ((not defined $essential) or (not defined $docs) or
	(not defined $help));

# ensure unique package names.
foreach $package ( @ARGV ) {
	if ($package =~ /\.tdeb$/) {
		my $fail = $_;
		printf( _g("'%s' is a Debian tdeb which cannot be gripped by %s.\n"),
			$fail, $progname);
		exit 0;
	}
	$package = realpath ($package);
	chomp ($package);
	if ($package =~ /\.changes$/) {
		$changes = $outdir . "/" . basename ($package);
		# convert a .changes file to an array of .deb
		@debs = `dcmd ls $package 2>/dev/null | grep -E '\.[t|u]?deb\$'`;
		@src = `dcmd ls $package 2>/dev/null | grep -vE '\.[t|u]?deb\$'`;
		$distchange = `grep Distribution $package`;
		chomp ($distchange);
		$distchange =~ s/^Distribution: //;
		chomp (@debs);
		chomp (@src);
		foreach my $d (@debs) {
			if ($d =~ /\.tdeb$/) {
				printf( _g("Skipping TDeb '%s' from %s.\n"), $d, $package)
					if ($verbose >= 2);
				next;
			}
			if (-f "$d") {
				printf( _g("Adding %s from %s.\n"), $d, $package)
					if ($verbose >= 2);
				push @filelist, $d;
			}
		}
	} elsif ($package =~ /\.dsc$/) {
		@src = `LC_ALL=C dcmd ls -1 $package 2>/dev/null | grep -vE '\.[t|u]?deb\$'`;
		chomp (@src);
		$changes = "$outdir/" . basename($package);
		# this file might not exist but it will be regenerated.
		$changes =~ s/\.dsc$/_${base_arch}.changes/;
	} else {
		printf( _g("Adding %s from %s.\n"), $package, $package)
			if ($verbose >= 2);
		push @filelist, $package;
	}
}

foreach $package ( @filelist ) {
	$builds{"$package"}++;
}

# copy sources over to $outdir
foreach my $s (@src) {
	my $sfile = basename ($s);
	open (SRC, "$s") or die (_g("Cannot open")." $s: $!\n");
	my @cont=<SRC>;
	close (SRC);
	open (DEST, ">$outdir/$sfile") or
		die (_g("Cannot open")." $outdir/$sfile: $!\n");
	print DEST @cont;
	close (DEST);
}

foreach $package ( keys %builds) {
	$deb = &build( $package, "$outdir" );
	if (not defined $deb) {
		$retval = 1;
	} else {
		push @gripped, $deb;
	}
}

exit ($retval) if ($retval > 0);

my @files_content;
# prepare debian/files content
foreach my $d (@gripped) {
	my $files_priority = `LC_ALL=C dpkg-deb -f $outdir/$d Priority`;
	my $files_section  = `LC_ALL=C dpkg-deb -f $outdir/$d Section`;
	$arch = `LC_ALL=C dpkg-deb -f $outdir/$d Architecture`;
	chomp ($files_section);
	chomp ($files_priority);
	chomp ($arch);
	my $name = basename ($d);
	push @files_content, "$name $files_section $files_priority\n";
}

# unpack the sources and re-generate the .changes file without changing
# the source package.
# use dget to ensure .orig.tar.gz exists, even for binary-only uploads,
# but that means depending on devscripts.
foreach my $s (@src) {
	next if ($s !~ /\.dsc$/);
	my $dir = dirname ($s);
	my $olddir = cwd;
	my $dsc = basename ($s);
	my $dscdir;
	chdir ($outdir);
	my $srcpkg = `grep Source: $s`;
	$srcpkg =~ s/^Source: (.*)$/$1/;
	chomp ($srcpkg);
	open (DSC, "$s") or die (_g("Cannot read")." $s: $!\n");
	my @dsclines=<DSC>;
	close (DSC);
	foreach my $l (@dsclines) {
		$l =~ /.* (.*)[\.orig]?\.tar\.(gz|bz2)$/;
		next if (not defined $1);
		$dscdir = "${outdir}/$1";
	}
	my $dsc_check = $dscdir;
	$dscdir =~ s/\.orig//;
	$dscdir =~ s/_/-/;
	my $output = `dget -ux file://$s`;
	print $output if ($verbose >= 2);
	if (not -d "$dscdir") {
		$dscdir = $dsc_check;
		$dscdir =~ s/\.orig//;
		$dscdir =~ s/(.*${srcpkg}_.*)-.*$/$1/;
		$dscdir =~ s/_/-/;
		die _g("Unable to find the unpacked source directory:")." $dscdir.\n"
			if (not -d "$dscdir");
	}
	printf( _g("Working in: %s\n"), $dscdir) if ($verbose >= 2);
	chdir ($dscdir);
	if (-x "/usr/bin/em_installtdeb" and (not defined $options{'debiangrip'})) {
		system ("em_installtdeb --no-sign");
	}
	# write out debian/files
	open (FILES, ">debian/files") or die (_g("Cannot write")." debian/files: $!\n");
	print FILES @files_content;
	close (FILES);
	if (defined $options{'debiangrip'}) {
		$distchange = "${distchange}-grip" if ($distchange != /-grip$/);
		system ("dch --force-distribution --no-auto-nmu -D $distchange ".
			"'Apply changes for $distchange' 2>/dev/null");
	}
	printf( _g("INF: Generating changes file for %s\n"), $base_arch);
	$retval = system ("dpkg-genchanges 2>/dev/null 1> $changes");
	chdir ("../");
	system ("rm -rf $dscdir");
	chdir ($olddir);
}

exit ($retval);

sub usageversion {
	printf STDERR (_g("
%s version %s

Usage:
 %s [-v|--verbose] [-q|--quiet] [--vendor VENDOR] [-o|--outdir] PACKAGE ...
 %s -?|-h|--help|--version

Converts a .deb package to Emdebian Grip by unpacking, pruning
particular classes of files (determined by DEB_BUILD_OPTIONS)
and repacking with the em1 version suffix.

If --outdir is not specified, the gripped package(s) will be
created in \$TMPDIR.

When creating a public Emdebian Grip repository, it is strongly
recommended to specify .changes files to allow the generation of
Emdebian TDebs from the source package as well as including the
source package itself in the repository.

If no options are set for DEB_BUILD_OPTIONS, %s does
nothing. Accepted options are:

'nodocs', 'nohelp' or 'noessential'. Pass 'usegrip' to set all
accepted options in one. See %s (1) for details of each
option.

"), $progname, $our_version, $progname, $progname, $progname, $progname)
	or die "$0: "._g("failed to write usage").": $!\n";
}

sub create_tmpdir {
	my $name = shift;
	my $pd = $ENV{'TMPDIR'} && -d $ENV{'TMPDIR'}
		? $ENV{'TMPDIR'}
		: '/tmp';
	return undef unless -d $pd;
	my $dir;

	eval { $dir = tempdir("$name.XXXXXXXX", DIR => $pd) };
	print("$@"), return undef if $@;

	return $dir;
}

sub fail {
	system("rm -rf $tmpdir");
	$SIG{'INT'} = 'DEFAULT';
	warn sprintf(_g("%s: conversion of %s failed.\n"),
		$progname, $package) unless $nofailmsg;
	return undef;
}

sub build {
	my %prunable=();
	my @candidates=();
	$package = shift(@_);
	my $debpath = shift(@_);
	# first of all, check if the file exists
	if (not -r $package) {
		warn sprintf(_g("%s: cannot access %s:"), $progname, $package)." $!\n";
		return "";
	}
	if ($verbose >= 2) {
		if (defined $vname) {
			printf( _g("Taking a grip on: '%s' for %s"), $package, $vname);
		} else {
			printf( _g("Taking a grip on: '%s'"), $package);
		}
		print ".\n";
	}
	$nofailmsg = 0;
	# set the umask (it may be bad by default)
	umask(0022);
	$tmpdir = &create_tmpdir('emgrip');
	if (!$tmpdir) {
		warn "$progname: "._g("failed to create temporary directory").": $!\n";
		return undef;
	}
	my ($src, $dst) = ("$tmpdir/src", "$tmpdir/dst");
	if (!(mkdir("$tmpdir/src") && mkdir("$tmpdir/dst"))) {
		warn "$progname: "._g("failed to prepare temporary directory").": $!\n";
		system("rm -rf $tmpdir");
		return undef;
	}
	# remove tmp files on C-c
	$SIG{'INT'} = sub {
		print _g("Removing tmp files...\n") if $verbose >= 2;
		system "rm -rf $tmpdir";
		die _g("Interrupted.\n");
	};
	printf( _g("Extracting %s\n"), $package) if $verbose >= 2;
	# extract package to $src
	if (system("dpkg --extract $package $src && dpkg --control $package $src/DEBIAN") != 0) {
		return &fail();
	}
	# get a list of directories that are intentionally empty.
	my @safe=`find $src -type d -empty`;
	foreach my $s (@safe) {
		chomp ($s);
		$origdirs{$s}++;
	}
	print _g("Extracting information from control file\n") if $verbose >= 2;
	# extract useful information from control file
	if (!(open(CONTROL, "$src/DEBIAN/control"))) {
		warn "$progname: "._g("cannot open package control file").": $!\n";
		return &fail();
	}
	unlink ("$src/DEBIAN/symbols") if ((-f "$src/DEBIAN/symbols")
		and (defined ($symbols)));
	# write useful info into destination control file.
	if (!(open(DST, ">$dst/control"))) {
		warn "$progname: "._g("cannot write package control file").": $!\n";
		return &fail();
	}
	my $field;
	my %control;
	while (<CONTROL>) {
		# size will be calculated later.
		next if (/^Installed-Size:\s*[0-9]+$/i);
		next if ((/^Essential:\s*yes\s*$/i) and (defined $essential));
		print DST $_;
		chomp;
		if (/^ /) {
			$control{$field} .= ("\n" . $_) if (defined($field));
		} elsif (/^(\S+):\s*(.*)$/i) {
			$field = lc($1);
			$control{$field} = $2;
		}
	}
	close(CONTROL);
	close (DST);
	if (defined ($noscripts)) {
		mkdir ("$outdir/scripts/");
		my @scripts = qw/ config templates postinst postrm preinst prerm
		conffiles md5sums triggers shlibs /;
		foreach my $s (@scripts) {
			if (-f "$src/DEBIAN/$s") {
				open (SCRIPT, "$src/DEBIAN/$s") or die ("Cannot open $s");
				my @scr=<SCRIPT>;
				close (SCRIPT);
				print ">$outdir/scripts/$control{'package'}.$s\n";
				open (BAKED, ">$outdir/scripts/$control{'package'}.$s");
				print BAKED @scr;
				close (BAKED);
				unlink ("$src/DEBIAN/$s");
			}
		}
	}
	# check for existance of required fields
	for $field (qw(package version architecture)) {
		if (!defined($control{$field})) {
			my $msg = sprintf(_g("%s: required field \'%s\' missing in control file\n"),
				$progname, $field);
			warn ($msg);
			return &fail();
		}
	}
	# check for package already processed by dpkg-cross, or created by
	# cross-gcc compilation
	if (($control{"architecture"} eq "all") and (
		($control{"description"} =~ /generated by dpkg-cross/) or
		($control{"description"} =~ /contains files for.*cross-compile/))) {
		warn sprintf(_g("%s: %s looks like a cross-compile package\n"),
			$progname, $package);
		return &fail();
	}
	if (! mkdir("$dst/DEBIAN")) {
		warn "$progname: "._g("failed to create")." $dst/DEBIAN: $!\n";
		return &fail();
	}
	# Need a way to convert a Debian TDeb to an
	# Emdebian one. Either way, /usr/share/locale/ should not be
	# retained in the gripped package.
	# EXCEPT /usr/share/locale/locale.alias
	my $dirs = &locale_dirs ($src);
	foreach my $d (@$dirs) {
		system ("rm -rf $src/usr/share/locale/$d")
			if (defined $locale);
	}
	if (defined $options{'debiangrip'}) {
		# remove Qt translation files too.
		system ("find $src -name '*.qm' -delete");
	}
	# nolintian support
	if (defined $nolintian) {
		system ("rm -f $src/usr/share/lintian/overrides/*");
		system ("rm -f $src/usr/share/linda/overrides/*");
	}
	# noperl support
	if (defined $noperl) {
		my @candidates=`find $src |xargs file`;
		foreach my $c (@candidates) {
			my @f=split(":", $c);
			unlink $f[0] if ($c =~ m:/usr/bin/perl.*script text executable:);
			unlink $f[0] if ($c =~ /Perl5 module source text/);
		}
	}
	# nohelp support
	# remove gtk-doc/html
	system ("rm -rf $src/usr/share/gtk-doc/html") if (defined $help);
	# remove gnome/help/
	# only C/ will exist once we have TDebs.
	# but $control{'package'} could be the -doc version which does not
	# match.
	system ("rm -rf $src/usr/share/gnome/help/") if (defined $help);
	# liferea has weird /usr/share/liferea/doc/html and others might too.
	system ("rm -rf $src/usr/share/$control{'package'}/doc/html/")
		if (defined $help);
	if (defined $docs) {
		# remove usr/share/man
		system ("rm -rf $src/usr/share/man");
		# remove usr/share/info
		system ("rm -rf $src/usr/share/info");
		# remove usr/share/doc-base
		system ("rm -rf $src/usr/share/doc-base");
		my $parent = $control{'source'};
		$parent = $control{'package'} if (not defined $parent);
		$parent =~ s/ .*$//;
		system ("mv $src/usr/share/doc/$control{'package'}/copyright $dst/copyright")
			if ((-d "$src/usr/share/doc/$control{'package'}/") and
			(-f "$src/usr/share/doc/$control{'package'}/copyright"));
		system ("mv $src/usr/share/doc/$control{'package'}/copyright.gz $dst/copyright.gz")
			if ((-d "$src/usr/share/doc/$control{'package'}/") and
			(-f "$src/usr/share/doc/$control{'package'}/copyright.gz"));
		# remove usr/share/doc-base/*
		system ("rm -rf $src/usr/share/doc-base/*");
		# remove usr/share/doc/$package/*
		system ("rm -rf $src/usr/share/doc/$parent/*");
		system ("rm -rf $src/usr/share/doc/$control{'package'}/*");
		# libgconf-dev puts docs in /usr/share/doc/gconf2/
		# but the source package is 'gconf'
		system ("rm -rf $src/usr/share/doc/${parent}*/*");
		if (-f "$dst/copyright") {
			# compress copyright and move copyright.gz back.
			system ("gzip -9 $dst/copyright");
			system ("mv $dst/copyright.gz $src/usr/share/doc/$control{'package'}/copyright.gz");
		}
		system ("mv $dst/copyright.gz $src/usr/share/doc/$control{'package'}/copyright.gz")
			if (-f "$dst/copyright.gz");
	}
	my $size = `du -kc $src | grep total | cut -d't' -f1`;
	chomp ($size);
	$size =~ s/\s+//g;
	# Create the control file.
	print _g("Creating control file\n") if $verbose >= 2;
	if (! open(CONTROL, ">$dst/DEBIAN/control")) {
		my $fail = $!;
		my $msg = sprintf(_g("%s: failed to open %s/DEBIAN/control for writing: %s\n"),
			$progname, $dst, $fail);
		warn ($msg);
		return &fail();
	}
	my $epoch = $control{'version'};
	# need to retain the epoch in DEBIAN/control
#	$no_epoch =~ s/[0-9]://;
	print CONTROL "Package: $control{'package'}\n";
	if (defined($control{"source"})) {
		# cope with existing versioned source listing (libgcc1)
		my $ctrl = ($control{'source'} !~ /\(.+\)/) ?
			"Source: $control{'source'} ($epoch)\n"
			: "Source: $control{'source'}\n";
		print CONTROL $ctrl;
	} else {
		my $ctrl = ($control{'package'} !~ /\(.+\)/) ?
			"Source: $control{'package'} ($epoch)\n"
			: "Source: $control{'package'}\n";
		print CONTROL $ctrl;
	}
	my $v = ($control{'version'} =~ /em1$/) ?
		"Version: $control{'version'}\n" :
		"Version: $control{'version'}em1\n";
	print CONTROL $v;
	print CONTROL "Architecture: $control{'architecture'}\n";
	print CONTROL "Maintainer: $control{'maintainer'}\n"
		if (defined($control{"maintainer"}));
	print CONTROL "Installed-Size: $size\n";
	if (defined($control{"pre-depends"})) {
		# handle strictly versioned pre-depends
		if ($control{'pre-depends'} =~ / \(= /) {
			# fixup direct dependencies to match the gripped version.
			my $strict = $control{'pre-depends'};
			my @base = split(",", $strict);
			my @res = ();
			foreach my $t (@base) {
				$t =~ s/ \(= (.+)?\)/ \(= ${1}em1\)/;
				push @res, $t;
			}
			$strict =~ s/ \(= (.+)?\)/ \(= ${1}em1\)/;
			$control{'pre-depends'} = join(",",@res);
		}
		print CONTROL "Pre-Depends: $control{'pre-depends'}\n";
	}
	if (defined($control{"depends"})) {
		if ($control{'depends'} =~ / \(= /) {
			# fixup strict dependencies to match the gripped version.
			my $strict = $control{'depends'};
			my @base = split(",", $strict);
			my @res = ();
			foreach my $t (@base) {
				$t =~ s/ \(= (.+?)?\)/ \(= ${1}em1\)/g;
				push @res, $t;
			}
			$strict =~ s/ \(= (.+?)?\)/ \(= ${1}em1\)/g;
			$control{'depends'} = join(",",@res);
		}
		print CONTROL "Depends: $control{'depends'}\n";
	}
	if (defined($control{"replaces"})) {
		# allow for replacing a gripped version
		if ($control{'replaces'} =~ / \(/) {
			my $strict = $control{'replaces'};
			my @base = split(",", $strict);
			my @res = ();
			foreach my $t (@base) {
				$t =~ s/ \((.+)?\)/ \(${1}em1\)/;
				push @res, $t;
			}
			$strict =~ s/ \((.+)?\)/ \(${1}em1\)/;
			$control{'replaces'} = join(",",@res);
		}
		print CONTROL "Replaces: $control{'replaces'}\n"
	}
	if (defined($control{"conflicts"})) {
		# preserve handle versioned conflicts against gripped versions
		if ($control{'conflicts'} =~ / \(/) {
			my $strict = $control{'conflicts'};
			my @base = split(",", $strict);
			my @res = ();
			foreach my $t (@base) {
				# Conflicts with 'less than' do not need to change.
				# see libungif4g and libgif4
				$t =~ s/ \((.+)?\)/ \(${1}em1\)/
					unless ($t =~ /<</);
				push @res, $t;
			}
			$strict =~ s/ \((.+)?\)/ \(${1}em1\)/;
			$control{'conflicts'} = join(",",@res);
		}
		print CONTROL "Conflicts: $control{'conflicts'}\n"
	}
	# Support 'Provides:' (no versioning)
	print CONTROL "Provides: $control{'provides'}\n"
		if (defined($control{"provides"}));
	# Support Multi-Arch fields
	print CONTROL "Multi-Arch: $control{'multi-arch'}\n"
		if (defined($control{"multi-arch"}));
	print CONTROL "Section: $control{'section'}\n";
	print CONTROL "Priority: $control{'priority'}\n";
	print CONTROL "Task: $control{'task'}\n"
		if (defined $control{'task'});
	print CONTROL "Python-Version: $control{'python-version'}\n"
		if (defined $control{'python-version'});
	# grip the description to 4 lines (stripping the fourth line
	# if it is empty).
	my @t = split ("\n", $control{"description"});
	my $limit = (scalar @t < 4) ? scalar @t : 4;
	$limit = 3 if ((not defined $t[$limit]) or ($t[$limit] =~ / \./));
	my $c = 0;
	my $str;
	foreach my $l (@t) {
		chomp ($l);
		$str .= $t[$c]."\n" if ($l !~ /^\s?$/);
		$c++;
	}
	$str = "";
	my @r = ();
	@r = @t[0..$limit];
	foreach my $line (@r) {
		$str .= $line."\n" if (defined $line);
	}
	chomp ($str);
	$control{"description"} = $str;
	my $t = $r[0] if (defined $r[0]);
	$t .= $r[1] if (defined $r[1]);
	$t .= $r[2] if (defined $r[2]);
	$t .= $r[3] if (defined $r[3]);
	$control{"description"} .= "\n . ...\n" if ($t !~ / \.\.\./);
	# Output modified description
	my @desclines = split("\n", $control{'description'});
	# split out the first line
	my $first = shift @desclines;
	my $rest = join ("\n", @desclines);
	if (($first !~ / \(crushed\)/) or ($first !~ / \(gripped\)/)
		or ($first != / \($vshort\)/)) {
		if (defined $vshort) {
			print CONTROL "Description: $first ($vshort)";
		} else {
			print CONTROL "Description: $first (gripped)";
		}
	} else {
		print CONTROL "Description: $first"
	}
	print CONTROL "\n$rest\n";
	close (CONTROL);
	# check package architecture
	my $control_arch = $control{"architecture"};
	# prepare destination filename
	my $evers = ($control{'version'} =~ /em1$/) ?
		$control{"version"} : $control{"version"}."em1";
	# strip epoch for filename
	$evers =~ s/^\d+://;
	$debname = $control{"package"} . "_" . $evers . "_".$control_arch.".deb";
	system ("mv $dst/DEBIAN/control $src/DEBIAN/control");
	system ("cat $src/DEBIAN/control") if ($verbose >= 2);
	# Find out if fakeroot is needed and if it is available
	my $wrapper = "";
	if (geteuid() != 0) {
		$wrapper = "/usr/bin/fakeroot";
		if (! -x $wrapper) {
			warn sprintf(_g("%s: %s is not available, package files will not be owned by root\n"),
				$progname, $wrapper);
			$wrapper = "";
		}
	}
	# update the md5sums to reduce the size of that file too.
	# borrowed from dh_md5sums.
	my $exclude = "";
	if (-f "$src/DEBIAN/conffiles") {
		open (CONF, "$src/DEBIAN/conffiles") or
			die _g("Unable to read conffiles").": $!\n";
		while (<CONF>) {
			chomp;
			s/^\///;
			$exclude.="! -path \"./$_\" ";
		}
	}
	mkdir ("$src/DEBIAN")
		if (not -d "$src/DEBIAN");
	my $find = "cd $src/; ";
	$find .= "find . -type f $exclude ! -regex '.*/DEBIAN/.*' -printf '%P\\0' | ";
	$find .= "xargs -r0 md5sum > ./DEBIAN/md5sums";
	system ($find);

	# Build the .deb
	printf( _g("INF: Building %s in %s\n"), $debname, $debpath)
		if $verbose >= 1;
	if (system( "$wrapper dpkg-deb -b $src ${debpath}/$debname 1>/dev/null")) {
		warn "$progname: "._g("building package with dpkg-deb -b failed.\n");
		return &fail();
	}
	$SIG{'INT'} = 'DEFAULT';
	system "rm -rf $tmpdir";
	return $debname;
}

sub locale_dirs {
	my @dirs=();
	my $src = shift;
	opendir (LOCALE, "$src/usr/share/locale/") or return \@dirs;
	my @list=grep(!/^\.\.?$/, readdir(LOCALE));
	closedir (LOCALE);
	foreach my $file (@list) {
		push @dirs, $file if (-d "$src/usr/share/locale/$file");
	}
	return \@dirs;
}

sub scripts_version {
	my $query = `dpkg-query -W -f='\${Version}' emdebian-grip`;
	(defined $query) ? return $query : return "";
}

sub _g {
	return gettext(shift);
}