#!/usr/bin/perl
# Perl script that converts PtinyOS .nc files to Ptolemy .moml files
# Authors:  Elaine Cheong
# Version: $Id: nc2moml-main,v 1.15 2005/10/25 22:57:54 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-main <input prefix> <sub prefix> <output prefix>
#          <dirs without prefix>

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

##### SETTINGS #####
$VERBOSE = $ENV{NC2MOML_VERBOSE};
$SRC_DIR = "$ENV{PTII}/ptolemy/domains/ptinyos/util/nc2moml";
$outputsuffix = ".ncxml";
$toplevelindexfilename = "_TOSIndex.moml";
$indexfilename = "_index.moml";
$tempfilename = ".nc2moml-tempfile";
####################

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

# Put the arguments into variables.
$inputprefix = shift @args;
$subprefix = 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 files to nesc xml files.  Any errors will be redirected \
to $outputprefix/$ENV{NC2MOML_LOG}\n");

print("\nYou will see a series of \"+\" 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.  \
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 \
  nc2moml with the -v (verbose) option. \n");

foreach $mydir (@mydirs) {
  # Find all .nc files.
  $mydir = "$inputprefix/$mydir";
  $files=`/usr/bin/find $mydir -name "*.nc" -print`;

  # Call script to convert .nc files into .xml files.
  # Append the command and its output to the .txt file
  foreach $file (split /\n/, $files) {
    if ($VERBOSE) {
      system("echo \"$SRC_DIR/nc2moml-get-args $inputprefix $outputprefix $outputsuffix $file\" >> $outputprefix/$ENV{NC2MOML_LOG} 2>&1");
    }
    system("$SRC_DIR/nc2moml-get-args $inputprefix $outputprefix $outputsuffix $file") == 0
      or die "system @args failed: $?";
  }

  # Log the .nc filename in short format to the temporary file.
  foreach $file (split /\n/, $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 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/.nc2moml-util $outputprefix $outputsuffix $subprefix $outputprefix $outputprefix/$tempfilename\" >> $outputprefix/$ENV{NC2MOML_LOG} 2>&1");
$nc2momlcmd = "$ENV{PTII}/bin/.nc2moml-util $outputprefix $outputsuffix $subprefix $outputprefix $outputprefix/$tempfilename >> $outputprefix/$ENV{NC2MOML_LOG} 2>&1";
print("\nConverting nesc xml files to .moml files.  Any errors will be \
redirected to $outputprefix/$ENV{NC2MOML_LOG}\n");
system($nc2momlcmd) == 0
  or die "system @args failed: $?";

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

# Call utility to create index files from .moml files.
# Append the command and its output to the .txt file.
system("echo \"$ENV{PTII}/bin/.momllib-util .moml $toplevelindexfilename $indexfilename $outputprefix\" >> $outputprefix/$ENV{NC2MOML_LOG} 2>&1");
print("\nCreating index files from .moml files.  Any errors will be redirected \
to $outputprefix/$ENV{NC2MOML_LOG}\n");
$momllibcmd = "$ENV{PTII}/bin/.momllib-util .moml $toplevelindexfilename $indexfilename $outputprefix >> $outputprefix/$ENV{NC2MOML_LOG} 2>&1";
system($momllibcmd) == 0
  or die "system @args failed: $?";

print("\nFinished creating index files from .moml files.  See results in \
$outputprefix/$ENV{NC2MOML_LOG}\n");

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

