#!/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="co"
STATUS=0
local="";reason="";bug_id="";test="";date="-ci-date";branch="";claim="";noisy="";item="";items="";tmp="";lastdir="";recursive="";
usage="usage: $script_name [-[zRtdh]+] [-(n|s|x)] [-b <branch>] [-r <reason>] [-i <bug-id>] <file>+"
while getopts "Rznlb:tr:xsi:dh" opt; do
  case $opt in
    l) local="-local";;
    n) noisy="-working-file skip -missing-dir create -extra-files delete";;
    b) branch="-branch $OPTARG";;
    t) test="test";;
    r) reason="$OPTARG";;
    x) claim="-claim hard";;
    s) claim="-claim soft";;
    i) bug_id="-bug-number $OPTARG";;
    d) date="";;
    z|R) recursive="-recursive";;
    h|\?)
      echo $usage; 
      echo "    h for this help"
      echo "    b <branch>"
      echo "    t run in hope test mode"
      echo "    l to run hope locally"
      echo "    r <claim reason>"
      echo "    i <bug-id>"
      echo "    s soft claim"
      echo "    x hard claim"
      echo "    z zzz... $cmd recursively on subcompounds"
      echo "    R same as z"
      echo "    n less dialogs from hope during unclaimed [recursive] checkouts only"
      echo "    d use current date"
      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
elif [ -n "$claim" -a -z "$bug_id" ]; then
  echo "warning no bug-id given"
fi


items=""
lastdir=""
for i in $*
do
  if [ $i = '/' ]
   then
     echo $script_name: ignoring /
   else
     # strip trailing /
     tmp=`expr match $i '\(.*[^/]\)'`
     # find the compound
     eval `findcomp $tmp`
	# defines $dir, $compound, $unit
     if [ -z "$compound" ]
     then
       echo $script_name failed : .compound file not found
       exit 3
     elif [ ! -z "$lastdir" -a "$lastdir" != "$dir" ]
     then
       echo $script_name failed : incompatible root directories $dir and $lastdir
       exit 4
     else
       if [ "$unit" = "." ]
       then
         item="-compound $compound"
       else
         item="-compound $compound -unit $unit"
       fi
       if [ -z "$items" ]
       then
         items="$item"
       else
         items="$items -and $item"
       fi
     fi
     lastdir=$dir
   fi
done

cd $dir
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 "$items" ]; then
  echo skipped everything
elif [ -z "$claim" ]; then # no claims
  echo $test $cmd $local $date $recursive $bug_id $branch $noisy $items > $temp_file
  hope source $hope_temp_file
  rm $temp_file
elif [ -n "$reason" ]; then # reason exists
  echo $test $cmd $local $date $recursive $bug_id -reason "$reason" $branch $noisy $claim $items > $temp_file
  hope source $hope_temp_file
  rm $temp_file
else
  echo $test $cmd $local $date $recursive $bug_id $branch $claim $items > $temp_file
  hope source $hope_temp_file
  rm $temp_file
fi

###############################################################################
# $Log: cout,v $
# Revision 1.29  1999/03/24 15:31:21  daveb
# [Bug #190541]
# Check both the OS and OSTYPE environment variables.
#
# Revision 1.28  1998/10/07  12:42:39  jont
# [Bug #70179]
# Avoid OSTYPE=linux problems
#
# Revision 1.27  1998/09/24  14:36:29  jont
# [Bug #70179]
# Make sure temp file name more carefully created to avoid drive letter problems
#
# Revision 1.26  1998/08/25  14:43:55  jont
# [Bug #70162]
# Use source files to get past hope 44 argument limit under Win32
#
# Revision 1.25  1997/04/15  12:00:51  jont
# Stop using script_path mechanism as this confuses Win32
#
# Revision 1.24  1996/10/18  14:03:05  io
# [Bug #1671]
# add back recursive option
#
# Revision 1.23  1996/09/11  10:28:33  io
# forgot to put in \$claim in the argument
#
# Revision 1.22  1996/09/04  14:48:25  io
# use of "-filename" in cin/cout scripts is causing an interaction
# with hope such that different .version files could contain
# the same units. I will restore to the old mechanism and lose
# the ability to handle compounds
#
# Revision 1.21  1996/07/12  12:37:44  io
# [Bug #1463]
# typo in help
#
# Revision 1.20  1996/07/11  17:42:55  io
# [Bug #1463]
# add support for directories and bug-ids,
# changes in options for:
# hard claims via "-x"
# recursive via "-z"
# current date via "-d"
# see "-h" for other options
#
# Revision 1.19  1996/01/24  10:41:25  matthew
# Adding option -n for use current date (n for now)
#
# Revision 1.18  1995/04/12  09:57:38  daveb
# Added -r (recursive) option.
#
# Revision 1.17  1995/03/20  11:14:59  daveb
# Changed this script drastically to take advantage of the greater speed
# offered by passing all arguments to hope at once.
#
# Revision 1.16  1995/02/28  12:51:22  jont
# Modify use of version (deprecated) to branch
#
# Revision 1.15  1995/02/23  17:19:13  jont
# Modify for new file structure /u
#
# Revision 1.14  1995/02/21  15:47:09  brianm
# Introducing SML_HOME
#
# Revision 1.13  1994/06/22  13:49:48  nickh
# New hope switches.
#
# Revision 1.12  1994/06/14  14:37:42  jont
# Add path setting code
#
# Revision 1.11  1994/06/09  15:34:48  nickh
# Better .compound awareness.
#
# Revision 1.10  1994/04/05  11:22:11  daveb
# Now handles trailing / in arguments.
#
# Revision 1.9  1994/03/02  12:42:22  daveb
# Revamping to take file names as arguments.
#
# Revision 1.8  1994/02/03  17:04:24  daveb
# Converted to hope.
#
# Revision 1.7  1993/01/15  14:53:40  richard
# Added -d option and corrected option passing.
#
# Revision 1.6  1991/12/24  09:38:37  richard
# Updated to use RCS 5.6 commands.  Much better!
#
# Revision 1.5  1991/11/20  15:39:35  jont
# Changed to use cat RCS/<file>,v rather than rlog file. Faster and simpler
#
# Revision 1.4  91/09/06  16:52:04  jont
# Added whoami to name constructed for /tmp to avoid conflicts
# 
# Revision 1.3  91/06/21  12:36:58  jont
# Changed to use program for writing time stamp in order to preserve
# second accuracy
# 
# Revision 1.2  91/06/13  11:29:39  jont
# Tidied up a bit
