#!/usr/bin/perl -w

#<TITLE>show</TITLE>
#<META name="description" content="A slideshow perl script.">
#<META name="icon" content="/~jipp/melk/perlt.jpg,66,38">
#made by: pip@dds.nl

use Image::Size;

`xset s off`;
$SIG{INT}='handler';
my ($do)=$ENV{_};
$do =~ s!.*/!!g;
#die("call as on of sr sl slf srf fp fpl") unless ($do =~ /sr|sl|slf|srf|fp|fpl/);

*n=get();
warn ("no gfx found") unless (@n);
my $num=@n;
my ($i) = 1;
$:=1;$debug=0;
$|=1;
$p = $ARGV[0]  ;
$p='.' unless ($p);
srand(time ^ $$);



print <<GROK;
called as:\t$do
grep for:\t$p
gfx files:\t$num
GROK
while (1){
    my $t="&$do();";
    print $t if $debug;
    eval($t);
}

1;
#------

sub spc{
    my $r=shift;
    $r =~ s/\s/\\\ /g; 
    $r=~s/\(/\\\(/g;
    $r=~s/\)/\\\)/g;
    $r=~s/\&/\\\&/g;
    $r;
}
sub fa{
    *mpg=getmpg();

    $i=$mpg[rand(@mpg)];
    return unless ($i =~ /$p/);
    print "$i";
    $i=spc($i);
    print $i;
    `mtvp -z $i`;
    print "\t\tdone.\n";
}

sub fal{
    *mpg=getmpg();
    foreach $i (@mpg){
	print "$i";
	`mtvp -z $i`;
	print "\t\tdone.\n";
    }
}
sub fm{
	*mp=getmp();
	foreach $i (@mp){
		play($i);}
}



sub slas{
    use File::Find;
    $limit=10;
    $limit=$p if $p;
    $pwd=`pwd`;
    chop($pwd);
    $TOP[0]=$pwd;
    find (sub {
	return unless -f;
	return unless ($_ =~ /\.jpg/);
	$when{$File::Find::name} = (stat _)[9];
    },@TOP);
    my @names = sort { $when{$b} <=> $when{$a}; } keys %when;
    splice(@names, $limit);       # discard older stuff
    @gfx=@names;
    foreach $i (@gfx){
	show($i,'xv -root -quit +noresetroot');
    }
}

sub fmr{
	*mp=getmp();
	$i=$mp[rand(@mp)];
	play($i);
}
sub fp{
    *gfx=get();
    $i=$gfx[rand(@gfx)];
    show ($i,'xv -root -quit +noresetroot') if ($i=~/$p/i);
}

sub fpa{
    my (@ar)=@ARGV;
    $p=$ar[rand(@ar)];
    print "$p\n";
    *gfx=get();
    foreach $i (@gfx){
	show($i,'xv -root -quit  +noresetroot -maxpect')  if ($i =~ /$p/i);
    }
} 


sub fpas{
    my (@ar)=@ARGV;
    $p=$ar[rand(@ar)];
    print "$p\n";
    *gfx=get();
    foreach $i (@gfx){
	show($i,'xv -root -quit +noresetroot')  if ($i =~ /$p/i);
    }
} 

sub sq{
    my (@ar)=@ARGV;
    $p=$ar[rand(@ar)];
    print "$p\n";
    *gfx=get();
    foreach $i (@gfx){
        show($i,'qiv --root --maxpect ')  if ($i =~ /$p/i);
    }
}


sub fpf{
    *gfx=get();
    $i=$gfx[rand(@gfx)];
    show ($i,'xv -root -quit +noresetroot -maxpect') if ($i=~/$p/i);
}
sub fpl{
    *gfx=get();
    foreach $i (@gfx){
	show($i,'xv -root -quit  +noresetroot')  if ($i =~ /$p/i);
    }
}
sub fplf{
    *gfx=get();
    foreach $i (@gfx){
	show($i,'xv -root -quit  +noresetroot -maxpect')  if ($i =~ /$p/i);
    }
}

sub sr{
    *gfx=get();
    $i=$gfx[rand(@gfx)];
    show ($i,'xv -root $i -quit +noresetroot') if ($i=~/$p/i);
}

sub srf{
    *gfx=get();
    $num = rand(@gfx);
    $i=$gfx[$num];
    if ($i =~ /$p/){
	show($i,'xv -root -quit +noresetroot -maxpect');
    }
	
}

sub sl{
    *gfx=get();
    foreach $i (@gfx){
	show($i,'xv -root -quit  +noresetroot')  if ($i =~ /$p/i);
    }
} 

sub slf{
    *gfx=get();
     foreach $i (@gfx){
	show($i,'xv -maxpect -root -quit  +noresetroot')  if ($i =~ /$p/i);
    }
}
#---------------


sub play{
    my ($i)=@_;
    print "$i ";
    if ($i=~/$p/i){
#	$i =~ s/\(/\\\(/;
#	$i =~ s/\)/\\\)/;
#	$i =~ s/\s/\ /g;
    $i=spc($i);
	$cmd = "~/bin/mp $i";
	print $cmd;		
	`$cmd` ;
        print "\tdone.\n";
    }else {print "\tnot\n";
}
}

sub show{
    my ($i,$xv)=@_;
    print "$i ";
    unless ($i =~ /\.xvpics/){
	$s=spc($i);
	`$xv $s` unless ($i =~ /thumbiowueroiewukjhkjghd/ || chkz($i));
    }
    print "\tdone.\n";
}

sub chkz{
    my ($i)=@_;
    my ($s)=200;
    my ($w,$h)=imgsize($i);
    print $w."x".$h;
    if ($w < $s){
	print " w < $s ";
	return 1;
    }elsif($h <$s){
	print " h < $s ";
	return 1;
    }else{
	return 0;
    }
}


sub handler{
    if ($do =~ /fm/){
	`zapp mpg123`;
	exit(1);
    }else{
	local ($sig)=@_;
	print "caught $sig\n";
	`xsetroot`;
	exit(1);
    }
    `xset s on`;
}

sub getmp{
    my @gfx;
    if ($do =~ /fm/){
	@gfx = `find /fire/mp3 -name \\*mp3;find . -name \\*MP3` ; 
    }else{
	@gfx = `ls -R *.mp3 >/dev/null` ; 
    }
    chop (@gfx);
    \@gfx;
}

sub getmpg{
    my @gfx;
    @gfx = `find . -name \\*mpg;find . -name \\*mpeg;find . -name \\*MPG`; 
    chop (@gfx);
    \@gfx;
}


sub get{
    my @gfx;
    if ($do =~ /fp|q/){
	@gfx = `find . -name \\*jp\\*;find . -name \\*JPG\\*;find . -name \\*gif\\*;` ; 
    }else{
	@gfx = `ls -R *.jpg *JPG 2>/dev/null` ; 
    }
    chop (@gfx);
    \@gfx;
}



