#!/usr/bin/perl -w

use lib '..';
use General::FileSystem;

my $functableSrc = "../../fvwm/functable.c";
my $contentRef = loadFile($functableSrc);
die "No commands found, so Commands.pm is not generated.\n" unless $contentRef;

my $commandEntriesCode = "";
foreach my $entry ($$contentRef =~
	m{(?:/\* )?CMD_ENT\(\s*(?:"|PRE_).*?\),(?: \*/)?.*?/\* - .*? \*/}sg)
{
	my ($string, $name, $flags, $cursor, $event, $descr) = $entry =~ m{
		(?:"|PRE_) (.+?) "? ,
		.*? CMD_(\w+) ,
		.*? ,
		.*? ([\w\s|]+?) ,
		.*? (?:CRS_)?(\w+) ,
		.*? (\w+)
		.*? \) ,
		.*? /\*\s-\s (.*?) \s\*/
	}xs;
	$name = $string if $string !~ /^\w/;
	my $window = $flags =~ FUNC_NEEDS_WINDOW? 1: 0;
	$cursor = "" if $cursor eq "0";
	$commandEntriesCode .=
		"\t{\n\t\tname => '$name',\n\t\tcursor => '$cursor',\n" .
		"\t\twindow => $window,\n\t\tdescr => q{$descr},\n\t},\n";
}

my $version = `egrep 'VERSION = |VERSIONINFO = ' Makefile | cut -d"=" -f2 \\
	| perl -pe 's/^ //;s/\\n//'`;
my $time = time;

my $output = <<ENDOUTPUT;
# Autogenerated from the fvwm sources.

package FVWM::Commands;

use vars qw(\$VERS \$TIME \@LIST);

\$VERS = '$version';
\$TIME = $time;

\@LIST = (
$commandEntriesCode);

1;

__END__

=head1 NAME

FVWM::Commands - lists all available FVWM commands

=head1 DESCRIPTION

This class may be used to get a list of all available FVWM commands
including the command name, its short description and some other command
properties.

=head1 USAGE

    use lib `fvwm-perllib dir`;
    use FVWM::Commands;
    use POSIX;

    my \$date = strftime("%d-%b-%Y", gmtime(\$FVWM::Commands::TIME));
    my \$version = \$FVWM::Commands::VERS;
    print "The recognized commands for fvwm \$version as of \$date:\\n\\n";
    foreach my \$command (\@FVWM::Commands::LIST) {
        printf "  %-21s - %s\\n", \$command->{name}, \$command->{descr};
    }

=head1 PUBLIC CONSTANTS

=over 4

=item \$VERS

The fvwm version number at the generation time like "2.6.0" plus the info
that may indicate that this is not a final version, but a cvs snapshot.

=item \$TIME

The unix time of the command list generation.

Example:

    print "The FVWM command list found on your system was generated "
        . (time() - \$FVWM::Commands::TIME) / 86400 . " days ago\\n";

=item \@LIST

The command list that is an array of hashes with keys I<name>, I<cursor>,
I<descr> (strings) and I<window> (boolean).

I<name> may be special, like "+", "#" and "*".  Other names usually contain
only isalpha characters, like B<Move>, B<SendToModule>.

I<cursor> may be either empty or the cursor context used in B<CursorStyle>
(like "SELECT" or "MOVE"), associated with the command.

I<descr> is a short one line description of the command.

I<window> is true for commands that need a window.

Example:

    # get command names only
    \@commandNames = map { \$_->{name} } \@FVWM::Commands::LIST;

=head1 SEE ALSO

For more information about the commands themselves, see fvwm(1).

=back

=cut
ENDOUTPUT

saveFile("Commands.pm", \$output);
