aboutsummaryrefslogtreecommitdiff
path: root/SBO-Lib/lib/SBO/Lib.pm
blob: 6bdd175a668d5d9d9a3a5d90c1bfc48408c4d171 (plain)
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
#!/usr/bin/env perl
#
# sbolib.sh
# shared functions for the sbo_ scripts.
#
# author: Jacob Pipkin <j@dawnrazor.net>
# date: Setting Orange, the 37th day of Discord in the YOLD 3178
# license: WTFPL <http://sam.zoy.org/wtfpl/COPYING>

package SBO::Lib 0.5;
my $version = "0.5";

require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(
	script_error
	show_version
	get_slack_version
	check_slackbuilds_txt
	slackbuilds_or_fetch
	fetch_tree
	update_tree
	get_installed_sbos
	get_available_updates
	do_slackbuild
	make_clean
	make_distclean
	do_upgradepkg
	get_sbo_location
	get_pkg_name
	make_temp_file
);

use warnings FATAL => 'all';
use strict;
use File::Basename;
use Tie::File;
use IO::File;
use Sort::Versions;
use Digest::MD5;
use File::Copy;
use File::Path qw(make_path remove_tree);
use Fcntl;
use File::Find;

$< == 0 or print "This script requires root privileges.\n" and exit (1);

our $conf_dir = '/etc/sbotools';
our $conf_file = "$conf_dir/sbotools.conf";
my @valid_conf_keys = (
	'NOCLEAN',
	'DISTCLEAN',
	'JOBS',
	'PKG_DIR',
	'SBO_HOME',
);

our %config;
# if the conf file exists, pull all the $key=$value pairs into a hash
if (-f $conf_file) {
	open my $reader, '<', $conf_file;
	my $text = do {local $/; <$reader>};
	%config = $text =~ /^(\w+)=(.*)$/mg;
	close $reader;
}
# undef any invalid $key=$value pairs
for my $key (keys %config) {
	unless ($key ~~ @valid_conf_keys) {
		undef $config{$key};
	}
}
# ensure we have sane configs, and defaults for anything not in the conf file
for my $key (@valid_conf_keys) {
	if ($key eq 'SBO_HOME') {
		$config{$key} = '/usr/sbo' unless exists $config{$key};
	} elsif ($key eq 'JOBS') {
		if (exists $config{$key}) {
			$config{$key} = 'FALSE' unless $config{$key} =~ /^\d+$/;
		} else {
			$config{$key} = 'FALSE';
		}
	} else {
		$config{$key} = 'FALSE' unless exists $config{$key};
	}
}

my $distfiles = "$config{SBO_HOME}/distfiles";
my $slackbuilds_txt = "$config{SBO_HOME}/SLACKBUILDS.TXT";

my $name_regex = '\ASLACKBUILD\s+NAME:\s+';

# this should be done a bit differently.
sub script_error {
	unless (exists $_[0]) {
		print "A fatal script error has occured. Exiting.\n";
	} else {
		print "A fatal script error has occured:\n";
		print "$_[0]\n";
		print "Exiting.\n";
	}
	exit 1;
} 

sub show_version {
	print "sbotools version $version\n";
	print "licensed under the WTFPL\n";
	print "<http://sam.zoy.org/wtfpl/COPYING>\n";
}

sub get_slack_version {
	if (-f '/etc/slackware-version') {
		open my $slackver, '<', '/etc/slackware-version';
		chomp (my $line = <$slackver>); 
		close $slackver;
		my $slk_version = split_line ($line, ' ', 1);
		# for now, we may as well die if $slk_version ne '13.37', since it and
		# current, which will also be '13.37' in this case, are the only
		# supported versions
		if ($slk_version eq '13.37.0') {
			$slk_version = '13.37';
		} else {
			print "Unsupported Slackware version: $slk_version\n" and exit (1);
		}
		return $slk_version;
	} else {
		print "I am unable to locate your /etc/slackware-version file.\n";
		exit 1;
	}
}

sub check_slackbuilds_txt {
	return 1 if -f $slackbuilds_txt;
	return;
}

