#! /bin/sh
# Modified by Princeton University on June 9th, 2015
# ========== Copyright Header Begin ==========================================
# 
# OpenSPARC T1 Processor File: bw_m4
# Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
# 
# The above named program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License version 2 as published by the Free Software Foundation.
# 
# The above named program is distributed in the hope that it will be 
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with this work; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
# 
# ========== Copyright Header End ============================================
#
# m4_gmp wrapper
#
#  Cloned from .common_tool_wrapper

loginfo () {
    echo "DATE:              "`date`
    echo "WRAPPER:           $TRE_PROJECT/tools"
    echo "USER:              $user"
    echo "HOST:              "`uname -n`
    echo "SYS:               "`uname -s` `uname -r`
    echo "PWD:               "`pwd`
    echo "ARGV:              "$ARGV
    echo "TOOL:              "$tool
    echo "VERSION:           "$version
    echo "TRE_SEARCH:        "$TRE_SEARCH
    echo "TRE_ENTRY:         "$TRE_ENTRY
}

mailinfo () {
    echo To: $1
    echo Subject: TRE_LOG
    echo "#"
    loginfo
}

mailerr () {
    echo "To: $1"
    echo "Subject: TRE ERROR"
    echo "#"
    echo "ERROR:             $2"
    loginfo
}

log () {
    # Log to TRE_LOG if it is set properly.
    # It is STRONGLY recommended that TRE_LOG be an e-mail address
    # in order to avoid problems with several people simultanously 
    # writing to the same file.
    # TRE_LOG must be set, but it can be broken.
    # TRE_ULOG is optional, for users who want their own logging.
    if [ ! -z "$TRE_LOG_ENABLED" ] ; then
    if [ ! -z "$TRE_LOG" ] ; then
	# Check first if TRE_LOG is a file (this is cheap).
	if [ -f $TRE_LOG -a -w $TRE_LOG ] ; then
    	    echo "#" >> $TRE_LOG
	    loginfo >> $TRE_LOG
	elif /usr/lib/sendmail -bv $TRE_LOG 1>&- 2>&- ; then
	    mailinfo $TRE_LOG | /usr/lib/sendmail $TRE_LOG
	else
	    mailerr $user "Can't log to TRE_LOG=$TRE_LOG. Fix environment." | /usr/lib/sendmail $user
	fi
    else
	die "TRE_LOG environment variable is not set."
    fi
    fi
    # TRE_ULOG is optional user log.  EMAIL address is recommended.
    if [ ! -z "$TRE_ULOG" ] ; then
	# Check first if TRE_ULOG is a file (this is cheap).
	if [ -f $TRE_ULOG -a -w $TRE_ULOG ] ; then
    	    echo "#" >> $TRE_ULOG
	    loginfo >> $TRE_ULOG
	elif /usr/lib/sendmail -bv $TRE_ULOG 1>&- 2>&- ; then
	    mailinfo $TRE_ULOG | /usr/lib/sendmail $TRE_ULOG
	else
	    mailerr $user "Can't log to TRE_ULOG=$TRE_ULOG. Fix environment." | /usr/lib/sendmail $user
	fi
    fi
}

die () {
    message="$1"
    echo "$tool -> .local_tool_wrapper: $message Exiting ..."
    if [ ! -z "$TRE_LOG" ] ; then
	if [ -f ${TRE_LOG} -a -w ${TRE_LOG} ] ; then
    	    echo "#" >> $TRE_LOG
    	    echo "ERROR:             $message" >> $TRE_LOG
	    loginfo >> $TRE_LOG
	elif /usr/lib/sendmail -bv $TRE_LOG 1>&- 2>&- ; then
            echo "$message"
	    #mailerr $TRE_LOG "$message" | /usr/lib/sendmail $TRE_LOG
	else
    	    echo  "Can not log to TRE_LOG=${TRE_LOG}. Logging to '$user.'"
	    #mailerr $user "$message" | /usr/lib/sendmail $user
	fi
    fi
    # TRE_ULOG is optional user log.  EMAIL address is recommended.
    if [ ! -z "$TRE_ULOG" ] ; then
	if [ -f ${TRE_ULOG} -a -w ${TRE_ULOG} ] ; then
    	    echo "#" >> $TRE_ULOG
    	    echo "ERROR:             $message" >> $TRE_ULOG
	    loginfo >> $TRE_ULOG
	elif /usr/lib/sendmail -bv $TRE_ULOG 1>&- 2>&- ; then
            echo "$message"
	    #mailerr $TRE_ULOG "$message" | /usr/lib/sendmail $TRE_ULOG
	else
    	    echo  "Can not log to TRE_ULOG=${TRE_ULOG}. Logging to '$user.'"
	    #mailerr $user "$message" | /usr/lib/sendmail $user
	fi
    fi
    exit 1 
}

############################ main ##############################

OS=`uname -s`

if [ $OS = "SunOS" ] ; then 
    user=`/usr/ucb/whoami`
    CPU=`uname -p`
fi
if [ $OS = "Linux" ] ; then
    user=`/usr/bin/whoami`
    CPU=`uname -m`
fi
if [ $OS = "Darwin" ]; then
    user=`/usr/bin/whoami`
    CPU=`uname -m`
fi

exe=$DV_ROOT/tools/$OS/$CPU/m4_gmp

LD_LIBRARY_PATH=$DV_ROOT/tools/$OS/$CPU/lib
export LD_LIBRARY_PATH

    if [ -x $exe ]; then
	log
	exec $exe $ARGV
    else
	die "executable $exe not found!"
    fi
fi
