wpsbuild: Rewrite to fix various issues and support .fms

wpsbuild.pl is enhanced to produce fallback themes that were previously
hardcoded in buildzip.pl. It also recognizes resolution strings as
regular expressions now so you can specify resolution dependant settings
as <setting>.<regex>: <value> (needed to support single themes on multiple
resolutions). The WPSBUILD syntax completely changed as
well and it includes the fallback themes as well. Ultimately wpsbuild is
also able to install .fms files.

Change-Id: Idf5994f17b9750983d7bc3ef583540a8cdae1dde
diff --git a/apps/plugins/bitmaps/viewer_defaults/remote_viewers.6x8x1.bmp b/apps/plugins/bitmaps/viewer_defaults/remote_viewers.6x8x1.bmp
deleted file mode 100644
index d7d8966..0000000
--- a/apps/plugins/bitmaps/viewer_defaults/remote_viewers.6x8x1.bmp
+++ /dev/null
Binary files differ
diff --git a/apps/plugins/bitmaps/viewer_defaults/remote_viewers.6x8x2.bmp b/apps/plugins/bitmaps/viewer_defaults/remote_viewers.6x8x2.bmp
deleted file mode 100644
index d7d8966..0000000
--- a/apps/plugins/bitmaps/viewer_defaults/remote_viewers.6x8x2.bmp
+++ /dev/null
Binary files differ
diff --git a/apps/plugins/bitmaps/viewer_defaults/viewers.6x8x1.bmp b/icons/viewers.6x8x1.bmp
similarity index 100%
rename from apps/plugins/bitmaps/viewer_defaults/viewers.6x8x1.bmp
rename to icons/viewers.6x8x1.bmp
Binary files differ
diff --git a/apps/plugins/bitmaps/viewer_defaults/viewers.6x8x16.bmp b/icons/viewers.6x8x16.bmp
similarity index 100%
rename from apps/plugins/bitmaps/viewer_defaults/viewers.6x8x16.bmp
rename to icons/viewers.6x8x16.bmp
Binary files differ
diff --git a/apps/plugins/bitmaps/viewer_defaults/viewers.6x8x2.bmp b/icons/viewers.6x8x2.bmp
similarity index 100%
rename from apps/plugins/bitmaps/viewer_defaults/viewers.6x8x2.bmp
rename to icons/viewers.6x8x2.bmp
Binary files differ
diff --git a/tools/buildzip.pl b/tools/buildzip.pl
index 7044557..56bbe6d 100755
--- a/tools/buildzip.pl
+++ b/tools/buildzip.pl
@@ -333,57 +333,6 @@
     return $size;
 }
 
-sub create_failsafefiles {
-    my ($dir, $remote_depth) = @_;
-    my $text = "# Dummy file to allow Rockbox to reset to the default skin config.
-# Do not edit this file.  It's never actually loaded by Rockbox.";
-    open (FOO, ">$dir/wps/rockbox_failsafe.wps");
-    print FOO $text;
-    close(FOO);
-    open (FOO, ">$dir/wps/rockbox_failsafe.sbs");
-    print FOO $text;
-    close(FOO);
-    open (FOO, ">$dir/wps/rockbox_failsafe.fms");
-    print FOO $text;
-    close(FOO);
-    if ($remote_depth) {
-        open (FOO, ">$dir/wps/rockbox_failsafe.rwps");
-        print FOO $text;
-        close(FOO);
-        open (FOO, ">$dir/wps/rockbox_failsafe.rsbs");
-        print FOO $text;
-        close(FOO);
-        open (FOO, ">$dir/wps/rockbox_failsafe.rfms");
-        print FOO $text;
-        close(FOO);
-    }
-    open (FOO, ">$dir/themes/rockbox_failsafe.cfg");
-    print FOO <<STOP
-# This config has been autogenerated to reload the failsafe setup
-wps: $dir/wps/rockbox_failsafe.wps
-sbs: $dir/wps/rockbox_failsafe.sbs
-fms: $dir/wps/rockbox_failsafe.fms
-STOP
-;
-    if ($remote_depth) {
-    print FOO <<STOP
-rwps: $dir/wps/rockbox_failsafe.rwps
-rsbs: $dir/wps/rockbox_failsafe.rsbs
-rfms: $dir/wps/rockbox_failsafe.rfms
-STOP
-;
-    }
-    print FOO <<STOP
-statusbar: top
-font: 08-Schumacher-Clean.fnt
-foreground color: 000000
-background color: B6C6E5
-selector type: bar (inverse)
-backdrop: -
-STOP
-;
-    close(FOO);
-}
 
 sub buildzip {
     my ($image, $fonts)=@_;    
@@ -454,24 +403,8 @@
     }
 
     glob_mkdir("$temp_dir/wps");
+    glob_mkdir("$temp_dir/icons");
     glob_mkdir("$temp_dir/themes");
-    if ($bitmap) {
-        open(THEME, ">$temp_dir/themes/rockbox_default_icons.cfg");
-        print THEME <<STOP
-# this config file was auto-generated to make it
-# easy to reset the icons back to default
-iconset: -
-# taken from apps/gui/icon.c
-viewers iconset: /$rbdir/icons/viewers.bmp
-remote iconset: -
-# taken from apps/gui/icon.c
-remote viewers iconset: /$rbdir/icons/remote_viewers.bmp
-
-STOP
-;
-        close(THEME);
-    }
-    
     glob_mkdir("$temp_dir/codepages");
 
     if($bitmap) {
@@ -576,14 +509,6 @@
 
     glob_unlink("$temp_dir/rocks/*.lua"); # Clean up unwanted *.lua files (e.g. actions.lua, buttons.lua)
 
-    if ($bitmap) {
-        glob_mkdir("$temp_dir/icons");
-        copy("$viewer_bmpdir/viewers.${icon_w}x${icon_h}x$depth.bmp", "$temp_dir/icons/viewers.bmp");
-        if ($remote_depth) {
-            copy("$viewer_bmpdir/remote_viewers.${remote_icon_w}x${remote_icon_h}x$remote_depth.bmp", "$temp_dir/icons/remote_viewers.bmp");
-        }
-    }
-
     copy("$ROOT/apps/tagnavi.config", "$temp_dir/");
     copy("$ROOT/apps/plugins/disktidy.config", "$temp_dir/rocks/apps/");
 
@@ -606,7 +531,6 @@
              "$temp_dir/rocks/demos/pictureflow_splash.bmp");
 
     }
