#!/usr/bin/perl

=head1 NAME

dh_epics_installlibs - Install host shared libraries in standard locations

=head1 SYNOPSIS

B<dh_epics_installlibs> [S<I<debhelper options>>]

=head1 DESCRIPTION

Looks for shared libraries in debian/tmp/${EPICS_BASE}/lib/${EPICS_HOST_ARCH}/.
Moves any shared libraries with SO names found to debian/tmp/usr/lib/
(or debian/tmp/usr/lib/<triplet>/ when using Multi-Arch)
and adjusts symlinks in the original directory.

This allows binaries linked against libXYZ.so.# to find it in the
default search path when run.  At the same time new binaries being
linked must explicitly add ${EPICS_BASE}/lib/<targetname>/ to
the search path.

The intent of this process is to prevent accidentally linking
against the wrong copy/version of a library.

Switching to the new Multi-Arch locations is controlled by
the debhelper compat level in debian/compat:
level 9 and up will use the new Multi-Arch locations.

=cut

use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;
use Debian::Debhelper::Dh_Epics qw(setepicsenv);

init();

my $multiarch=dpkg_architecture_value("DEB_HOST_MULTIARCH");

my $slashtriplet="";

if (! compat(8)) {
    if (defined $multiarch && $multiarch ne '') {
        $slashtriplet="/$multiarch";
    }
}

setepicsenv();

# collect host libraries with SO names
my @libs = glob("debian/tmp/$ENV{EPICS_BASE}/lib/$ENV{EPICS_HOST_ARCH}/lib*.so.*");

if( @libs > 0) {
    doit("install","-m","755","-d","debian/tmp/usr/lib$slashtriplet");
}

foreach my $lib (@libs) {
    my $edir = dirname($lib);
    my $lname = basename($lib);
    my ($base, $sov) = $lname =~ m/(lib.*\.so)\.(.*)/;

    if(-e "$edir/$base" and not -l "$edir/$base") {
        warning("Warning: Not a symlink:  $edir/$base");
    }

    doit("mv",$lib,"debian/tmp/usr/lib$slashtriplet/$lname");
    doit("rm","-f","$edir/$base");
    doit("ln","-s","../../..$slashtriplet/$lname","$edir/$base");
}


=head1 SEE ALSO

L<debhelper(7)>, L<epics-debhelper(7)>

This program is a not part of the official debhelper package.

=head1 AUTHOR

Michael Davidsaver <mdavidsaver@gmail.com>

=cut
