#!/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
  patch -p1 -i ../ndiff-mesa.patch
  ./configure
  make prefix=../$NDIFF_FOLDER all
  make prefix=../$NDIFF_FOLDER install

  cd ../$NDIFF_FOLDER/bin/
  rm ndiff-2.00
  mv ndiff ndiff-mesa

  cd ../../..
  }

echo "running install_ndiff"
install_ndiff &> /dev/null
echo "ndiff-mesa installed in utils/ndiff/"

#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."
  echo "We need this in various test cases during installation."
  exit 1
fi

do_one const
do_one utils
do_one mtx
do_one num

echo
echo

