#!/bin/bash

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

function do_one_parallel {
        cd $1
        check_okay
        echo
        pwd
        echo "building" $1 "package."
        echo
        # don't use nice with ./i1 or can miss errors
        ./i1p
        check_okay
        cd ..
        echo
  if [ -f skip_build ]
  then
        echo 'mesa/'$1 'has been tested and exported.'
  elif [ -f skip_test ]
  then
        echo 'mesa/'$1 'has been built and exported.'
  else
        echo 'mesa/'$1 'has been built, tested, and exported.'
  fi
        echo
        echo "************************************************"
}

function do_one {
	cd $1
	check_okay
	echo
	pwd
	echo "building" $1 "package."
	echo
	# don't use nice with ./i1 or can miss errors
	./i1
	check_okay
	cd ..
	echo
  if [ -f skip_build ]
  then
  	echo 'mesa/'$1 'has been tested and exported.'
  elif [ -f skip_test ]
  then
  	echo 'mesa/'$1 'has been built and exported.'
  else
  	echo 'mesa/'$1 'has been built, tested, and exported.'
  fi
	echo
	echo "************************************************"
}

# there is also a tool called ndiff which is part of the nmap security
# scanner.  if called with the -? argument, that tool returns 2.
# The ndiff utility we want recognizes this will return 0.
# We send the output, which is on stderr, to dev/null

ndiff -? 2> /dev/null
if [ $? -ne 0 ]
then
	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

makedepf90 > /dev/null
if [ $? -ne 0 ]
then
	echo
	echo "*******************************************************************"
	echo "*                                                                 *"
	echo "*    You need to install makedepf90 before you can install mesa.  *"
	echo "*                                                                 *"
	echo "*    The makedepf90 tar file is in the mesa/utils directory.      *"
	echo "*    Unpack it anywhere (tar -zxvf makedepf90-2.8.8.tar.gz),      *"
	echo "*    and cd to the makedepf90-2.8.8 directory.                    *"
	echo "*    Then do the usual sequence:                                  *"
	echo "*                                                                 *"
	echo "*           ./configure                                           *"
	echo "*           make all                                              *"
	echo "*           sudo make install                                     *"
	echo "*                                                                 *"
	echo "*    Note: 'make test' doesn't seem to work. I suggest skipping   *"
	echo "*    that step in the makedepf90 installation.                    *"
	echo "*                                                                 *"
	echo "******************************************************************"
	echo
	exit 1
fi
if [ ! -r lib ]
then
  mkdir lib
fi

if [ ! -r include ]
then
  mkdir include
fi


# don't use nice or can miss errors
./install_numerics_only
check_okay


if [ ! -r data ]
then
	echo
	echo "******************************************************************"
	echo "*                                                                *"
	echo "*    Please setup the current mesa data before doing ./install   *"
	echo "*    See the mesa README file for details.                       *"
	echo "*                                                                *"
	echo "******************************************************************"
	echo
	exit 1
fi

function do_input_data {
	cd $1
	check_okay
	echo
	pwd
	echo "installing mesa" $1 "data."
	echo
	# don't use nice or can miss errors
	./build_data_and_export
	check_okay
	echo $1 "data successfully installed."
	echo
	echo "************************************************"
	cd ..
}





if [ ! -r data/chem_data ]
then
	do_input_data chem
fi
do_one_parallel chem

if [ ! -r data/colors_data ]
then
	do_input_data colors
fi
do_one_parallel colors

# get rid of obsolete eos_data directory
rm -rf data/eos_data

# check that the eos data directories exist
if [ ! -r data/eosDT_data ]
then
	do_input_data eos
elif [ ! -r data/eosPT_data ]
then
	do_input_data eos
fi

# now have installed eos data, so can install the eos module
do_one_parallel eos

if [ ! -r data/kap_data ]
then
	do_input_data kap
fi
do_one_parallel kap

do_one_parallel rates
do_one_parallel neu
do_one_parallel net
do_one_parallel nse

if [ ! -r data/ionization_data ]
then
	do_input_data ionization
fi
do_one_parallel ionization

if [ ! -r data/atm_data ]
then
	do_input_data atm
fi
do_one_parallel atm

do_one_parallel mlt

do_one sample

do_one_parallel star

do_one gyre

do_one adipls

do_one_parallel binary



echo
echo
echo
echo
echo
echo
echo
echo "************************************************"
echo "************************************************"
echo "************************************************"
echo
echo 'MESA installation was successful'
echo
echo "************************************************"
echo "************************************************"
echo "************************************************"
echo
echo
echo
echo

