#!/usr/bin/perl
#
# dehenselize -- convert Hensel-archive LIF files to Xlife format
#
# Here is Al Hensel's description of LIF format from the Hensel archive:
#
# Niggly Technical Details of the .LIF file format:
# -------------------------------------------------
# 
# The first line of a .LIF file always identifies the kind of format
# it uses, for compatibility purposes.  The standard is:
# 
# #Life 1.05
# 
# where 1.05 is the current format version number.  A lower number is
# acceptable, and a higher number means that something may be outside
# these specifications.  1.05 is the latest.
# 
# The "#Life" line is followed by optional description lines, which
# begin with "#D" and are followed by no more than 78 characters of
# text.  Leading and trailing spaces are ignored, so the following two
# "#D" lines are equivalent:
# 
# #D This is a Description line
# #D    This is a Description line   
# 
# There should be no more than 22 "#D" lines in a .LIF file.
# 
# Next comes an optional rule specification.  If no rules are
# specified, then the pattern will run with whatever rules the Life
# program is currently set to.  The patterns in the collection here
# enforce "Normal" Conway rules using the "#N" specifier.  Alternate
# rules use "#R" ("#N" is exactly the same as "#R 23/3").
# 
# Rules are encoded as Survival/Birth, each list being a string of
# digits representing neighbor counts.  Since there are exactly eight
# possible neighbors in a Conway-like rule, there is no need to
# separate the digits, and "9" is prohibited in both lists.
# 
# For example,
# 
# #R 125/36
# 
# means that the pattern should be run in a universe where 1, 2, or 5
# neighbors are necessary for a cell's survival, and 3 or 6 neighbors
# allows a cell to come alive.
# 
# Next come the cell blocks.  Each cell block begins with a "#P" line,
# followed by "x y" coordinates of the upper-left hand corner of the
# block, assuming that 0 0 is the center of the current window to the
# Life universe.
# 
# This is followed by lines that draw out the pattern in a visual way,
# using the "." and "*" characters (off, on).  Each line must be
# between 1 and 80 characters wide, inclusive; therefore, a blank line
# is represented by a single dot, whereas any other line may truncate
# all dots to the right of the last "*".  There is no limit to the
# number of lines in a cell block.
# 
# Any line of zero length (just another carriage return) is completely
# ignored.  Carriage returns are MSDOS-style (both 10 and 13).
# 
# About Xlife compatibility:
# --------------------------
# Xlife recognizes the symbol "#C" for a comment, instead of "#D".
# The default extension is ".life" instead of ".LIF".  Wlife (a port
# of Xlife for Microsoft Windows) does not have these compatibility
# problems.
# 
# --------------------------------------------------------------------
#

foreach $f (@ARGV)
{
	next unless ($f =~ /LIF$/);

	$uf = $f;
	$uf =~ tr/A-Z/a-z/;
	$uf =~ s/lif$/l/;

	print "$f -> $uf\n";
	open(IN, "<$f");
	open(OUT, ">$uf");
	print OUT "##Life5\n#C This pattern was $f in the Hensel archive\n";
	while ($_ = <IN>)
	{
		next if /^#Life 1.05/;

		s/\r//;
		s/#N//;
		s/^#D/#C/;
		s/^#R/#T/;
		print OUT $_
	}
	close(IN);
	close(OUT);
}
