#!/usr/local/bin/perl
#
# Split a file containing multiple spice "libraries" into seperate files.
# a spice library starts with a line 
#	.LIB library-name
# ane ends with a line containing:
#	.ENDL
#
# Each library is put into its own file,
# and stuff not belonging to any library at all goes to stdout.
#
# $Log: spicelibsplit,v $
# Revision 1.2  2004/05/16 19:54:15  tell
# miscellaneous updates
#
# Revision 1.1  1999/12/08 14:17:21  tell
# Moved in files from the old "hcutil" that weren't RCS'ed
#
#

use Getopt::Long;

sub usage {
	print STDERR "usage: spicelibsplit [options] [input-file]\n";
	print STDERR "options:
  -p|--prefix 		prefix used on output filenames
";
}

%optctl = ("v|verbose!"    => \$verbose,
	   "p|prefix=s"     => \$prefix
);

$prefix = "";

if(!GetOptions(%optctl)) {
	&usage();
	exit 1;
}



$inlib = 0;
while($_ = <>) {
	if($_ =~ m/^\.LIB\s+(\w+)/i) {
		$fname = $prefix . $1;
		open(OUTF, ">$fname") || die "$fname: $!";
		$inlib = 1;
		print STDERR "writing to $fname\n";
	} elsif($_ =~ m/^\.ENDL/i) {
		close(OUTF);
		$inlib = 0;
	} else {
		if($inlib) {
			print OUTF $_;
		} else {
			print $_;
		}
	}
}
if($inlib) {
	printf STDERR "missing .ENDL noticed at end of input\n";
	close(OUTF);
}
