#!/bin/sh

# build --
#
#       This script compiles Mash and all of the libraries it needs.
#
# Copyright (c) 2001-2002 The Regents of the University of California.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# A. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# B. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# C. Neither the names of the copyright holders nor the names of its
#    contributors may be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# usage --
#
#       Print brief description of command-line syntax and exit script.
#
# Arguments:
#       none
#
# Results:
#       The script is exited.

usage() {
    echo Usage: $0 '[targets]'
    echo Options:
    echo '  targets    Specifies targets to compile.  Default is all.'
    echo '             all    otcl     srm'
    echo '             tcl    tclcl    mash'
    echo '             tk     gsm      mashbin'
    exit
}


# die --
#
#       Print error message and exit script.
#
# Arguments:
#       @   Error message.
#
# Results:
#       The script is exited.

die() {
    echo $0: $@ 1>&2
    exit 1
}


# Process command-line arguments.

if test $# -eq 0; then
    build_all=1
else
    for build_arg in $@; do
        case $build_arg in
        -h|--help)
            usage;;
        all)
            build_all=1;;
        tcl|tcl8.3)
            build_tcl=1;;
        tk|tk8.3)
            build_tk=1;;
        otcl)
            build_otcl=1;;
        tclcl)
            build_tclcl=1;;
        gsm)
            build_gsm=1;;
        srm)
            build_srm=1;;
        mash)
            build_mash=1;;
        mashbin)
            build_mash=1;
            build_mashbin=1;;
        esac
    done
fi

# FreeBSD 2 doesn't update PWD after a cd command, so we work around this bug
# by updating PWD ourselves.

# Build Tcl.

if test "$build_all" -o "$build_tcl"; then
    echo 'building tcl8.3/unix...'
    cd ./tcl8.3/unix
    PWD=`pwd`
    make -s distclean > /dev/null 2>&1
    ./configure > /dev/null || die 'configure failed'
    make -s || die 'make failed'
    cd ../..
fi

# Build Tk.

if test "$build_all" -o "$build_tk"; then
    echo 'building tk8.3/unix...'
    cd ./tk8.3/unix
    PWD=`pwd`
    make -s distclean > /dev/null 2>&1
    ./configure > /dev/null || die 'configure failed'
    make -s || die 'make failed'
    cd ../..
fi

# Build OTcl.

if test "$build_all" -o "$build_otcl"; then
    echo 'building otcl...'
    cd ./otcl
    PWD=`pwd`
    make -s clean > /dev/null 2>&1
    ./configure > /dev/null || die 'configure failed'
    make -s || die 'make failed'
    cd ..
fi

# Build TclCL.

if test "$build_all" -o "$build_tclcl"; then
    echo 'building tclcl...'
    cd ./tclcl
    PWD=`pwd`
    make -s realclean > /dev/null 2>&1
    ./configure > /dev/null || die 'configure failed'
    make -s || die 'make failed'
    cd ..
fi

# Build GSM.

if test "$build_all" -o "$build_gsm"; then
    echo 'building gsm...'
    cd ./gsm
    PWD=`pwd`
    make -s clean > /dev/null 2>&1
    ./configure > /dev/null || die 'configure failed'
    make -s || die 'make failed'
    cd ..
fi

# Build SRM.

if test "$build_all" -o "$build_srm"; then
    echo 'building srm...'
    cd ./srm
    PWD=`pwd`
    make -s realclean > /dev/null 2>&1
    ./configure > /dev/null || die 'configure failed'
    make -s || die 'make failed'
    cd ..
fi

# Build Mash.

if test "$build_all" -o "$build_mash"; then
    cd ./mash
    PWD=`pwd`

    echo 'configuring mash...'
    make -s realclean > /dev/null 2>&1
    ./configure > /dev/null || die 'configure failed'

    echo 'building mash...'
    make -s || die 'make failed'

    echo 'building smash...'
    make -s smash || die 'make failed'

    echo 'building import...'
    make -s import || die 'make failed'

    echo 'building megatools...'
    make -s megatools || die 'make failed'

    if test "$build_mashbin"; then
        echo 'building mashbin...'
        make -s mashbin || die 'make failed'
    fi

    cd ..
fi
