#!/usr/bin/perl
####################################################################### 
#	this is a script which will help me develop
#	a way to strip <img> references out of html files.
#       and background statements as well.
# 
#	Jed Reynolds, Nov 1995
#######################################################################

########################################################################
# subroutine EatUp which eats up the img line and spits out the alt-line.
########################################################################
sub EatUp {

    local($mystring) = pop (@_);

    if ( $mystring =~ /alt=/i )  
    {
	#
	# get the alt Description
	#
	$mystring =~ /.*alt=\s*"(.*)"/i;
	$alt = $1;

	# 
	# get the trailing parts of the </a> if there is one
	# 
	if ( $mystring =~ /<.*a>/i)
	{
	    $mystring =~ /.*(<.*a>.*)/i;
	    $tailend = $1;
	}

	# 
	# now time to put the whole thing back together
	# 
	$mystring =~ s/<img.*>/$alt/i;
	$mystring .= $tailend;
	print DEGIFFED "$mystring\n";
    }
}

#################################################################
# Subroutine MopUp cleans up text colors and background colors
#################################################################
sub MopUp {

    #
    # search for these strings:
    #  background = "..", text = , link = , vlink = , bgcolor =
    #

    local($mystring) = pop (@_);
    $mystring =~ s/.background\s*=\s*".*"//ig;
    $mystring =~ s/.text\s*=\s*\#\w*//ig;
    $mystring =~ s/.link\s*=\s*\#\w*//ig;
    $mystring =~ s/.vlink\s*\s*\#\w*//ig;
    $mystring =~ s/.bgcolor\s*=\s*\#\w*//ig;
    print DEGIFFED "$mystring\n";
    
}

#################################################################
# begin main script
#################################################################

if ( $ARGV[0] eq "" ) { die "Must name a file to un-gif.\n"; }

foreach $infile (@ARGV) {	# open as many files as you like!
    $multiLine = 0;		# indicates a multi-line statement

    #
    # design a degiffed filename
    #
    $df_name = "text_" . $infile;
    print STDERR "Ungiffing from $infile to $df_name\n";

    #
    # open file to write to
    #
    open (  DEGIFFED, ">".$df_name );
    
    $line = 0;			# current line number
    $dobody = 0;		# for removing <body> descriptions
    $jumpout = 0;		# for jumping outta files

    #
    # mark the output file as degiffed, so we don't ever have to loop.
    #
    $DaTe = `date`;
    chop $DaTe;
    print DEGIFFED "<!-- Degiffed: $DaTe, degiffer by Jed Reynolds, Nov 1995. -->\n";

    open (INFILE, $infile);

    while (<INFILE>) {

	goto this if ($jumpout || eof);

	#
	# make sure we aren't operating on an already de-giffed file
	#
	if (/<!-- Degiffed:/) {
	    print "$line: Recursion Infinite: exiting file $infile\n";
	    $jumpout = 1;
	    goto this;
	}
	
	chop;
	$line++;
	
	#
	# continue from last line?
	
	if ($multiLine == 1) {
	    $imagestring = $imagestring ." ". $_;
	    if (/>/) {
		$multiLine = 0;
		if ($dobody) {
		    do MopUp($imagestring);
		    $dobody = 0;
		} else {
		    do EatUp($imagestring);
		}
	    }
	    next;
	}
	
	#
	# find </body and background text and link...>
	#
	if (/<body/i) {
	    $dobody = 1;
	    if ( !/<body.*>/i) {
		# time to cat a multi-line background
		$multiLine =+1;
		$imagestring = $_;
	    }
	}
	
	#
	# find <img> statements!
	#
	if  (/<img/i) {
	    
	    if ( !/<img.*>/i) {	# Time to cat a multi line string!
		$multiLine =+ 1;
		$imagestring  = $_;
	    }
	}
	elsif ($multiLine == 0)	# not dealing with an img statement
	{
	    #print "$_\n";
	    print DEGIFFED "$_\n";
	}
	else
	{
	    #$multiLine--;
	}			
    }				# end of while(<>)
  this:
    close DEGIFFED;
    close INFILE;
}
exit;

#eof










