eval 'exec ${HYDRA_PERL_PATH:=/usr/dist/exe}/perl -S $0 $*'
if 0;
################################################################################
#
#       incbring 1.2 09/05/96
#
#	Incremental bringover for the last "n" days.
#
#	usage:
#		incbring
#		incbring <n>
#
#       If it is before 8 am, it is considered to be 11:59 the previous day.
#	The default for n is 1.
#	n=1 gives today's changes (or yesterday's and today's if before 8 am).
#
#	Limitations:  this program does not check the individual files.  It
#	relies entirely on the history file in the parent of the workspace it
#	is executed from.  Thus, it will not pick up changes that were made
#	directly in the parent.  Only those changes that have been properly
#	put back will be made.
#
#	If filenames contain characters that are special characters in regular
#	expressions, then modifications to the lines that set newrec2 will need
#	to be made.
#
# THIS SAMPLE PROGRAM IS BEING PROVIDED "AS IS" AND ONLY AS A COURTESY TO
# THE RECIPIENT.  SUN MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS 
# OR IMPLIED WITH RESPECT TO THIS SAMPLE PROGRAM INCLUDING QUALITY, PERF- 
# FORMANCE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON- 
# INFRINGEMENT.  IN NO EVENT WILL SUN BE LIABLE FOR ANY DIRECT, INDIRECT,  
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR  
# INABILITY TO USE THIS SAMPLE PROGRAM.
################################################################################


################################################################################
#	Get the number of days, if present
################################################################################

$report_days = 1 ;
if ($#ARGV == 0) {
	$report_days = $ARGV [0] ;
	if ( $report_days < 0 || $report_days > 100) {
		die ("Number of days is limited to 0-100.\n") ;
	}
}
elsif ($#ARGV > 0) {
	die ("Usage:  $0 [n]\n") ; 
}

$months   = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" ;
$days [0] = "000 031 059 090 120 151 181 212 243 273 304 334" ;
$days [1] = "000 031 060 091 121 152 182 213 244 274 305 335" ;

$today = `date` ;
($x1, $month, $day, $time, $pdt, $year) = split (" ", $today, 6) ;
$leap1 = 0 ;
$leap1 = 1   if (($year % 4) == 0) ;
$month1num = index ($months, $month) ;
$thismonth1 = substr ($days [$leap1], $month1num, 3) ;
($hour, @other) = split (":", $time) ;
$report_days += 1 if (($hour < 8) || ($report_days == 0)) ;

################################################################################
#	Obtain name of parent workspace and open its history file
################################################################################
open (FILE, "< ./Codemgr_wsdata/parent") ||
	die ("Parent file (./Codemgr_wsdata/parent) not found:  $!\n") ;
$parent = <FILE> ;
chop ($parent = <FILE>) ;
close (FILE) ;
open (FILE, "< $parent/Codemgr_wsdata/history") ||
	die ("Cannot open history file $parent/Codemgr_wsdata/history:  $!\n") ;

################################################################################
#	Extract info from history file.
################################################################################
$bringover_list = "" ;
$found_date = 0 ;
$in_comment = 0 ;
while ($record = <FILE>) {

	########################################################################
	#	Skip all records until proper date is found
	########################################################################
	if ($found_date == 0) {
		if ($record =~ m#^START#) {
			($start, $weekday, $month2, $day2, $time, $year2, 
				@other) = split (" ", $record) ;
			$found_date = &check_date ;
			$bringover_list = "" if ($found_date == 0) ;
		}
	}

	########################################################################
	#	Remove old names of renamed files from update list
	########################################################################
	if ($record =~ m#^BEGIN COMMENT#) {
		$in_comment = 1 ;
	}
	elsif ($record =~ m#^END COMMENT#) {
		$in_comment = 0 ;
	}
	elsif ($in_comment == 1) {
		# Skip this record
	}
	elsif ($record =~ m#^rename from:#) {
		($temp, $newrec) = split (":", $record) ;
		$newrec2 = $newrec ;
		$newrec2 =~ s/\+/\\\+/g ;
		$bringover_list =~ s#$newrec2## ;
	}

	########################################################################
	#	Get the new files into an update list (eliminating duplicates)
	########################################################################
	elsif (($record =~ m#^create:#) || ($record =~ m#^update:#) ||
	    ($record =~ m#^\s+to:#)) {
		($temp, $newrec) = split (":", $record) ;
		$newrec2 = $newrec ;
		$newrec2 =~ s/\+/\\\+/g ;
		$bringover_list .= $newrec if ($bringover_list !~ m#$newrec2#) ;
	}
}

################################################################################
#	Do the bringover
################################################################################
if ($bringover_list eq "") {
	print "Nothing to bring over from $parent.\n" ;
}
else {
	$bringover_list =~ s#\n# #g ;
	system ("bringover $bringover_list") ;
}

exit 0 ;


################################################################################
#	Subroutine to compute the number of days between the time this script
#	started to run and the date in the history file.
################################################################################

sub check_date {
	local ($leap2) = 0 ;
	$leap2 = 1   if (($year2 % 4) == 0) ;
	$month2num = index ($months, $month2) ;

	if ($month1num < $month2num) {
		$add_year = 365 ;
		if (($leap1 > 0) && ($month1num > 3)) {
			$add_year = 366 ;
		}
		if ($year <= $year2) {
			print "ERROR IN DATE:  This report " ;
			print "date < summary date.\n" ;
		}
		elsif ($year - $year2 > 1) {
			$add_year += 365 ;
		}
	}
	else {
		if ($year > $year2) {
			$add_year = 365 ;
		}
		else {
			$add_year = 0 ;
		}
	}

	$thismonth2 = substr ($days [$leap2], $month2num, 3) ;
	$days_ago = $day - $day2 + $add_year + $thismonth1 - $thismonth2 ;
	return 1 if ($days_ago < $report_days) ;
	return 0 ;
}
