#!/usr/bin/perl -w

=head1 NAME

dh_gstscancodecs - enumerate and classify gstreamer codecs

=cut

use strict;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

  dh_gstscancodecs [debhelper options]

=head1 DESCRIPTION

This program is meant to assist in building a package that provides
codecs, demultiplexers and other media-handling components for
gstreamer-based applications.

dh_gstscancodecs generates a file
/usr/share/gstreamer-1.0/plugin-info/<package>.supported in each
package for which it is run, by scanning libraries
/usr/lib/gstreamer-0.10/*.so.

See wiki.ubuntu.com/EasyCodecInstallation

=head1 OPTIONS

The standard debhelper options are supported.

=cut

init();

$::pluginlibdirprefix = '/usr/lib/gstreamer-';
$::plugininfodirprefix = '/usr/share/gstreamer-';
$::plugininfodirsuffix = '/plugin-info';
$::plugininfofilesuffix = '.supported';

foreach my $package (@{$dh{DOPACKAGES}}) {
  my $tmp = tmpdir($package);

  foreach my $sodir (glob "$tmp$::pluginlibdirprefix*") {
    my $gstversion= substr($sodir, length("$tmp$::pluginlibdirprefix"));
    my $infodir= "$tmp$::plugininfodirprefix$gstversion$::plugininfodirsuffix";
    my @codecs= ();
    verbose_print("# gstreamer version $gstversion");
    foreach my $so (glob "$sodir/*.so") {
      verbose_print("# inspecting $so");
      my $info= `gst-inspect-$gstversion --print-plugin-auto-install-info $so`;
      die "$0: gst-inspect-$gstversion failed on $so: $?\n" if $?;
      foreach $_ (split /\n/, $info) {
       push @codecs, "${gstversion}:${_}";
      }
    }
    next unless @codecs;
    my $infofile= "$infodir/$package$::plugininfofilesuffix";
    doit ("mkdir", "-p", "$infodir");
    verbose_print("# noting ".scalar(@codecs)." codecs in $infofile");
    open F, "> $infofile" or die "$infofile: $!";
    map { print F $_,"\n" or die $! } @codecs;
    close F or die $!;
  }
}

=head1 SEE ALSO

L<debhelper(1)>

This program is an extension to debhelper.

=head1 AUTHOR

Ian Jackson <iwj@ubuntu.com>

=cut
