#!/bin/sh
# Shell script that converts PtinyOS .nc files to Ptolemy .moml files
# Authors:  Elaine Cheong, Christopher Brooks
# Version: $Id: ncapp2moml,v 1.2 2005/10/24 22:48:02 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

# Set up the args to convert .nc files to .moml files.

##### SETTINGS #####
SRC_DIR=${PTII}/ptolemy/domains/ptinyos/util/ncapp2moml
MOML_OUTPUT_DIR=$PTII/vendors/ptinyos/momlapp
DIR_LIST="apps"

# Note: You should set NCAPP2MOML_VERBOSE environment variable to 1 to
# turn on verbose output.
export NCAPP2MOML_VERBOSE=0

# Name of log file.  This is used in the perl scripts that get called
# from this script.
export NCAPP2MOML_LOG=ncapp2moml-log.txt
# ####################

# ${1#-} will not work under Solaris 8
#while [ "${1#-}" != "$1" ]; do
# jode takes a -d argument, which causes problems unless we use "x..."
while [ "x$1" != "x" -a  "x`echo $1 | egrep '^-'`" = "x$1" -a $# -gt 0 ]; do
    case $1 in
        -h|-help|--help)
	    shift    
	    echo "$0: Usage: $0 -v|-h|-d [TinyOS_Directory] . . ."
	    echo "    Convert the .nc application files in 'TinyOS_Directory' to moml."
	    echo "    If no directory names are present in the command line,"
	    echo "    then we process these directories:"
	    echo "        $DIR_LIST"
	    echo "    Flags:"
	    echo "        -d  show the execution of this script"
	    echo "        -h  print this help message"
	    echo "        -v  verbose output of subscripts"
	    echo "    Debugging output ends up in $MOML_OUTPUT_DIR/$NCAPP2MOML_LOG"
	    echo "    This script requires that \$TOSROOT be set to the"
	    echo "    location of the TinyOS tree,"
	    echo "    typically \"\$PTII/vendors/ptinyos/tinyos-1.x\"."
	    echo "    For a quick test, run \"$0 tos/lib/Counters\"."
	    exit 0
            ;;

        -v|-verbose|--verbose)
            shift
            NCAPP2MOML_VERBOSE=1
            continue
            ;;
        -d|-debug)
	    shift
	    set -x
	    continue
	    ;;
	*)
	    echo "$0: Error: Don't understand '$1' argument, run with -h for help"
	    exit 3
	    ;;
    esac
done

if [ $# -gt 0 ]; then 
    DIR_LIST="" 
    # Accumulate another arguments and set DIRLIST
    while [ "x$1" != "x" ]; do
        # FIXME: what about directories with spaces in their names?
        DIR_LIST="$DIR_LIST \"$1\"";
        shift    
    done
fi

if [ -z $TOSROOT ]; then
  echo "$0: Error: \$TOSROOT must be defined"
  echo "    \$TOSROOT should be set to the tinyos tree,"
  echo "    typically \"\$PTII/vendors/ptinyos/tinyos-1.x\""
  exit 1
fi
TOS_INPUT_DIR=$TOSROOT

if [ -z $PTII ]; then
  echo "$0: Error: \$PTII must be defined"
  exit 2
fi

# Check for ncc
NCC_VERSION=`ncc -v 2>&1`
status=$?
if [ $status -ne 0 ]; then
    echo "$0: Error: Running \"ncc -v\" returned non-zero: \"$NCC_VERSION\""
    echo "    You may need to install ncc by going to tinyos-1.x/tools/src/ncc"
    echo "    and doing \"configure; make; make install\"."
    exit 3
fi

# Clear $NCAPP2MOML_LOG
rm -f "$MOML_OUTPUT_DIR/$NCAPP2MOML_LOG"

echo ""
echo "Running $SRC_DIR/ncapp2moml-main $TOS_INPUT_DIR \\\''\$CLASSPATH'\\\' $MOML_OUTPUT_DIR $DIR_LIST"

$SRC_DIR/ncapp2moml-main $TOS_INPUT_DIR $MOML_OUTPUT_DIR $DIR_LIST
