#!/usr/bin/perl
# Perl script that creates correct compile options to pass to ncc.
# Authors:  David Gay, Elaine Cheong
# Version: $Id: ncapp2moml-get-args,v 1.3 2005/10/25 00:51:33 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 ncapp2moml instead.
# Modification of the arguments to this script is for development
# purposes only.

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

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

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

@args = @ARGV;
$inputprefix = shift @args;
$outputprefix = shift @args;
$outputsuffix_ncxml = shift @args;

# Suffix for file that contains PFLAGS to pass to PtinyOSDirector.
# Example: ".opts"
$outputsuffix_opts = 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";
  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);

  # Check to see if the -board= option is already the in opts array.
  my $has_sb = 0;
  foreach $opt (@opts) {
    if ($opt =~ /^-board=/) {
      # Options list contains sensorboard.
      $has_sb = 1;
      last;
    }
  }
  # Add the sensorboard if the options list doesn't already have it.
  if ($has_sb eq 0) {
    $opts = "$opts -board=$DEFAULT_SENSORBOARD"
  }

  # Look for include options in opts array.
  my @ptinyos_pflags;
  push @ptinyos_pflags, $pf;
  foreach $opt (@opts) {
    if ($opt =~ /^-I/) {
      # Make sure we don't add the contents of $pf twice.
      if (!($opt eq $pf)) {
        push @ptinyos_pflags, $opt;
      }
    }
  }
  $ptinyos_pflags = join(' ', @ptinyos_pflags);

  # Create output names.
  $output = $mod;
  $output =~ s/^$inputprefix/$outputprefix/;

  # Create name of opts output file.
  $ptinyos_pflags_outputfile = $output;
  $ptinyos_pflags_outputfile =~ s/\.nc$/$outputsuffix_opts/;

  # Create directory.
  $outputdir = $output;
  $outputdir =~ s/\/[\d\w.]+$//;
  system("mkdir -p $outputdir") == 0
    or die "call to mkdir failed in @args: $?";

  # Write ptinyos_pflags to output file.
  system("echo \"$ptinyos_pflags\" > $ptinyos_pflags_outputfile");

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

