#!/bin/sh

# NAME:
#	mk - simple wrapper around make
#
# SYNOPSIS:
#	mk ...
#
# DESCRIPTION:
#	This script simply searches upwards for a file called
#	'.sandbox-env' and sources it, as well as setting the variable
#	$SB to the directory where it is found.
#
#
# AUTHOR:
#	Simon J. Gerraty <sjg@crufty.net>
#

# RCSid:
#	$Id: mk,v 1.5 2010/01/05 08:38:00 sjg Exp $
#
#	@(#) Copyright (c) 2009 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

Mydir=`dirname $0`
. $Mydir/os.sh

ev=.sandbox-env
rc=.sandboxrc

find_sb() {
    while :
    do
        here=`${pwd:-pwd}`
        if [ -s ${1:-$ev} ]; then
            echo $here
            return
        fi
        case $here in
        /) break;;                      # not in a sandbox
        esac
        cd ..
    done
}

source_rc() {
    for f in "$@"
    do
	[ -s $f ] || continue
	. $f
    done
}

unset ENV
source_rc ${SB_RCFILES:-$HOME/$rc}

here=`${pwd:-pwd}`
sb=`find_sb`
if [ -d ${sb:-/dev/null} ]; then
    SB=$sb
    export SB
    cd $SB
    source_rc ../$rc ./$rc ./$ev
    cd $here
fi

exec ${REAL_MAKE:-${MAKE:-make}} "$@"