-    create_failsafefiles($temp_dir, $remote_depth);
 
     if($image) {
         # image is blank when this is a simulator
diff --git a/wps/WPSLIST b/wps/WPSLIST
index 2d08392..34f6cbd 100644
--- a/wps/WPSLIST
+++ b/wps/WPSLIST
@@ -11,7 +11,7 @@
 # This should list all WPS and RWPS files we have and what their requirements
 # and preferences are.
 #
-# Each <wps> section will also cause a "theme" .cfg to be made, which the
+# Each <theme> section will also cause a "theme" .cfg to be made, which the
 # <rwps> sections will not cause.
 #
 # A <wps> section can thus mention a "preferred" rwps file to load when that
@@ -19,21 +19,80 @@
 #
 # All sections should list what font and status bar setting they "like".
 
-
-<rwps>
-Name: rockbox_default.rwps
+<theme>
+Name: rockbox_default_icons
 Author: Rockbox team
-Font: 08-Schumacher-Clean.fnt
-Statusbar: top
-</rwps>
 
-<wps>
-Name: cabbiev2.wps
-RWPS: cabbiev2.rwps
-SBS:
-RSBS:
+<main>
+iconset: -
+viewers iconset..+x16: icons/viewers.6x8x16.bmp
+viewers iconset..+x1:  icons/viewers.6x8x1.bmp
+viewers iconset..+x2:  icons/viewers.6x8x2.bmp
+</main>
+
+<remote>
+iconset: -
+viewers iconset..+x1: icons/viewers.6x8x1.bmp
+viewers iconset..+x2: icons/viewers.6x8x2.bmp
+</remote>
+
+</theme>
+
+<theme>
+Name: rockbox_failsafe
+Author: Rockbox team
+
+WPS: yes
+RWPS: yes
+SBS: yes
+RSBS: yes
+FMS: yes
+RFMS: yes
+
+<main>
+backdrop: -
+font: -
+statusbar: top
+foreground color: 000000
+background color: B6C6E5
+selector type: bar (inverse)
+ui viewport: -
+</main>
+
+<remote>
+font: -
+statusbar: top
+ui viewport: -
+</remote>
+</theme>
+
+<theme>
+Name: cabbiev2
 # Real name of the creator of the WPS
-Author: Johannes Voggenthaler, Apoo Maha, Marc Guay, Alex Vanderpol, Jerry Lange, Keith Perri, Mark Fawcus, and Marianne Arnold with support from Rockbox developers and forums. Based on Cabbie by Yohann Misquitta.
+Authors: Johannes Voggenthaler, Apoo Maha, Marc Guay, Alex Vanderpol, Jerry Lange, Keith Perri, Mark Fawcus, and Marianne Arnold with support from Rockbox developers and forums. Based on Cabbie by Yohann Misquitta.
+WPS: yes
+RWPS: yes
+SBS: no
+RSBS: no
+
+<main>
+
+# override implicit .wps filename
+wps.800x480x16:  cabbiev2.800x480x16.wps
+wps.480x800x16:  cabbiev2.480x800x16.wps
+wps.320x480x16:  cabbiev2.320x480x16.wps
+wps.320x240x16:  cabbiev2.320x240x16.wps
+wps.128x128x16:  cabbiev2.128x128x16.wps
+wps.128x160x16:  cabbiev2.128x160x16.wps
+wps.132x80x16:   cabbiev2.132x80x16.wps
+wps.138x110x2:   cabbiev2.138x110x2.wps
+wps.160x128x16:  cabbiev2.160x128x16.wps
+wps.160x128x2:   cabbiev2.160x128x2.wps
+wps.176x132x16:  cabbiev2.176x132x16.wps
+wps.176x220x16:  cabbiev2.176x220x16.wps
+wps.220x176x16:  cabbiev2.220x176x16.wps
+wps.240x320x16:  cabbiev2.240x320x16.wps
+wps.240x400x16:  cabbiev2.240x400x16.wps
 
 # Preferred font (including .fnt extension - leave blank for player):
 Font.800x480x16: 35-Adobe-Helvetica.fnt
@@ -46,19 +105,16 @@
 Font.176x220x16: 12-Adobe-Helvetica.fnt
 Font.176x132x16: 12-Adobe-Helvetica.fnt
 Font.160x128x16: 12-Adobe-Helvetica.fnt
-Font.160x128x2: 12-Adobe-Helvetica.fnt
-Font.160x128x1: 12-Adobe-Helvetica.fnt
-Font.138x110x2: 12-Adobe-Helvetica.fnt
+Font.160x128x2:  12-Adobe-Helvetica.fnt
+Font.160x128x1:  12-Adobe-Helvetica.fnt
+Font.138x110x2:  12-Adobe-Helvetica.fnt
 Font.128x128x16: 12-Adobe-Helvetica.fnt
 Font.128x160x16: 12-Adobe-Helvetica.fnt
-Font.128x128x2: 12-Adobe-Helvetica.fnt
-Font.132x80x16: 11-Sazanami-Mincho.fnt
-Font.128x64x1: 08-Rockfont.fnt
-Font.112x64x1: 08-Rockfont.fnt
-Font.96x96x16: 08-Rockfont.fnt
-
-# Remote font for iriver
-Remote Font.128x64x1: 08-Rockfont.fnt
+Font.128x128x2:  12-Adobe-Helvetica.fnt
+Font.132x80x16:  11-Sazanami-Mincho.fnt
+Font.128x64x1:   08-Rockfont.fnt
+Font.112x64x1:   08-Rockfont.fnt
+Font.96x96x16:   08-Rockfont.fnt
 
 #misc settings that should be ignored on grayscale targets
 foreground color: CCCCCC
@@ -66,92 +122,73 @@
 line selector start color: fde89e
 line selector end color: b58c00
 line selector text color: 000000
-filetype colours:
+filetype colours: -
 
 #backdrop - remember this is the source file name in your SVN folder, not dest name!
-backdrop.800x480x16: backdrops/cabbiev2.800x480x16.bmp
-backdrop.480x800x16: backdrops/cabbiev2.480x800x16.bmp
-backdrop.320x480x16: backdrops/cabbiev2.320x480x16.bmp
-backdrop.320x240x16: backdrops/cabbiev2.320x240x16.bmp
-backdrop.128x128x16: backdrops/cabbiev2.128x128x16.bmp
-backdrop.128x160x16: backdrops/cabbiev2.128x160x16.bmp
-backdrop.132x80x16: backdrops/cabbiev2.132x80x16.bmp 
-backdrop.138x110x2: backdrops/cabbiev2.138x110x2.bmp
-backdrop.160x128x16: backdrops/cabbiev2.160x128x16.bmp
-backdrop.160x128x2: backdrops/cabbiev2.160x128x2.bmp
-backdrop.176x132x16: backdrops/cabbiev2.176x132x16.bmp
-backdrop.176x220x16: backdrops/cabbiev2.176x220x16.bmp
-backdrop.220x176x16: backdrops/cabbiev2.220x176x16.bmp
-backdrop.240x320x16: backdrops/cabbiev2.240x320x16.bmp
-backdrop.240x400x16: backdrops/cabbiev2.240x400x16.bmp
-backdrop.96x96x16: backdrops/cabbiev2.96x96x16.bmp
+backdrop.800x480x16:  backdrops/cabbiev2.800x480x16.bmp
+backdrop.480x800x16:  backdrops/cabbiev2.480x800x16.bmp
+backdrop.320x480x16:  backdrops/cabbiev2.320x480x16.bmp
+backdrop.320x240x16:  backdrops/cabbiev2.320x240x16.bmp
+backdrop.128x128x16:  backdrops/cabbiev2.128x128x16.bmp
+backdrop.128x160x16:  backdrops/cabbiev2.128x160x16.bmp
+backdrop.132x80x16:   backdrops/cabbiev2.132x80x16.bmp
+backdrop.138x110x2:   backdrops/cabbiev2.138x110x2.bmp
+backdrop.160x128x16:  backdrops/cabbiev2.160x128x16.bmp
+backdrop.160x128x2:   backdrops/cabbiev2.160x128x2.bmp
+backdrop.176x132x16:  backdrops/cabbiev2.176x132x16.bmp
+backdrop.176x220x16:  backdrops/cabbiev2.176x220x16.bmp
+backdrop.220x176x16:  backdrops/cabbiev2.220x176x16.bmp
+backdrop.240x320x16:  backdrops/cabbiev2.240x320x16.bmp
+backdrop.240x400x16:  backdrops/cabbiev2.240x400x16.bmp
+backdrop.96x96x16:    backdrops/cabbiev2.96x96x16.bmp
 
 #selection bar settings for color targets
-selector type.800x480x16: bar (gradient)
-selector type.480x800x16: bar (gradient)
-selector type.320x480x16: bar (gradient)
-selector type.320x240x16: bar (gradient)
-selector type.128x128x16: bar (gradient)
-selector type.128x160x16: bar (gradient)
-selector type.132x80x16:  bar (gradient)
-selector type.220x176x16: bar (gradient)
-selector type.160x128x16: bar (gradient)
-selector type.240x400x16: bar (gradient)
-selector type.240x320x16: bar (gradient)
-selector type.176x132x16: bar (gradient)
-selector type.176x220x16: bar (gradient)
-selector type.160x128x2: bar (inverse)
-selector type.138x110x2: bar (inverse)
-selector type.96x96x16: bar (gradient)
+selector type..+x16: bar (gradient)
+selector type..+x2: bar (inverse)
 
 #icons
-iconset.800x480x16: icons/tango_icons.32x32.bmp
-iconset.480x800x16: icons/tango_icons.32x32.bmp
-iconset.320x480x16: icons/tango_icons.24x24.bmp
-iconset.320x240x16: icons/tango_icons.16x16.bmp
-iconset.128x128x16: icons/tango_icons.12x12.bmp
-iconset.128x160x16: icons/tango_icons.12x12.bmp
-iconset.132x80x16:  icons/tango_icons.12x12.bmp
-iconset.138x110x2:  icons/tango_small_mono.bmp
-iconset.160x128x16: icons/tango_icons.12x12.bmp
-iconset.160x128x2:  icons/tango_small_mono.bmp
-iconset.176x132x16: icons/tango_icons.12x12.bmp
-iconset.176x220x16: icons/tango_icons.12x12.bmp
-iconset.220x176x16: icons/tango_icons.12x12.bmp
-iconset.240x320x16: icons/tango_icons.16x16.bmp
-iconset.240x400x16: icons/tango_icons.16x16.bmp
-iconset.96x96x16:   icons/tango_icons.8x8.bmp
+iconset.800x480x16:  icons/tango_icons.32x32.bmp
+iconset.480x800x16:  icons/tango_icons.32x32.bmp
+iconset.320x480x16:  icons/tango_icons.24x24.bmp
+iconset.320x240x16:  icons/tango_icons.16x16.bmp
+iconset.128x128x16:  icons/tango_icons.12x12.bmp
+iconset.128x160x16:  icons/tango_icons.12x12.bmp
+iconset.132x80x16:   icons/tango_icons.12x12.bmp
+iconset.160x128x16:  icons/tango_icons.12x12.bmp
+iconset.176x132x16:  icons/tango_icons.12x12.bmp
+iconset.176x220x16:  icons/tango_icons.12x12.bmp
+iconset.220x176x16:  icons/tango_icons.12x12.bmp
+iconset.240x320x16:  icons/tango_icons.16x16.bmp
+iconset.240x400x16:  icons/tango_icons.16x16.bmp
+iconset.96x96x16:    icons/tango_icons.8x8.bmp
+iconset..+x2:        icons/tango_small_mono.bmp
 
 #viewer icons
-viewers iconset.800x480x16: icons/tango_icons_viewers.32x32.bmp
-viewers iconset.480x800x16: icons/tango_icons_viewers.32x32.bmp
-viewers iconset.320x480x16: icons/tango_icons_viewers.24x24.bmp
-viewers iconset.320x240x16: icons/tango_icons_viewers.16x16.bmp
-viewers iconset.128x128x16: icons/tango_icons_viewers.12x12.bmp
-viewers iconset.128x160x16: icons/tango_icons_viewers.12x12.bmp
-viewers iconset.132x80x16:  icons/tango_icons_viewers.12x12.bmp
-viewers iconset.138x110x2:  icons/tango_small_viewers_mono.bmp
-viewers iconset.160x128x16: icons/tango_icons_viewers.12x12.bmp
-viewers iconset.160x128x2:  icons/tango_small_viewers_mono.bmp
-viewers iconset.176x132x16: icons/tango_icons_viewers.12x12.bmp
-viewers iconset.176x220x16: icons/tango_icons_viewers.12x12.bmp
-viewers iconset.220x176x16: icons/tango_icons_viewers.12x12.bmp
-viewers iconset.240x320x16: icons/tango_icons_viewers.16x16.bmp
-viewers iconset.240x400x16: icons/tango_icons_viewers.16x16.bmp
-viewers iconset.96x96x16:   icons/tango_icons_viewers.8x8.bmp
+viewers iconset.800x480x16:  icons/tango_icons_viewers.32x32.bmp
+viewers iconset.480x800x16:  icons/tango_icons_viewers.32x32.bmp
+viewers iconset.320x480x16:  icons/tango_icons_viewers.24x24.bmp
+viewers iconset.320x240x16:  icons/tango_icons_viewers.16x16.bmp
+viewers iconset.128x128x16:  icons/tango_icons_viewers.12x12.bmp
+viewers iconset.128x160x16:  icons/tango_icons_viewers.12x12.bmp
+viewers iconset.132x80x16:   icons/tango_icons_viewers.12x12.bmp
+viewers iconset.160x128x16:  icons/tango_icons_viewers.12x12.bmp
+viewers iconset.176x132x16:  icons/tango_icons_viewers.12x12.bmp
+viewers iconset.176x220x16:  icons/tango_icons_viewers.12x12.bmp
+viewers iconset.220x176x16:  icons/tango_icons_viewers.12x12.bmp
+viewers iconset.240x320x16:  icons/tango_icons_viewers.16x16.bmp
+viewers iconset.240x400x16:  icons/tango_icons_viewers.16x16.bmp
+viewers iconset.96x96x16:    icons/tango_icons_viewers.8x8.bmp
+viewers iconset..+x2:        icons/tango_small_viewers_mono.bmp
 
-# Whether the WPS is designed to have the statusbar on or off
-Statusbar: top
-remote statusbar: top
+statusbar: top
+ui viewport: -
+</main>
 
-# list & remote ui viewports
-ui viewport:
-remote ui viewport:
-</wps>
+<remote>
+rwps.128x64x1: cabbiev2.128x64x1.wps
+Font.128x64x1: 08-Rockfont.fnt
+statusbar: top
+ui viewport: -
+</remote>
 
-<rwps>
-Name: cabbiev2.rwps
-Author: Johannes Voggenthaler, Apoo Maha, Marc Guay, Alex Vanderpol, Jerry Lange, Keith Perri, Mark Fawcus, and Marianne Arnold with support from Rockbox developers and forums. Based on Cabbie by Yohann Misquitta.
-Font: 12-Adobe-Helvetica.fnt
-Statusbar: top
-</rwps>
+</theme>
diff --git a/wps/rockbox_default.txt.wps b/wps/rockbox_default.txt.wps
deleted file mode 100644
index b1427ca..0000000
--- a/wps/rockbox_default.txt.wps
+++ /dev/null
@@ -1,2 +0,0 @@
-# Dummy file to allow Rockbox to reset to the default WPS config.
-# Do not edit this file.  It's never actually loaded by Rockbox.
diff --git a/wps/rockbox_failsafe.fms b/wps/rockbox_failsafe.fms
new file mode 100644
index 0000000..4169115
--- /dev/null
+++ b/wps/rockbox_failsafe.fms
@@ -0,0 +1,2 @@
+# Dummy file to allow Rockbox to reset to the default skin config.
+# Do not edit this file.  It's never actually loaded by Rockbox.
diff --git a/wps/rockbox_failsafe.sbs b/wps/rockbox_failsafe.sbs
new file mode 100644
index 0000000..4169115
--- /dev/null
+++ b/wps/rockbox_failsafe.sbs
@@ -0,0 +1,2 @@
+# Dummy file to allow Rockbox to reset to the default skin config.
+# Do not edit this file.  It's never actually loaded by Rockbox.
diff --git a/wps/rockbox_failsafe.wps b/wps/rockbox_failsafe.wps
new file mode 100644
index 0000000..4169115
--- /dev/null
+++ b/wps/rockbox_failsafe.wps
@@ -0,0 +1,2 @@
+# Dummy file to allow Rockbox to reset to the default skin config.
+# Do not edit this file.  It's never actually loaded by Rockbox.
diff --git a/wps/wpsbuild.pl b/wps/wpsbuild.pl
index e516c3d..8733313 100755
--- a/wps/wpsbuild.pl
+++ b/wps/wpsbuild.pl
@@ -12,6 +12,7 @@
 use Getopt::Long qw(:config pass_through);	# pass_through so not confused by -DTYPE_STUFF
 
 my $ROOT="..";
+my $wpsdir;
 my $verbose;
 my $rbdir=".rockbox";
 my $tempdir=".rockbox";
@@ -31,21 +32,22 @@
 
 my $firmdir="$ROOT/firmware";
 my $cppdef = $target;
-my @depthlist = ( 16, 8, 4, 2, 1 );
 
 # These parameters are filled in as we parse wpslist
-my $req_size;
-my $req_g_wps;
 my $req_t;
-my $req_t_wps;
+my $theme;
+my $has_wps;
 my $wps;
-my $wps_prefix;
-my $sbs_prefix;
+my $has_rwps;
 my $rwps;
+my $has_sbs;
 my $sbs;
-my $sbs_w_size;
+my $has_rsbs;
 my $rsbs;
-my $rsbs_w_size;
+my $has_fms;
+my $fms;
+my $has_rfms;
+my $rfms;
 my $width;
 my $height;
 my $font;
@@ -53,13 +55,16 @@
 my $fgcolor;
 my $bgcolor;
 my $statusbar;
+my $remotestatusbar;
 my $author;
 my $backdrop;
 my $lineselectstart;
 my $lineselectend;
 my $selecttype;
 my $iconset;
+my $remoteiconset;
 my $viewericon;
+my $remoteviewericon;
 my $lineselecttextcolor;
 my $filetylecolor;
 my $listviewport;
@@ -135,28 +140,33 @@
 
 sub mkdirs
 {
-    my $wpsdir = $wps;
-    $wpsdir =~ s/\.(r|)wps//;
     mkdir "$tempdir/wps", 0777;
     mkdir "$tempdir/themes", 0777;
+    mkdir "$tempdir/icons", 0777;
 
-    if( -d "$tempdir/wps/$wpsdir") {
-        #print STDERR "wpsbuild warning: directory wps/$wpsdir already exists!\n";
+    if( -d "$tempdir/wps/$theme") {
+        #print STDERR "wpsbuild warning: directory wps/$theme already exists!\n";
     }
     else
     {
-       mkdir "$tempdir/wps/$wpsdir", 0777;
+       mkdir "$tempdir/wps/$theme", 0777;
     }
 }
 
+sub normalize
+{
+    my $in = $_[0];
+    # strip resolution
+    $in =~ s/(\.[0-9]*x[0-9]*x[0-9]*)//;
+    return $in;
+}
+
 sub copybackdrop
 {
     #copy the backdrop file into the build dir
     if ($backdrop ne '') {
-        my $dst = $backdrop;
-        $dst =~ s/(\.[0-9]*x[0-9]*x[0-9]*)//;
-        my $cmd = "cp $ROOT/$backdrop $tempdir/$dst";
-        `$cmd`;
+        my $dst = normalize($backdrop);
+        system("cp $ROOT/$backdrop $tempdir/$dst");
     }
 }
 
@@ -167,31 +177,30 @@
 
     $o =~ s/\.fnt/\.bdf/;
     mkdir "$tempdir/fonts";
-    my $cmd ="$ROOT/tools/convbdf -f -o \"$tempdir/fonts/$_[0]\" \"$ROOT/fonts/$o\" ";
-    `$cmd`;
+    system("$ROOT/tools/convbdf -f -o \"$tempdir/fonts/$_[0]\" \"$ROOT/fonts/$o\" ");
 }
 
 sub copythemeicon
 {
+    my $i = $_[0];
     #copy the icon specified by the theme
-    if ($iconset ne '') {
-        my $tempicon = $tempdir . "/" . $iconset;
-        $iconset = $rbdir . "/" . $iconset;
+    if ($i ne "-") {
+        my $tempicon = $tempdir . "/" . $i;
         $tempicon =~ /\/.*icons\/(.*)/i;
-        `cp $ROOT/icons/$1 $tempicon`;
+        system("cp $ROOT/icons/$1 $tempicon");
     }
 }
 
-sub copythemeviewericon
-{
-    #copy the viewer icon specified by the theme
-
-    if ($viewericon ne '') {
-        my $tempviewericon = $tempdir . "/" . $viewericon;
-        $viewericon = $rbdir . "/" . $viewericon;
-        $tempviewericon =~ /\/.*icons\/(.*)/i;
-        `cp $ROOT/icons/$1 $tempviewericon`;
+sub uniq {
+    my %seen = ();
+    my @r = ();
+    foreach my $a (@_) {
+        unless ($seen{$a}) {
+            push @r, $a;
+            $seen{$a} = 1;
+        }
     }
+    return @r;
 }
 
 sub copywps
@@ -199,181 +208,104 @@
     # we assume that we copy the WPS files from the same dir the WPSLIST
     # file is located in
     my $dir;
+    my %skinfiles = ("wps", $wps,
+                     "sbs", $sbs,
+                     "fms", $fms,
+                     "rwps", $rwps,
+                     "rsbs", $rsbs,
+                     "rfms", $rfms);
     my @filelist;
     my $file;
-    my $__sb;
 
-    if($wpslist =~ /(.*)WPSLIST/) {
+    if($wpslist =~ /(.*)\/WPSLIST/) {
         $dir = $1;
-        $__sb = $sbs_prefix . "." . $req_size . ".sbs";
-        #print "$req_t_wps $req_g_wps $sbs_prefix\n";
-        #print "$dir/$__sb\n";
 
-#        system("cp $dir/$wps .rockbox/wps/");
-        # check for <name>.WIDTHxHEIGHTxDEPTH.sbs
-        if (-e "$dir/$__sb") {
-            system("cp $dir/$__sb $tempdir/wps/$sbs");
+        # copy fully-fledged wps, sbs, etc. including graphics
+        foreach my $ext (keys %skinfiles) {
+            next unless ($skinfiles{$ext});
+            $file = $skinfiles{$ext};
+            system("cp $dir/$file $tempdir/wps/$theme.$ext");
+            open(SKIN, "$dir/$file");
+            while (<SKIN>) {
+                $filelist[$#filelist + 1] = $1 if (/[\(,]([^,]*?.bmp)[\),]/);
+            }
+            close(SKIN);
         }
-        # check for <name>.WIDTHxHEIGHTxDEPTH.<model>.sbs and overwrite the
-        # previous sb if needed
-        $__sb = $sbs_prefix . "." . $req_size . "." . $modelname . ".sbs";
-        if (-e "$dir/$__sb") {
-            system("cp $dir/$__sb $tempdir/wps/$sbs");
+
+        if ($#filelist >= 0) {
+            if (-e "$dir/$theme") {
+                foreach $file (uniq(@filelist)) {
+                    system("cp $dir/$theme/$file $tempdir/wps/$theme/");
+                }
+            }
+            else {
+                print STDERR "beep, no dir to copy WPS from!\n";
+            }
         }
-        
-        if (-e "$dir/$req_t_wps" ) {
-          system("cp $dir/$req_t_wps $tempdir/wps/$wps");
-
-        } elsif (-e "$dir/$req_g_wps") {
-           system("cp $dir/$req_g_wps $tempdir/wps/$wps");
-
-           open(WPSFILE, "$dir/$req_g_wps");
-           while (<WPSFILE>) {
-              $filelist[$#filelist + 1] = $1 if (/[\(,]([^,]*?.bmp)[\),]/);
-           }
-           close(WPSFILE);
-
-           if ($#filelist >= 0) {
-              if (-e "$dir/$wps_prefix/$req_size") {
-                 foreach $file (@filelist) {
-                     system("cp $dir/$wps_prefix/$req_size/$file $tempdir/wps/$wps_prefix/");
-                 }
-              }
-              elsif (-e "$dir/$wps_prefix") {
-                 foreach $file (@filelist) {
-                     system("cp $dir/$wps_prefix/$file $tempdir/wps/$wps_prefix/");
-                 }
-              }
-              else {
-                  print STDERR "beep, no dir to copy WPS from!\n";
-              }
-           }
-
-       } else {
-           print STDERR "Skipping $wps - no matching resolution.\n";
-       }
     } else {
         print STDERR "No source directory!\n";
     }
 }
 
 sub buildcfg {
-    my $cfg = $wps;
-    my @out;    
-
-    $cfg =~ s/\.(r|)wps/.cfg/;
+    my $cfg = $theme . ".cfg";
+    my @out;
 
     push @out, <<MOO
 \#
 \# $cfg generated by wpsbuild.pl
 \# $wps is made by $author
 \#
-wps: $rbdir/wps/$wps
 MOO
 ;
-    if(defined($sbs)) {
-        if ($sbs eq '') {
-            push @out, "sbs: -\n";
+
+    my %skinfiles = ("wps" => $wps,
+                     "sbs" => $sbs,
+                     "fms" => $fms,
+                     "rwps" => $rwps,
+                     "rsbs" => $rsbs,
+                     "rfms" => $rfms);
+    for my $skin (keys %skinfiles) {
+        my $val = $skinfiles{$skin};
+        print "$skin: $val\n";
+        if (!defined($val)) {
+            # dont put value if not defined (e.g. rwps when there's no remote)
+            next;
+        } elsif ($val eq '') {
+            # empty resets to built-in
+            push @out, "$skin: -\n";
         } else {
-            push @out, "sbs: $rbdir/wps/$sbs\n";
+            # file name is always <theme>.{wps,rwps,...} (see copywps())
+            push @out, "$skin: $rbdir/wps/$theme.$skin\n";
         }
     }
-    if(defined($rsbs)  && $has_remote) {
-        if ($rsbs eq '') {
-            push @out, "rsbs: -\n";
-        } else {
-            push @out, "rsbs: $rbdir/wps/$rsbs\n";
-        }
+
+    push @out, "selector type: $selecttype\n"   if (defined($selecttype));
+    push @out, "backdrop: $backdrop\n"          if (defined($backdrop));
+    push @out, "filetype colours: $filetylecolor\n" if (defined($filetylecolor));
+
+    if ($main_depth > 2) {
+        push @out, "foreground color: $fgcolor\n"                     if($fgcolor);
+        push @out, "background color: $bgcolor\n"                     if($bgcolor);
+        push @out, "line selector start color: $lineselectstart\n"    if($lineselectstart);
+        push @out, "line selector end color: $lineselectend\n"        if($lineselectend);;
+        push @out, "line selector text color: $lineselecttextcolor\n" if($lineselecttextcolor);
     }
-    if($font) {
-        if ($font eq '') {
-            push @out, "font: -\n";
-        } else {
-            push @out, "font: $rbdir/fonts/$font\n";
-        }
+
+    push @out, "font: $font\n"                  if (defined($font));
+    push @out, "statusbar: $statusbar\n"        if (defined($statusbar));
+    push @out, "iconset: $iconset\n"            if (defined($iconset));
+    push @out, "viewers iconset: $viewericon\n" if (defined($viewericon));
+    push @out, "ui viewport: $listviewport\n"   if (defined($listviewport));
+
+    if ($has_remote) {
+        push @out, "remote font: $remotefont\n"                  if (defined($remotefont));
+        push @out, "remote statusbar: $remotestatusbar\n"        if (defined($remotestatusbar));
+        push @out, "remote iconset: $remoteiconset\n"            if (defined($remoteiconset));
+        push @out, "remote viewers iconset: $remoteviewericon\n" if (defined($remoteviewericon));
+        push @out, "remote ui viewport: $remotelistviewport\n"   if (defined($remotelistviewport));
     }
-    if(defined($remotefont) && $has_remote) {
-        if ($remotefont eq '') {
-            push @out, "remote font: -\n";
-        } else {
-            push @out, "remote font: $rbdir/fonts/$remotefont\n";
-        }
-    }
-    if($fgcolor && $main_depth > 2) {
-        push @out, "foreground color: $fgcolor\n";
-    }
-    if($bgcolor && $main_depth > 2) {
-        push @out, "background color: $bgcolor\n";
-    }
-    if($statusbar) {
-        if($rwps && $has_remote ) {
-            push @out, "remote statusbar: $statusbar\n";
-        }
-        push @out, "statusbar: $statusbar\n";
-    }
-    if(defined($backdrop)) {
-        if ($backdrop eq '') {
-            push @out, "backdrop: -\n";
-        } else {
-            # clip resolution from filename
-            $backdrop =~ s/(\.[0-9]*x[0-9]*x[0-9]*)//;
-            push @out, "backdrop: $rbdir/$backdrop\n";
-        }
-    }
-    if($lineselectstart && $main_depth > 2) {
-        push @out, "line selector start color: $lineselectstart\n";
-    }
-    if($lineselectend && $main_depth > 2) {
-        push @out, "line selector end color: $lineselectend\n";
-    }
-    if($selecttype) {
-        push @out, "selector type: $selecttype\n";
-    }
-    if(defined($iconset)) {
-        if ($iconset eq '') {
-            push @out, "iconset: -\n";
-        } else {
-            push @out, "iconset: $iconset\n";
-        }
-    }
-    if(defined($viewericon)) {
-        if ($viewericon eq '') {
-            push @out, "viewers iconset: -\n";
-        } else {
-            push @out, "viewers iconset: $viewericon\n";
-        }
-    }
-    if($lineselecttextcolor && $main_depth > 2 ) {
-        push @out, "line selector text color: $lineselecttextcolor\n";
-    }
-    if($filetylecolor && $main_depth > 2) {
-        if ($filetylecolor eq '') {
-            push @out, "filetype colours: -\n";
-        } else {
-            push @out, "filetype colours: $filetylecolor\n";
-        }
-    }
-    if($rwps && $has_remote ) {
-        if ($rwps eq '') {
-            push @out, "rwps: -\n";
-        } else {
-            push @out, "rwps: $rbdir/wps/$rwps\n";
-        }
-    }
-    if(defined($listviewport)) {
-        if ($listviewport eq '') {
-            push @out, "ui viewport: -\n";
-        } else {
-            push @out, "ui viewport: $listviewport\n";
-        }
-    }
-    if(defined($remotelistviewport) && $has_remote) {
-        if ($remotelistviewport eq '') {
-            push @out, "remote ui viewport: -\n";
-        } else {
-            push @out, "remote ui viewport: $listviewport\n";
-        }
-    }
+
     if(-f "$tempdir/wps/$cfg") {
         print STDERR "wpsbuild warning: wps/$cfg already exists!\n";
     }
@@ -391,8 +323,47 @@
 #print "LCD: ${main_width}x${main_height}x${main_depth}\n";
 $has_remote = 1 if ($remote_height && $remote_width && $remote_depth);
 
-my $isrwps;
-my $within;
+
+# check if line matches the setting string or if it contains a regex
+# that contains the targets resolution
+sub check_res {
+    my ($line, $string, $remote) = @_;
+    if ($line =~ /^${string}: *(.*)/i) {
+        return $1;
+    }
+    elsif($line =~ /^${string}.(.*): *(.*)/i) {
+        # $1 is a resolution regex, $2 the filename incl. resolution
+        my $fn = $2;
+        my $size_str = "${main_width}x${main_height}x${main_depth}";
+        if ($remote) {
+            $size_str = "${remote_width}x${remote_height}x${remote_depth}";
+        }
+        if ($size_str =~ /$1$/) {
+            return $fn;
+        }
+    }
+    return "";
+}
+
+# check if <theme>.<model>.<ext> exists. If not, check if <theme>.<ext> exists
+sub check_skinfile {
+    my $ext = $_[0];
+    my $req_skin = $theme . "." . $modelname . ".$ext";
+    if (-e "$wpsdir/$req_skin") {
+        return $req_skin;
+    } else {
+        $req_skin = $theme . ".$ext";
+        if (-e "$wpsdir/$req_skin") {
+            return $req_skin;
+        }
+    }
+    return '';
+}
+
+
+# Infer WPS (etc.) filename from the the if it wasnt given
+$wpslist =~ /(.*)WPSLIST/;
+$wpsdir = $1;
 
 open(WPS, "<$wpslist");
 while(<WPS>) {
@@ -410,15 +381,21 @@
         $rbdir = "/" . $rbdir;
     }
 
-    if($l =~ /^ *<(r|)wps>/i) {
-        $isrwps = $1;
-        $within = 1;
+    if($l =~ /^ *<theme>/i) {
         # undef is a unary operator (!)
+        undef $theme;
+        undef $has_wps;
+        undef $has_rwps;
+        undef $has_sbs;
+        undef $has_rsbs;
+        undef $has_fms;
+        undef $has_rfms;
         undef $wps;
-        undef $wps_prefix;
         undef $rwps;
         undef $sbs;
         undef $rsbs;
+        undef $fms;
+        undef $rfms;
         undef $width;
         undef $height;
         undef $font;
@@ -426,224 +403,213 @@
         undef $fgcolor;
         undef $bgcolor;
         undef $statusbar;
+        undef $remotestatusbar;
         undef $author;
-        undef $req_g_wps;
-        undef $req_t_wps;
         undef $backdrop;
         undef $lineselectstart;
         undef $lineselectend;
         undef $selecttype;
         undef $iconset;
+        undef $remoteiconset;
         undef $viewericon;
+        undef $remoteviewericon;
         undef $lineselecttextcolor;
         undef $filetylecolor;
         undef $listviewport;
         undef $remotelistviewport;
-
-        next;
     }
-    if($within) {
-        if($l =~ /^ *<\/${isrwps}wps>/i) {
-            # Get the required width and height
-            my ($rheight, $rwidth, $rdepth);
-            if($isrwps) {
-                ($rheight, $rwidth, $rdepth) =
-                         ($remote_height, $remote_width, $remote_depth);
+    elsif($l =~ /^Name: *(.*)/i) {
+        $theme = $1;
+    }
+    elsif($l =~ /^Authors: *(.*)/i) {
+        $author = $1;
+    }
+    elsif ($l =~ /^WPS: *(yes|no)/i) {
+        $has_wps = $1;
+    }
+    elsif ($l =~ /^RWPS: *(yes|no)/i) {
+        $has_rwps = $1;
+    }
+    elsif ($l =~ /^SBS: *(yes|no)/i) {
+        $has_sbs = $1;
+    }
+    elsif ($l =~ /^RSBS: *(yes|no)/i) {
+        $has_rsbs = $1;
+    }
+    elsif ($l =~ /^FMS: *(yes|no)/i) {
+        $has_fms = $1;
+    }
+    elsif ($l =~ /^RFMS: *(yes|no)/i) {
+        $has_rfms = $1;
+    }
+    elsif($l =~ /^ *<main>/i) {
+        # parse main unit settings
+        while(<WPS>) {
+            my $l = $_;
+            if ($l =~ /^ *<\/main>/i) {
+                last;
             }
-            else {
-                ($rheight, $rwidth, $rdepth) =
-                         ($main_height, $main_width, $main_depth);
+            elsif($_ = check_res($l, "wps")) {
+                $wps = $_;
+            }
+            elsif($_ = check_res($l, "sbs")) {
+                $sbs = $_;
+            }
+            elsif($_ = check_res($l, "fms")) {
+                $fms = $_;
+            }
+            elsif($_ = check_res($l, "Font")) {
+                $font = $_;
+            }
+            elsif($_ = check_res($l, "Statusbar")) {
+                $statusbar = $_;
+            }
+            elsif($_ = check_res($l, "Backdrop")) {
+                $backdrop = $_;
+            }
+            elsif($l =~ /^Foreground Color: *(.*)/i) {
+                $fgcolor = $1;
+            }
+            elsif($l =~ /^Background Color: *(.*)/i) {
+                $bgcolor = $1;
+            }
+            elsif($l =~ /^line selector start color: *(.*)/i) {
+                $lineselectstart = $1;
+            }
+            elsif($l =~ /^line selector end color: *(.*)/i) {
+                $lineselectend = $1;
+            }
+            elsif($_ = check_res($l, "selector type")) {
+                $selecttype = $_;
+            }
+            elsif($_ = check_res($l, "iconset")) {
+                $iconset = $_;
+            }
+            elsif($_ = check_res($l, "viewers iconset")) {
+                $viewericon = $_;
+            }
+            elsif($l =~ /^line selector text color: *(.*)/i) {
+                $lineselecttextcolor = $1;
+            }
+            elsif($l =~ /^filetype colours: *(.*)/i) {
+                $filetylecolor = $1;
+            }
+            elsif($_ = check_res($l, "ui viewport")) {
+                $listviewport = $_;
+            }
+        }
+    }
+    elsif($l =~ /^ *<remote>/i) {
+        while(<WPS>) {
+            # parse remote settings
+            my $l = $_;
+            if ($l =~ /^ *<\/remote>/i) {
+                last;
+            }
+            elsif(!$has_remote) {
+                next; # dont parse <remote> section
+            }
+            elsif($_ = check_res($l, "rwps", 1)) {
+                $rwps = $_;
+            }
+            elsif($_ = check_res($l, "rsbs", 1)) {
+                $rsbs = $_;
+            }
+            elsif($_ = check_res($l, "rfms", 1)) {
+                $rfms = $_;
+            }
+            elsif($_ = check_res($l, "Font", 1)) {
+                $remotefont = $_;
+            }
+            elsif($_ = check_res($l, "iconset", 1)) {
+                $remoteiconset = $_;
+            }
+            elsif($_ = check_res($l, "viewers iconset", 1)) {
+                $remoteviewericon = $_;
+            }
+            elsif($_ = check_res($l, "statusbar", 1)) {
+                $remotestatusbar = $_;
+            }
+            elsif($_ = check_res($l, "ui viewport", 1)) {
+                $remotelistviewport = $_;
+            }
+        }
+    }
+    elsif($l =~ /^ *<\/theme>/i) {
+        # for each wps,sbs,fms (+ remote variants) check if <theme>[.<model>].wps
+        # exists if no filename was specified in WPSLIST
+        my $req_skin;
+
+        if ($has_wps eq "yes" && !$wps) {
+            $wps = check_skinfile("wps");
+        } elsif ($has_wps eq "no") {
+            $wps = '';
+        }
+
+        if ($has_sbs eq "yes" && !$sbs) {
+            $sbs = check_skinfile("sbs");
+        } elsif ($has_sbs eq "no") {
+            $sbs = '';
+        }
+
+        if ($has_fms eq "yes" && !$fms) {
+            $fms = check_skinfile("fms");
+        } elsif ($has_fms eq "no") {
+            $fms = '';
+        }
+
+        # now check for remote skin files (use main screen's extension)
+        if ($has_remote) {
+            if ($has_rwps eq "yes" && !$rwps) {
+                $rwps = check_skinfile("wps");
+            } elsif ($has_rwps eq "no") {
+                $rwps = '';
             }
 
-            if(!$rheight || !$rwidth) {
-                #printf STDERR "wpsbuild notice: No %sLCD size, skipping $wps\n",
-                #$isrwps?"remote ":"";
-                $within = 0;
-                next;
+            if ($has_rsbs eq "yes" && !$rsbs) {
+                $rsbs = check_skinfile("sbs");
+            } elsif ($has_rsbs eq "no") {
+                $rsbs = '';
             }
-            $wpslist =~ /(.*)WPSLIST/;
-            my $wpsdir = $1;
-            # If this WPS installable on this platform, one of the following
-            # two files will be present
-            foreach my $d (@depthlist) {
-                next if ($d > $rdepth);
 
-                $req_size = $rwidth . "x" . $rheight . "x" . $d;
-
-                # check for model specific wps
-                $req_g_wps = $wps_prefix . "." . $req_size . "." . $modelname . ".wps";
-                last if (-e "$wpsdir/$req_g_wps");
-
-                # check for normal wps (with WIDTHxHEIGHTxDEPTH)
-                $req_g_wps = $wps_prefix . "." . $req_size . ".wps";
-                last if (-e "$wpsdir/$req_g_wps");
-
-                if ($isrwps) {
-                    $req_size = $req_size . "." . $main_width . "x" . $main_height . "x" . "$main_depth";
-
-                    $req_g_wps = $wps_prefix . "." . $req_size . ".wps";
-                    last if (-e "$wpsdir/$req_g_wps");
-                }
+            if ($has_rfms eq "yes" && !$rfms) {
+                $rfms = check_skinfile("fms");
+            } elsif ($has_rfms eq "no") {
+                $rfms = '';
             }
-            $req_t_wps = $wps_prefix . ".txt" . ".wps";
+        }
+        #print "LCD: $wps wants $width x $height\n";
 
-            #print "LCD: $wps wants $width x $height\n";
-            #print "LCD: is $rwidth x $rheight\n";
-
-            #print "gwps: $wpsdir/$req_g_wps" . "\n";
-            if (-e "$wpsdir/$req_g_wps" || -e "$wpsdir/$req_t_wps" ) {
-                #
-                # The target model has an LCD that is suitable for this
-                # WPS
-                #
-                #print "Size requirement is fine!\n";
-                mkdirs() if (-e "$wpsdir/$req_g_wps");
-                # Do the copying before building the .cfg - buildcfg()
-                # mangles some filenames
-                if ($backdrop) {
-                    copybackdrop();
-                }
-                if ($iconset) {
-                    copythemeicon();
-                }
-                if ($viewericon) {
-                    copythemeviewericon();
-                }
-                if ($font) {
-                    copythemefont($font);
-                }
-                if ($remotefont) {
-                    copythemefont($remotefont);
-                }
-                if(!$isrwps) {
-                    # We only make .cfg files for <wps> sections:
-                    buildcfg();
-                }
-                copywps();
+        #
+        # The target model has an LCD that is suitable for this
+        # WPS
+        #
+        #print "Size requirement is fine!\n";
+        mkdirs() if (-e "$wpsdir/$theme");
+        # Do the copying before building the .cfg - buildcfg()
+        # mangles some filenames
+        if (defined($backdrop) && $backdrop ne "-") {
+            copybackdrop();
+            $backdrop = normalize($backdrop);
+        }
+        foreach my $i ($iconset, $viewericon, $remoteiconset, $remoteviewericon) {
+            if (defined($i) && $i ne "-") {
+                copythemeicon($i);
             }
-            else {
-                #print "(${wps_prefix}-${rwidth}x${rheight}x$rdepth) ";
-                #print "Skip $wps due to size restraints\n";
-            }
-            $within = 0;
         }
-        elsif($l =~ /^Name: *(.*)/i) {
-            # Note that in the case this is within <rwps>, $wps will contain the
-            # name of the rwps. Use $isrwps to figure out what type it is.
-            $wps = $wps_prefix = $1;
-            $wps_prefix =~ s/\.(r|)wps//;
-            #print $wps_prefix . "\n";
+        if (defined($font) && $font ne "-") {
+            copythemefont($font);
+            $font = "$rbdir/fonts/$font";
         }
-        elsif($l =~ /^RWPS: *(.*)/i) {
-            $rwps = $1;
+        if (defined($remotefont) && $remotefont ne "-") {
+            copythemefont($remotefont);
+            $remotefont = "$rbdir/fonts/$remotefont";
         }
-        elsif($l =~ /^RWPS\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $rwps = $1;
-        }
-        elsif($l =~ /^SBS: *(.*)/i) {
-            $sbs = $sbs_prefix = $1;
-            $sbs_prefix =~ s/\.(r|)sbs//;
-        }
-        elsif($l =~ /^SBS\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $sbs = $sbs_prefix = $1;
-            $sbs_prefix =~ s/\.(r|)sbs//;
-        }
-        elsif($l =~ /^RSBS: *(.*)/i) {
-            $rsbs = $1;
-        }
-        elsif($l =~ /^RSBS\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $rsbs = $1;
-        }
-        elsif($l =~ /^Author: *(.*)/i) {
-            $author = $1;
-        }
-        elsif($l =~ /^Width: *(.*)/i) {
-            $width = $1;
-        }
-        elsif($l =~ /^Width\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $width = $1;
-        }
-        elsif($l =~ /^Height: *(.*)/i) {
-            $height = $1;
-        }
-        elsif($l =~ /^Height\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $height = $1;
-        }
-        elsif($l =~ /^Font: *(.*)/i) {
-            $font = $1;
-        }
-        elsif($l =~ /^Font\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $font = $1;
-        }
-        elsif($l =~ /^Remote Font\.${remote_width}x${remote_height}x$remote_depth: *(.*)/i) {
-            $remotefont = $1;
-        }
-        elsif($l =~ /^Remote Font: *(.*)/i) {
-            $remotefont = $1;
-        }
-        elsif($l =~ /^Foreground Color: *(.*)/i) {
-            $fgcolor = $1;
-        }
-        elsif($l =~ /^Background Color: *(.*)/i) {
-            $bgcolor = $1;
-        }
-        elsif($l =~ /^Statusbar: *(.*)/i) {
-            $statusbar = $1;
-        }
-        elsif($l =~ /^Statusbar\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $statusbar = $1;
-        }
-        elsif($l =~ /^Backdrop: *(.*)/i) {
-            $backdrop = $1;
-        }
-        elsif($l =~ /^Backdrop\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $backdrop = $1;
-        }
-        elsif($l =~ /^line selector start color: *(.*)/i) {
-            $lineselectstart = $1;
-        }
-        elsif($l =~ /^line selector end color: *(.*)/i) {
-            $lineselectend = $1;
-        }
-        elsif($l =~ /^selector type: *(.*)/i) {
-            $selecttype = $1;
-        }
-        elsif($l =~ /^selector type\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $selecttype = $1;
-        }
-        elsif($l =~ /^iconset: *(.*)/i) {
-            $iconset = $1;
-        }
-        elsif($l =~ /^iconset\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $iconset = $1;
-        }
-        elsif($l =~ /^viewers iconset: *(.*)/i) {
-            $viewericon = $1;
-        }
-        elsif($l =~ /^viewers iconset\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $viewericon = $1;
-        }
-        elsif($l =~ /^line selector text color: *(.*)/i) {
-            $lineselecttextcolor = $1;
-        }
-        elsif($l =~ /^filetype colours: *(.*)/i) {
-            $filetylecolor = $1;
-        }
-        elsif($l =~ /^ui viewport: *(.*)/i) {
-            $listviewport = $1;
-        }
-        elsif($l =~ /^ui viewport\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $listviewport = $1;
-        }
-        elsif($l =~ /^remote ui viewport: *(.*)/i) {
-            $remotelistviewport = $1;
-        }
-        elsif($l =~ /^remote ui viewport\.${main_width}x${main_height}x$main_depth: *(.*)/i) {
-            $remotelistviewport = $1;
-        }
-        else{
-            #print "Unknown line:  $l!\n";
-        }
+        buildcfg();
+        copywps();
+    }
+    else{
+        #print "Unknown line:  $l!\n";
     }
 }