#! /usr/bin/perl -w
# -*- perl -*-
#
# Copyright 2011 Sandia Corporation. Under the terms of Contract
# DE-AC04-94AL85000 with Sandia Corporation, the U.S.  Government
# retains certain rights in this software.
#
# This file is part of the Sandia OpenSHMEM software package. For license
# information, see the LICENSE file in the top level directory of the
# distribution.
#
# Note that we use an interpreter value ("PERL") from configure
# because even "#!/usr/bin/env perl" to all systems (e.g., NetBSD).
#
# Originally taken from Open MPI Project, under the following license:
#
# Most files in this release are marked with the copyrights of the
# organizations who have edited them.  The copyrights below are in no
# particular order and generally reflect members of the Open MPI core
# team who have contributed code to this release.  The copyrights for
# code used under license from other parties are included in the
# corresponding files.
#
# Copyright (c) 2004-2010 The Trustees of Indiana University and Indiana
#                         University Research and Technology
#                         Corporation.  All rights reserved.
# Copyright (c) 2004-2010 The University of Tennessee and The University
#                         of Tennessee Research Foundation.  All rights
#                         reserved.
# Copyright (c) 2004-2010 High Performance Computing Center Stuttgart,
#                         University of Stuttgart.  All rights reserved.
# Copyright (c) 2004-2008 The Regents of the University of California.
#                         All rights reserved.
# Copyright (c) 2006-2010 Los Alamos National Security, LLC.  All rights
#                         reserved.
# Copyright (c) 2006-2010 Cisco Systems, Inc.  All rights reserved.
# Copyright (c) 2006-2010 Voltaire, Inc. All rights reserved.
# Copyright (c) 2006-2011 Sandia National Laboratories. All rights reserved.
# Copyright (c) 2006-2010 Sun Microsystems, Inc.  All rights reserved.
#                         Use is subject to license terms.
# Copyright (c) 2006-2010 The University of Houston. All rights reserved.
# Copyright (c) 2006-2009 Myricom, Inc.  All rights reserved.
# Copyright (c) 2007-2008 UT-Battelle, LLC. All rights reserved.
# Copyright (c) 2007-2010 IBM Corporation.  All rights reserved.
# Copyright (c) 1998-2005 Forschungszentrum Juelich, Juelich Supercomputing
#                         Centre, Federal Republic of Germany
# Copyright (c) 2005-2008 ZIH, TU Dresden, Federal Republic of Germany
# Copyright (c) 2007      Evergrid, Inc. All rights reserved.
# Copyright (c) 2008      Chelsio, Inc.  All rights reserved.
# Copyright (c) 2008-2009 Institut National de Recherche en
#                         Informatique.  All rights reserved.
# Copyright (c) 2007      Lawrence Livermore National Security, LLC.
#                         All rights reserved.
# Copyright (c) 2007-2009 Mellanox Technologies.  All rights reserved.
# Copyright (c) 2006-2010 QLogic Corporation.  All rights reserved.
# Copyright (c) 2008-2010 Oak Ridge National Labs.  All rights reserved.
# Copyright (c) 2006-2010 Oracle and/or its affiliates.  All rights reserved.
# Copyright (c) 2009      Bull SAS.  All rights reserved.
# Copyright (c) 2010      ARM ltd.  All rights reserved.
# Copyright (c) 2010-2011 Alex Brick <bricka@ccs.neu.edu>.  All rights reserved.
#
# $COPYRIGHT$
#
# Additional copyrights may follow
#
# $HEADER$
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer listed
#   in this license in the documentation and/or other materials
#   provided with the distribution.
#
# - Neither the name of the copyright holders nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# The copyright holders provide no reassurances that the source code
# provided does not infringe any patent, copyright, or any other
# intellectual property rights of third parties.  The copyright holders
# disclaim any liability to any recipient for claims brought against
# recipient by any third party for infringement of that parties
# intellectual property rights.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use File::Basename qw(basename);
use File::Spec::Functions;

my $includedir = "/usr/shmem/gcc/sandia-openshmem-1.4.2-hfi/include";
my $libdir = "/usr/shmem/gcc/sandia-openshmem-1.4.2-hfi/lib64";

