#!/bin/sh -f

# Special settings to run MuPAD without any local modifications

MuPAD_ROOT_PATH=`dirname $0`/../..
export MuPAD_ROOT_PATH

# This Bourne or Korn Shell script runs mmg with predefined paths.

# These are the directories in which you installed MuPAD

# MuPAD_ROOT_PATH is the root directory of the MuPAD-System  
# SYSINFO         is the path of the MuPAD script `sysinfo`
# INCLUDE         is the path of the MuPAD include files
# LIBRARY         is the path to the MuPAD library files 
# This ensures that mmg will find all needed files.

SYS=`$MuPAD_ROOT_PATH/share/bin/sysinfo`
INC=$MuPAD_ROOT_PATH/share/mmg/include
LIB=$MuPAD_ROOT_PATH/$SYS/lib
MOP="-I$INC/kernel -I$INC/acmatch -I$INC/pari"
MMG=$MuPAD_ROOT_PATH/$SYS/bin/mmg
COL=""

#####################################################################
## Funktion zum zeilenweisen Einfaerben einer Eingabe von stdin
##
## Attribute
## ------------
## 0 = default
## 1 = bright
## 4 = underline
## 5 = blink
##
## Farbe    Vordergrund  Hintergrund
## ---------------------------------
## black     30           40
## red       31           41
## green     32           42
## yellow    33           43
## blue      34           44
## purple    35           45
## cyan      36           46
## white     37           47
#####################################################################
color()
{
   while read line; do 
     echo "$line" | sed \
         -e 's/\(Error[: ]\)/[1;4;31m\1[0m/'                         \
         -e 's/\(error[: ]\)/[1;4;31m\1[0m/'                         \
         -e 's/\(Warning[: ]\)/[4;35m\1[0m/'                         \
         -e 's/\(warning[: ]\)/[4;35m\1[0m/'                         \
         -e 's/\(Warn\.:\)/[4;35m\1[0m/'                             \
         -e 's/\(Mesg\.:\)/[1;4m\1[0m/'                              \
         -e 's/\(undeclared\)/[1;4;31m\1[0m/'                        \
         -e 's/\(not declared\)/[1;4;31m\1[0m/'                      \
                                                                         \
         -e 's/^\(g++\)/[1;34m\1[0m/'                                \
         -e 's/^\(CC\)/[1;34m\1[0m/'                                 \
         -e 's/^\(xlC\)/[1;34m\1[0m/'                                \
         -e 's/^\(cxx\)/[1;34m\1[0m/'                                \
         -e 's/^\(ld\)/[1;34m\1[0m/'                                 \
                                                                         \
         -e 's/\(([_a-zA-Z0-9 	][_a-zA-Z0-9   ][^)]*)\)/[1m\1[0m/'  \
         -e 's/[ 	]\([-+][_a-zA-Z0-9/\.][_a-zA-Z0-9/\.-]*\)/ [;34m\1[0m/g' 
     done
}

for option in "$@" ; do
   if [ "$option" = "-color" ] ; then
      COL="YES";
      break;
   fi
done

if [ -n "${COL}" ] ; then
   $MMG "$@" $MOP -L$LIB 2>&1 | color
else
   $MMG "$@" $MOP -L$LIB
fi
