#!/usr/bin/perl -w
#
# This script parses the output of  wx-config and generates xcconfig file contents
# on its standard output. Redirect it to one of the xcconfig files mentioned in
# README.MAC.DEVELOPERS.txt
#
# Usage:
#
# It takes two or three parameters.
# The first (optional) is "--homebrew", which tells the script to add YubiKey
# personalization library paths from Homebrew to the configuration.
# The next is either "-d" or "-r", to tell this script if you want to generate the
# xcconfig for a Debug or Release build.  The last parameter is the full path to
# the 'wx-config' script of the wxWidgets build you want to use.
#

sub usage {
	die qq{
    usage: generate-configs [--homebrew]
	  or
    generate-configs [-d|-r <path to wx-config>]

	  where -d & -r stand for Debug/Release.

	  "--homebrew" assumes you want to build with homebew and have installed the dependencies as in docs/README.MAC.DEVELOPERS.md

	  You should redirect the output when using "--homebrew" to Xcode/pwsafe-release-homebrew.xcconfig
	};
}

# Check for --homebrew flag
my $homebrew = 0;
if (scalar @ARGV > 0 && $ARGV[0] eq "--homebrew") {
	$homebrew = 1;
	shift @ARGV;
}

# If --homebrew is set and no other arguments, use defaults
my $wxconfig;
my $flag;
if ($homebrew && scalar @ARGV == 0) {
	$flag = "-r";  # Default to Release build
	$wxconfig = "wx-config";  # Use wx-config from PATH
} else {
	if (scalar @ARGV != 2){
		&usage;
	}
	$wxconfig = pop @ARGV;
	$flag = pop @ARGV;
}

die "unexpected flag: $flag" unless $flag eq "-d" || $flag eq "-r";

# Validate wx-config exists and is executable
if ($wxconfig =~ /\//) {
	# If it's a path (contains /), check file directly
	die "wx-config \"$wxconfig\" does not exist" unless -e $wxconfig;
	die "wx-config \"$wxconfig\" is not executable" unless -x $wxconfig;
} else {
	# If it's just a command name, check if it's in PATH
	my $which_result = `which $wxconfig 2>/dev/null`;
	die "wx-config \"$wxconfig\" not found in PATH" unless $which_result;
}

my $dbgflag = $flag eq "-d" ? "yes" : "no";
my $libflags;
my $rpath;
foreach(split /\s+/, `$wxconfig --libs aui xrc html qa adv core xml net base --unicode=yes --debug=$dbgflag`) {
	$libflags .= $_." " if /\A-[lL]|\.a\Z/;
	$rpath .= $1." " if /\A-Wl,-rpath,(.*)\Z/;
}
print "OTHER_LDFLAGS = ", $libflags, "\n";

my $cxxflags;
foreach(split /\s+/, `$wxconfig --cxxflags --unicode=yes --debug=$dbgflag`) {
	$cxxflags .= $1." " if /\A-I(.+)/;
}

# Get YubiKey personalization library prefix if --homebrew flag is set
my $ykpers_prefix;
my $googletest_prefix;
if ($homebrew) {
	$ykpers_prefix = `brew --prefix ykpers`;
	$googletest_prefix = `brew --prefix googletest`;
	chomp $ykpers_prefix;  # Remove trailing newline
	chomp $googletest_prefix;
	$cxxflags .= "$ykpers_prefix/include/ykpers-1 $googletest_prefix/include";
}

print "HEADER_SEARCH_PATHS = ", $cxxflags, "\n";

# Add LIBRARY_SEARCH_PATHS if --homebrew flag is set
if ($homebrew) {
	print "LIBRARY_SEARCH_PATHS = $ykpers_prefix/lib\n";
}

print "LD_RUNPATH_SEARCH_PATHS = ", $rpath, "\n" if defined($rpath);
