#!/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="claim"
STATUS=0
test=""; reason=""; update=""; bug_id=""; claim=""; branch="trunk";
usage="usage: $script_name [-[husxt]+] [-b <branch>] [-r <reason>] [-i <bug-id>] (file|dir)+"

while getopts "tur:usxi:hb:" opt; do
  case $opt in
    b) branch="$OPTARG";;
    t) test="test";;
    u) update="-update";;
    i) bug_id="-bug-number $OPTARG";;
    r) reason="$OPTARG";;
    s) claim="-claim soft";;
    x) claim="-claim hard";;
    h|\?)
      echo $usage;
      echo "    h for this help"
      echo "    t to show the hope expression passed"
      echo "    u update existing claim, allows bugid & reason changes"
      echo "    r <reason>"
      echo "    s soft claim"
      echo "    x hard claim"
      echo "    i <bug-id>"
      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 -fn $1"
else
  entry="-c $compound -u $unit -fn $1"
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 -fn $arg"
  else
    entry="$entry -a -c $compound -u $unit -fn $arg"
  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
elif [ -n "$reason" ]; then # reason exists
  echo $test $cmd -branch "$branch" $update $bug_id -reason "$reason" $entry > $temp_file
  hope source $hope_temp_file
  rm $temp_file
else
  echo $test $cmd -branch "$branch" $update $bug_id $entry > $temp_file
  hope source $hope_temp_file
  rm $temp_file
fi

###############################################################################
#
# claim - make a claim on an existing (edited) file
#
# $Log: claim,v $
# Revision 1.13  1999/03/24 15:37:58  daveb
# [Bug #190541]
# Check both the OS and OSTYPE environment variables.
#
# Revision 1.12  1998/10/07  12:45:36  jont
# [Bug #70179]
# Avoid OSTYPE=linux problems
#
# Revision 1.11  1998/09/24  14:31:33  jont
# [Bug #70179]
# Make sure temp file name more carefully created to avoid drive letter problems
#
# Revision 1.10  1998/08/25  14:52:49  jont
# [Bug #70162]
# Use source files to get past hope 44 argument limit under Win32
#
# Revision 1.9  1997/04/15  12:00:38  jont
# Stop using script_path mechanism as this confuses Win32
#
# Revision 1.8  1996/10/07  11:57:59  io
# allow updating of bug numbers of existing claims
#
# Revision 1.7  1996/08/01  12:54:10  daveb
# The -i option now correctly inserts the -bug-number argument.
#
# Revision 1.6  1996/07/12  11:31:23  io
# [Bug #1463]
# add support for directories and bug-ids
#
# Revision 1.5  1996/04/09  17:19:42  daveb
# Added -u (= update-reason) option.
#
# Revision 1.4  1995/05/16  15:44:36  daveb
# Changed this script drastically to take advantage of the greater speed
# offered by passing all arguments to hope at once.
#
# Revision 1.3  1995/02/23  17:17:02  jont
# Modify for new file structure /u
#
# Revision 1.2  1995/02/21  15:47:09  brianm
# Introducing SML_HOME
#
# Revision 1.1  1994/07/04  12:52:56  brianm
# new file
#
# 
