#!/usr/bin/perl
# Paul Johnson <pauljohn@ku.edu>

# Script to randomly select Enlightenment DR17 backgrounds AND TO DELETE
# the large *.edj files that Enlightenment creates in $HOME/.e/e/backgrounds
# I delete those because my hard disk fills up with picture description files
# otherwise.  Using a 100KB jpeg file will often cause a 2.5MB edj file to be created.
#
# example usage: epjbackground -w 4 -d /usr/local/share/backgrounds
#
# give argument -d [directory name] to recursively search for image files
# give argument -w [workspace number] to specify workspaces. I prefer to number them 1, 2, 3
# If no -w parameter is given, then the root window will be changed.

use File::Find; #to get all files under mainDir
use Getopt::Std;  # command line processing
use File::Basename;

getopts("w:d:")|| die "Invalid argument\n";

if ($opt_w =~ /\D/) { die "w is not a digit";}

@images = ();

if ( $opt_w ) {
    $workspace = $opt_w-1;
}


sub process_file{
    $filename = $File::Find::name;
    if ( -f $filename ){
	push(@images, $filename);
    }
}


if ( $opt_d ) {
	$basedir = $opt_d;
    }
else{
   $basedir = "/usr/local/share/Backgrounds";
} 


find(\&process_file, $basedir);

srand;

$index=int(rand(@images));


# If a specific workspace is selected:
if ($opt_w){

    @OLDBGS= `enlightenment_remote -desktop-bg-list`;

    my $deletMe ;
    while ( $thevar =  shift @OLDBGS ) {
	# print "while  " . $thevar;
	if ( $thevar =~ /DESK_X=\Q$workspace/ ){
	    $deletMe = $thevar;
	}
    }

#WORKS IN BASH:
#echo $OLDBGS | sed -e 's/^REPLY.*DESK_X=3//' -e 's/REPLY.*END//' -e 's/^.*FILE="//'  -e 's/"//'`;

    $deletMe =~ s/REPLY.*DESK_X=\Q$workspace//;
    $deletMe =~ s/DESK.*=//;
    $deletMe =~ s/"//g;  #"
    print "The file to be removed is: ". $deletMe . "\n";


    my $cmd1 = "e17setroot -n -s \"@images[$index]\" > /dev/null 2>&1";
    my $file = basename(@images[$index]);
    $file =~ s/jpg/edj/ ;
    my $cmd2 = "enlightenment_remote -desktop-bg-add 0 0 $workspace 0 ~/.e/e/backgrounds/$file";
    my $retval = system $cmd1;
    print "\n \n \n return value is $retval \n";
    if ( $retval == 0 )
    {
	print "That succeeded\n";
	system $cmd2;
    }
    my $cmd3 = "rm -f $deletMe"; 
    exec $cmd3;
}
else{   #no workspace was specified, so set the root window


    @OLDBGS= `enlightenment_remote -default-bg-get`;

    my $deleteMe ;
    shift @OLDBGS;
    $deleteMe = shift @OLDBGS; 
    
    $deleteMe =~ s/REPLY:\ "//; #"remove front part up to quotation mark
    $deleteMe =~ s/"//;  #"remove trailing quotation mark
    print "The file to be removed is: ". $deleteMe . "\n";
    
    print "\n The background image of the root window is @images[$index] \n";
    my $cmd1 = "e17setroot -n -s \"@images[$index]\" > /dev/null 2>&1";
    system $cmd1;
    my $file = basename(@images[$index]);
    $file =~ s/jpg/edj/ ;
    
    my $cmd1 = "enlightenment_remote -default-bg-set ~/.e/e/backgrounds/$file";
    
    system $cmd1; 
    my $cmd3= "rm -f $deleteMe";
    exec $cmd3
}


