Skip to content

Instantly share code, notes, and snippets.

@maxatome
Created May 1, 2020 20:33
Show Gist options
  • Save maxatome/dcd48de511f087c10f02a59405f134b6 to your computer and use it in GitHub Desktop.
Save maxatome/dcd48de511f087c10f02a59405f134b6 to your computer and use it in GitHub Desktop.
Install ImageMagick6 *AND* ImageMagick7 FreeBSD packages
#!/usr/local/bin/perl
# FreeBSD packages ImageMagick6 and ImageMagick7 cannot cohabit as
# they have some files in common.
#
# The goal is to strip common files from ImageMagick6 package and
# create a derived package (with the same name) so it can be installed
# close to ImageMagick7 one.
#
# Give the ImageMagick6-6.xxx.txz package file to this script. It then
# generates a ImageMagick6-libonly-6.xxx.txz you can install using:
#
# pkg install ImageMagick6-libonly-6.xxx.txz
use strict;
use warnings;
use 5.030;
use File::Temp 'tempdir';
use Cwd 'abs_path';
use JSON::PP;
use autodie;
@ARGV == 1 and $ARGV[0] !~ /^--?h/ or die "usage: $0 ImageMagick6-6.xxx.txz\n";
my $orig_pkg = abs_path($ARGV[0]);
my $target_dir = tempdir(CLEANUP => 1);
chdir $target_dir;
say "Extracting $orig_pkg";
system(tar => xpf => $orig_pkg) == 0 or die "Extraction of $orig_pkg failed\n";
my $json = JSON::PP->new->canonical;
my $manifest = $json->decode(do { local $/; open(my $fh, '<', '+MANIFEST'); <$fh> });
say "Patching";
my @files;
foreach my $file (keys %{$manifest->{files}})
{
if ($file =~ m,^(?:/usr/local/bin/
|/usr/local/lib/perl5/
|/usr/local/libdata/pkgconfig/
|/usr/local/man/man1/
),x)
{
delete $manifest->{files}{$file};
unlink substr($file, 1);
no autodie 'rmdir';
rmdir $file =~ s,/[^/]+,,r;
}
else
{
push(@files, substr($file, 1));
}
}
@files = sort @files;
{
open(my $fh, '>', '+MANIFEST');
print $fh $json->encode($manifest);
close $fh;
}
my $final_pkg = $orig_pkg =~ s,/ImageMagick6\K|\z,-libonly,r;
say "Creating new $final_pkg";
{
open(my $fh, '>', 'files.txt');
say $fh join("\n", '+COMPACT_MANIFEST', '+MANIFEST', @files);
close $fh;
}
system(tar => -JcPf => $final_pkg,
-T => 'files.txt',
-s => '!^usr!/usr!') == 0 or die "tar failed\n";
say "$final_pkg produced.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment