#!/bin/csh
#
# @(#)dni_insert_file.sh 7.4 91/02/11 SMI
#
#   Copyright (c) 1988 by Sun Microsystems, Inc.
#
#
#  dni_insert_file:  Cshell Script to insert one (delta) file at a specified 
#	point within a second (original) file.  If successful, the result is
#	placed in the specified output (new) File.  Both the delta and original 
#	files are unchanged.
#
#   Arguments:
#
#	Arg 1 - Path to Original File
#	Arg 2 - Path to delta file (to be inserted)
#	Arg 3 - Match String in Original file
#	Arg 4 - Set to "B" if insertion before first line where match occurs,
#		Set to "A" if insertion after last line where match occurs,
#		Set to "R" if insertion replaces line where match string occurs
#		   (in this case, there better be only 1 match)
#	Arg 5 - Path to new file (result)
#
#
#   Description:
#
#	All arguments are validated, and write access is verified to the
#  original file.  A search is then made for all lines matching the search
#  string.
#
#	The head/tail facilities are used to split the file up and (according
#  to Arg 4), insert the new data, writing to Arg 5.
#
#
#   Exit Conditions
#
#	In any case where an error in this procedure is detected, an immediate
#  exit with a negative code will be taken.  Otherwise, the total number of
#  matches found will be returned (> 0, and if Arg 4 = "R", the value will
# be 1 since all others are illegal).
#
#
unalias rm
unalias cat
unset noclobber
set errstr = "Usage: $0 orig_file delta_file match_string insert_opt out_file"
#
if ($#argv != 5) then
   echo  $errstr
   exit (-1)
endif
#
if (!(-r $1)) then
   echo "$0:  File $1 does not exist or cannot be read"
   echo  $errstr
   exit (-2)
endif
if (!(-r $2)) then
   echo "$0:  File $2 does not exist or cannot be read"
   echo  $errstr
   exit (-4)
endif
if (-e $5) then
   rm -f $5
   if ($status) then
	echo "$0:  File $5 initially exists and cannot be deleted"
	echo  $errstr
	exit (-5)
   endif
endif
#
switch ($4)
    case B:
    case b:
	@ mopt = -1
	breaksw
    case A:
    case a:
	@ mopt = 1
	breaksw
    case R:
    case r:
	@ mopt = 0
	breaksw
    default:
	echo "Illegal Insert option"
	echo $errstr
	exit (-6) 
endsw
#
#    All input arguments validate.
#	mopt 	< 0 if insertion before match,
#		> 0 if insertion after match,
#		= 0 if insertion replaces match
#
#  Variables used:
#
#	matches:	list of line #s of matches in original file (with ":")
#	match:		Line Number of match in original file
#	split		line number where match occurred
#	before:		Number of lines before match
#	after:		Number of lines after match
#
set matches = `grep  -n "$3"  $1 | awk '{print $1}'`
if ($status) then
   echo "Strange pattern match failure for +$3+ in $1"
   exit (-7)
endif
if (($#matches != 1) && (!($mopt))) then
   echo "Got $#matches matches of +$3+ in File $1   Expected 1"
   exit (-8)
endif
if (!($#matches)) then
   echo "No matches of +$3+ in File $1"
   exit (-8)
endif
#
#    At this point, $matches[1] = line_number if option B or R, and
#	$matches[$#matches] = line number if option A.
#
#	In addition, the trailing colon must be stripped.
#
if ($mopt > 0) then
   @ idx = $#matches
else
   @ idx = 1
endif
#
#
#	matches might = "29:set 51:grep ...."
#
set match = `echo $matches[$idx] | sed "s/:/ :/g"`
#
#	match might = "29 :set"
#
@ split = $match[1]
#
#	split might = "29"
#
if ($mopt > 0) then
    @ before = $split
    @ after = $split + 1
else if ($mopt < 0) then
    @ before = $split - 1
    @ after = $split
else
    @ before = $split - 1
    @ after = $split + 1
endif
#
#   Try to create new file
#
head -$before $1 > $5
if ($status) then
    echo "Failed to create output file $5"
    echo $errstr
    exit (-9)
endif
cat $2 >> $5
if ($status) then
    echo "Failed to cat file $2 to output file $5"
    echo $errstr
    exit (-10)
endif
tail +$after $1 >> $5
if ($status) then
    echo "Failed to complete output file $5 Creation .. disk full??"
    echo $errstr
    exit (-11)
endif
#
#  Success
#
exit (0)

