#!/usr/bin/perl
#
# xs, Copyright (c) 2001 by Eric Lassauge <lassauge@mail.dotcom.fr>
# This program parses the ~/.xscreensaver file to give you the hack number
# to be used for xscreensaver-command -select <N>
# See xscreensaver-command(1)
#
# Permission to use, copy, modify, distribute, and sell this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation.  No representations are made about the suitability of this
# software for any purpose.  It is provided "as is" without express or
# implied warranty.

# Get "~/.xscreensaver" file
$xsc=$ENV{'HOME'}."/.xscreensaver";
open (SAV,"<$xsc") || die "Can't open $xsc";
while(<SAV>)
{
	# strip out newlines, compress whitespace
        s/[\r\n\t ]+/ /go;

	if (/^programs:/)
	{
		$prog = 0;
		next;
	}
	# Continue if not in programs part
	next unless defined($prog);
	# Continue if empty line
	next if (/^ $/);
	# Stop if ended programs part
	last if (/^pointerPollTime:/);

	@fields = split;
	# Check if field is multi-line	
	if ($fields[$#fields] eq "\\")
	{
		# Keep first line text
		$text = $_ unless defined($text);
		next;
	}
	# Update displayed datas
	$text = $_ unless defined($text);
	$prog++;
	$text =~ s/\\n//;
	$text =~ s/\\//;
	$text =~ s/[ ]*$//;

	# Display informations
	printf ("%3d : %s\n",$prog,$text);
	undef($text);
}
close(SAV);
