#!/bin/sh
# $Id: build,v 1.1.1.1 1995/05/01 15:48:42 zeller Exp $
# rebuild package in batch mode -- useful for the night

# Copyright (C) 1993 Technische Universitaet Braunschweig, Germany.
# Written by Andreas Zeller (zeller@ips.cs.tu-bs.de).
# 
# This file is part of NORA.
# 
# NORA 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.
# 
# NORA 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 NORA -- see the file COPYING.
# If not, write to the Free Software Foundation, Inc.,
# 675 Mass Ave, Cambridge, MA 02139, USA.
# 
# NORA is an experimental inference-based software development
# environment. Contact nora@ips.cs.tu-bs.de for details.

# $Log: build,v $
# Revision 1.1.1.1  1995/05/01  15:48:42  zeller
# DDD 0.9 distribution
#
# Revision 1.1.1.1  1995/02/09  09:14:42  zeller
# ICE 0.5 distribution
#
# Revision 1.34  1994/12/15  17:48:36  zeller
# Fix: usage doc fixed
#
# Revision 1.33  1994/12/13  17:45:58  zeller
# Fix: leave one space before $env
#
# Revision 1.32  1994/12/11  23:02:10  zeller
# New: also run on old-style shells
# New: show pid in log
# Fix: accept make variable settings
#
# Revision 1.31  1994/12/09  10:00:41  zeller
# New: don't rely on batch system
#
# Revision 1.30  1994/11/07  12:47:59  zeller
# New: configure if needed
#
# Revision 1.29  1994/10/18  13:35:31  zeller
# New: echo start and done date
#
# Revision 1.28  1994/10/11  19:25:33  zeller
# Fix: at does not like being exec'ed
#
# Revision 1.27  1994/10/06  16:07:05  zeller
# New: configure only if needed
# New: include current dir in log
#
# Revision 1.26  1994/09/27  22:21:30  zeller
# Minor changes made for ICE
#

invoke="$0 $@"
log=./MakeOut

# Parse options
env="PATH=$PATH USER=$USER HOME=$HOME"
targets=
options=
niceval=-10

usage=\
"build: usage: $0 [-help] [-nice NICE] [OPTION...] TARGET...

  NICE is a nice value [0..19] (see nice(1), default: 10),
  OPTION is a make(1) option, such as -k or VAR=VALUE
  TARGET is a make(1) target, as \`clean', \`all', or \`install'.

  User examples:
  $0 clean all                      # clean and all
  $0 CCOPT= CXXOPT= all             # all without C++ optimiziation
  $0 -nice 15 dist install-dist     # dist now with nice value 15

  Output is written to $log and mailed to you.
"

while [ $# != 0 ]; do
  case "$1" in
    -help) echo "$usage" >&2; exit 0;;
    -nice) niceval=-$2; shift 2;;
    -*)    options="$options $1"; shift;;
    *=*)   env="$env $1"; shift;;
    *)     targets="$targets $1"; shift;;
  esac
done

if [ "$targets" = "" ]; then
  echo "$usage" >&2
  exit 1
fi

# Start the batch job

# We use env and make -e to set macro values, since this also works
# in nested Makefiles.

cat < /dev/null > $log
hostname=`hostname`
(
(
echo build[pid $$]: start at `date`
echo ${hostname}% cd `pwd`
if [ ! -f Makefile ]; then
  echo ${hostname}% ./configure -v
  ./configure -v
fi
echo ${hostname}% "$invoke"
set -x
nice $niceval env - $env make -e $options$targets 2>&1
echo build[pid $$]: done at `date`
) > $log 2>&1
(
echo "To: $USER"; 
echo "Subject: Building$targets on $hostname"; 
echo
cat $log
) | mail $USER
) &
exit 0
