#!/bin/bash

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

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
	echo 'mesa/'$1 'has been made, tested, and exported.'
	echo
	echo "************************************************"
}



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 chem

if [ ! -r data/colors_data ]
then
	do_input_data colors
fi
do_one 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 eos

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

if [ ! -r data/weaklib_data ]
then
	do_input_data weaklib
fi
do_one weaklib

if [ ! -r data/ecapture_data ]
then
	do_input_data ecapture
fi
do_one ecapture

do_one screen
do_one reaclib
do_one rates
do_one neu
do_one net
do_one nse
do_one mlt

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

do_one atm

do_one sample

do_one star

do_one gyre

do_one adipls

do_one 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

