File: emsource

package info (click to toggle)
emdebian-tools 1.4.3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,112 kB
  • ctags: 274
  • sloc: perl: 6,297; xml: 4,828; sh: 1,902; php: 406; ansic: 189; makefile: 15
file content (936 lines) | stat: -rwxr-xr-x 33,253 bytes parent folder | download
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
#!/usr/bin/perl
#
#  emsource -- get Debian source, create and apply emdebian patches
#  Copyright (C) 2007-2008  Neil Williams <codehelp@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/>.
#

# Uses config::auto for the user's preferred working directory.
# use apt-get source to get the package, calls dpkg-source -x for us.
# checkout the emdebian changes, if any, from:
# http://buildd.emdebian.org/repos/source/target/trunk/$package/trunk/
# check for $package/ from the output of:
# svn list http://buildd.emdebian.org/repos/source/target/trunk/
# if exists, checkout the patches - use debconf setting for username to commit.
# ready for emdebuild which can modify the patches for the next release.

use Cwd;
use Config::Auto;
use File::HomeDir;
use File::Basename;
use Text::Wrap;
use Debian::DpkgCross;
use Cache::Apt::Package;
use Cache::Apt::Config;
use Cache::Apt::Lookup;
use Emdebian::Tools;
use Term::ANSIColor qw(:constants);
use strict;
use warnings;

use vars qw/$username $verbose $our_version $home $dpkg_cross_dir
$cfile $workdir $package $retval $svnadd $svnlogin $progname $suite
$indexchar $clog $debver $cleanup $msg $v $src $arch $autobuild $next
$newversion $chrootbuild $findonly $stats $builddep $patchdir
$machine $variant $machine_path $machine_done /;

$progname = basename($0);
$our_version = &tools_version();
$verbose = 1;
$cleanup = 0;
$autobuild = 0;
$findonly = 0;
$builddep = 0;
$machine_done = 0;
$stats = 0;
$chrootbuild = 0;
$next = 0;
# tracks whether debian/changelog applies cleanly
$newversion = 0;
$username = "";
$suite = &get_targetsuite;
# svnadd == 0 if package exists in svn, 1 if files need to be added.
$svnadd = 1;
&read_config();
$arch = &get_architecture();
$home = File::HomeDir->my_home;
$dpkg_cross_dir = &get_aptcross_dir();

sub usageversion {
	print(STDERR <<END)
$progname version $our_version

Usage:
 $progname [-u USERNAME] [-a ARCH] [-m NAME] [-V NAME] [-v] [-q] PACKAGE
 $progname [-a ARCH] [--build-dep] [-m NAME] [-V NAME] [--machine-path PATH] [-v] [-q] -b PACKAGE
 $progname [-a ARCH] [-m NAME] [-V NAME] [--machine-path PATH] [-v] [-q] -c PACKAGE
 $progname [-a ARCH] [-m NAME] [-V NAME] [--machine-path PATH] [-v] [-q] -p PACKAGE
 $progname [-a ARCH] [-v] [-q] --where PACKAGE
 $progname [-a ARCH] [-v] [-q] --status PACKAGE
 $progname -?|-h|--help|--version

Commands:
 -c|--clean PACKAGE:  clean up the source tree for PACKAGE
 -b|--build PACKAGE:  Attempt an automated build of PACKAGE using emdebuild
 -p|--chroot PACKAGE: Attempt an automated build of PACKAGE using empdebuild
    --where PACKAGE:  Echo the path to the Emdebian build for PACKAGE and exit
    --status PACKAGE: Print a brief summary of the package build status and exit

Options:
 -a|--arch ARCH:          Set architecture (default: defined by dpkg-cross)
 -u|--username:           Your username to commit to Emdebian SVN, if any.
    --build-dep:          Pass --build-dep option to emdebuild. Requires -b.
 -m|--machine NAME:       Specify a machine name for customisation hooks
 -V|--variant NAME:       Specify a machine variant name for customisation hooks.
                          Requires -m.
    --machine-path PATH:  Specify the root of the path to replace the '\$WORK/machine'
                          default. Requires -m.
 -v|--verbose:            Be verbose (repeat for more verbosity)
 -q|--quiet:              Be quiet [default]
 -?|-h|--help:            Print this usage message and exit
 --version:               Print this usage message and exit

$progname combines the functionality of 'apt-get source' and
'svn checkout' to maintain a record of Emdebian changes to
Debian packages. Changes can be committed back to Emdebian
SVN after a successful build using 'emdebuild --svn'.

User-specific configuration values in ~/.dpkg-cross/emsource,
override any debconf default in /etc/emsource.conf. If no
preferred working directory is set, $progname unpacks the requested
package to a new subdirectory in the current working directory.

Automated builds should only be attempted when updating an existing
Emdebian package. Most Debian packages will fail to build first time
without manual changes to the emdebian patch files.

If a username is neither set in the configuration file nor supplied
using -u | --username, $progname will use anonymous svn and
modifications to the emdebian patches (by emdebuild) should be
contributed back to Emdebian via other means.

Machine:variant customisation uses patches stored (or accessible via)
your Emdebian working directory, configured via debconf. Patches
are only used when -m is used. If -V is omitted, the 'default' variant
is assumed. The full path to the patches is constructed from the default
root path ('\$WORK/machine/'), the machine-variant path ('\$machine/\$variant/')
and the package path ('\$initial_char/\$package_name/') - similar
to the path used in repositories and by $progname. Use --machine-path to
change the default root path - the rest of the patch path is unchanged.

END
		or die "$0: failed to write usage: $!\n";
}

