#! /bin/sh
# $Id: build,v 1.7 1997/05/13 15:09:49 zeller Exp $
# rebuild package in batch mode -- useful for the night

# Copyright (C) 1996,1997 Technische Universitaet Braunschweig, Germany.
# Written by Andreas Zeller <zeller@ips.cs.tu-bs.de>.
# 
# This file is part of ICE.
# 
# ICE 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.
# 
# ICE 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 ICE -- see the file COPYING.
# If not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
# ICE is the incremental configuration environment.
# For details, see the ICE World-Wide-Web page, 
# `http://www.cs.tu-bs.de/softech/ice/',
# or send a mail to the ICE developers at `ice@ips.cs.tu-bs.de'.

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.

exec < /dev/null > $log 2>&1
hostname=`uname -n || hostname`
{
pid=$$
echo build[pid $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
set +x
echo build[pid $pid]: done at `date`
{
echo "To: $USER"; 
echo "Subject: Building$targets on $hostname"; 
echo
cat $log
} | mail $USER
} &
exit 0
