#!/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.
script_name=`basename $0`
cmd="merge"
STATUS=0
branch=""; base=""; test="";
usage="usage: $script_name [-[th]+] [-e <base>] [-b <branch>] (file1|dir1)+"

while getopts "te:b:h" opt; do
  case $opt in
    e) base="-base $OPTARG";;
    b) branch="-branch $OPTARG";;
    t) test="test";;
    h|\?) 
      echo $usage; 
      echo "    h for this help"
      echo "    t run in hope test mode"
      echo "    e <base version>"
      echo "      applies to single file argument only"
      echo "    b <branch version>"
      exit 1;;
  esac
done
shift `expr $OPTIND - 1`


: echo $script_name: checking arguments...
if [ $# -eq 0 ]; then
  echo "nothing to $script_name"
  echo $usage
  exit 1
else
  : echo ok
fi

: echo prime $entry

eval `findcomp2 $1`;
if [ -z "$compound" ]; then
  entry=;
  echo "cannot identify compound \"$1\""
  exit 1
elif [ -z "$unit" ]; then
  entry="-c $compound"
else
  entry="-c $compound -u $unit"
fi
shift

: echo stuff in the separators

for arg in $*; do
  eval `findcomp2 $arg`;
  if [ -z "$compound" ]; then
    echo "cannot identify compound \"$arg\""
    exit 1
  elif [ -z "$unit" ]; then
    entry="$entry -a -c $compound"
  else
    entry="$entry -a -c $compound -u $unit"
  fi
done

: echo final processing

if [ -z "$TMP" ]; then
  temp_dir=/tmp
else
  temp_dir=`echo $TMP | sed -e 's!\\\\!/!g'`
fi
temp_file=$temp_dir/hope_tmp$$

# We need to work out whether we're on Unix or Windows.
# NT sets the OS variable to Windows_NT.
# Some MLWorkers set the OS variable themselves.
# Failing that, bash sets the OSTYPE variable.
hope_temp_file=""
case "$OS" in
  Win*)
    hope_temp_file=`echo $temp_file | sed -e 's!/!\\\\!g'`;;
  NT*)
    hope_temp_file=`echo $temp_file | sed -e 's!/!\\\\!g'`;;
  Solaris*)
    hope_temp_file=$temp_file;;
  SunOS*)
    hope_temp_file=$temp_file;;
  Irix*)
    hope_temp_file=$temp_file;;
  Linux*)
    hope_temp_file=$temp_file;;
esac

if [ -z "$hope_temp_file" ]; then
  case "$OSTYPE" in
    Win*)
      hope_temp_file=`echo $temp_file | sed -e 's!/!\\\\!g'`;;
    Solaris*)
      hope_temp_file=$temp_file;;
    SunOS*)
      hope_temp_file=$temp_file;;
    linux*)
      hope_temp_file=$temp_file;;
    *)
      # Assume Unix if we have no other clue.
      # We could also get here on Win95, but we don't use that
      # for development.
      hope_temp_file=$temp_file;;
  esac
fi

if [ -z "$entry" ]; then
  echo skipped everything
else
  cd $dir
  echo $test $cmd $base $branch $entry > $temp_file
  hope source $hope_temp_file
  rm $temp_file
fi

###############################################################################
# $Log: hmerge,v $
# Revision 1.7  1999/03/24 15:38:18  daveb
# [Bug #190541]
# Check both the OS and OSTYPE environment variables.
#
# Revision 1.6  1998/10/07  12:45:45  jont
# [Bug #70179]
# Avoid OSTYPE=linux problems
#
# Revision 1.5  1998/09/24  14:32:53  jont
# [Bug #70179]
# Make sure temp file name more carefully created to avoid drive letter problems
#
# Revision 1.4  1998/08/25  14:48:12  jont
# [Bug #70162]
# Use source files to get past hope 44 argument limit under Win32
#
# Revision 1.3  1997/12/15  13:50:29  jont
# [Bug #70020]
# Ensure we're in the correct directory before doing the hope command
#
# Revision 1.2  1997/04/15  12:01:11  jont
# Stop using script_path mechanism as this confuses Win32
#
# Revision 1.1  1996/08/01  15:26:57  io
# new unit
# When you get a list of claimed writeable files in your directory and
# you need to update it, pass that list to this script.
#
#
