#!/usr/bin/perl -w
#
# fusion figlet - running figlet text using fusionterm
#
# author: Max-Gerd Retzlaff <m.retzlaff@gmx.net>
#                           <mgr@hannover.ccc.de>
#
# date:    $Date: 2002/08/02 21:53:57 $
# version: $Revision: 1.1 $
#
# GnuPG-Information:
#  Fingerprint: 49CD 21F2 41AC 72C5 D0D1  27DC C2B2 48AE 8123 9F12
#  pub  1024D/81239F12 2002-03-12 Max-Gerd Retzlaff <m.retzlaff@gmx.net>
#  uid                            Max-Gerd Retzlaff <mgr@hannover.ccc.de>
#  sub  4096g/63E36E39 2002-03-12
#
# This application 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 application 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111 USA.  

use termfusion;
set_clients_from_file("clients.fusion");
connect_clients();
$SIG{'INT'} = sub {exit();};
show_clients_info();
my ( $termcols, $termrows ) = get_global_dimensions();

my $figlet = "/usr/bin/figlet";
my $figlet_options = "-w 1000000"; # $termcols";
my $font = "figletfont/univers";
my $text = $ARGV[0] || "this is fusion figlet";
my $figtext = qx($figlet -f $font $figlet_options $text);
$| = 1;
my @figarray = ();
my ( $x, $y, $xmax, $ymax ) = ( 0, 0, 0, 0 );

foreach my $line ( split( "\n", $figtext ) ) {
    foreach my $char ( split( "", $line ) ) {
	$figarray[$y]->[$x] = $char;
	$xmax = $x if ( $x > $xmax );
	$x++;
    }
    $x = 0;
    $ymax = $y if ( $y > $ymax );
    $y++;
}

clrscr;
printallcenter( "\e[?25l" );

my $xswab = int( ( $termcols - $xmax ) / 2 );

for( my $yswab = $termrows + $ymax; ; $yswab = ( $yswab < 0 - $ymax )?$termrows+$ymax:$yswab-1 ) {

    for( $y = 0; $y <= $#figarray; $y++ ) {
	for( $x = 0; $x <= $#{ $figarray[$y] }; $x++ ) {
	    $char = $figarray[$y]->[$x];
	    $prevchar = $figarray[$y+1]->[$x];
	    $preprevchar = $figarray[$y+2]->[$x];
	    $prevchar = " " if ( !defined( $prevchar ) );
	    $preprevchar = "x" if ( !defined( $preprevchar ) );
	    printxy( $x + $xswab, $y + $yswab + 2, " ")
		if ( ( ( ( $prevchar ne " " ) && ( $preprevchar eq " " ) )
		       || ( $y == $termrows ) )
		     && ( 0 <= $y + $yswab + 2 )
		     && ( $y + $yswab + 2 <= $termrows ) );
	    printxy( $x + $xswab, $y + $yswab, $char)
		if ( ( $char ne " " )
		     && ( 0 <= $y + $yswab )
		     && ( $y + $yswab <= $termrows ) );
	    # print "-$prevchar-$char-\n";
	    # print $char;
	}
	# print "\n";
    }
    
    # select( undef, undef, undef, 0.3 );
    # clrscr();
    # <STDIN>;
}

END {
    printallcenter( "\e[?25h" );
    clrscr();
    home();
    disconnect_clients();
 }
