#!/bin/bash

. ./tmtools_setup.bourne

# Sanity check on TMTOOLS_ROOT setting

if (! [ -f ${TMTOOLS_ROOT}/tmtools_setup.bourne ] ) ; then
   echo "TMTOOLS_ROOT is set incorrectly; edit setup scripts and try again."
   exit
fi

# CDF library and utilities 

echo "Beginning build at " `date`
echo ""
echo "======= Building CDF library and tools ======"
echo ""
cd src/cdf33_1-dist
make clean
make OS=linux ENV=gnu FORTRAN=yes all
make INSTALLDIR=${TMTOOLS_ROOT} install
make test

echo ""
echo "====== CDF tools built ======"
echo ""

cd ${TMTOOLS_ROOT}
# Now that the CDF definitions file is built, source the setup file again
# to pick up the new aliases
. ./tmtools_setup.bourne

# Decommutation library
echo ""
echo "======= Building libdecom  ======"
echo ""
cd src/libdecom
make install
cd ${TMTOOLS_ROOT}
echo ""
echo "======= libdecom built ======"
echo ""

# CDF utility library
echo ""
echo "======= Building libcdfutil  ======"
echo ""
cd src/libcdfutil
make install
cd ${TMTOOLS_ROOT}
echo ""
echo "======= libcdfutil built ======"
echo ""

# Command line programs and scripts

for util in file_splitter pkt2cdf pktcompress pkt_datesort pkt_extent pkt_trange scripts timecvt timetools tmstrip mastercdfs pktmerge parse_vc_filename goes2cdf doy2ymd pkt_wavestats pkt_memdump404 configtools
do
   echo ""
   echo "======= Building ${util}  ======"
   echo ""
   cd src/${util}
   make install
   cd ${TMTOOLS_ROOT}
   echo ""
   echo "======= ${util} built ======"
   echo ""
done

echo "Build completed at " `date`
echo "Remaining steps (to be performed as user thmsoc): "
echo "Create " ${TMTOOLS_DATAROOT} " hierarchy"
echo "Populate data directories with master CDFs (make datadirs in src/mastercdfs)"