# check for the existence of $config{SBO_HOME}, and whether or not it already
# has stuff in
sub check_home {
	my $sbo_home = $config{SBO_HOME};
	if (-d $sbo_home) {
		opendir (my $home_handle, $sbo_home);
		while (readdir $home_handle) {
			next if /^\.[\.]{0,1}$/;
			print "$sbo_home exists and is not empty. Exiting.\n";
			exit 1;
		}
	} else {
		make_path ($sbo_home) or print "Unable to create $sbo_home. Exiting.\n"
			and exit (1);
	}
}

sub rsync_sbo_tree {
	my $slk_version = get_slack_version ();
	my $cmd = 'rsync';
	my @arg = ('-a', '--exclude=*.tar.gz', '--exclude=*.tar.gz.asc');
	push (@arg, "rsync://slackbuilds.org/slackbuilds/$slk_version/*");
	push (@arg, $config{SBO_HOME});
	system ($cmd, @arg);
	print "Finished.\n";
	return 1;
}

sub fetch_tree {
	check_home ();
	print "Pulling SlackBuilds tree...\n";
	rsync_sbo_tree ();
}

sub update_tree {
	check_slackbuilds_txt ();
	print "Updating SlackBuilds tree...\n";
	rsync_sbo_tree ();
}

# if the SLACKBUILDS.TXT is not in $config{SBO_HOME}, we should assume the tree
# has not been populated there, since we rely on that file anyway; prompt the
# user to automagickally pull the tree.
sub slackbuilds_or_fetch {
	if (! check_slackbuilds_txt () ) {
		print "It looks like you haven't run \"sbosnap fetch\" yet.\n";
		print "Would you like me to do this now? [y] ";
		my $fetch = <STDIN>;
		$fetch = 'y' if $fetch eq "\n";
		if ($fetch =~ /^[Yy]/) {
			fetch_tree ();
		} else {
			print "Please run \"sbosnap fetch\"\n";
			exit 0;
		}
	}
}

# pull an array of hashes, each hash containing the name and version of an sbo
# currently installed. starting to think it might be better to only pull an
# array of names, and have another sub to pull the versions.
sub get_installed_sbos {
	my @installed;
	opendir my $diread, '/var/log/packages';
	while (my $ls = readdir $diread) {
		next if $ls =~ /\A\./;
		if (index ($ls, "SBo") != -1) {
			my @split = split (/-/, reverse ($ls), 4);
			my %hash;
			$hash{name} = reverse ($split[3]);
			$hash{version} = reverse ($split[2]);
			push (@installed, \%hash);
		}
	}
	return @installed;
}

