#!/usr/bin/perl

use strict;
use warnings;

my $lang;

if (scalar(@ARGV) == 0)
{
	$lang = 'c++'
}
elsif (scalar(@ARGV) == 1 and not ($ARGV[0] eq '-h' or $ARGV[0] eq '--help'))
{
	$lang = $ARGV[0];
}
else
{
	print << 'END_OF_USAGE';

gcc-stdinc 1.0
--------------
This is free software.  License is GPL2.

gcc-std-inc prints to standard output the list of system include paths that
are searched by the gcc preprocessor.

    USAGE: gcc-stdinc <lang>

...where lang is one of "c", "c++" etc. (see man gcc -x option)

    EXAMPLE: gcc-stdinc c++

-Andrew Tomazos <andrew@tomazos.com>, Feb 2007

END_OF_USAGE
	
	exit 1;
}

my $std_inc_path = `echo | g++ -E -v -x $lang - 2>&1`;

if ($? != 0)
{
	print "gcc-stdinc: ERROR: 'echo | g++ -E -v -x $lang - 2>&1' failed ". ($? >> 8) . "\n";
	exit 1;
}

my $start_delim = '#include <...> search starts here:';

my $end_delim = 'End of search list.';

if ($std_inc_path =~ /\Q$start_delim\E/ and $std_inc_path =~ /\Q$end_delim\E/)
{
	$std_inc_path =~ s/.*?\Q$start_delim\E(.*?)\Q$end_delim\E.*/$1/s;
	$std_inc_path =~ s/\s+/\n/sg;
	$std_inc_path =~ s/(^\n*)|(\n*$)//g;          

	print $std_inc_path . "\n";
}
else
{
	exit 1;
}

exit 0;

