#!/usr/bin/perl -w

=head1 NAME

gsdh_gnustep - moves files in the GNUstep hierarchy to FHS-compliant locations

=cut

use strict;
use File::Path;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

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

=head1 DESCRIPTION

gsdh_gnustep is a program based on debhelper that is responsible for moving files
in the GNUstop hierarchy within the System domain, rooted at
/usr/lib/GNUstep/System, to Filesystem Hierarchy Standard (FHS)-compliant
locations.

gsdh_gnustep makes the following changes:

=over 4

=item man and info documentation:

Moves man and info documentation to /usr/share/man and /usr/share/info,
respectively.

=item arch-indep directories:

Moves architecture-independent directories to /usr/share/GNUstep.

=item header files:

Moves header files to /usr/include/GNUstep/Headers.

=item library resources:

Moves library resources to /usr/share/GNUstep/Libraries.

=item shared library objects:

Moves .so files to /usr/lib.

=item frameworks:

Moves framework headers to /usr/include/GNUstep/Frameworks, and framework
resources to /usr/share/GNUstep/Frameworks.

=back

=head1 OPTIONS

gsdh_gnustep accepts the common debhelper options.

If the DEB_GNUSTEP_NO_MOVE environment variable is set to a non-empty, non-zero
value, then gsdh_gnustep exits without doing anything.

=head1 BUGS

Should implement B<-X> option.

Should do something with resources in .app bundles.

Should remove library_paths.openapp and stamp.make files.

Doesn't really do the right thing with info files (should call install-info).

Should do something with Java classes -- make a jar file and move from
Library/Libraries/Java to /usr/share/java -- to comply with Java policy.

=head1 CONFORMS TO

Debian policy, version 3.6.2

FHS, version 2.3

=cut

exit 0 if ($ENV{DEB_GNUSTEP_NO_MOVE});

init();

my $SYS_ROOT = '/usr/lib/GNUstep/System';
my $LIB_ROOT = $SYS_ROOT . '/Library';
my $SHARE_ROOT = 'usr/share/GNUstep';
my $INCLUDE_ROOT = 'usr/include/GNUstep';

my @ARCH_INDEP_DIRS = qw(Colors DTDs DocTemplates Documentation Fonts Images Makefiles KeyBindings PostScript Sounds);

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

	# move man and info pages to usr/share/man and usr/share/info
	if (-d "$tmp/$LIB_ROOT/Documentation/man") {
		mkpath "$tmp/usr/share/man";
		rename "$tmp/$LIB_ROOT/Documentation/man", "$tmp/usr/share/man";
	}
	if (-d "$tmp/$LIB_ROOT/Documentation/info") {
		mkpath "$tmp/usr/share/info";
		rename "$tmp/$LIB_ROOT/Documentation/info", "$tmp/usr/share/info";
	}

	# move arch-indep directories to usr/share/GNUstep
	foreach my $directory (@ARCH_INDEP_DIRS) {
		if (-d "$tmp/$LIB_ROOT/$directory") {
			mkpath "$tmp/$SHARE_ROOT/$directory";
			rename "$tmp/$LIB_ROOT/$directory", "$tmp/$SHARE_ROOT/$directory";
		}
	}

	# move headers to usr/include/GNUstep
	if (-d "$tmp/$LIB_ROOT/Headers") {
		mkpath "$tmp/$INCLUDE_ROOT/Headers";
		rename "$tmp/$LIB_ROOT/Headers", "$tmp/$INCLUDE_ROOT/Headers";
	}

	# move library resources to usr/share/GNUstep/Libraries
	if (-d "$tmp/$LIB_ROOT/Libraries/Resources") {
		mkpath "$tmp/$SHARE_ROOT/Libraries";
		rename "$tmp/$LIB_ROOT/Libraries/Resources", "$tmp/$SHARE_ROOT/Libraries";
	}

	# move shared library objects to usr/lib
	if (-d "$tmp/$LIB_ROOT/Libraries") {
		opendir LIBDIR, "$tmp/$LIB_ROOT/Libraries";
		my @libraries = grep -f "$tmp/$LIB_ROOT/Libraries/$_", readdir LIBDIR;
		closedir LIBDIR;
		foreach my $library (@libraries) {
			if (-l "$tmp/$LIB_ROOT/Libraries/$library") {
				my $linkend = readlink "$tmp/$LIB_ROOT/Libraries/$library";
				# if symlink, create new link with right target; leave original in place
				if ($linkend =~ m|^../Frameworks|) {
					# Framework library -- fix relative path
					$linkend =~ s|^..|GNUstep/System/Library|;
				}
				symlink $linkend, "$tmp/usr/lib/$library";
			} else {
				# regular library -- move to usr/lib and create compatibility symlink
				rename "$tmp/$LIB_ROOT/Libraries/$library", "$tmp/usr/lib/$library";
				symlink "../../../../$library", "$tmp/$LIB_ROOT/Libraries/$library";
			}
		}
	}

	# find frameworks and move resources and headers
	if (-d "$tmp/$LIB_ROOT/Frameworks") {
		opendir FRAMEWORKDIR, "$tmp/$LIB_ROOT/Frameworks";
		my @frameworks = grep !/^\./, readdir FRAMEWORKDIR;
		closedir FRAMEWORKDIR;
		foreach my $framework (@frameworks) {
			my $fwdir = "$tmp/$LIB_ROOT/Frameworks/$framework";
			opendir CURFWDIR, "$fwdir/Versions";
			my @versions = grep !/^(\.|Current$)/, readdir CURFWDIR;
			closedir CURFWDIR;
			foreach my $version (@versions) {
				if (-d "$fwdir/Versions/$version/Headers") {
					mkpath "$tmp/$INCLUDE_ROOT/Frameworks/$framework/Versions/$version";
					rename "$tmp/$LIB_ROOT/Frameworks/$framework/Versions/$version/Headers", "$tmp/usr/include/GNUstep/Frameworks/$framework/Versions/$version";
					symlink "../../../../../../../../include/GNUstep/Frameworks/$framework/Versions/$version", "$tmp/$LIB_ROOT/Frameworks/$framework/Versions/$version/Headers";
				}
				if (-d "$fwdir/Versions/$version/Resources") {
					mkpath "$tmp/$SHARE_ROOT/Frameworks/$framework/Versions/$version";
					rename "$tmp/$LIB_ROOT/Frameworks/$framework/Versions/$version/Resources", "$tmp/usr/share/GNUstep/Frameworks/$framework/Versions/$version";
					symlink "../../../../../../../../share/GNUstep/Frameworks/$framework/Versions/$version", "$tmp/$LIB_ROOT/Frameworks/$framework/Versions/$version/Resources";
				}
			}
			if (-l "$fwdir/Headers") {
				my $linkend = readlink "$fwdir/Versions/Current";
				symlink "Versions/$linkend", "$tmp/$INCLUDE_ROOT/Frameworks/$framework/Headers";
			}
			if (-l "$fwdir/Resources") {
				my $linkend = readlink "$fwdir/Versions/Current";
				symlink "Versions/$linkend", "$tmp/$SHARE_ROOT/Frameworks/$framework/Resources";
			}
		}
	}
}

=head1 SEE ALSO

L<debhelper(7)>

This program is not yet part of debhelper.

=head1 AUTHOR

Hubert Chan <hubert@uhoreg.ca>

=cut
