#!/usr/bin/awk
# (c) Copyright 1995, 1996, 1997 James H. Lowe, Jr.
#
# Distribution terms: 
#      Free Software Foundation General Public License (GPL)
#
#$Id: shak0_qfilter_v 1.17 1997/03/23 16:11:56 jhl Exp $


# Reads header from stdin and prints those that match all
# Re's that are the arguments below.

# Command line arguments. ARGV []
# 0: Prog name			shak_prog 
# 1: outputformat		0=no formatting; 1=format style 1; 2=format style 2
# 2: allinclusive 		0=only-backup-headers  1=all-headers (0 default)
# 3: ascii date 		shak_date
# 4: least calendar time 	shak_timel
# 5: greatest calendar time	shak_timeg
# 6: host			shak_host
# 7: type			shak_type
# 8: backhost			shak_backhost
# 9: rcsfile			shak_rcsfile
# 10: rev			shak_rev
# 11: tape			shak_tape
# 12: headeruse			shak_headeruse
# 13: inactive			shak_inactive
# 14: user			shak_user
# 15: filelbl			shak_filelbl
# 16: logname			shak_logname
# 17: exit status
# 18: state
# 19: secureheaders              
# 20: filtertimel               shak_filtertimel
# 21: filtertimeg               shak_filtertimeg

BEGIN {
	parse_error=0
	debug=0
	if (ARGC != 22) exit
	
	outputformat=ARGV[1]
	allheaders=ARGV[2]	
	shak_date=ARGV[3]
	shak_timeg=ARGV[4]
	shak_timel=ARGV[5]
	shak_host=ARGV[6]
	shak_type=ARGV[7]
	shak_backhost=ARGV[8]
	shak_rcsfile=ARGV[9]
	shak_rev=ARGV[10]
	shak_tape=ARGV[11]
	shak_headeruse=ARGV[12]
	shak_inactive=ARGV[13]
	shak_user=ARGV[14]
	shak_filelbl=ARGV[15]
        shak_logname=ARGV[16]	
	shak_exitstatus=ARGV[17]
	shak_state=ARGV[18]
	secureheaders=ARGV[19]
	shak_filtertimel=ARGV[21]
	shak_filtertimeg=ARGV[20]
	
	if ( allheaders == "" ) {
		allheaders=0
	}
	
	for (i = 1; i < ARGC; i++)
		ARGV[i]=""
}


