#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
#    \\/     M anipulation  |
#-------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software; you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by the
#     Free Software Foundation; either version 2 of the License, or (at your
#     option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM; if not, write to the Free Software Foundation,
#     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script
#     wmake
#
# Description
#     General, easy to use make system for multi-platform development.
#
#------------------------------------------------------------------------------
# check environment variables
#------------------------------------------------------------------------------

if [ ! "$WM_OPTIONS" ]
then
    echo $0: 'environment variable $WM_OPTIONS not set, exiting !'
    exit 1
fi


if [ ! "$WM_LINK_LANGUAGE" ]
then
    echo $0: 'environment variable $WM_LINK_LANGUAGE not set, exiting !'
    exit 1
fi


if [ ! "$WM_DIR" ]
then
    echo 'wmake : environment variable $WM_DIR not set, exiting !'
    exit 1
fi


if [ ! "$WM_PROJECT" -o ! "$WM_PROJECT_DIR" ]
then
    if [ $# != 1 -o "$1" != "exe" ]
    then
        echo $0: 'environment variable $WM_PROJECT'
        echo ' or $WM_PROJECT_DIR  not set'
        echo '        for building project library or executable, exiting !'
        exit 1
    fi
fi


#------------------------------------------------------------------------------
# check arguments and change to the directory in which to run wmake
#------------------------------------------------------------------------------

curDir=`pwd`

makeOption=""
dir="_"

if [ $# -ge 1 ]
then
    if [ $1 = "lib" -o $1 = "libso" -o $1 = "libo"  -o $1 = "jar" -o $1 = "exe" -o $1 = "project" ]
    then
        makeOption=$1
    else
        dir=$1
    fi
fi

if [ $# -ge 2 ]
then
    dir=$2
fi

if [ $dir != "_" ]
then
    if [ -d $dir ]
    then
        cd $dir
    else
        echo $0: directory $dir does not exist
        exit 1
    fi
fi

if [ $# = 3 ]
then
    MakeDir=$3
else
    MakeDir=Make
fi

if [ ! -d $MakeDir ]
then
    echo $0: $MakeDir directory does not exist, exiting !
    exit 0
fi


#------------------------------------------------------------------------------
# Check the existance of the Make directory and files file
# If both exist make the wmake derived files
#------------------------------------------------------------------------------

if [ ! -d $MakeDir ]
then
    echo $0: cannot make, $MakeDir directory does not exist
    cd $curDir
    exit 1
fi

if [ ! -r $MakeDir/files ]
then
    echo $0: cannot make, file $MakeDir/files does not exist
    cd $curDir
    exit 1
fi


cd $MakeDir

make -f $WM_DIR/MakefileFiles allFiles

cd ..


#------------------------------------------------------------------------------
# Check the $OBJECTS_DIR = $MakeDir/$WM_OPTIONS/objectFiles file 
# was created successfully
#------------------------------------------------------------------------------

OBJECTS_DIR=$MakeDir/$WM_OPTIONS

if [ ! -r $OBJECTS_DIR/objectFiles ]
then
    echo $0: cannot make, file $OBJECTS_DIR/objectFiles was not created successfully
    cd $curDir
    exit 1
fi


#------------------------------------------------------------------------------
# Select the version of make to be used (pmake if available otherwise make)
#------------------------------------------------------------------------------

make="make"

if [ "$WM_NCOMPPROCS" ]
then
    if [ "$WM_NCOMPPROCS" -ne 1 ]
    then
        if [ -r /usr/sbin/pmake ]
        then
            make="pmake -v -J "$WM_NCOMPPROCS
        else
            make="make -j "$WM_NCOMPPROCS
        fi
    fi
fi


#------------------------------------------------------------------------------
# Make the dependency files
#------------------------------------------------------------------------------

touch $OBJECTS_DIR/dontIncludeDeps

if [ "$makeOption" = "lib" -o "$makeOption" = "libso" -o "$makeOption" = "libo" ]
then
    $make -f $WM_DIR/Makefile MAKE_DIR=$MakeDir INCLUDE_DEPS=$OBJECTS_DIR/dontIncludeDeps lnInclude/uptodate
fi

$make -f $WM_DIR/Makefile MAKE_DIR=$MakeDir INCLUDE_DEPS=$OBJECTS_DIR/dontIncludeDeps $OBJECTS_DIR/dependencies

retVal=$?
if [ $retVal -ne 0 ]; then
    cd $curDir
    exit $retVal
fi


#------------------------------------------------------------------------------
# make the object files and link
#------------------------------------------------------------------------------

echo  $makeOption
$make -f $WM_DIR/Makefile MAKE_DIR=$MakeDir INCLUDE_DEPS=$OBJECTS_DIR/includeDeps $makeOption

retVal=$?
if [ $retVal -ne 0 ]; then
    cd $curDir
    exit $retVal
fi


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

cd $curDir

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