#!/usr/bin/perl
#
# Wrapper script to run tests from inside the Wine tree
#
# Usage: runtest [options] input_file [perl_args...]
#
# Copyright 2002 Alexandre Julliard
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use strict;

sub usage
{
    print STDERR <<EOF;

Usage: $0 [options] input_file [perl_args...]

Options:
    -q       quiet mode
    -v       verbose mode (can be specified multiple times)
    -p prog  name of the program to run for C tests
    -I dir   prepend dir to Perl include path
    -P name  set the current platform name
    -M names set the module names to be tested
    -T dir   set Wine tree top directory (autodetected if not specified)

EOF
    exit 1;
}

# default values
my $platform = $ENV{WINETEST_PLATFORM};
$ENV{WINETEST_DEBUG} ||= 1;

my $topobjdir;
my $infile;
my $program;
my @include_dirs;
my @modules;

# parse command-line options
while ($#ARGV >= 0)
{
    my $arg = shift @ARGV;
    if ($arg eq "-h") { usage; }
    if ($arg eq "-p") { $program = shift @ARGV; next; }
    if ($arg eq "-q") { $ENV{WINETEST_DEBUG} = 0; next; }
    if ($arg eq "-v") { $ENV{WINETEST_DEBUG}++; next; }
    if ($arg eq "-P") { $platform = shift @ARGV; next; }
    if ($arg eq "-M") { push @modules, split /,/, shift @ARGV; next; }
    if ($arg eq "-I") { push @include_dirs, shift @ARGV; next; }
    if ($arg eq "-T")
    {
        $topobjdir = shift @ARGV;
        usage unless (-d $topobjdir);
        next;
    }
    $infile = $arg;
    last;
}

# we must have found an input file
usage unless defined($infile);

if ($infile =~ /\.c$/ && !defined($program))
{
    # set program to the .c file base name if not specified otherwise
    ($program = $infile) =~ s/\.c$//;
}

# check/detect topobjdir
if (defined($topobjdir))
{
    unless (-f $topobjdir . "/server/wineserver")
    {
        printf STDERR "Wrong -T argument, %s/server/wineserver does not exist\n", $topobjdir;
        usage;
    }
}
else  # try to detect it automatically
{
    if (-f "./server/wineserver") { $topobjdir = "."; }
    elsif (-f "../server/wineserver") { $topobjdir = ".."; }
    elsif (-f "../../server/wineserver") { $topobjdir = "../.."; }
    elsif (-f "../../../server/wineserver") { $topobjdir = "../../.."; }
}

# check for include/ dir in script source directory and append it to search path
my $basedir = $0;
if ($basedir =~ /\//) { $basedir =~ s!/[^/]+$!!; }
else { $basedir = "."; }
if (-d $basedir . "/include") { push @include_dirs, $basedir . "/include"; }

$ENV{PERL5LIB} = join( ":", @include_dirs, split( ":", $ENV{PERL5LIB} ) );
if (@modules)
{
    if (defined($ENV{WINEOPTIONS})) { $ENV{WINEOPTIONS} .= " "; }
    $ENV{WINEOPTIONS} .= "--dll " . join(',',@modules) . "=b";
}

# set environment variables needed for Wine
if (defined($topobjdir))
{
    chop($topobjdir = `cd $topobjdir && pwd`);
    $ENV{LD_LIBRARY_PATH} = $topobjdir . "/dlls:" . $topobjdir . ":" . $ENV{LD_LIBRARY_PATH};
    $ENV{WINESERVER} ||= $topobjdir . "/server/wineserver";
    $ENV{WINELOADER} ||= $topobjdir . "/wine";
    $ENV{WINETEST_PLATFORM} = $platform || "wine";
    $ENV{WINEPRELOAD}=($program || ($topobjdir . "/programs/winetest/winetest")) . ".so";
    # try to exec the wine loader directly; if it fails continue on to normal exec
    exec $ENV{WINELOADER}, $infile, @ARGV;
}
else
{
    $ENV{WINETEST_PLATFORM} = $platform || "windows";
}

# and now exec the program
$program ||= "winetest";
exec $program, $infile, @ARGV;
print STDERR "Could not exec $program\n";
exit 1;
