#!/usr/bin/perl
# $Id: configure 1083 2005-06-05 09:56:01Z kiza $

use strict;

# The defaults. Change here if you need.
my($prefix) = "/usr/local";
my($cflags) = '-Wall -Wno-format-y2k -O2 -DLOCALEPATH="\"$(LOCALEPATH)\"" -DOS=\"$(shell uname)\" `xml2-config --cflags` $(EXTRA_CFLAGS) ';
my($ldflags) = '-lncurses `xml2-config --libs` $(EXTRA_LDFLAGS) ';

parse_cmdl_line();

my($os) = `uname`;
$os =~ tr/A-Z/a-z/;				# Make sure it's all lower case
chomp($os);

##############################
# OS dependent configuration #
##############################

if ($os eq "linux") {
	print "Configuring for a Linux system... ";
} elsif ($os =~ /bsd|darwin|freemint/) {
	print "Configuring for a BSD style system... ";
	$cflags = $cflags.' -DSTATIC_CONST_ICONV -I/usr/local/include';
	$ldflags = $ldflags.' -lintl -liconv';
} elsif ($os =~ /cygwin/) {
	print "Configuring for a Cygwin system... ";
	$cflags = $cflags.' -DSTATIC_CONST_ICONV';
	$ldflags = $ldflags.' -lintl -liconv';
} elsif ($os eq "sunos") {
	print "Configuring for a Solaris system... ";
	$cflags = $cflags.' -DSUN -I/opt/sfw/include';
	$ldflags = $ldflags.' -L/opt/sfw/lib -lintl -liconv';
} else {
	print "Unknown host system. Type 'make' and hope for the best. :) ... \n";
	$cflags = $cflags,' -I/usr/local/include';
	$ldflags = $ldflags.' -L/usr/local/lib -lintl -liconv';
}

open (SETTINGS, ">platform_settings") || die "Cannot write platform_settings file: $!\n";
print SETTINGS "PREFIX=  $prefix\n";
print SETTINGS "CFLAGS=  $cflags\n";
print SETTINGS "LDFLAGS= $ldflags\n";
close (SETTINGS);

# And we're...
print "Done\n";
# with the program. :)

sub parse_cmdl_line {
	while ($_ = shift @ARGV) {
		if (($_ eq "--help") || ($_ eq "-h")) {
			print_help();
		} elsif (/--prefix=/) {
			$prefix = (split(/=/,$_))[1];
		} elsif (/--charset=/) {
			my($charset) = (split(/=/,$_))[1];
			$cflags = $cflags." -DTARGET_CHARSET=\\\"$charset\\\"";
			if ($charset eq "UTF-8") {
				# Need ncursesw
				$cflags = $cflags." -DUTF_8";
				$ldflags = '-lncursesw `xml2-config --libs` $(EXTRA_LDFLAGS) ';
			}
		} else {
			print_help();
		}
	}
}

sub print_help {
	print	"Snownews configure script\n".
			"=========================\n\n".
			"Available options:\n".
			"\t--prefix=[path]    Install root (default: /usr/local)\n".
			"\t--help             This help screen\n\n".
			"\t--charset=[string] Charset to use (default: ISO-8859-1)\n".
			"\t                   UTF-8 needs libncursesw!\n";
	exit(0);
}