while( @ARGV ) {
	$_= shift( @ARGV );
	last if m/^--$/;
	if (!/^-/) {
		unshift(@ARGV,$_);
		last;
	}
	elsif (/^(-\?|-h|--help|--version)$/) {
		&usageversion();
		exit (0);
	}
	elsif (/^(--where|--where-is)$/) {
		$findonly = 1;
	}
	elsif (/^(--status)$/) {
		$stats = 1;
	}
	elsif (/^(--build-dep)$/) {
		$builddep = 1;
	}
	elsif (/^(-v|--verbose)$/) {
		$verbose++;
	}
	elsif (/^(-q|--quiet)$/) {
		$verbose--;
	}
	elsif (/^(-c|--clean)$/) {
		$cleanup++;
	}
	elsif (/^(-b|--build)$/) {
		$autobuild++;
		$cleanup++;
	}
	elsif (/^(-p|--chroot)$/) {
		$chrootbuild++;
		$cleanup++;
	}
	elsif (/^(-u|--username)$/) {
		$username = shift(@ARGV);
	}
	elsif (/^(-a|--arch)$/) {
		$arch = shift(@ARGV);
	}
	elsif (/^(-m|--machine)$/) {
		$machine = shift(@ARGV);
	}
	elsif (/^(-V|--variant)$/) {
		$variant = shift(@ARGV);
	}
	elsif (/^(--machine-path)$/) {
		$machine_path = shift(@ARGV);
	}
	else
	{
		&usageversion();
		die RED, "unrecognised option:  $_.", RESET . "\n";
	}
}

if (! $ARGV[0])
{
	&usageversion;
	print RED, "Please specify a package to download and patch.\n\n", RESET;
	exit (1);
}

my $target_gnu_type = &check_cache_arch($arch);
if ((not defined $arch)||($arch eq "")||(not defined $target_gnu_type))
{
	&usageversion;
	$msg = "\n$progname: Cannot determine the architecture to build";
	$msg .= " and no default architecture found.";
	$msg .= " Please use '$progname --arch ARCH'.\n";
	die RED, wrap('','',$msg), RESET, "\n";
}

my $quiet = "";
$quiet = "-q" if ($verbose < 1);
$package = $ARGV[0];
&set_verbose($verbose);
&set_suite($suite);
my $config = &init_cache($verbose);
my $val = &get_primary;
&set_mirror("ftp://$val/debian") if (defined $val);

# check apt-cross is up to date
print CYAN, "Checking the apt-cross cache is up to date for $arch.\n", RESET if ($verbose >= 1);
&check_update($verbose);

