#!/usr/bin/perl
#
# geninstallfiles 1.0
# Copyright (C) 1999 Stephen Rust <steve@tp.org>
#
# Parses the output from make -n uninstall, 
# forming a script which will list all of the files 
# that are installed for the package.
#
# Package must use the GNU automake/autoconf system.

$ignoreline="off";

if (!@ARGV) {
	$file = "geninstallfiles.sh";
}
else {
	$file = $ARGV[0];
}

@lines = `make -n uninstall`;

open(FILEH, ">$file");
print FILEH "# This script created from geninstallfiles\n";
print FILEH "# Copyright (C) 1999 Stephen Rust <steve\@tp.org>\n";
print FILEH "# Do not edit this file, re-run geninstallfiles instead\n\n";
print FILEH "autofileecho=\"echo\"\n";
chmod 0755, $file;
foreach $line (@lines) {

	# do the ignore
	if ($ignoreline eq "for") {
		if ($line =~ /^done/) {
			$ignoreline="off";
		}
		next;
	}

	# do the ignore
	if ($ignoreline eq "if") {
		if ($line =~ /^fi/) {
			$ignoreline="off";
		}
		next;
	}

	# ignore echos from make
	if ($line =~ /^Making/ or $line =~ /^make/) {
		next;
	}

	# ignore if commands
	if ($line =~ /^if/) {
		$ignoreline="if";
		next;
	}

	# ignore subdir commands
	if ($line =~ /for subdir/) {
		$ignoreline="for";
		next;
	}

	# replace the rm -f lines with an echo
	$line =~ s/rm -f/\$autofileecho/g;

	# remove unneeded chars from the echo line
	$line =~ s/(.*)(\$autofileecho.*)/$2/;

	# print the line to the geninstall script
	print FILEH "$line";
}
close(FILEH);