my $lang = "C++";
my $CC = "gcc";
my $CXX = "g++ -std=gnu++11";
my $FC = "gfortran";
my $extra_ldflags = "-Wl,-rpath -Wl,/usr/shmem/gcc/sandia-openshmem-1.4.2-hfi/lib64 -Wl,--enable-new-dtags";
my $extra_libs = "-lpmi_simple";

# Someone might want to fix for windows
my $include_flag = "-I";
my $libdir_flag = "-L";

my $comp = "";
my $wrapper = basename($0);
my $preproc_flags = $include_flag . $includedir;
my $comp_flags = "";
my $comp_flags_prefix = "";
my $linker_flags = $libdir_flag . $libdir . " " . $extra_ldflags;
my $libs = "-lsma " . $extra_libs;


sub check_env {
    my $envvar = shift;
    my $str = shift;

    foreach my $var (("SHMEM_", "SMA_")) {
        my $testvar = $var . $envvar;
        if (exists($ENV{$testvar})) {
            $str = $ENV{$testvar};
            return $str;
        }
    }

    return $str;
}


if ($lang eq "C") {
    $comp = check_env("CC", $CC);
} elsif ($lang eq "C++") {
    $comp = check_env("CXX", $CXX);
} elsif ($lang eq "Fortran") {
    $comp = check_env("FC", $FC);
} else {
    $lang = "none";
}

if ($lang eq "none") {
    print "Could not determine requested language\n";
    exit 1;
}

# figure out what user wants
my @args = @ARGV;
my $want_preproc = 1;
my $want_compile = 1;
my $want_link = 1;
my $want_help = 0;
my $dry_run = 0;
my $disable_flags = 1;
my $real_flag = 0;
my @appargs = ();

while (scalar(@args) > 0) {
    my $arg = shift(@args);

    if ($arg eq "-showme" || $arg eq "-show") {
        $dry_run = 1;
    } else {
        if ($arg eq "-c") {
            $want_link = 0;
            $real_flag = 1;
        } elsif ($arg eq "-E" || $arg eq "-M") {
            $want_compile = 0;
            $want_link = 0;
            $real_flag = 1;
        } elsif ($arg eq "--help") {
            $want_help = 1;
            $real_flag = 1;
        } elsif ($arg eq "-S") {
            $want_link = 0;
            $real_flag = 1;
        } elsif ($arg =~ /^-.*/) {
            $real_flag = 1;
        } else {
            $real_flag = 1;
            $disable_flags = 0;
        }
        push(@appargs, $arg);
    }
}

if ($disable_flags == 1 && !($dry_run == 1 && $real_flag == 0)) {
    $want_preproc = $want_compile = $want_link = 0;
}

my @exec_argv = ();

# assemble command
push(@exec_argv, split(' ', $comp));
if ($want_compile == 1) {
    push(@exec_argv, split(' ', $comp_flags_prefix));
}
push(@exec_argv, @appargs);
if ($want_preproc == 1) {
    push(@exec_argv, split(' ', $preproc_flags));
}
if ($want_compile == 1) {
    push(@exec_argv, split(' ', $comp_flags));
}
if ($want_link == 1) {
    push(@exec_argv, split(' ', $linker_flags));
    push(@exec_argv, split(' ', $libs));
}
if ($want_help == 1) {
    print("== ",uc $wrapper," HELP ===================================\n");
    print("usage: ",$wrapper," [",$wrapper,"_options] [compiler_arguments]\n\n");
    print("COMPILER = $comp\n\n");
    print($wrapper," options:\n");
    print("\t--help            Display this information\n");
    print("\t-showme | -show   Print the command that would be run without executing it\n");
    print("\t-E | -M           Disable compilation and linking\n");
    print("\t-S                Disable linking\n");
    print("\t-c                Compile and assemble. Disable linking\n\n");
    print("Options provided by ",$comp," can also be used by ",$wrapper,".\n\n");
    print("== ",uc $comp," HELP =====================================\n");
}

if ($dry_run == 1) {
    print join(" ", @exec_argv) . "\n";
    exit 0;
}

$cmd = shift(@exec_argv);
if ($real_flag == 0) {
    @exec_argv = ();
}
exec($cmd, (@exec_argv)) || die "Could not exec " . $exec_argv[0] . ": $!\n";
