#! /usr/bin/perl -w
# This script is licensed under CC-BY-SA, by Laith Juwaidah.

# version 0.21
use strict;

if ( $#ARGV != 2 ) {
  print "usage: $0 xsize ysize output\n";
  exit 0;
}

my $x = $ARGV[0];
my $y = $ARGV[1];
my $file = $ARGV[2];

my @times = localtime();
my $time = splice(@times,2,1);
my $half = "am";

if ($time > 11)
{
$half = "pm";
$time -= 12;
}

if ($time < 10)
{
	$time = "0$time";
} else {
	$time = "$time";
}

# Look for images in standard wallpaper paths
# in same dir as this script
use IO::Handle;
use IPC::Open3;
my $kde_config_writer = new IO::Handle;
my $kde_config_reader = new IO::Handle;
my $kde_config_error  = new IO::Handle;
my $kde_config_pid = open3($kde_config_writer,$kde_config_reader,$kde_config_error, "kde-config --path wallpaper");
my @wallpaper_path;
chomp(@wallpaper_path=split(/:/,<$kde_config_reader>));
undef $kde_config_reader;

use FindBin qw($Bin);
$wallpaper_path[$#wallpaper_path+1]=$Bin."/..";

foreach (@wallpaper_path) 
{
  if ( -f "$_/Infinity/${half}${time}.png" )
  {
    system("convert -resize ${x}x${y}! $_/Infinity/${half}${time}.png ${file}");
    exit 0;
  }
}

print "Error: Infinity/${half}${time}.png not found\n.";
exit 1;