function parseheader ( hf, wholeline ) {

	hf["k_date"] = hf["k_time"] = hf["k_host"] = hf["k_type"] = hf["k_backhost"] = hf["k_rcsfile"] = hf["k_header"] =\
	hf["k_rev"] = hf["k_tape"] = hf["k_headeruse"] = hf["k_inactive"] = hf["k_user"] = hf["k_filelbl"]=\
	hf["k_exitstatus"]=hf["k_state"]=hf["k_filtertime"]=wholeline
	
	subst=0
	
	subst+=gsub (/^File /,"",hf["k_filelbl"])
	subst+=gsub (/:__SHAK__.*$/,"",hf["k_filelbl"])
	
	subst+=gsub (/__CALT__.*$/,"",hf["k_date"]) 
	subst+=gsub (/^F.*__SHAK__/,"",hf["k_date"]) 

	subst+=gsub (/^F.*__CALT__/,"",hf["k_time"]) 
	subst+=gsub (/__SHAK__.*/,"",hf["k_time"]) 

	if ( hf["k_filelbl"] != 0 ) {
		subst+=gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__/,"",hf["k_host"]) 
		subst+=gsub (/:.*/,"",hf["k_host"]) 

		subst+=gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__[^:]*:/,"",hf["k_type"]) 
		subst+=gsub (/:.*/,"",hf["k_type"]) 

		subst+=gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__[^:]*:[^:]*:/,"",hf["k_backhost"]) 
		subst+=gsub (/:.*/,"",hf["k_backhost"]) 

		subst+=gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__[^:]*:[^:]*:[^:]*:\$Id: /,"",hf["k_rcsfile"])
		subst+=gsub (/ .*/,"",hf["k_rcsfile"]) 

		subst+=gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__[^:]*:[^:]*:[^:]*:\$Id: [^ ]* /,"",hf["k_rev"])
		subst+=gsub (/ .*/,"",hf["k_rev"]) 
		hf["k_header"]=""
	} else {
		hf["k_host"]=hf["k_type"]=hf["k_backhost"]=hf["k_rcsfile"]=hf["k_rev"]=""	
		subst+=gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__/,"",hf["k_header"])
		subst+=gsub (/__TAPE_NO__.*$/,"",hf["k_header"]) 
		subst+=8
	}
	subst+=gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__.*__TAPE_NO__/,"",hf["k_tape"])
	subst+=gsub (/__.*$/,"",hf["k_tape"]) 

	gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__.*__TAPE_NO__....__/,"",hf["k_headeruse"])
	gsub (/:.*$/,"",hf["k_headeruse"]) 
	if (hf["k_headeruse"] == "SHAK__" || hf["k_headeruse"] == "" ) {
		hf["k_headeruse"]="yes"
	}
	subst+=2
	
	gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__.*__TAPE_NO__....__[^:]*:[^:]*:/,"",hf["k_inactive"])
	gsub (/:.*$/,"",hf["k_inactive"]) 
	if ( hf["k_inactive"] ~ /File [0-9]|^$/ || hf["k_inactive"] == "" ) {
		hf["k_inactive"]="0"
	}
	subst+=2
	
	gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__.*__TAPE_NO__....__[^:]*:[^:]*:[^:]*:/,"",hf["k_user"])
	gsub (/:.*$/,"",hf["k_user"]) 
	if (hf["k_user"] ~ /File [0-9]|^$/ ) {
		hf["k_user"]="root"
	}
	subst+=2

	gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__.*__TAPE_NO__....__[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:/,"",hf["k_exitstatus"])
	gsub (/:.*$/,"",hf["k_exitstatus"]) 
	if (hf["k_exitstatus"] ~ /^$/ ) {
		hf["k_exitstatus"]="256"
	}
	subst+=2

	gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__.*__TAPE_NO__....__[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:/,"",hf["k_state"])
	gsub (/:.*$/,"",hf["k_state"]) 
	if (hf["k_state"] ~ /^$/ ) {
		hf["k_state"]="unset"
	}
	subst+=2

	gsub (/^Fil.*__SHAK__.*__CALT__[0-9]*__SHAK__.*__TAPE_NO__....__[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:/,"",hf["k_filtertime"])
	gsub (/__SHAK__$/,"",hf["k_filtertime"])
	if (hf["k_filtertime"] ~ /^$/ ) {
		hf["k_filtertime"]="-1"
	}
	subst+=2
	return subst

}

