#!/usr/bin/perl
#
# ltunzip - Lossy Text Uncompression. Perl, Unix.
#
# This program uncompresses text files that were compressed using
#  ltzip - the lossy text compression tool. Like ltzip, ltunzip is
#  also a lossy algorithm, and so performs lossy uncompression.
#  There is a high probability that the original text file will
#  NOT be returned. The result file has a ".un" extension, and 
#  the original ltz file remains.
#
# 24-Apr-2005	ver 0.70
#
# USAGE: ltunzip [-v] textfile.ltz
#    eg,
#        ltunzip -v unimportant.txt.ltz
#
#
# SEE ALSO: cat textfile.ltz > /dev/null
#
# THANKS: Michael Herman
#
# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#  (http://www.gnu.org/copyleft/gpl.html)
#
# 24-Apr-2005	Brendan Gregg	Created this.


#
#  Process Command Line Arguments
#
use Getopt::Std;
getopts('v') || &usage();
$verbose = $opt_v;
$pathname = $ARGV[0];
&usage() if $pathname eq "";


#
#  Read Text File
#
open(TEXT,$pathname) || die("ERROR1: Can't read $pathname: $!\n");
@Raw = <TEXT>;
close(TEXT);

#
#  Create Uncompressed File
#
$outfile = "$pathname.un";
if (-e "$outfile") {
	print STDERR "ERROR2: Output file $outfile already exists. Exiting.\n";
	exit(2);
}
open(OUT,">$outfile") || die("ERROR3: Can't creat $outfile: $!\n");
foreach $line (@Raw) {
	### Compress,
	$line = ltuncompress($line);
	print OUT "$line";
}
close OUT;

if ($verbose) {
	$sizeold = (-s "$pathname");
	$sizenew = (-s "$outfile");
	if ($sizenew == 0) {
		$ratio = 1;
	} else {
		$ratio = $sizeold / $sizenew;
	}
	printf("Read    : %-40s %8d bytes\n",$pathname,$sizeold);
	printf("Created : %-40s %8d bytes\n",$outfile,$sizenew);
	printf("Expanded: %.2f%%\n",(100 - ($ratio * 100)));
}

#
#  Subroutines
#
sub ltuncompress {
	my $line = shift;
	my ($newline,$char);
	@Chars = split('',$line);
	
	foreach $char (@Chars) {
		# We don't know the original location for whitespace or caps,
		# so we guess.
		if (int(rand(4)) == 0) { $newline .= " " x int(rand(2)+1); }
		if (int(rand(64)) == 0) { $newline .= "\t"; }
		if (int(rand(64)) == 0) { $newline .= "0"; }
		if (int(rand(4)) == 0) { $char =~ tr/a-z/A-Z/; }
		if (int(rand(2)) == 0) { $char =~ tr/_/-/; }
		if (int(rand(2)) == 0) { $char =~ tr/./:/; }
		if (int(rand(2)) == 0) { $char =~ tr/,/;/; }
		if (int(rand(2)) == 0) { $char =~ tr/'/"/; }
		$newline .= $char;
	}
	 
	return($newline);
}

sub usage {
	print STDERR "USAGE: ltunzip [-v] textfile\n";
	print STDERR "   eg,\n";
	print STDERR "       ltunzip unimportant.txt.ltz\n";
	exit(1);
}
