#!/bin/bash

function check_okay {
	if [ $? -ne 0 ]
	then
		exit 1
	fi
}

function do_one {
	cd $1
	echo
	pwd
	echo "building" $1 "package."
	echo
	./i1
	check_okay
	cd ..
	echo
	echo 'mesa' $1 'has been made, tested, and exported.'
	echo
	echo "************************************************"
}

function install_ndiff {
NDIFF_FOLDER=ndiff

cd utils
tar -xzvf ndiff-2.00.tar.gz
check_okay

mkdir $NDIFF_FOLDER
mkdir $NDIFF_FOLDER/bin
mkdir $NDIFF_FOLDER/man
mkdir $NDIFF_FOLDER/man/man1

cd ndiff-2.00
./configure
make prefix=../$NDIFF_FOLDER all
make prefix=../$NDIFF_FOLDER install

cd ../..
}


MESA_NDIFF=utils/ndiff/bin/ndiff
SYS_NDIFF=`which ndiff 2> /dev/null`

echo "checking if ndiff is installed ..."

if $MESA_NDIFF -author &> /dev/null; then
    echo "yes!"
else
    if [ -n "$SYS_NDIFF" ] && $SYS_NDIFF -author &> /dev/null; then
	echo "yes! creating symlink to $SYS_NDIFF"
	mkdir -p ${MESA_NDIFF%%ndiff}
	ln -sf $SYS_NDIFF $MESA_NDIFF
    else
	echo "no! running install_ndiff"
	install_ndiff &> /dev/null
	echo "ndiff installed in utils/ndiff/"
    fi
fi

#utils/ndiff/bin/ndiff .checkndifftmp1 .checkndifftmp2
#if [ $? -ne 0 ]
#then
#	rm .checkndifftmp1 .checkndifftmp2
#	echo
#	echo "****************************************************************"
#	echo "*                                                              *"
#	echo "*    You need to install ndiff before you can install mesa.    *"
#	echo "*                                                              *"
#	echo "*    The ndiff tar file is in the mesa/utils directory.        *"
#	echo "*    Unpack it anywhere (tar -zxvf ndiff-2.00.tar.gz),         *"
#	echo "*    and cd to the ndiff directory.                            *"
#	echo "*    Then do the usual sequence:                               *"
#	echo "*                                                              *"
#	echo "*           ./configure                                        *"
#	echo "*           make all                                           *"
#	echo "*           sudo make install                                  *"
#	echo "*                                                              *"
#	echo "*    Note: 'make check' seems to give lots of false alarms.    *"
#	echo "*    I suggest skipping that step in the ndiff installation.   *"
#	echo "*                                                              *"
#	echo "*    BTW: there's another 'ndiff' out there in the unix world. *"
#	echo "*           http://nmap.org/ndiff/                             *"
#	echo "*    I'm sure it's a great tool, but it isn't what we need.    *"
#	echo "*                                                              *"
#	echo "****************************************************************"
#	echo
#	exit 1
#fi

if [ -z "${OMP_NUM_THREADS}" ]; then
  echo "Please set the environment variable OMP_NUM_THREADS."
  echo "Suggested value is the number of cores on your machine."
  exit 1
fi

do_one const
do_one alert
do_one utils
do_one mtx
do_one num

echo
echo
echo
echo
echo
echo
echo
echo "************************************************"
echo "************************************************"
echo "************************************************"
echo
echo 'installation of mtx and num modules was successful.'
echo
echo "************************************************"
echo "************************************************"
echo "************************************************"
echo
echo
echo
echo

