#!/usr/bin/perl -w

BEGIN {
  unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
}

use strict;

use Build;

my ($dist, $archs, $configdir, $debug, $type, $argument);

while (@ARGV)  {
  if ($ARGV[0] eq '--dist') {
    shift @ARGV;
    $dist = shift @ARGV;
    next;
  } elsif ($ARGV[0] eq '--archpath') {
    shift @ARGV;
    $archs = shift @ARGV;
    next;
  } elsif ($ARGV[0] eq '--configdir') {
    shift @ARGV;
    $configdir = shift @ARGV;
    next;
  } elsif (defined($type)) {
    $argument = shift @ARGV; 
  } else {
    $type = shift @ARGV;
  }
}

my $cf = Build::read_config_dist($dist, $archs, $configdir);
die("Unable to read config\n") unless $cf;

if ($type eq 'buildflags') {
  die("Specify which buildflag to query\n") unless $argument;

  my $result = $cf->{"buildflags:$argument"};
  print "$result\n" if defined $result;
} elsif ($type eq 'target' || $type eq 'binarytype' || $type eq 'rawmacros') {
  my $cf = Build::read_config_dist($dist, $archs, $configdir);
  print "$cf->{$type}\n" if $cf->{$type};
} else {
  die("unsupported query type: $type\n");
}

exit(0);