# check user input and convert to a source package name if not already.
$package = &check_package($package);
die "\n" if ($package eq "");
($indexchar)=split(//, $package);
my $dir = $src . "-" . $v;

&where_is if ($findonly > 0);
&show_stats if ($stats > 0);

# make this go away with libsvn-perl eventually.
$msg = "$progname: please install 'subversion' before using $progname.\n";
die RED, $msg, RESET if (! -x "/usr/bin/svn");

# read emsource config file.
$workdir = &get_workdir;
$workdir = "/" if ($workdir eq "");
$msg = &check_workdir($workdir);
die RED, $msg, RESET if ($msg ne "");
chdir ("$workdir") if ($workdir ne ".");
print CYAN, "Working directory: '" . cwd . "'\n", RESET if ($verbose >= 2);
$username = &get_username if ($username eq "");
# only unstable builds should be committed to SVN or uploaded.
# see emsource (1)
$username = "" if (($suite ne "unstable") && ($suite ne "sid"));

# handle developer vs anonymous SVN access
$svnlogin = "svn+ssh://" . $username .
	"\@buildd.emdebian.org/var/emdebian/svn/current/target/trunk/"   if ($username ne "");
$svnlogin = "http://buildd.emdebian.org/repos/current/target/trunk/" if ($username eq "");

# Possible calls:
# 1: Completely new build. svnadd = 1, no patch files exist locally or in SVN.
# 2. Partial data exists in SVN. (if one developer has started but failed and someone else tries.)
#		only an empty directory tree exists in SVN. add patch files upon successful build. svnadd=0
# 3. Existing build, no fresh package upstream. abort. (patch files exist and SVN exists). svnadd=0
# 4. Existing build, new upstream package. Remove .old and restart as new. svnadd=0
# Note: do not remove .svn without also (re)moving ../trunk and using svn update!

print CYAN, "Checking for existing emdebian patches\n", RESET if ($verbose >= 1);
# need the login here because we don't necessarily have a working directory yet.
my $svncheck = `svn --non-interactive list $svnlogin/$indexchar/ 2>/dev/null`;
$svnadd = 0 if ($svncheck =~ /\Q$package\E\//);

# now check out the svn, if any
if ($svnadd == 0)
{
	print CYAN, "Checking out working copies of existing emdebian patches\n", RESET
		if ($verbose >= 2);
	# existing co ignores ./trunk which is created when adding a new package
	# so keep all packages together.
	mkdir ("trunk") if (! -d "trunk");
	mkdir ("trunk/$indexchar") if (! -d "trunk/$indexchar");
	chdir ("trunk/$indexchar");
	# create a local working copy
	system ("svn co --non-interactive $quiet ${svnlogin}${indexchar}/${package}");
	# now change to the package trunk
	chdir ("$package/trunk");
}
else
{
	print GREEN, "Adding new package '$package' to emdebian SVN.\n", RESET
		if (($verbose >= 2) && ($username ne ""));
	system ("svn co --non-interactive $quiet -N ${svnlogin}");
	chdir ("trunk");
	system ("svn co --non-interactive $quiet -N ${svnlogin}/$indexchar");
	mkdir ("$indexchar/$package");
	mkdir ("$indexchar/$package/trunk");
	mkdir ("$indexchar/$package/branches");
	mkdir ("$indexchar/$package/tags");
	system ("svn add --non-interactive $quiet $indexchar/$package") if ($username ne "");
	# this doesn't ignore all built files, just the main ones
	# unfortunately, svn doesn't have an 'ignore-except'
	# whilst retaining an ability to add new files (like a new cache file).
	# Using propset also *replaces* existing settings, so it has to be used
	# here and not later. (svn isn't easy to automate sometimes.) :-(
	# some packages will still need manual svn propedit commands.
	# note that packages beginning with e have no ignore property set - needs manual intervention.
	system ("svn propset --non-interactive svn:ignore \"${indexchar}*\" $indexchar/$package/trunk/")
		if (($username ne "") && ($indexchar ne "e"));
	system ("svn ci --non-interactive $quiet -m \"automated $progname checkin of empty tree\" $indexchar/$package")
		if ($username ne "");
	chdir ("$indexchar/$package/trunk");
}

my $top = cwd;

if ($cleanup > 0)
{
	$dir = &catch_no_dir if(! -d "$dir");
	if (! -d $dir)
	{
		print GREEN, wrap('','',"$progname: refreshing source directory ".
		"'$top/$dir' for $package."), RESET, "\n";
		chdir ("$top");
		$retval = 0;
		$retval = system ("apt-get source $package");
		exit($retval) if ($retval != 0);
	}
	chdir ("$dir");
	&emcleanup($package);
	print CYAN, wrap('','',"\nCleaned and emdebianised source tree for '$package' exists at\n"),"'$top/$dir'\n", RESET;
	# look in the configured alternative tree for more patches.
	if ($autobuild > 0)
	{
		chdir ("$top/$dir");
		if ($builddep > 0)
		{
			print CYAN, "Installing build dependencies with apt-get\n", RESET
				if ($verbose >= 1);
			print GREEN, "Enter your sudo password if prompted.\n", RESET if ($verbose >= 2);
			system ("sudo apt-get -y build-dep $package");
			print CYAN, "Installing cross dependencies with emdebuild -a $arch --build-dep\n", RESET
				if ($verbose >= 1);
			my $quiet = "";
			$quiet = "-q" if ($verbose < 1);
			system ("emdebuild -a $arch $quiet --build-dep");
		}
		print GREEN, "Attempting an automatic build for $arch\n", RESET
			if ($verbose >= 1);
		my $n = "";
		# run emdebuild and die if it dies.
		$n="-n" if ($next > 0);
		my $retval = system ("emdebuild $n -a $arch");
		# if emdebuild fails, emsource -b needs to just die.
		$retval = $retval >> 8 if ($retval != 0);
		exit ($retval);
	}
	if ($chrootbuild > 0)
	{
		chdir ("$top/$dir");
		print GREEN, "Attempting an automatic chroot build.\n", RESET if ($verbose >= 1);
		my $retval = system ("nice -n 7 sudo empdebuild --arch $arch --build");
		exit (($retval >> 8));
	}
	print CYAN, wrap('','',"Change to this directory before running 'emdebuild' or".
		" 'sudo empdebuild --arch $arch --build'\n"), RESET if ($verbose >= 2);
	print "\n";
	exit(0);
}

print CYAN, "Checking for existing build tree in $dir.\n", RESET if ($verbose >= 2);
# check first in case there is already an emdebianised build tree here.
# leave it alone if it is up to date.
# A refreshed Debian version will have the same source DIRECTORY so CHECK!
# no source has been downloaded, yet.
if ( -d "$dir")
{
	chdir ("$dir");
	$clog = `parsechangelog --format dpkg | grep Version`;
	$clog =~ /^Version: (.*)$/;
	$debver = $1;
	my $retver = "";
	$retver = &extract_emdebversion($clog);
	my $msg = "\n$progname: Error. Source exists but no emdebian version found!\n";
	die (RED, $msg, RESET) if ($retver eq "");
	print "emdebian version = $retver\n" if ($verbose >=2);
	$clog =~ s/$retver//;
	chomp ($clog);
	if ($clog =~ /^Version: (.*)$/)
	{
		my $check=$1;
		# 1. the source directory exists, 2. the emdebian patch files exist and have been applied
		# 3. the Debian package is the same version as the emdebianised tree - Abort.
		$msg="\n$progname: $src version $check has already been emdebianised - nothing to do. ";
		$msg .= "Emdebianised source tree for '$package' exists at '$top/$dir' ";
		$msg .= "Change to this directory before running 'emdebuild'\n" if ($verbose >= 2);
		die (RED, wrap('','',$msg), RESET, "\n") if ("${check}$retver" eq $debver);
		# if we get here, the debian version does not match the emdebian version.
		# need to move this $dir and the emdebian patch files then start afresh.
		# new debian version => em1 emdebian version string.
		# reprepro won't complain as long as the md5sum of the .orig.tar.gz is the same.
		chdir ("../");
		$msg = "$progname has detected a fresh Debian release: $v. The old emdebianised tree ";
		$msg .= "for emdebian version ${v}${retver} will be moved to ../branches/${dir} for later reference. ";
		$msg .= "You should delete ../branches/${dir} if it is no longer needed. It will be overwritten if ";
		$msg .= "$progname is called again.\n";
		$msg .= "Proceeding to clean the trunk directory.\n";
		print RED, wrap('', '', $msg), RESET;
		system ("rm -rf ${dir}.tmp") if (-d "${dir}.tmp");
		system ("rm -rf ../branches/${dir}") if (-d "../branches/${dir}");
		# remove the old pristine debian files, prepare for a new package.
		system ("rm -rf ${src}.old") if (-d "${src}.old");
		system ("mv $dir ../branches/${dir}");
		# With an updated upstream, any existing emdebian patch files will be overridden anyway
		unlink ("../emdebian-changelog.patch");
		unlink ("../emdebian-control.patch");
		unlink ("../emdebian-rules.patch");
		# we now have a fresh location for the new tree.
	}
	else
	{
		$msg = "\n$progname: Error. Unable to compare emdebian and debian version strings!\n";
		die (RED, $msg, RESET);
	}
}
# no build tree exists for the current Debian version, check for svn patches, get source then apply.
chdir ("$top");
print GREEN, wrap('','',"No unpacked source directory for Debian version $v,".
	" getting Debian source\n"), RESET if ($verbose >= 2);
$retval = system ("apt-get $quiet source $package");
exit($retval) if ($retval != 0);
# catch errors where package-version does not match the unpacked directory name.
$dir = &catch_no_dir if(! -d "$dir");
chdir ("$dir");
&prepare_pristine($package);

# there is always a changelog patch
if (-f "../emdebian-changelog.patch")
{
	print CYAN, "Emdebianising source tree. Applying patches from SVN.\n", RESET
		if ($verbose >= 2);
	&apply_patches;
	opendir (PATCHES, "../") or die (RED, "Unable to open parent directory.", RESET, "\n");
	# patches intended for debian/patches use the names: debian-patch-$file
	my @patchfiles=grep(/^debian-patch-.*$/, readdir PATCHES);
	closedir (PATCHES);
	foreach my $patch (@patchfiles)
	{
		$patch =~ s/^debian-patch-//;
		my $patchdir;
		if (-d "debian/patches-applied")
		{
			$patchdir = "debian/patches-applied";
		}
		else
		{
			$patchdir = "debian/patches";
		}
		mkdir $patchdir unless (-d $patchdir);
		open (PATCH, "../debian-patch-$patch") or
			warn (RED, "Cannot open debian-patch-$patch : $!", RESET, "\n");
		my @p=<PATCH>;
		close (PATCH);
		print GREEN, "Restoring debian patch: $patch\n", RESET;
		open (DPATCH, ">$patchdir/$patch") or
			warn (RED, "Cannot write debian patch: $!", RESET, "\n");
		print DPATCH @p;
		close (DPATCH);
	}
}
else
{
	# Need to manage patch files that are no longer created or which are newly added. FIXME
	# intention is that a fresh tree - including a new upstream version needs to have em_make run.
	print CYAN, "Emdebianising source tree\n", RESET if ($verbose >= 2);
	system ("em_make $quiet --forceold --arch $arch");
}
if ($svnadd == 1)
{
	chdir("../");
	if (($verbose >= 2) && ($username ne ""))
	{
		my $msg = "Placing initial emdebian patch files under SVN version control. ";
		$msg .= "Completed patch files can be checked into Emdebian SVN ";
		$msg .= " using 'emdebuild --svn' or individually using 'svn ci <file>'.\n";
		print GREEN, wrap('','', $msg), RESET;
		system ("svn add --non-interactive $quiet emdebian-changelog.patch emdebian-rules.patch emdebian-control.patch") if ($username ne "");
	}
	chdir("$dir");
}
print CYAN, wrap('','',"\nEmdebianised source tree for '$package' exists at '$top/$dir'\n"), RESET;
print CYAN, wrap('','',"Change to this directory before running 'emdebuild' or 'sudo empdebuild --build'\n"), RESET
	if ($verbose >= 2);
print "\n";

exit (0);

sub cleanup_vers
{
	my $t = $_[0];
	return undef if (not defined($t));
	$t =~ s/^[0-9]*\://;
	$t =~ s/\-.*$//;
	return $t;
}

sub try_patch ($$)
{
	$patchdir = shift;
	my $patchfile = shift;
	my $reverse_sym = "";
	my $reverse = 0;
	print CYAN, "Trying a dry-run for ${patchdir}${patchfile}\n", RESET
		if ($verbose >= 2);
	$retval = 0;
	$retval = system ("patch --dry-run -p1 $reverse_sym < $patchdir/$patchfile >/dev/null");
	if ($retval == 256)
	{
		$reverse_sym = "-R";
		$reverse = 1;
	}
	$retval = system ("patch --dry-run -p1 $reverse_sym < $patchdir/$patchfile >/dev/null");
	$reverse = -1 if ($retval == 256);
	return $reverse;
}

sub check_package
{
	&setup_config;
	my $pkgchk = $_[0];
	my $emp = AptCrossPackage->new();
	$emp->Package($pkgchk);
	my $apt_cross_dir = &get_cachedir;
	$suite = &get_targetsuite;
#	&use_hostsources;
	my $config = &init_cache($verbose);
	#start by downloading sources, but only if not already done this 24hrs
	my $mtime = (stat ("$apt_cross_dir/.aptupdate-stamp-$suite-host"))[9];
	$mtime = 0 if (!defined($mtime));
	my $time_now = time();
	if ($time_now - $mtime > 86400) {
		print "Updating apt-cache for host\n" if ($verbose >= 2);
		&cache_update($config);
		utime(time, time, "$apt_cross_dir/.aptupdate-stamp-$suite-host")
			or ( open(F, ">$apt_cross_dir/.aptupdate-stamp-$suite-host") && close F )
	}
	$emp = &srclookup($pkgchk);
	$emp = &binlookup($pkgchk) if (not defined $emp);
	if (defined $emp)
	{
		my $vers = $emp->{Version};
		$v = &cleanup_vers($vers);
		$src = (defined $emp->{SourcePkg}) ? $emp->{SourcePkg} : $emp->{Package};
		print CYAN, "Using $src as source package for $package ($v)\n", RESET if ($verbose >= 2);
		my $bins = $emp->{Binaries};
		my %archlist=();
		foreach my $bin_chk (@$bins)
		{
			my $bemp = AptCrossPackage->new();
			$bemp->Package($bin_chk);
			$bemp = &lookup_pkg($bemp);
			next unless (defined $$bemp->Architecture);
			$archlist{$$bemp->Architecture}++;
		}
		warn (RED, wrap('','',"$progname: Warning. '$pkgchk' provides no packages for '$arch' " .
			"or '$pkgchk' could be a new package that is not yet available for '$arch'."),
			RESET,"\n") if (not defined ($archlist{$arch}));
		# ensure the source package is returned.
		return $src;
	}
	my $msg = "Unable to locate source for package '$_[0]' - please check the spelling of the package name. ";
	$msg .= "Consider using 'dpkg -S $_[0]' to identify the source package if the spelling is correct.\n";
	print RED, wrap('','', $msg), RESET
		if ($verbose >= 1);
	return "";
}

sub catch_no_dir
{
	opendir(DIR, ".") or die (RED, "Cannot find or open $dir: $!", RESET);
	my @dirs=readdir(DIR);
	closedir(DIR);
	foreach my $f (@dirs)
	{
		if ((-d $f) && ($f =~ /^\Q$package\E/) && ($f !~ /\.old$/) && (-d "$f/debian"))
		{
			$dir = $f;
		}
	}
	return $dir;
}

sub apply_patches()
{
	$patchdir = shift;
	$patchdir = "../" if (not defined $patchdir);
	# emdebian patch files use the names: emdebian-$file.patch
	opendir (PATCHES, "$patchdir") or die ("Unable to open parent directory.\n");
	my @patchfiles=grep(/^emdebian-.*\.patch$/, readdir PATCHES);
	closedir (PATCHES);
	foreach my $patch (sort @patchfiles)
	{
		my $reverse = &try_patch($patchdir, $patch);
		if ($reverse == -1)
		{
			warn (RED, "Skipping $patch, unable to apply it.", RESET, "\n");
			if ($patch =~ /^emdebian-changelog.patch$/)
			{
				if ($autobuild > 0)
				{
					print GREEN, "Using 'emdebuild -n' to refresh the changelog patch\n"
						, RESET;
					# need to output something about the version being attempted here.
					$clog = `parsechangelog --format dpkg | grep Version`;
					$clog =~ s/^Version: (.*)$/$1/;
					chomp ($clog);
					# If the changelog patch fails to apply, assume a new version em1
					print RED, "$progname: Attempted version: ${clog}em1\n", RESET;
				}
				$next = 1;
			}
			else
			{
				# flag this package as having out of date patches.
				system ("embug -o");
				# do not autobuild if patches have failed.
				exit (1) if ($autobuild > 0);
			}
			next;
		}
		if ($reverse == 1)
		{
			warn (RED, "$patch has already been applied, skipping", RESET, "\n");
			next;
		}
		print GREEN, "Applying $patch\n", RESET if ($verbose >= 2);
		$patch =~ /^emdebian-(.*)\.patch$/;
		my $tidy = $1;
		$newversion = 1 if ($patch =~ /^emdebian-changelog.patch$/);
		$retval = 0;
		$retval = system ("patch -p1 < $patchdir/$patch");
		print RED, "patch reported an error for $patchdir/$patch.\n", RESET if ($retval != 0);
		# tidy up after the patch command.
		unlink "debian/$tidy.orig" if ( -f "debian/$tidy.orig");
		unlink "debian/$tidy.rej" if ( -f "debian/$tidy.rej");
	}
	# now repeat for machine variant patches
	return if ($machine_done > 0);
	&machine_variant;
}

sub machine_variant
{
	# outline support for machine/variant patching.
	return if (not defined $machine);
	return if ($machine_done > 0);
	$variant = 'default' if (not defined $variant);
	# progress marker
	print GREEN, "Checking for customised patches for $machine:$variant\n", RESET;
	if (not defined $machine_path)
	{
		$workdir = &get_workdir;
		$workdir = "/" if ($workdir eq "");
	}
	else
	{
		$workdir = $machine_path;
	}
	$msg = &check_workdir($workdir);
	die RED, $msg, RESET if ($msg ne "");
	$patchdir = "${workdir}/machine/$machine/$variant/${indexchar}/${package}/";
	$patchdir =~ s://:/:g;
	print CYAN, "Patch directory: '$patchdir'\n", RESET if ($verbose >= 2);
	$machine_done = 1;
	&apply_patches($patchdir) if (-d $patchdir);
	# stamp file to prevent emdebuild committing machine-specific changes
	# to Emdebian SVN.
	open (STAMP, ">../machine-patch.stamp")
		or die (RED,"Unable to write stamp file",RESET,".\n");
	print STAMP "$machine:$variant\n";
	close (STAMP);
}

# attempt to unwind a damaged/borked or otherwise bust emsource directory.
sub emcleanup ()
{
	my $cwd = cwd;
	print CYAN, "Cleaning $cwd:\n", RESET if ($verbose >= 1);
	# need to remove ../trunk and run svn update from there or barff if there is no ../trunk
	if (! -d "../.svn")
	{
		my $msg = "No .svn directory found, no ../../trunk/ - cannot clean up $dir for $package";
		$msg .= "You may have to put the Debian source into a safe directory and ";
		$msg .="then remove ../../trunk before rerunning '$progname $package'.";
		die (RED, wrap('','',"$msg\n"), RESET)
			if ( ! -d "../../trunk");
	}
	# remove the stamp file for machine_variant, if any
	unlink ("../machine-patch.stamp") if (-f "../machine-patch.stamp");
	system ("rm -f ../emdebian*.patch");
	system ("rm -f ../debian-patch*");
	my $v = ($verbose >= 1) ? "" : "-q";
	system ("svn update --non-interactive $v ../");
	# remove .old, remove and replace source
	system ("rm -rf ../$package.old") if (-d "../$package.old");
	chdir ("../");
	system ("rm -rf ./$dir") if((-d "./$dir") && ("$dir" ne ""));
	$retval = system ("apt-get source $package");
	exit($retval) if ($retval != 0);
	# in case a new upstream release has changed the directory name.
	$cwd = &catch_no_dir;
	chdir ($cwd);
	&prepare_pristine($package);
	print CYAN, "Emdebianising source tree. Applying patches from SVN.\n", RESET
		if ($verbose >= 2);
	&apply_patches();
	opendir (PATCHES, "../") or die (RED, "Unable to open parent directory.", RESET, "\n");
	# patches intended for debian/patches use the names: debian-patch-$file
	my @patchfiles=grep(/^debian-patch-.*$/, readdir PATCHES);
	closedir (PATCHES);
	foreach my $patch (sort @patchfiles)
	{
		$patch =~ s/^debian-patch-//;
		mkdir "debian/patches" unless (-d "debian/patches");
		open (PATCH, "../debian-patch-$patch") or
			warn (RED, "Cannot open debian-patch-$patch : $!", RESET, "\n");
		my @p=<PATCH>;
		close (PATCH);
		print GREEN, "Restoring debian patch: $patch\n", RESET;
		open (DPATCH, ">debian/patches/$patch") or
			warn (RED, "Cannot write debian patch: $!", RESET, "\n");
		print DPATCH @p;
		close (DPATCH);
		# add this patch to the patch series, if one is in use
		`echo $patch >> debian/patches/series` 
			if (( -f "debian/patches/series" ) && ($patch !~ /^series$/));
	}
}

sub where_is ()
{
	$workdir = &get_workdir;
	$workdir = "/" if ($workdir eq "");
	$msg = &check_workdir($workdir);
	die RED, $msg, RESET if ($msg ne "");
	print CYAN, "Working directory: '$workdir'\n", RESET if ($verbose >= 2);
	my $where = "$workdir/trunk/$indexchar/$src/trunk/$dir/";
	$where =~ s://:/:;
	if ( ! -d $where)
	{
		chdir ("$workdir/trunk/$indexchar/$src/trunk");
		$dir = &catch_no_dir;
		$where = "$workdir/trunk/$indexchar/$src/trunk/$dir/";
		$where =~ s://:/:;
		if ( ! -d $where)
		{
			print RED, "Cannot find '$where'.\n", RESET;
			print GREEN, wrap('','', "$package exists in Debian but does not appear" .
				" to have been built on this machine yet. Try '$progname $package'\n"), RESET
				if ($verbose >= 2);
			exit (0);
		}
	}
	print GREEN, "$package is located in: '$where'", RESET, "\n";
	exit (0);
}

sub show_stats ()
{
	$workdir = &get_workdir;
	$workdir = "/" if ($workdir eq "");
	$msg = &check_workdir($workdir);
	die RED, $msg, RESET if ($msg ne "");
	print CYAN, "Working directory: '$workdir'\n", RESET if ($verbose >= 2);
	my $where = "$workdir/trunk/$indexchar/$src/trunk/";
	$where =~ s://:/:;
	if ( ! -d $where)
	{
		print RED, "Cannot find '$where'.\n", RESET;
		print GREEN, wrap('','', "$package exists in Debian but does not appear" .
			" to have been built on this machine yet. " . "Try '$progname $package'\n"), RESET if ($verbose >= 2);
		exit (0);
	}
	print CYAN, "Checking status of $src in $where\n", RESET if ($verbose >= 1);
	my $empatches = `find $where -name emdebian\\*.patch | grep -c emdebian.*\.patch`;
	chomp($empatches);
	print GREEN, "$empatches emdebian patch files\n", RESET;
	my $debpatches = `find $where -name debian-patch\\*.patch | grep -c debian-patch.*\.patch`;
	chomp($debpatches);
	if (($debpatches == 0) or ($debpatches > 1)) { print CYAN, "$debpatches debian patch files\n", RESET; }
	else { print CYAN, "$debpatches debian patch file\n", RESET; }
	# trunk == emdebuild pbuilder/result == empdebuild
	my @locations = qw: trunk pbuilder/result:;
	foreach my $loc (@locations)
	{
		$where = "$workdir/${loc}/$indexchar/$src/trunk/";
		$where =~ s://:/:;
		next if (! -d $where);
		print CYAN, "\nChecking emdebuild status in $where\n", RESET
			if ($loc =~ /^trunk$/);
		print CYAN, "\nChecking empdebuild status\n", RESET if ($loc =~ /^pbuilder\/result$/);
		$where =~ s://:/:;
		my @all = `find "$where" -name "${src}_*_${arch}.build"`;
		my @embuild = `find "$where" -name "${src}_${v}*_${arch}.build"`;
		if (scalar @embuild == 1)
		{
			print GREEN, "build log:\n", RESET;
			my $state = `tail $where/${src}_${v}*_${arch}.build`;
			print RED, "\t${src} (${v}) FAILED to cross build for $arch.\n", RESET
				if ($state =~ /dpkg-buildpackage: failure/)
		}
		print GREEN, "build logs:\n", RESET
			if ((scalar @embuild == 0) and (scalar @all == 0));
		print RED, "$package may be out of date.\n", RESET
			if ((scalar @embuild == 0) and (scalar @all > 0));
		print GREEN, "build logs:\n", RESET if (scalar @embuild >1);
		foreach my $l (@embuild)
		{
			$l =~ /\Q${src}_${v}\E(.*)_${arch}.build/;
			my $emver = $1;
			$emver =~ s/^\-//;
			print "${where}${src}_${v}-${emver}_${arch}.build\n";
		}
		my @emchanges = `find "$where" -name "${src}_${v}*_${arch}.changes"`;
		print GREEN, ".changes:\n", RESET if (@emchanges);
		my $latest_v = 0;
		my $latest_c;
		foreach my $l (@emchanges)
		{
			chomp($l);
			$l =~ /\Q${src}_${v}\E(.*)_${arch}.changes/;
			my $emver = $1;
			$emver =~ s/^\-//;
			my $lv = $emver;
			$lv =~ s/[^0-9]//g;
			if ($lv > $latest_v)
			{
				$latest_v = $lv;
				$latest_c = $l;
			}
			print "$l";
			if (( -e "/usr/bin/gpg") and ($verbose >= 2))
			{
				`gpg --verify $l 2>/dev/null`;
				if ( $? == 0)
				{
					print "\n";
					system ("gpg --verify $l");
				}
				else { print GREEN, " unsigned.", RESET; }
			}
			print "\n";
		}
		if ((defined $latest_c) and ($loc eq "trunk"))
		{
			# run lintian
			chomp($latest_c);
			print GREEN, "Running lintian checks for Emdebian only.\n", RESET;
			print GREEN, wrap ('','',"Checking only the most recent .changes file:".
				" '$latest_c'\n"), RESET if ($verbose >= 2);
			system ("lintian --color auto -C em $latest_c");
			system ("embug --lintian -p $src") if ($? != 0);
			print GREEN, "Finished running lintian.\n", RESET;
		}
		my @emupload = `find "$where" -name "${src}_${v}*_${arch}.upload"`;
		my %repository=();
		print GREEN, ".upload:\n", RESET if (@emupload);
		foreach my $l (@emupload)
		{
			$l =~ /\Q${src}_${v}\E(.*)_${arch}.upload/;
			my $emver = $1;
			my $upcount = 0;
			my $upfile = "${where}${src}_${v}${emver}_${arch}.upload";
			print "$upfile\n";
			if ((defined $emver) and ( -f "$upfile"))
			{
				$emver =~ s/^\-//;
				open (UP, "$upfile");
				my @repo=<UP>;
				close (UP);
				foreach my $rep (@repo)
				{
					$rep =~ /\s(\S+)\.$/;
					$repository{$1}++;
					$upcount++;
				}
				print CYAN, "$upcount files uploaded to " . join(", ", keys (%repository));
				my @about = stat "$upfile";
				# read the ctime of the .upload file
				print " on " . scalar localtime($about[10]) . " for ${v}${emver}\n", RESET;
			}
		}
	}
	if  (-f "/usr/bin/emrecent")
	{
		$where = "$workdir/trunk/$indexchar/$src/trunk/";
		$where =~ s://:/:;
		print CYAN, "Checking for error logs in $where", RESET,"\n";
		if (-f "${where}emrecent_error.log")
		{
			open (LOG, "${where}emrecent_error.log")
				or die ("Cannot open error log. $!\n");
			my @log=<LOG>;
			close (LOG);
			print RED, @log, RESET;
			print GREEN, wrap('','',"If these errors have been resolved, delete ".
			"the '${where}emrecent_error.log' file.\n"), RESET;
		}
	}
	print CYAN, "Checking bug status",RESET,"\n";
	# read embug data.
	my $bugs = &bug_config;
	if (not defined $bugs->{$package})
	{
		print GREEN, "No open cross-building bugs for $package\n", RESET;
	}
	elsif ($bugs->{$package} eq "")
	{
		print GREEN, "$package is flagged but no bug has been reported.", RESET, "\n";
	}
	else
	{
		my $number = $bugs->{$package};
		my $rr = (ref($number) eq 'ARRAY') ? join(' #', @$number) : $number;
		print GREEN, "$package has an open cross-building bug report: $rr\n", RESET;
		$rr = (ref($number) eq 'ARRAY') ? join("\nhttp://bugs.debian.org/", @$number) : $number;
		print GREEN, "http://bugs.debian.org/$rr\n", RESET;
	}
	my $config = &target_config();
	if (defined $config->{"$package"})
	{
		print RED, "Emdebian patches for $package are out of date.\n", RESET
			if ($config->{"$package"} eq "patches");
		print RED, "$package FAILED to build.\n", RESET
			if ($config->{"$package"} eq "failed");
		print RED, wrap('','',"$package is waiting for a dependency - see ".
		"above.\n"), RESET
			if ($config->{"$package"} eq "waiting");
	}
	exit (0);
}