use alienfile;

# Do not probe for system libmupdf becasue we need to compile with shared
# library flags.

share {
	requires 'Alien::gmake' => 0.14;
	requires 'Alien::patch' => 0.08;

	plugin Download => (
		url => 'https://mupdf.com/downloads/',
		version => qr/mupdf-([\w\.]+)-source\.tar\.gz/,
	);

	if( exists $ENV{MSYSTEM} ) {
		# Using CommandLine to extract rather than ArchiveTar because
		# the extraction using ArchiveTar is not always successful on
		# Windows.  This can happen because the mupdf tarball contains
		# long paths that can push the entire path over the 260
		# character path limit.
		plugin 'Extract::CommandLine' => 'tar.gz';
	} else {
		plugin Extract => 'tar.gz';
	}

	plugin 'Build::MSYS' => ();
	my $XCFLAGS;
	if( $^O eq 'MSWin32' ) {
		$XCFLAGS = ''; # builds on Windows are already position independent
	} else {
		$XCFLAGS = 'XCFLAGS=-fPIC';
	};

	my @EXTRA_MAKE_FLAGS = ();
	if( $^O eq 'darwin' ) {
		# See bug #697842 <https://bugs.ghostscript.com/show_bug.cgi?id=697842>
		push @EXTRA_MAKE_FLAGS, q|'RANLIB_CMD=xcrun ranlib $@'|;
	}
	build [
		'%{patch} -p1 < %{.install.patch}/0001-Change-MSVC-specific-ifdef-to-Win32-specific.patch',
		"%{gmake} HAVE_GLFW=no HAVE_GLUT=no HAVE_X11=no $XCFLAGS prefix=%{.install.prefix} install"
			. join(' ', ('', @EXTRA_MAKE_FLAGS)),
	];

	gather sub {
		my($build) =@_;
		my $prefix = $build->runtime_prop->{prefix};
		$build->runtime_prop->{cflags}        = "-I$prefix/include";
		$build->runtime_prop->{cflags_static} = "-I$prefix/include";
		$build->runtime_prop->{libs}          = "-L$prefix/lib";
		$build->runtime_prop->{libs_static}   = "-L$prefix/lib";
	};
};
