#!/usr/bin/perl

# rudimentary processing of command line input
# use first 2 arguments for input/output discard the rest

$argc=@ARGV;
if ($argc == 0) {
	usage();
}
else {
	open(INFILE, $ARGV[0]) || die "Can't open <infile>: $ARGV[0]\n";
	$infile = $ARGV[0]; 	#backup if seek does not work
	if ($ARGV[1])	{
		open(OUTFILE, ">$ARGV[1]") || die "Can't open [outfile]: $ARGV[1]\n";
        select OUTFILE; 	# Make default output. Cludge to have the choice of either
							# a file or STDOUT as output handle, since I am unable to 
							# reopen STDOUT or duplicate this filehandle
	}
}



sub usage {
	print STDERR "\nUsage: remove_17.pl <infile> [outfile]\n";
    print STDERR "Utility program to remove object #17 from raster3d input code\n";
	print STDERR "\t<infile> : valid raster3D input file, w/o standard header!\n";
	print STDERR "\t[outfile] : optional output file name, defaults to screen\n";
    exit;
}

sub check_input	{
# try to decide if we are dealing with a standard raster3d header, a r3d file w/o header
# or just plain illegible junk.
# Return values:
# 	header : standard header
#   r3d :    potential r3d file w/o header
#   junk:    guess what?
}


# assume r3d detected

while (<INFILE>)	{
	if (/^@/)	{
		print;		# keep File indirect reads
		next;
	}
	if (/^#/)	{
		print;		# keep comments
		next;
	}
	next if /^$/;       # discard empty lines
	if ( $_ !~ /^17$/ )	{		# match everything except 17
		print $_;
		$_ = <INFILE>;			# print next line unaltered
		print  $_;
	}
	else {					# 17 detected 
		$_ = <INFILE>;      # discard 17 and read next line, which will be discarded by while(<INFILE>)
	}
}
