#!/usr/bin/perl -w

# Copyright © 2012 Enrico Tassi <gareuselesinge@debian.org>
# Heavily based on dh_linktree, 
#   Copyright © 2011-2012 Raphaël Hertzog <hertzog@debian.org>
#
# This program 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 package 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/>.

=head1 NAME

dh_lua - postprocess a lua package

=cut

use strict;
use File::Find::Rule;
use File::Compare;
use Debian::Debhelper::Dh_Lib;
use Dpkg::IPC;

=head1 SYNOPSIS

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

=head1 DESCRIPTION

B<dh_lua> is a debhelper program that postprocesses the package build
directories. It performs the following actions:

=over 4

=item deduplication

Files in /usr/share/lua/5.{2,3}/ that are copies of files in /usr/share/lua/5.1/ 
are replaced by symlinks.

=back

=cut

init();


sub mklfn {
  my ($base, $version, $path) = @_;
  return "$base/usr/share/lua/$version/$path";
}

sub mklrel {
  my ($base,$version, $path) = @_;
  $path=~s:^$base/usr/share/lua/$version/::;
  return $path;
}

sub mklorig {
  my ($path) = @_;
  my @pieces=split(m:/+:,$path);
  my $res="../";
  for (1..$#pieces) {
    $res.="../";
  }
  $res.="5.1/$path";
  return $res;
}

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

        my @srclinks=();
	if (-d mklfn($tmp,"5.1","")) {
		@srclinks = File::Find::Rule->
			name('*.lua')->in(mklfn($tmp,"5.1",""));
	}

	while (@srclinks) {
		my $src=shift @srclinks;
		my $what=mklrel($tmp,"5.1",$src);
		my $dest=mklfn($tmp,"5.2",$what);
		if (compare($src, $dest) == 0) {
			print "deduplicating $what\n";
			doit("ln","-sf", mklorig($what), $dest);
		}
		$dest=mklfn($tmp,"5.3",$what);
		if (compare($src, $dest) == 0) {
			print "deduplicating $what\n";
			doit("ln","-sf", mklorig($what), $dest);
		}
	}
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=head1 AUTHOR

Enrico Tassi <gareuselesinge@debian.org>

=cut
