#!/bin/sh
set -eu

. ./BUILD $@
home=$1
echo
echo Building the graphics into directory $home
echo 

# Get ready for installation - FLTK's build mechanism is broken,
# and assumes that the library directory is called lib - because
# of that idiocy, we use a temporary directory for building

dirname=/tmp/NMM_C++_$$
mkdir $dirname
ln -s $dirname/Libs $dirname/lib
# Add the following command on MCS Linux, heaven help us
# set +e
cat FLTK.tar.gz | (cd $dirname; tar -xzf -)
cat GUI.tar.gz | (cd $dirname; tar -xzf -)
# Add the following command on MCS Linux, heaven help us
# set -e

# Configure, build and install FLTK

(
    set -eux
    cd $dirname/FLTK
    ./configure --includedir=$dirname/Includes --libdir=$dirname/Libs
    make
# Use the following command on MCS Linux, heaven help us
#   NOHLINKS=yes make install
# Use the following form on any sane Unix-like system
    make install
)

# Configure, build and install the graphics interface - this is a
# bit of a horrible hack, in order to fit in with the above

(
    set -eux
    cp $home/Includes/* $dirname/Includes
    cd $dirname/GUI
    FLTK=$dirname/Includes make
    INCDIR=$dirname/Includes LIBDIR=$dirname/Libs make install
)

# Copy the files, clean up and say we have finished

cp -p run_gr $home/Bin
cp $dirname/FLTK/documentation/* $home/Documentation
(cd $dirname; tar -cf - Includes Libs) | (cd $home; tar -xf -)
rm -r $dirname
echo
echo "The installation is complete (see directory $home)"
echo All of directories Bin, Includes, Libs and Documentation should contain files
echo 
