#!perl
# $Id: /mirror/perl/Text-MeCab/trunk/tools/probe_mecab.pl 38046 2008-01-06T12:44:20.889262Z daisuke  $
#
# Copyright (c) 2006-2008 Daisuke Maki <daisuke@endeworks.jp>
# All rights reserved.

use strict;
use File::Spec;
use ExtUtils::MakeMaker;

my($version, $cflags, $libs, $mecab_config);

$cflags = '';
$mecab_config = '';

# Save the poor puppies that run on Windows
if ($^O eq 'MSWin32') {
    $version = prompt(
        join(
            "\n",
            "",
            "You seem to be running on an environment that may not have mecab-config",
            "available. This script uses mecab-config to auto-probe",
            "  1. The version string of libmecab that you are building Text::MeCab",
            "     against. (e.g. 0.90)",
            "  2. Additional compiler flags that you may have built libmecab with, and",
            "  3. Additional linker flags that you may have build libmecab with.",
            "",
            "Since we can't auto-probe, you should specify the above three to proceed",
            "with compilation:",
            "",
            "Version of libmecab that you are compiling against (e.g. 0.90)? (REQUIRED) []"
        )
    );
    chomp $version;
    die "no version specified! cowardly refusing to proceed." unless $version;

    $cflags = prompt("Additional compiler flags (e.g. -DWIN32 -Ic:\\path\\to\\mecab\\sdk)? []");

    $libs = prompt("Additional linker flags (e.g. -lc:\\path\\to\\mecab\\sdk\\libmecab.lib? [] ");
} else {
    # try probing in places where we expect it to be
    my $default_config;
    foreach my $path qw(/usr/bin /usr/local/bin) {
        my $tmp = File::Spec->catfile($path, 'mecab-config');
        if (-f $tmp && -x _) {
            $default_config = $tmp;
            last;
        }
    }

    $mecab_config = prompt( "Path to mecab config?", $default_config );

    if (!-f $mecab_config || ! -x _) {
        print STDERR "Can't proceed without mecab-config. Aborting...\n";
        exit 1;
    }
    
    $version = `$mecab_config --version`;
    chomp $version;

    $cflags = `$mecab_config --cflags`;
    chomp($cflags);

    $libs   = `$mecab_config --libs`;
    chomp($libs);
}

print "detected mecab version $version\n";
if ($version < 0.90) {
    print " + mecab version < 0.90 doesn't contain some of the features\n",
          " + that are available in Text::MeCab. Please read the documentation\n",
          " + carefully before using\n";
}

my($major, $minor, $micro) = map { s/\D+//g; $_ } split(/\./, $version);

$cflags .= " -DMECAB_MAJOR_VERSION=$major -DMECAB_MINOR_VERSION=$minor";

# remove whitespaces from beginning and ending of strings
$cflags =~ s/^\s+//;
$cflags =~ s/\s+$//;

print "Using compiler flags '$cflags'...\n";

if ($libs) {
    $libs =~ s/^\s+//;
    $libs =~ s/\s+$//;
    print "Using linker flags '$libs'...\n";
} else {
    print "No linker flags specified\n";
}

my $default_encoding = 'utf-8';
my $encoding = prompt(
    join(
        "\n",
        "",
        "Text::MeCab needs to know what encoding you built your dictionary with",
        "to properly execute tests.",
        "",
        "Encoding of your mecab dictionary? (shift_jis, euc-jp, utf-8)",
    ),
    $default_encoding
);

print "Using $encoding as your dictionary encoding\n";

return {
    version  => $version,
    cflags   => $cflags,
    libs     => $libs,
    encoding => $encoding,
    config   => $mecab_config,
};