#!/bin/sh

# Make all libraries and selected (mainly) dyn exectables, for faster
# Starlab construction.  Write output to a file (make.log) and suppress
# some annoying extraneous messages.

log=$STARLAB_PATH/make.log

echo Writing output to $log

echo Building all libraries...

cd $STARLAB_PATH
(cd sbin; make all; cd ..)		>  $log  2>&1
make "$@" libs 2>&1 | grep -v 'make\['	>> $log  2>&1

# Build specific executables:

echo Building selected executables...

cd $STARLAB_PATH/src/node/util
build_report $log mknode mkmass mksecondary

cd $STARLAB_PATH/src/node/dyn/init
build_report $log mkplummer mkking mk_aniso_king mkbinary

cd $STARLAB_PATH/src/node/dyn/util
build_report $log compute_com compute_density compute_max_cod \
		  compute_mean_cod extract_snap flatten freeze \
		  lagrad scale to_cod to_com

cd $STARLAB_PATH/src/node/dyn/hdyn/util
build_report $log sys_stats

cd $STARLAB_PATH/src/star/sstar/init
build_report $log addstar

#-----------------------------------------------------------------------

# Slightly different treatment of hdyn/evolve executables (approach
# follows that used in build_report).

cd $STARLAB_PATH/src/node/dyn/hdyn/evolve
echo -n "   " `pwd`...

# Rebuild if kira is out of date with respect to local objects or
# libraries.  Note that this (very tricky) checking is what we would
# like make to do, but the syntax is too hard for make to handle in
# a default rule...

# Do not rebuild if the libs are not up to date -- presumably this
# means that 'make lib' failed previously.

TMPF=/tmp/$$.make
trap "/bin/rm -f $TMPF" 0 1 2 15
rebuild=1
make list_obj | awk '
  /\.o$/ {
    s = substr($0,0,length($0)-2);
    print "check: " $0;
    print $0 ": " s ".C";
    print "\t/bin/false";
  }' >$TMPF
if make -f $TMPF check >/dev/null 2>&1; then
    echo "(not rebuilt)"
else
    last_obj=`latest_time *.o $STARLAB_PATH/lib/*.a`
    exe="`make list_exe`"
    first_exe=`earliest_time $exe`
    last_src=`latest_time kira.C`

    if [ $last_src -gt $first_exe -o $last_obj -gt $first_exe ]; then
        rm -f $exe
        make bin 2>&1 | grep -v 'make\[' >> $log

	if [ `earliest_time $exe` -lt $last_src ]; then
	    echo "(failed)"
	else
	    echo "(built)"
	fi
    else
        echo "(up to date)"
    fi
fi
#-----------------------------------------------------------------------
