#!/usr/bin/perl 
#
# splash_resize -- a helper script to automatically create theme configs
#                  for different resolutions
#
# (c) 2004 Michal Januszewski <spock@gentoo.org>
#
# NOTE: This script only creates config files, it doesn't provide the necessary
#       images - they have to be created by hand.
#
# Usage: splash_resize <theme> <old_res> <new_res>
#
# $Header: /srv/cvs/splash/utils/scripts/splash_resize,v 1.2 2004/11/12 17:42:16 spock Exp $

if ($#ARGV < 2) {
	print "splash_resize/splashutils-1.5.4.4\n";
	print "Usage: splash_resize <theme> <old_res> <new_res>\n";
	exit 0;
}

$theme = $ARGV[0];
$old_res = $ARGV[1];
$new_res = $ARGV[2];

open IN,"<//etc/splash/$theme/$old_res.cfg" || die "Can't open source config file."; 
open OUT,">//etc/splash/$theme/$new_res.cfg" || die "Can't write to destination config file."; 

$old_res =~ /(\d+)x(\d+)/; $old_x = $1; $old_y = $2;
$new_res =~ /(\d+)x(\d+)/; $new_x = $1; $new_y = $2;

while(<IN>) { 

	if (/^\s*tx=(\d+)/ || /^\s*tw=(\d+)/ || /^\s*text_x=(\d+)/) { 
		$t = int(($1/$old_x)*$new_x); 
		s/=\d+/=$t/; 
	} elsif (/^\s*ty=(\d+)/ || /^\s*th=(\d+)/ || /^\s*text_y=(\d+)/) { 
		$t = int(($1/$old_y)*$new_y); 
		s/=\d+/=$t/; 
	} elsif (/^\s*text_size=(\d+)/) { 
		$t = int(($1/$old_y)*$new_y);
		s/=\d+/=$t/; 

	} elsif (/^\s*box\s*[a-zA-Z ]*\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) { 
	
		$x0 = int(($1/$old_x)*$new_x); 
		$x1 = int(($3/$old_x)*$new_x); 
		$y0 = int(($2/$old_y)*$new_y);  
		$y1 = int(($4/$old_y)*$new_y); 
		s/box\s*([a-zA-Z ]*)\s*\d+\s+\d+\s+\d+\s+\d+\s*(.*)/box $1 $x0 $y0 $x1 $y1 $2/;

	} elsif (/^\s*(silent)?pic(256)?=(.*)\n/) {
		
		$sil = $1;
		$col = $2;
		$src = $3;
		
		if ($src =~ /$max_res/) {
			$dest = $src;
			$dest =~ s/$old_res/$new_res/g;
		} else {
			$dest = $src;
			$dest =~ s/\.[^\.]+$//g;
			$dest .= "-$new_res.jpeg";
		}
	}

	s/$old_res/$new_res/g; 

	print OUT $_;
}

close IN; 
close OUT;

print "Resized config file for theme '$theme' for '$new_res' resolution successfully created.\n";

