#!/usr/bin/env perl

use strict;
use warnings;
use File::Glob qw( bsd_glob );
use File::Path qw( rmtree );

sub run
{
  print "-> @_\n";
  system @_;
  die "failed!" if $?;
}

$ENV{ALIEN_INSTALL_TYPE}||='';

rmtree '/tmp/pkgconf', 0, 0 if -d '/tmp/pkgconf';
mkdir '/tmp/pkgconf';
run 'git', 'clone', 'https://github.com/pkgconf/pkgconf.git', '/tmp/pkgconf/src';

if($ENV{ALIEN_INSTALL_TYPE} eq 'share')
{
  chdir '/tmp/pkgconf/src/';
  run './autogen.sh';
  run './configure';
  run 'make dist';
  
  my $tarball = bsd_glob '*.tar.xz';
  run 'xz', '-d', $tarball;
  $tarball =~ s/\.xz$//;
  run 'gzip', $tarball;
  $tarball .= ".gz";
  rename $tarball => 'pkgconf.tar.gz';
  $tarball = 'pkgconf.tar.gz';
}

elsif($ENV{ALIEN_INSTALL_TYPE} eq 'system')
{
  chdir '/tmp/pkgconf/src/';
  run './autogen.sh';
  run './configure',
    '--prefix=/tmp/pkgconf', 
    '--with-pic', 
    '--disable-shared',
    '--with-pkg-config-dir=/tmp/pkgconf/lib/pkgconfig',
    '--with-system-libdir=/usr/lib',
    '--with-system-system-includedir=/usr/include';
  run 'make';
  run 'make', 'install';
}

else
{
  die "must be invoked with ALIEN_INSTALL_TYPE set to either share or system";
}
