#!/usr/bin/perl
# Perl script that converts PtinyOS .nc files to Ptolemy .moml files
# Authors:  Elaine Cheong
# Version: $Id: ncapp2moml-main,v 1.8 2005/10/26 06:57:23 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-main <input prefix> <output prefix>
#          <dirs without prefix>

if ($#ARGV < 1) {
  die "Usage: ncapp2moml-main <input prefix> <output prefix> <dirs without prefix>";
}

##### SETTINGS #####
$VERBOSE = $ENV{NCAPP2MOML_VERBOSE};
$SRC_DIR = "$ENV{PTII}/ptolemy/domains/ptinyos/util/ncapp2moml";
$MICABOARD_FILE = "$ENV{PTII}/ptolemy/domains/ptinyos/lib/MicaBoard.xml";

$outputsuffix_ncxml = ".ncxml";
$outputsuffix_opts = ".opts";
$indexfilename = "_TOSIndex.moml";
$tempfilename = ".ncapp2moml-tempfile";
####################

# Get the command line argument list.
@args = @ARGV;

# Put the arguments into variables.
$inputprefix = shift @args;
$outputprefix = shift @args;
@mydirs = @args;

# Create the output directory.
system("mkdir -p $outputprefix") == 0
  or die "call to mkdir failed in @args: $?";

# Create the temporary file that will contain the .nc file names in short format.
open(TEMPFILE, ">$outputprefix/$tempfilename")
  or die "Can't create .tempfile: $!";

print("\nConverting .nc app files to nesc xml files.  This script looks for \
makefiles that contain the string \"COMPONENT=\" to identify .nc application \
components.  Any errors will be redirected \
to $outputprefix/$ENV{NCAPP2MOML_LOG}\n");

print("\nYou will see a series of \"+\" and \"-\" and \"!\" and \"?\".  \
A \"+\" means that the .nc file had no ncc compiler errors.  \
A \"-\" means that the .nc file had some ncc compiler errors or warnings.  \
A \"!\" means that this script does not know how to deal with the component name \
  found in the makefile.\
A \"?\" means that this script could not find the component name in the makefile. \
Most errors or warnings can be ignored here, since some files \
  in the TinyOS tree do not compile properly or the correct compiler arguments \
  or include files cannot be found.
Detailed warnings can be viewed in <outputdir>/component.txt if you are running \
  ncapp2moml with the -v (verbose) option. \n");

foreach $mydir (@mydirs) {
  # Find all makefiles.  Look for "Makefile" or "makefile".
  # Note: We don't bother to look for GNUMakefile.
  $makefiles=`/usr/bin/find $inputprefix/$mydir -name "[Mm]akefile" -print`;

  my @files;

  foreach $makefile (split /\n/, $makefiles) {
    # Find makefiles that contain the COMPONENT setting.
    $component = `grep '^COMPONENT[[:space:]]*[?]\\?=' $makefile | head -1`;
    chomp $component;
    if ($component) {
      # Get the component name.
      $component =~ s/^COMPONENT[\s]*[?]?=[\s]*//;

      # Remove trailing whitespace (important on Cygwin because of DOS line endings).
      $component =~ s/[\s]*$//;

      # Note: we do not handle component strings w/ non-alphanumeric
      # characters.  This means that component strings that contain
      # makefile variables will not work with this script.
      if ($component =~ /\W/) {
        system("echo \"Do not know how to deal with component name in $makefile\" >> $outputprefix/$ENV{NCAPP2MOML_LOG} 2>&1");
        print("!");
      } else {

        # Get the path to the makefile, including trailing slash.
        $path = $makefile;
        $path =~ s/[Mm]akefile$//;

        # Get the full pathname for the component.
        $ncfile = "$path$component.nc";
        push @files, $ncfile;

        # Call script to convert .nc files into .xml files.
        # Append the command and its output to the .txt file
        if ($VERBOSE) {
          system("echo \"$SRC_DIR/ncapp2moml-get-args $inputprefix $outputprefix $outputsuffix_ncxml $outputsuffix_opts $ncfile\" >> $outputprefix/$ENV{NCAPP2MOML_LOG} 2>&1");
        }
        system("$SRC_DIR/ncapp2moml-get-args $inputprefix $outputprefix $outputsuffix_ncxml $outputsuffix_opts $ncfile") == 0
          or die "system @args failed: $?";
      }
    } else {
      system("echo \"Could not find the component name in $makefile\" >> $outputprefix/$ENV{NCAPP2MOML_LOG} 2>&1");
      print("?");
    }
  }

  # Log the .nc filename in short format to the temporary file.
  foreach $file (@files) {
    $ncfilename = $file;
    $ncfilename =~ s/^$inputprefix\///;
    print TEMPFILE "$ncfilename\n";
  }
}

# Close (and flush) the temporary file.
close(TEMPFILE);

if (!$VERBOSE) {
  print("\n");
}

print("\nFinished converting .nc app files to nesc xml files.  See results in \
$outputprefix/.../*.txt.\n");

# Call utility to convert nesC xml files to .moml files.
# Append the command and its output to the .txt file.
system("echo \"$ENV{PTII}/bin/.ncapp2moml-util $inputprefix $outputprefix $outputsuffix_ncxml $outputsuffix_opts $outputprefix $outputprefix/$tempfilename $MICABOARD_FILE\" >> $outputprefix/$ENV{NCAPP2MOML_LOG} 2>&1");
$ncapp2momlcmd = "$ENV{PTII}/bin/.ncapp2moml-util $inputprefix $outputprefix $outputsuffix_ncxml $outputsuffix_opts $outputprefix $outputprefix/$tempfilename $MICABOARD_FILE >> $outputprefix/$ENV{NCAPP2MOML_LOG} 2>&1";
print("\nConverting nesc xml files to .moml files.  Any errors will be \
redirected to $outputprefix/$ENV{NCAPP2MOML_LOG}\n");
system($ncapp2momlcmd) == 0
  or die "system @args failed: $?";

print("\nFinished converting nesc xml files to .moml files.  See results in \
$outputprefix/$ENV{NCAPP2MOML_LOG}\n");

print("\nFinished.  See results in $outputprefix/$ENV{NCAPP2MOML_LOG}\n");

