#!/usr/bin/perl
# Perl script that creates correct compile options to pass to ncc.
# Authors:  David Gay, Elaine Cheong
# Version: $Id: nc2moml-get-args,v 1.10 2005/11/10 03:52:28 celaine Exp $
#
# Copyright (c) 2005 The Regents of the University of California.
# 	All Rights Reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in all
# copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
# THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
# PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
# CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.
#
# 						PT_COPYRIGHT_VERSION_2
# 						COPYRIGHTENDKEY

# Note: Do not call this script directly.  Use nc2moml instead.
# Modification of the arguments to this script is for development
# purposes only.

# Usage: nc2moml-get-args <input prefix> <output prefix> <output suffix> <filenames with prefix>

if ($#ARGV < 1) {
  die "Usage: nc2moml-get-args <input prefix> <output prefix> <output suffix> <filenames with prefix>";
}

##### SETTINGS #####
$SRC_DIR = "$ENV{PTII}/ptolemy/domains/ptinyos/util/nc2moml";
####################

@args = @ARGV;
$inputprefix = shift @args;
$outputprefix = shift @args;
$outputsuffix = shift @args;
@files = @args;

#$here = $ENV{PWD};
foreach (@files) {
  chdir $inputprefix;
  chomp;
  split / /;
  $mod = $_[0];
  $dir = $mod;
  $dir =~ s![^/]+$!!;
  @opts = ();
  if (-f "$dir/Makefile") {
    $sb=`grep '^SENSORBOARD=' $dir/Makefile | head -1`;
    chomp $sb;
    if ($sb) {
      $sb =~ s/SENSORBOARD=//;
      push @opts, "-board=$sb";
    }

    $pf=`grep '^PFLAGS[^=]*=' $dir/Makefile | grep -v '\\\$' | head -1`;
    chomp $pf;
    if ($pf) {
      $pf =~ s/[^=]*=//;
      push @opts, "$pf";
    }
  }
  if ($dir =~ m!platform/([^/]+)/! && $1 ne "avrmote") {
    push @opts, "-target=$1";
  } else {
    if ($mod =~ /2420/) {
      push @opts, "-target=micaz";
    } else {
      push @opts, "-target=mica2";
    }
  }
  push @opts, "-fnesc-include=AM";
  if (-f "$dir/opts-nolocalincludes") {
    # A hack to not include the local directory.  Mostly for
    # tinyos-1.x/tos/system, since some of the included files there
    # don't work.
  } else {
    push @opts, "-I$dir";
  }
  $odir = $dir;
  while ($odir) {
    if (-f "$odir/opts") {
      die unless open OPTS, "$odir/opts";
      while (<OPTS>) {
        chomp;
        if (/includes (.*);/) {
          push @opts, "-fnesc-include=$1";
        } else {
          push @opts, $_;
        }
      }
      close OPTS;
    }
    if ($odir =~ m!/!) {
      $odir =~ s!/[^/]*$!!;
    } else {
      last;
    }
  }
  for ($i = 1; $i <= $#_; $i++) {
    if ($_[$i] =~ /^-/) {
      push @opts, $_[$i];
    } else {
      $file = $_[$i];
      die "$file" unless $file =~ m!(.*)/([^/]+).h$!;
      push @opts, "-I$1";
      push @opts, "-fnesc-include=$2";
    }
  }
  $opts = join(' ', @opts);


  system("$SRC_DIR/nc2moml-run-ncc $inputprefix $outputprefix $outputsuffix $mod $opts") == 0
    or die "call to $nc2moml_run_ncc failed in @args: $?";
}

