#!/bin/ksh
#
#--------------------------------------------------------------------------
# $Header: mcs,v 1.2.114.1 95/10/02 18:15:10 kcs Exp $
# $Source: /nfs/hpindgr/dcia1/KCS.cm/rcs/nfs_davis_800/bin/RCS/mcs,v $
#--------------------------------------------------------------------------
#
# Filename:  mcs [-b branch_name -b brach_name...] file1 <file2...>
#
# Syntax:    See syntax_display below
#
# Called by: User
#
# Reads:     $HOME/.mcsrc
#
# Description:
#	mcs is a "multiple" kcs tool.  This tools will perform a kcs
#	function, like lock or unlock on a group of branches.  
#       For example:
#	   mcs -l -b branch1 -b branch2 foo.1 and foo.2
#       will lock foo.1 and foo2 on branch and branch2.  Use the -u
#	option to unlock files on multiple branches.
#
# 	Branches can either be passed in on the command line with the 
#	-b option, or they can be read out of the $HOME/.mcsrc file.
#	A -b option on the command line will override the .mcsrc file.
#
#	An array, B_ARRAY, is used to store the branch names. Indexing
#	starts at 0.
#
# Exit Conditions:
# ----------------
#    0  - The script completed successfully
#    1  - An error occurred
#
###########################################################################
# 	FUNCTIONS START HERE
###########################################################################
#
#---------------------------------------------------------------------------
# Function: syntax_display
# Purpose:
# 	This function displays the proper syntax and exits the script
#---------------------------------------------------------------------------
function syntax_display
{
    echo "syntax: mcs [-b branch -b branch] ... file1 <file2> <file3> ..."
    echo "        Must give at least one branch name and filename."
    echo "        Branches can either be passed with the -b option"
    echo "        or specified in ${HOME}/.mcsrc"
    echo "        (If -b is used, the .mcsrc file is NOT read)"
}

#########################################################################
#	GLOBAL VARIABLES                                                #
#########################################################################
USER=`id -un`
integer FILE_NUM=1
integer BRANCH_NUM=0
integer i		#A counter used to march through the branch array

#########################################################################
#	               MAIN                                             #
#########################################################################

#########################################################################
# Go sort out the parameters.
#########################################################################
while [ "$#" -gt 0 ]
do
  case ${1} in
    -b | -B)
      shift
      B_ARRAY[$BRANCH_NUM]="${1}"
      BRANCH_NUM=BRANCH_NUM+1
      ;;

    -b* | -B*)
      B_ARRAY[$BRANCH_NUM]=`expr ${1} : "-[bB]\(.*\)"`
      BRANCH_NUM=BRANCH_NUM+1
      ;;

    -*)
      KCS_OPTS="$KCS_OPTS $1"
      ;;

     *)
      # All other parameters are filenames, so just read'em in.
      TOTAL_FILES="$#"
      FILE_LIST="$*"
      break
      ;;
  esac
  shift
done

# IFF no branch names have been passed in as parameters, attempt to
# read them out of the file .mcsrc 
if [ ${BRANCH_NUM} -eq 0 ]; then
  #Source the mcirc file to read in the branch names.  The .mcsrc
  #file contains the line BRANCHES=" " which lists out what multiple
  #branches files should be checked in on.
  if [ -f ${HOME}/.mcsrc ]; then
     . ${HOME}/.mcsrc
  fi

  #Set up an array of branch names and determine how many elements we have.
  set -A B_ARRAY `echo ${BRANCHES}`
  BRANCH_NUM=${#B_ARRAY[*]}

  #Since we read the branches out of the configuration files, ask
  #to make sure they are OK.
  if [ "${BRANCH_NUM}" -ne 0 ]; then
    echo "The following branches will be used: ${BRANCHES}"
    echo "Is this OK? (y or n) \c" > /dev/tty
    read ans < /dev/tty
    if [ "$ans" != "y" -a "$ans" != "Y" ]; then
       exit 1
    fi
  fi
fi

#Check to make sure they have listed at least one branch and one file
if [ "${BRANCH_NUM}" -eq 0 -o "${FILE_LIST}" = "" ]; then
    syntax_display
    exit 1
fi

##########################################################################
#Loop through all the files on all the branches and and call kcs with the
#options that were passed in each time, usually -l to lock or -u to unlock.
##########################################################################
for each in $FILE_LIST
do
  #Now loop through each branch starting with index 0
  i=0  #initialize the counter
  while [ $i -lt ${BRANCH_NUM} ]
  do
    kcs ${KCS_OPTS} -r${B_ARRAY[$i]} $each 

    #Check to make sure the klog didn't exit with an error.
    if [ $? != 0 ]; then
      #An error occurred, exit with an error
      exit 1
    fi
    i=i+1
  done #looping through branches
done #looping through all the files

# exit the script, call function to clean up the log files and then exit
exit 0

