#!/usr/bin/env perl
use strict;

if (`ps x | grep ' xscreensaver ' | grep -v grep`) {
	print "XScreenSaver seems to be running. Exit it before running this script.\n";
	exit;
}

my $screensavers = { };

$screensavers->{'rss-cyclone'} 	= '  GL:                "Cyclone"  rss-cyclone --root                              \\n\\';
$screensavers->{'rss-euphoria'}	= '  GL:               "Euphoria"  rss-euphoria --root                             \\n\\';
$screensavers->{'rss-fieldlines'} 	= '  GL:             "Fieldlines"  rss-fieldlines --root                           \\n\\';
$screensavers->{'rss-flocks'} 	= '  GL:                 "Flocks"  rss-flocks --root                               \\n\\';
$screensavers->{'rss-flux'} 	= '  GL:                   "Flux"  rss-flux --root                                 \\n\\';
$screensavers->{'rss-helios'} 	= '  GL:                 "Helios"  rss-helios --root                               \\n\\';
$screensavers->{'rss-hyperspace'} 	= '  GL:             "Hyperspace"  rss-hyperspace --root                           \\n\\';
$screensavers->{'rss-lattice'} 	= '  GL:                "Lattice"  rss-lattice --root                              \\n\\';
$screensavers->{'rss-plasma'} 	= '  GL:                 "Plasma"  rss-plasma --root                               \\n\\';
$screensavers->{'rss-skyrocket'} 	= '  GL:              "Skyrocket"  rss-skyrocket --root                            \\n\\';
$screensavers->{'rss-solarwinds'} 	= '  GL:             "Solarwinds"  rss-solarwinds --root                           \\n\\';
$screensavers->{'rss-colorfire'} 	= '  GL:              "Colorfire"  rss-colorfire --root                            \\n\\';
$screensavers->{'rss-hufo_smoke'} 	= '  GL:           "Hufo\'s Smoke"  rss-hufo_smoke --root                           \\n\\';
$screensavers->{'rss-hufo_tunnel'} 	= '  GL:          "Hufo\'s Tunnel"  rss-hufo_tunnel --root                          \\n\\';
$screensavers->{'rss-sundancer2'} 	= '  GL:             "Sundancer2"  rss-sundancer2 --root                           \\n\\';
$screensavers->{'rss-biof'} 	= '  GL:                   "BioF"  rss-biof --root                                 \\n\\';
$screensavers->{'rss-busyspheres'} 	= '  GL:            "BusySpheres"  rss-busyspheres --root                          \\n\\';
$screensavers->{'rss-spirographx'} 	= '  GL:            "SpirographX"  rss-spirographx --root                          \\n\\';
$screensavers->{'rss-matrixview'} 	= '  GL:             "MatrixView"  rss-matrixview --root                           \\n\\';
$screensavers->{'rss-lorenz'}	= '  GL:                 "Lorenz"  rss-lorenz --root                               \\n\\';
$screensavers->{'rss-drempels'}	= '  GL:               "Drempels"  rss-drempels --root                             \\n\\';
$screensavers->{'rss-feedback'}	= '  GL:               "Feedback"  rss-feedback --root                             \\n\\';
$screensavers->{'rss-pixelcity'}	= '  GL:             "Pixel City"  rss-pixelcity --root                            \\n\\';

open XSCREENSAVER, "$ENV{'HOME'}/.xscreensaver";
my @xscreensaver_config_file = <XSCREENSAVER>;
close XSCREENSAVER;

open XSCREENSAVER, ">$ENV{'HOME'}/.xscreensaver";

my $programs_section_flag = 0;
foreach my $line (@xscreensaver_config_file) {
	if ($line =~ /^programs:/) {
		$programs_section_flag = 1;
	} elsif ($programs_section_flag) {
		if ($line =~ /\\\s+/) {
			foreach my $screensaver (keys %{$screensavers}) {
				if ($line =~ /\s$screensaver\s/) {
					delete $screensavers->{$screensaver};
				}
			}
		} else {
			foreach my $screensaver (keys %{$screensavers}) {
				print XSCREENSAVER "$screensavers->{$screensaver}\n";
			}

			$programs_section_flag = 0;
		}
	}
	print XSCREENSAVER "$line";
}

close XSCREENSAVER;
