#!/usr/bin/perl

my $readFile = "testFifo"; # Or: "/dev/stty0" or what have you
my $startTag = "START";
my $endTag = "END";
my $dumpFile = "dump";

# Don't touch the variables below this line
my $DUMPFD = -1;

sub closeFile() {
	close(DUMPFD);
	$DUMPFD = -1;
}

sub openFile {
	if ($DUMPFD == -1) {
		closeFile();
	}

	open(DUMPFD, ">", $dumpFile) || die "Can't open $dumpFile for writing";
	$DUMPFD = 1;
}

open($READFD, "<", $readFile) || die "Can't open $readFile for reading";
while (<$READFD>) {
	if (/^$startTag$/) {
		openFile();
		print "File opened\n";
	} elsif (/^$endTag$/) {
		if ($DUMPFD == -1) {
			print STDERR "Got close tag before start tag\n";
		} else {
			closeFile();
			print "File closed\n";
		}
	} elsif ($DUMPFD != -1) {
		print DUMPFD $_;
	} else {
		print "No start tag yet, ignoring input\n";
	}
}

print "EOF caught, exiting\n";

exit 0;
