#!/usr/bin/perl
# Perl script that runs ncc on .nc files to create xml files.
# Authors:  Elaine Cheong
# Version: $Id: ncapp2moml-run-ncc,v 1.4 2007/12/07 06:36:15 cxh 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-run-ncc <input prefix> <output prefix> <output suffix> <.nc file name with full path> <options>

# Runs ncc on .nc files to create xml files.
# Note: .xml cannot be used because ptII cannot use this type of input.

# FIXME: assumes ncc is in path
#$ncc = "ncc";

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

##### SETTINGS #####
if (-z "$ENV{NCAPP2MOML_VERBOSE}") {
    $verbose = 1;
} else {
    $verbose = $ENV{NCAPP2MOML_VERBOSE};
}

if ( -z "$ENV{NCAPP2MOML_LOG}") {
    $masterlog = "$outputprefix/ncapp2moml-log.txt";
} else {
    $masterlog = "$outputprefix/$ENV{NCAPP2MOML_LOG}";
}
####################

# Create output names (complete path).
$filename = $args[0];
$output = $filename;
$output =~ s/^$inputprefix/$outputprefix/;

# Rename .nc input file to xml output file.
$outputxmlfile = $output;
$outputxmlfile =~ s/\.nc$/$outputsuffix/;

# Rename .nc input file to .txt output file.
$outputresultsfile = $output;
$outputresultsfile =~ s/\.nc$/\.txt/;

# Create directory
$outputdir = $output;
$outputdir =~ s/\/[\d\w.]+$//;

system("mkdir -p $outputdir") == 0
  or die "call to mkdir failed in @args: $?";

# Don't concatenate output to the log file each time.
system("rm -rf $outputresultsfile");

# Write command to .txt file
if ($verbose) {
    system("echo \"ncc -fsyntax-only \'-fnesc-dump=components\(wiring, file\($filename\)\)\' \'-fnesc-dump=referenced\(interfaces\)\' -fnesc-dumpfile=$outputxmlfile -Wnesc-all @args\" > $outputresultsfile");
}

# Run command and append errors to .txt file.
if ($verbose) {
    $results = system("ncc -fsyntax-only \'-fnesc-dump=components\(wiring, file\($filename\)\)\' \'-fnesc-dump=referenced\(interfaces\)\' -fnesc-dumpfile=$outputxmlfile -Wnesc-all @args >> $outputresultsfile 2>&1");
} else {
    $results = system("ncc -fsyntax-only \'-fnesc-dump=components\(wiring, file\($filename\)\)\' \'-fnesc-dump=referenced\(interfaces\)\' -fnesc-dumpfile=$outputxmlfile -Wnesc-all @args >> /dev/null 2>&1");
}

if ($results eq 0) {
    if ($verbose) {
        system("echo \"$filename Ok\" >> $masterlog 2>&1");
    }
    print("+");
} else {
    if ($verbose) {
        system("echo \"$filename ERRORS\" >> $masterlog 2>&1");
    }
    print("-");
}
