#!/bin/sh
#
# $Id$
# Karsten Kruse www.tecneeq.de
#
# mk_rel.sh - Build Releases from NetBSD-Sourcetree
#
# TODO
#  - enable/disable X11-crosscompiling
#  - make logging an option
#  - create a bootable iso-cd (optional)

###### HARDWIRED DEFAULTS ##############################################
opt_debug=no                                 # debug the script
opt_nothing=no                               # just print what we would do
opt_timing=yes                               # print buildtime
opt_release=yes                              # build also release-sets
opt_update=yes                               # update existing builds
opt_cleandest=yes                            # remove destinationdir
opt_cvsup=no                                 # update source first
opt_cvscmd="/root/bin/mk_cvs_up.sh"          # command used to update source
opt_srcdir=/usr/src                          # where the sources are
opt_workdir=/mnt/data3/systems/netbsd/builds # where we do the work
opt_logdir=$opt_workdir                      # where we store the logfiles
opt_arches="i386"                            # arches to build
###### HARDWIRED DEFAULTS ##############################################

#  Copyright (c) 2002-2005, Karsten Kruse <tecneeq(at)tecneeq.de>
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following  disclaimer.
#  2. 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.
#  3. Neither the name of the author 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 COPYRIGHT
#  OWNER 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.

func_usage() {
cat <<END
 Build NetBSD stable or current automatted
 Usage: $0 [OPTIONS]
  -h                  => print this help
  -p                  => print hardwired defaults
  -f <file>           => configfile
  -d <yes|no>         => debug the script
  -n <yes|no>         => just print what we would do
  -t <yes|no>         => print buildtime
  -r <yes|no>         => build also release-sets
  -u <yes|no>         => update existing build or new
  -m <yes|no>         => remove destinationdir 
  -c <yes|no>         => update sources first
  -s <dir>            => where the sources are
  -w <dir>            => where we do the work
  -l <dir>            => where we store the logfiles
  -a <arches>         => arches to build
END
}

func_defaults() {
cat <<END
Settings for $0
configfile            :    $opt_file
debug the script      : -d $opt_debug
just print actions    : -n $opt_nothing
print buildtime       : -t $opt_timing
build release-sets    : -r $opt_release
update existing build : -u $opt_update
remove destinationdir : -m $opt_cleandest
update sources first  : -c $opt_cvsup
sources are in        : -s $opt_srcdir
do the work in        : -w $opt_workdir
store the logfiles in : -l $opt_logdir
arches to build       : -a $opt_arches
END
}

func_file() {
if [ -n $2 ] ; then
  if [ -f $2 ] ; then
    read $2
  else
    echo >&2 "$2 not found, check $1" ; exit 1
  fi
fi
}

func_yesno() {
case $2 in
  yes) shift ;;
  no)  shift ;;
  *)   echo >&2 "$2 is not a valid value for $1" ; exit 1
esac
}

func_work() {
$precmd mkdir -p $mkdirs || exit 1
$precmd ./build.sh $build_opts release
}

while getopts hpd:n:t:r:u:m:c:s:w:l:a: opt ; do
    case "$opt" in
      h)  func_usage ; exit                                     ;;
      p)  func_defaults ; exit                                  ;;
      f)  func_file "$OPTARG"                                   ;;
      d)  func_yesno "$opt" "$OPTARG" ; opt_debug="$OPTARG"     ;;
      n)  func_yesno "$opt" "$OPTARG" ; opt_nothing="$OPTARG"   ;;
      t)  func_yesno "$opt" "$OPTARG" ; opt_timing="$OPTARG"    ;;
      r)  func_yesno "$opt" "$OPTARG" ; opt_release="$OPTARG"   ;;
      u)  func_yesno "$opt" "$OPTARG" ; opt_update="$OPTARG"    ;;
      m)  func_yesno "$opt" "$OPTARG" ; opt_cleandest="$OPTARG" ;;
      c)  func_yesno "$opt" "$OPTARG" ; opt_cvsup="$OPTARG"     ;;
      s)  opt_srcdir="$OPTARG"                                  ;;
      w)  opt_workdir="$OPTARG"                                 ;;
      l)  opt_logdir="$OPTARG"                                  ;;
      a)  opt_arches="$OPTARG"                                  ;;
      \?) func_usage >&2 ; exit 1                               ;;
    esac
done
shift `expr $OPTIND - 1`

if [ _$opt_debug = _yes ] ; then
  set -v     # write input to standard error as it is read
  set -x     # write each command to standard error preceded by a "+"
  set -e     # terminate the script at first error
  set -u     # unset variables are fatal errors
fi

cd $opt_srcdir || $(echo >&2 "Unable to cd into $opt_srcdir" ; exit 1 )

version=`/bin/sh sys/conf/osrelease.sh`

if [ _$opt_nothing = _yes ] ; then
  precmd=echo
elif [ _$opt_timing = _yes ] ; then
  precmd=time
else
  precmd=""
fi

if [ _$opt_cvsup = _yes ] ; then
  $precmd $opt_cvscmd || exit
fi

# todo: improve logging
#       build x11-sets
#       make image.iso

for arch in $opt_arches ; do

  echo "==> Starting build for $arch"

  objdir=${opt_workdir}/NetBSD-${version}-${arch}-obj
  destdir=${opt_workdir}/NetBSD-${version}-${arch}-dest
  reldir=${opt_workdir}/NetBSD-${version}-${arch}-rel
  tooldir=${opt_workdir}/NetBSD-${version}-${arch}-tool
  logfile=${opt_workdir}/NetBSD-${version}-${arch}.log


  if [ _$opt_cleandest = _yes ] ; then
    $precmd rm -rf $destdir
  fi
  mkdirs="$objdir $destdir "
  build_opts="-O $objdir -D $destdir"

  if [ _$opt_release = _yes ] ; then
    $precmd rm -rf $reldir 
    mkdirs="$mkdirs $reldir"
    build_opts="$build_opts -R $reldir"
  fi

  if [ ! _$arch = _`uname -m` ] ; then
    mkdirs="$mkdirs $tooldir"
    build_opts="$build_opts -T $tooldir -m $arch"
  fi
  if [ _$opt_update = _yes ] ; then
    build_opts="$build_opts -u"
  else
    $precmd rm -rf $objdir $tooldir
  fi

  func_work 2>&1 | tee $logfile

done

# eof