# take a line and get rid of newlines, spaces, double quotes, and backslashes
sub clean_line {
	script_error ('clean line requires an argument') unless exists $_[0];
	chomp (my $line = shift);
	$line =~ s/[\s"\\]//g;
	return $line;
}

# given a line, pattern, and index, split the line on the pattern, and return
# a clean_line'd version of the index
sub split_line {
	script_error ('split_line requires three arguments') unless exists $_[2];
	my ($line, $pattern, $index) = @_;
	my @split;
	if ($pattern eq ' ') {
		@split = split ("$pattern", $line);
	} else {
		@split = split (/$pattern/, $line);
	}
	return clean_line ($split[$index]);
}

# pull a clean_line'd value from a $key=$value pair
sub split_equal_one {
	script_error ('split_equal_one requires an argument') unless exists $_[0];
	return split_line ($_[0], '=', 1);
}

# search the tree for a given sbo's directory
sub get_sbo_location {
	script_error ('get_sbo_location requires an argument.Exiting.')
		unless exists $_[0];
	my $sbo = shift;
	my $location;
	my $regex = qr#$config{SBO_HOME}/[^/]+/\Q$sbo\E\z#;
	find (
		sub {
			$location = $File::Find::dir if $File::Find::dir =~ $regex
		},
		$config{SBO_HOME}
	);
	return unless defined $location;
	return $location;
}

# for each installed sbo, find out whether or not the version in the tree is
# newer, and compile an array of hashes containing those which are
sub get_available_updates {
	my @updates;
	my @pkg_list = get_installed_sbos ();
	FIRST: for my $key (keys @pkg_list) {
		my $location = get_sbo_location ($pkg_list[$key]{name});
		# if we can't find a location, assume invalid and skip
		next FIRST unless defined $location;
		my $regex = qr/^VERSION=/;
		open my $info, '<', "$location/$pkg_list[$key]{name}.info";
		SECOND: while (my $line = <$info>) {
			if ($line =~ $regex) {
				my $sbo_version = split_equal_one ($line);
				if (versioncmp ($sbo_version, $pkg_list[$key]{version}) == 1) {
					my %hash = (
						name => $pkg_list[$key]{name},
						installed => $pkg_list[$key]{version},
						update => $sbo_version,
					);
					push (@updates, \%hash);
				}
				last SECOND;
			}
		}
		close $info;
	}
	return @updates;
}

# pull links or md5sums (type - 'download','md5sum') from a given sbo's .info
# file, first checking for x86_64-specific info we are told to
sub find_download_info {
	script_error('find_download_info requires four arguments.')
		unless exists $_[3];
	my ($sbo, $location, $type, $x64) = @_;
	my @return;
	my $regex;
	if ($type eq 'download') {
		$regex = qr/^DOWNLOAD/;
	} elsif ($type eq 'md5sum') {
		$regex = qr/^MD5SUM/;
	}
	if ($x64) {
		$regex = qr/${regex}_x86_64=/;
	} else {
		$regex = qr/$regex=/;
	}
	# the x86_64 info may be empty
	my $empty_regex = qr/=""$/;
	# we need to know whether or not there are more than one lines for a given
	# key
	my $back_regex = qr/\\$/;
	# assume there's not
	my $more = 'FALSE';
	open my $info, '<', "$location/$sbo.info";
	FIRST: while (my $line = <$info>) {
		unless ($more eq 'TRUE') {
			if ($line =~ $regex) {
				last FIRST if $line =~ $empty_regex;
				# some sbos have UNSUPPORTED for the x86_64 info, meaning we
				# proceed to pull the non-x86_64-specific info
				unless (index ($line, 'UNSUPPORTED') != -1) {
					push (@return, split_equal_one ($line) );
					$more = 'TRUE' if $line =~ $back_regex;
				} else {
					last FIRST;
				}
			}
		} else {
			$more = 'FALSE' unless $line =~ $back_regex;
			$line = clean_line ($line);
			push (@return, $line);
		}
	}
	close $info;
	return @return if exists $return[0];
	return;
}

sub get_arch {
	chomp (my $arch = `uname -m`);
	return $arch;
}

# assemble an array of hashes containing links and md5sums for a given sbo,
# with the option of only checking for 32-bit links, for -compat32 packaging
sub get_sbo_downloads {
	script_error ('get_sbo_downloads requires three arguments.')
		unless exists $_[2];
	script_error ('get_sbo_downloads given a non-directory.') unless -d $_[1];
	my ($sbo, $location, $only32) = @_;
	my $arch = get_arch ();
	my (@links, @md5s);
	if ($arch eq 'x86_64') {
		unless ($only32 eq 'TRUE') {
			@links = find_download_info ($sbo, $location, 'download', 1);
			@md5s = find_download_info ($sbo, $location, 'md5sum', 1);
		}
	}
	unless (exists $links[0]) {
		@links = find_download_info ($sbo, $location, 'download', 0);
		@md5s = find_download_info ($sbo, $location, 'md5sum', 0);
	}
	my @downloads;
	for my $key (keys @links) {
		my %hash = (link => $links[$key], md5sum => $md5s[$key]);
		push (@downloads, \%hash);
	}
	return @downloads;
}

sub get_filename_from_link {
	script_error ('get_filename_from_link requires an argument')
		unless exists $_[0];
	my @split = split ('/', reverse (shift) , 2);
	chomp (my $filename = $distfiles .'/'. reverse ($split[0]) );
	return $filename;
}

sub compute_md5sum {
	script_error ('compute_md5sum requires an argument.') unless exists $_[0];
	script_error ('compute_md5sum argument is not a file.') unless -f $_[0];
	my $filename = shift;
	open my $reader, '<', $filename;
	my $md5 = Digest::MD5->new;
	$md5->addfile ($reader);
	my $md5sum = $md5->hexdigest;
	close $reader;
	return $md5sum;
}

# for a given distfile, see whether or not it exists, and if so, if its md5sum
# matches the sbo's .info file
sub check_distfile {
	script_error ('check_distfile requires two arguments.') unless exists $_[1];
	my ($link, $info_md5sum) = @_;
	my $filename = get_filename_from_link ($link);
	return unless -d $distfiles;
	return unless -f $filename;
	my $md5sum = compute_md5sum ($filename);
	return unless $info_md5sum eq $md5sum;
	return 1;
}

# for a given distfile, attempt to retrieve it and, if successful, check its
# md5sum against that in the sbo's .info file
sub get_distfile {
	script_error ('get_distfile requires an argument') unless exists $_[1];
	my ($link, $expected_md5sum) = @_;
	my $filename = get_filename_from_link ($link);
	mkdir ($distfiles) unless -d $distfiles;
	chdir ($distfiles);
	my $out = system ("wget $link");
	return unless $out == 0;
	my $md5sum = compute_md5sum ($filename);
	if ($md5sum ne $expected_md5sum) {
		print "md5sum failure for $filename.\n";
		exit 1;
	}
	return 1;
}

# find the version in the tree for a given sbo
sub get_sbo_version {
	script_error ('get_sbo_version requires two arguments.')
		unless exists $_[1];
	my ($sbo, $location) = @_;
	my $version;
	open my $info, '<', "$location/$sbo.info";
	my $version_regex = qr/\AVERSION=/;
	FIRST: while (my $line = <$info>) {
		if ($line =~ $version_regex) {
			$version = split_equal_one ($line);
			last FIRST;
		}
	}
	close $info;
	return $version;
}

# for a given distfile, what will be the full path of the symlink?
sub get_symlink_from_filename {
	script_error ('get_symlink_from_filename requires two arguments')
		unless exists $_[1];
	script_error ('get_symlink_from_filename first argument is not a file')
		unless -f $_[0];
	my ($filename, $location) = @_;
	my @split = split ('/', reverse ($filename), 2);
	my $fn = reverse ($split[0]);
	return "$location/$fn";
}

# determine whether or not a given sbo is 32-bit only
sub check_x32 {
	script_error ('check_x32 requires two arguments.') unless exists $_[1];
	my ($sbo, $location) = @_;
	open my $info, '<', "$location/$sbo.info";
	my $regex = qr/^DOWNLOAD_x86_64/;
	FIRST: while (my $line = <$info>) {
		if ($line =~ $regex) {
			return 1 if index ($line, 'UNSUPPORTED') != -1;
		}
	}
	return;
}

# can't do 32-bit on x86_64 without this file, so we'll use it as the test to
# to determine whether or not an x86_64 system is setup for multilib
sub check_multilib {
	return 1 if -f '/etc/profile.d/32dev.sh';
	return;
}

# necessary to rewrite the .SlackBuild on the fly, at the very least, in order
# to add our tee commands in, so that we can grok the output; optionally, to 
# alter the LIBDIRSUFFIX, for 32-bit things, to edit the "make" command for -j,
# or to change the output architecture. first thing we do is backup the
# existent .SlackBuild file.
sub rewrite_slackbuild {
	script_error ('rewrite_slackbuild requires two arguments.')
		unless exists $_[1];
	my ($slackbuild, $tempfn, %changes) = @_;
	copy ($slackbuild, "$slackbuild.orig");
	my $tar_regex = qr/(un|)tar .*$/;
	my $makepkg_regex = qr/makepkg/;
	my $libdir_regex = qr/^\s*LIBDIRSUFFIX="64"\s*$/;
	my $make_regex = qr/^\s*make(| \Q||\E exit 1)$/;
	my $arch_out_regex = qr/\$VERSION-\$ARCH-\$BUILD/;
	tie my @sb_file, 'Tie::File', $slackbuild;
	FIRST: for my $line (@sb_file) {
		# get the output of the tar and makepkg commands. hope like hell that v
		# is specified among tar's arguments
		if ($line =~ $tar_regex || $line =~ $makepkg_regex) {
			$line = "$line | tee -a $tempfn";
		}
		if (%changes) {
			SECOND: while (my ($key, $value) = each %changes) {
				if ($key eq 'libdirsuffix') {
					if ($line =~ $libdir_regex) {
						$line =~ s/64/$value/;
					}
				}
				if ($key eq 'make') {
					if ($line =~ $make_regex) {
						$line =~ s/make/make $value/;
					}
				}
				if ($key eq 'arch_out') {
					if ($line =~ $arch_out_regex) {
						$line =~ s/\$ARCH/$value/;
					}
				}
			}
		}
	}
	untie @sb_file;
	return 1;
}

# move a backed-up .SlackBuild file back into place
sub revert_slackbuild {
	script_error ('revert_slackbuild requires an argument') unless exists $_[0];
	my $slackbuild = shift;
	if (-f "$slackbuild.orig") {
		if (-f $slackbuild) {
			unlink $slackbuild;
		}
		rename ("$slackbuild.orig", $slackbuild);
	}
	return 1;
}

# given a location and a list of download links, assemble a list of symlinks,
# and create them.
#
# actually, we're also handling the links themselves here. odd.
sub create_symlinks {
	script_error ('create_symlinks requires two arguments.')
		unless exists $_[1];
	my ($location, @downloads) = @_;
	my @symlinks;
	for my $key (keys @downloads) {
		my $link = $downloads[$key]{link};
		my $md5sum = $downloads[$key]{md5sum};
		my $filename = get_filename_from_link ($link);
		unless (check_distfile ($link, $md5sum) ) {
			die unless get_distfile ($link, $md5sum);
		}
		my $symlink = get_symlink_from_filename ($filename, $location);
		push (@symlinks, $symlink);
		symlink ($filename, $symlink);
	}
	return @symlinks;
}

# make a .SlackBuild executable.
sub prep_sbo_file {
	script_error ('prep_sbo_file requires two arguments') unless exists $_[1];
	my ($sbo, $location) = @_;
	chdir ($location);
	chmod (0755, "$location/$sbo.SlackBuild");
	return 1;
}

# pull the untarred source directory or created package name from the temp
# file (the one we tee'd to)
sub grok_temp_file {
	script_error ('grok_temp_file requires two arguments') unless exists $_[1];
	my ($tempfn, $find) = @_;
	my $out;
	open my $fh, '<', $tempfn;
	FIRST: while (my $line = <$fh>) {
		if ($find eq 'pkg') {
			if ($line =~ /^Slackware\s+package\s+([^\s]+)\s+created\.$/) {
				$out = $1;
				last FIRST;
			}
		} elsif ($find eq 'src') {
			if ($line =~ /^([^\/]+)\/.*$/) {
				$out = $1;
				last FIRST;
			}
		}
	}
	close $fh;
	return $out;
}

# wrappers around grok_temp_file
sub get_src_dir {
	script_error ('get_src_dir requires an argument') unless exists $_[0];
	my $filename = shift;
	return grok_temp_file ($filename, 'src');
}

sub get_pkg_name {
	script_error ('get_pkg_name requires an argument') unless exists $_[0];
	my $filename = shift;
	return grok_temp_file ($filename, 'pkg');
}

# safely create a temp file
sub make_temp_file {
	make_path ('/tmp/sbotools') unless -d '/tmp/sbotools';
	my $temp_dir = -d '/tmp/sbotools' ? '/tmp/sbotools' : $ENV{TMPDIR} ||
		$ENV{TEMP};
	my $filename = sprintf "%s/%d-%d-0000", $temp_dir, $$, time;
	sysopen my ($fh), $filename, O_WRONLY|O_EXCL|O_CREAT;
	return ($fh, $filename);
}

# prep and run .SlackBuild
sub perform_sbo {
	script_error ('perform_sbo requires five arguments') unless exists $_[4];
	my ($jobs, $sbo, $location, $arch, $c32, $x32) = @_;
	prep_sbo_file ($sbo, $location);
	my $cmd;
	my %changes;
	unless ($jobs eq 'FALSE') {
		$changes{make} = "-j $jobs";
	}
	if ($arch eq 'x86_64' and ($c32 eq 'TRUE' || $x32) ) {
		if ($c32 eq 'TRUE') {
			$changes{libdirsuffix} = '';
		} elsif ($x32) {
			$changes{arch_out} = 'i486';
		}
		$cmd = ". /etc/profile.d/32dev.sh && $location/$sbo.SlackBuild";
	} else {
		$cmd = "$location/$sbo.SlackBuild";
	}
	my ($tempfh, $tempfn) = make_temp_file ();
	close $tempfh;
	rewrite_slackbuild ("$location/$sbo.SlackBuild", $tempfn, %changes);
	my $out = system ($cmd);
	revert_slackbuild ("$location/$sbo.SlackBuild");
	die unless $out == 0;
	my $src = get_src_dir ($tempfn);
	my $pkg = get_pkg_name ($tempfn);
	unlink $tempfn;
	return $pkg, $src;
}

# "public interface", sort of thing.
sub do_slackbuild {
	script_error ('do_slackbuild requires two arguments.') unless exists $_[1];
	my ($jobs, $sbo, $location, $compat32) = @_;
	my $arch = get_arch ();
	my $version = get_sbo_version ($sbo, $location);
	my @downloads = get_sbo_downloads ($sbo, $location, $compat32);
	my $x32;
	if ($compat32 eq 'TRUE') {
		unless ($arch eq 'x86_64') {
			print 'You can only create compat32 packages on x86_64 systems.';
			exit 1;
		} else {
			if (! check_multilib () ) {
				print "This system does not appear to be setup for multilib.\n";
				exit 1;
			}
			if (! -f '/usr/sbin/convertpkg-compat32') {
				print "compat32 pkgs require /usr/sbin/convertpkg-compat32.\n";
				exit 1;
			}
		}
	} else {
		if ($arch eq 'x86_64') {
			$x32 = check_x32 ($sbo, $location);
			if ($x32 && ! check_multilib () ) {
				print "$sbo is 32-bit only, however, this system does not appear 
to be setup for multilib.\n";
				exit 1;
			}
		}
	}
	my @symlinks = create_symlinks ($location, @downloads);
	my ($pkg, $src) = perform_sbo
		($jobs, $sbo, $location, $arch, $compat32, $x32);
	if ($compat32 eq 'TRUE') {
		my ($tempfh, $tempfn) = make_temp_file ();
		close $tempfh;
		my $cmd = "/usr/sbin/convertpkg-compat32 -i $pkg -d /tmp | tee $tempfn";
		my $out = system ($cmd);
		die unless $out == 0;
		unlink $pkg;
		$pkg = get_pkg_name ($tempfn);
	}
	unlink ($_) for @symlinks;
	return $version, $pkg, $src;
}

# remove work directories (source and packaging dirs under /tmp/SBo)
sub make_clean {
	script_error ('make_clean requires two arguments.') unless exists $_[1];
	my ($sbo, $src, $version) = @_;
	print "Cleaning for $sbo-$version...\n";
	my $tmpsbo = "/tmp/SBo";
	remove_tree ("$tmpsbo/$src") if -d "$tmpsbo/$src";
	remove_tree ("$tmpsbo/package-$sbo") if -d "$tmpsbo/package-$sbo";
	return 1;
}

# remove distfiles
sub make_distclean {
	script_error ('make_distclean requires three arguments.')
		unless exists $_[2];
	my ($sbo, $src, $version, $location) = @_;
	make_clean ($sbo, $src, $version);
	print "Distcleaning for $sbo-$version...\n";
	my @downloads = get_sbo_downloads ($sbo, $location, 0);
	for my $key (keys @downloads) {
		my $filename = get_filename_from_link ($downloads[$key]{link});
		unlink ($filename) if -f $filename;
	}
	return 1;
}

# run upgradepkg for a created package
sub do_upgradepkg {
	script_error ('do_upgradepkg requires an argument.') unless exists $_[0];
	my $pkg = shift;
	system ("/sbin/upgradepkg --reinstall --install-new $pkg");
	return;
}