#!/bin/sh
# Copyright 2013 Ravenbrook Limited <http://www.ravenbrook.com/>.
# 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.
# 
# 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
# HOLDER 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.
#
# $Log: findcomp2,v $
# Revision 1.3  1997/09/24 17:04:47  jont
# [Bug #70005]
# Avoid loops by stopping at root
#
# Revision 1.2  1997/06/27  14:54:57  jont
# [Bug #20089]
# Change to use .version file instead of .compound file
#
# Revision 1.1  1996/07/11  15:25:14  io
# new unit
# [Bug #1463]
# tweaked findcomp but did not wish to disturb dependent scripts, so added it as this...
#
# Revision 1.2  1995/03/17  22:01:14  daveb
# Returned the parent of the directory containing the .compound file
# instead of the directory itself, so that check-ins and check-outs
# of multiple files can be based in the same directory.
#
# Revision 1.1  1994/06/09  15:31:25  nickh
# new file
#
#

# Climbs the directory tree up from the argument, searching for a
# .compound file. When it finds one, constructs compound and unit names
# accordingly.
# 
# Meant for use as eval `findcomp foo/bar`, in which case it defines 3
# shell variables: 
#
# $unit: the unit name
# $compound: the compound name
# $dir: the compound directory
#
if [ -d "$1" ]; then
  unit=;
  dir="$1";
else
  unit=`basename $1`
  dir=`dirname $1`
fi
cd $dir
dir=`pwd`
while [ ! -r .version ]; do
  thisdir=`basename $dir`
  if [ -z "$thisdir" ]; then
    # echo empty answers; need to test for this case
    echo compound=
    echo unit=
    echo dir=
    exit
  else 
    dir=`dirname $dir`
    unit=$thisdir:$unit
    cd $dir
    if [ "$dir" = "/" ] ; then
      # stop here to avoid infinite loop
      echo compound=
      echo unit=
      echo dir=
      exit
    fi
  fi
done
compound=`sed -e "1s/,.*//g;2,$ s/.*//g" .version`

# output the results so that eval will get them

echo compound=$compound
echo unit=$unit
echo dir=`dirname $dir`