{ #PER LINE CODE

parse_errorline=0

if ( parseheader( hf , $0 ) != 30 ) {
	print "shak0_qfilter: parse error :" $0 | "cat  1>&2"
	parse_error++
	parse_errorline++
} else {

debug = 0
if ( debug == 1 ) {
	printf ("%s\n", $0)
	printf ("%s %s %s\n", hf["k_date"], shak_date, match (hf["k_date"], shak_date) )
	printf ("%s %s %s \n", hf["k_time"], shak_timel, hf["k_time"] <= shak_timel )
	printf ("%s %s %s \n", hf["k_time"], shak_timeg, hf["k_time"] > shak_timeg )
	printf ("%s %s %s \n", hf["k_host"], shak_host, match (hf["k_host"], shak_host) )
	printf ("%s %s %s \n", hf["k_type"], shak_type, match (hf["k_type"], shak_type) )
	printf ("%s %s %s \n", hf["k_backhost"], shak_backhost, match (hf["k_backhost"], shak_backhost) )
	printf ("%s %s %s \n", hf["k_rcsfile"], shak_rcsfile, match (hf["k_rcsfile"], shak_rcsfile) )
	printf ("%s %s %s \n", hf["k_rev"], shak_rev, match (hf["k_rev"], shak_rev) )
	printf ("%s %s %s \n", hf["k_tape"], shak_tape, match (hf["k_tape"], shak_tape ) )
	printf ("%s %s %s \n", hf["k_headeruse"], shak_headeruse, match (hf["k_headeruse"], shak_headeruse) )
	printf ("%s %s %s \n", hf["k_inactive"], shak_inactive, match (hf["k_inactive"], shak_inactive) )
	printf ("%s %s %s \n", hf["k_user"], shak_user, match (hf["k_user"], shak_user) )
	printf ("%s %s %s \n", hf["k_filelbl"], shak_filelbl, match (hf["k_filelbl"], shak_filelbl) )
	printf ("%s %s %s \n", hf["k_filtertime"], shak_filtertimel, hf["k_filtertime"] <= shak_filtertimel )
	printf ("%s %s %s \n", hf["k_filtertime"], shak_filtertimeg, hf["k_filtertime"] > shak_filtertimeg )
}

#m_date = match (hf["k_date"], shak_date)
if ( shak_date == "" ) { 
      m_date=1
} else {
      m_date = match (hf["k_date"], shak_date)
}

if ( hf["k_filtertime"] >= 0 ) { 
	if ( shak_filtertimel == "" ) {
	      m_filtertimel=1
	} else {
	      m_filtertimel =  hf["k_filtertime"] <= shak_filtertimel
	}

	if ( shak_filtertimeg == "" ) { 
	      m_filtertimeg=1
	} else {
	      m_filtertimeg =  hf["k_filtertime"] >= shak_filtertimeg
	}
} else {
      m_filtertimeg=m_filtertimel=1 
}

if ( shak_timel == "" )  
	m_timel=1
else
	m_timel = hf["k_time"] <= shak_timel

if ( shak_timeg == "" )  
	m_timeg=1
else
	m_timeg = hf["k_time"] >= shak_timeg

m_time = m_timel *  m_timeg
m_filtertime = m_filtertimel * m_filtertimeg
"shak_host"  ? m_host = hf["k_host"] ~ shak_host : m_host=1
"shak_type"  ? m_type = hf["k_type"] ~ shak_type : m_type=1
"shak_backhost" ? m_backhost = hf["k_backhost"] ~ shak_backhost : m_backhost=1
"shak_rcsfile" ? m_rcsfile = hf["k_rcsfile"] ~ shak_rcsfile : m_rcsfile=1
"shak_rev" ? m_rev = hf["k_rev"] ~ shak_rev : m_rev=1
"shak_tape" ? m_tape = hf["k_tape"] ~ shak_tape : m_tape=1
"shak_headeruse" ? m_headeruse = hf["k_headeruse"] ~ shak_headeruse : m_headeruse=1
"shak_inactive" ? m_inactive = hf["k_inactive"] ~ shak_inactive : m_inactive=1
"shak_user" ? m_user = hf["k_user"] ~ shak_user : m_user=1
"shak_filelbl" ? m_filelbl = hf["k_filelbl"] ~ shak_filelbl : m_filelbl=1
"shak_exitstatus" ? m_exitstatus = hf["k_exitstatus"] ~ shak_exitstatus : m_exitstatus=1
"shak_state" ? m_state = hf["k_state"] ~ shak_state : m_state=1


if ( secureheaders == yes && shak_logname != "root" ) {
     m_authuser = ( hf["k_user"] ==  shak_logname )
} else {
     m_authuser=1
}

#print  m_date ":" m_time ":" m_host ":" m_type ":" m_backhost ":" m_rcsfile ":"  m_rev ":" m_tape ":" m_headeruse ":" m_inactive ":" m_user ":" m_filelbl ":" m_authuser ":" m_exitstatus ":" m_state 

if ( (( m_date * m_time * m_host * m_type * m_backhost * m_rcsfile * m_filtertime * \
     m_rev * m_tape * m_headeruse * m_inactive * m_user * m_filelbl * m_authuser * m_exitstatus * m_state ) > 0 ) && parse_errorline >= 0 ) {
	
	if (outputformat == 0) {
		print $0
	} 
	if (outputformat == 1) {
		if ( hf["k_headeruse"] == "yes" ) {
			fsf_count=hf["k_filelbl"] + 1
		} else {
			fsf_count=hf["k_filelbl"]
		}
	
		if ( hf["k_filelbl"] == 0 ) {
			if ( allheaders == 1 ) {

			print hf["k_tape"] ":" hf["k_filelbl"] ":" fsf_count " " hf["k_time"] " "  "\""hf["k_date"]"\"" " " \
		        "\""hf["k_header"]"\"" " " hf["k_user"] " " hf["k_inactive"] " " hf["k_exitstatus"] " " hf["k_state"] " " hf["k_filtertime"]
			
			}
		} else {

			print hf["k_tape"] ":" hf["k_filelbl"] ":" fsf_count " " hf["k_time"] " "  "\""hf["k_date"]"\"" " " \
		        hf["k_backhost"]  " " hf["k_type"]  " " hf["k_rcsfile"] " " hf["k_rev"]  " " hf["k_host"] " " hf["k_user"] " " \
			hf["k_inactive"] " " hf["k_exitstatus"] " " hf["k_state"] " " hf["k_filtertime"]

		}
	
	}

}
}

} # END OF PER LINE CODE


END {
	if ( parse_error > 0 )
	  print "shak0_qfilter: parse error" | "cat  1>&2"
	exit parse_error
}


