#!/usr/bin/perl
# Joseph Bylund 2011/10/18

use strict;

my $max_row = 8;
my $row = 0;
my $col = 0;
my $home = $ENV{'HOME'};

my @icons_to_hide = `find ~/Desktop -mindepth 1 -maxdepth 1 -type f|grep '~'|sort`;
chomp(@icons_to_hide);
for(my $icon=0; $icon < scalar(@icons_to_hide); $icon++)
{
	my $curr_icon = @icons_to_hide[$icon];
	my $new_icon = $curr_icon;
	$new_icon =~ s/(.*)\/(.*)~/$1\/.$2/; # cut off up until last slash
	system("mv $curr_icon $new_icon");
}

# first list desktop directories in order
my @desktop_icons = `find ~/Desktop -mindepth 1 -maxdepth 1 -type d|sort`;

# then list desktop files in order, omit backups
push(@desktop_icons,`find ~/Desktop -mindepth 1 -maxdepth 1 -type f|grep -Ev '\/\\\.'|sort`);

my $outfile = "$home/.config/xfce4/desktop/icons.screen0.rc";
open(my $outfilehandle, ">", "$outfile") or die "Could not open $outfile:\n$!"; #open for write, overwrite
chomp(@desktop_icons);
for(my $icon=0; $icon < scalar(@desktop_icons); $icon++)
{
	my $curr_icon = @desktop_icons[$icon];
#	system("touch $curr_icon");
	$curr_icon =~ s/.*\///; # cut off up until last slash
	print $outfilehandle "[$curr_icon]\n";
	print $outfilehandle "row=$row\n";
	print $outfilehandle "col=$col\n";
	print $outfilehandle "\n";
	$row++;
	if($row > $max_row)
	{
		$row = 0;
		$col++;
	}
}

close $outfilehandle;
#system("cat $outfile");
system("touch $outfile");
system("xfdesktop --reload");
