#!/bin/sh
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is Mozilla Communicator.
#
# The Initial Developer of the Original Code is Chris McAfee.
# Portions created by Chris McAfee are
# Copyright (C) The Mozilla Organization.
# All Rights Reserved.
#
# Contributor(s): Chris McAfee <mcafee@netscape.com>
#

#
# Tinderbox control script.
#
# Assumes that at most two tinderboxes are running
# simultaneously, depend and clobber.
#
# Assumes that if mozconfig exists, use it. Format:
#   mk_add_options MOZ_MAKE_FLAGS=j8
#   ac_add_options --disable-debug
#   ac_add_options --enable-optimize
#


tinderbox_usage() {
  echo "Usage: tinderbox [depend|clobber|multi] {start|stop|status|restart}"
  exit 1
}

run_in_bg() {
  nohup /bin/sh -c "exec $* > /dev/null 2>&1" &
}

if [ $# != 2 ]; then
  echo "Wrong number of arguments."
  tinderbox_usage
fi

build_type="$1"
build_action="$2"

# Depend or clobber build?
if [ "$build_type" != "depend" -a \
     "$build_type" != "clobber" -a \
     "$build_type" != "multi" ]; then
    echo "Unknown option: $1"
    tinderbox_usage
fi

# See how we were called.
case "$build_action" in
  start)
    if test -f $build_type.pid; then
      echo "$build_type build already running with PID "`cat $build_type.pid`
    else
      echo "Starting $build_type tinderbox..."

	  # Say if we found post-mozilla.pl
	  if test -f post-mozilla.pl; then
	    echo "Found post-mozilla.pl"
	  fi

	  # Grab and use mozconfig if it exists.
      if test "$build_type" = "multi"; then
        run_in_bg ./multi-tinderbox.pl
      elif test -f mozconfig; then
        echo "Found mozconfig..."
        run_in_bg ./build-seamonkey.pl --$build_type --mozconfig mozconfig
      elif test -f .mozconfig; then
        echo "Found .mozconfig..."
        run_in_bg ./build-seamonkey.pl --$build_type --mozconfig .mozconfig
      else
        run_in_bg ./build-seamonkey.pl --$build_type
      fi
	  # Write out PID so we can shut things down later.
	  # This needs to be right after the ./build-seamonkey.pl call to
	  # get the right PID.
      echo "PID $!"
      echo $! > $build_type.pid
      /bin/sh -c "sleep 5; \rm -f nohup.out" &
    fi
    ;;
  stop)
    if test -f $build_type.pid; then
      pid=`cat $build_type.pid`
      echo "Shutting down $build_type tinderbox, PID = $pid"
      kill $pid
      \rm $build_type.pid
    else
      echo "$build_type is not running."
    fi
    echo
    ;;
  status)
    if test -f $build_type.pid; then
      echo "$build_type build is running with PID of "`cat $build_type.pid`
    else
      echo "$build_type build is not running."
    fi
    ;;
  restart)
    echo "Restarting $build_type tinderbox:"
    $0 $build_type stop
    $0 $build_type start
    ;;
  *)
    tinderbox_usage
    exit 1
esac

exit 0
