#! /usr/bin/perl
# $Header: maketoc,v 1.1 89/01/28 07:53:24 viktor Exp $
#
# Author: Viktor Dukhovni, 
#         Math. Dept.  
#         Princeton University.
#
# $Log:	maketoc,v $
# Revision 1.1  89/01/28  07:53:24  viktor
# Initial revision
# 
# 

#  Parsing something like this 
#  loop until see #0,  grab the level.
#  loop until see #1,  grab the mount point name,  compare with first arg. 
#  loop until see #2,  the first dump pass should be pass III,  start timer.
#  loop until see #3,  stop timer
#  dump rest of input.
#
#------------------------------------------------------------------
#  DUMP: Date of this level 5 dump: Thu Jan 19 04:21:23 1989		#0
#  DUMP: Date of last level 0 dump: Sat Jan 14 03:06:29 1989
#  DUMP: Dumping /dev/rxy3g (/a) to /dev/nrrt0 on host cucumber		#1
#  DUMP: mapping (Pass I) [regular files]
#  DUMP: mapping (Pass II) [directories]
#  DUMP: mapping (Pass II) [directories]
#  DUMP: mapping (Pass II) [directories]
#  DUMP: estimated 11909 tape blocks on 0.01 tape(s).
#  DUMP: dumping (Pass III) [directories]		 		#2
#  DUMP: dumping (Pass IV) [regular files]
#  DUMP: 97.87% done, finished in 0:00
#  DUMP: DUMP: 11802 tape blocks on 1 tape(s)
#  DUMP: DUMP IS DONE							#3
#  DUMP: level 5 dump on Thu Jan 19 04:21:23 1989
#  DUMP: Tape rewinding
#-----------------------------------------------------------------

#  name of directory being dumped,  for sanity.
#  currently abort if we see something else.
$DIR = shift ;

select(stderr)	;# as default filehandle for print
$|=1		;# make it unbuffered

#  If no command line hostname
#  use name of local machine (with trailing new line chopped)
if ( ! ($host = shift) ) {
	$host = `hostname`;
	chop($host);
}

#  take a filename to read from
#  else read stdin
if ( $file = shift ) {
	open(input,"<$file") ;
} else {
	open(input,"-") ;
}

#  Loop 0
#  Detect first line of dump stderr messages,  and remember the level.
while(<input>) {
	print ;
	if ( m#^  DUMP: Date of this level (.) dump.*# ) {
		$level = $1 ;
		$date=`date +%y.%m.%d.%H.%M` ;
		last ; # i.e. break ;
	} ;
} ;
#  Loop 1
#  find mount point name and date of dump. 
#  On line with "Dumping"
#
while(<input>) {
	print ;
	if( m#^  DUMP: Dumping /dev/[^ ]* \(([^)]*)\) .*# ) {
		$dir=$1 ;
		last ;
	};
};
print "MAKETOC: warning: dumping $dir,  expected $DIR\n" if ( $dir ne $DIR ) ;

#  Loop 2
# If dump produces any output (on the tape!) 
# we should see  Pass III 
# If not the tape could not be opened  and no file was written
# In which case we can exit without appending to the  Table of contents
#
while(<input>) {
	print ;
	if( m#^  DUMP: dumping.*\(Pass ([^)]*)\).*# ) {
		$pass=$1 ;
		last ;
	};
};
if ($pass ne "III") {
	while(<input>) {
		print ;
	}
	exit(1) ;
} ;
#  Start the timer
$start = time ;

#  Loop 3
#
while(<input>) {
	print ;
	if( m#.*DUMP: ([0-9]*) tape blocks.*# ) {
			$bytes=$1*1024;
	}
	if( /  DUMP: DUMP IS DONE/ ) {
			$ok=1 ;
			last ;
	};
};
if ( $ok ) {
	#  Hooray good dump! ---
	#  NB $date is new line terminated here.  
	#  `` gives literal output in perl
	#  MAKE SURE THE DATE IS IN THE FOURTH FIELD !
	#  The scripts rely on it!
	#
	printf stdout "%s %s %s %.14s %s dump\n",$host,$dir,$level,$date,$bytes;
	$status=0 ;
} else {
	#  Write a dud TOC entry.
	#  (Can one open the drive for writing with a read protected
	#  tape inside?  If so,  the dump will abort after outputting
	#  the pass III banner,  but without writing any data or file
	#  marks) Can this be handled correctly?  Assume it won't
	#  happen for now.
	#  MAKE SURE THE DATE IS IN THE FOURTH FIELD !
	#  The scripts rely on it!
	printf stdout "%s %s %s %.14s 0 dump\n",$host,$dir,"FAILED",$date ;
	$status=1;
};
#  
$elapsed = time - $start ;
print "MAKETOC:  $elapsed seconds from start of dump pass III\n" ;
#
#  Lets finish reading (and echoing to stderr) the input.
#
while(<input>) {
	print ;
};
exit($status) ;
