#!/usr/bin/perl -w
# Test that no templates use undefined variables.

use File::Find;

#
#  A list of variables we can support
#
my %VALID_KEY;
$VALID_KEY{ 'VERSION' } = 1;
$VALID_KEY{ 'RELEASE' } = 1;
$VALID_KEY{ 'HOSTNAME' } = 1;
$VALID_KEY{ 'HEADER' } = 1;
$VALID_KEY{ 'DIRECTORY' } = 1;
$VALID_KEY{ 'BANNER' } = 1;
$VALID_KEY{ 'HEADING' } = 1;
$VALID_KEY{ 'TEXT' } = 1;
$VALID_KEY{ 'ERROR_MESSAGE' } = 1;
$VALID_KEY{ 'PLAYLISTS' } = 1;
$VALID_KEY{ 'SONGS' } = 1;
$VALID_KEY{ 'DIRECTORIES' } = 1;
$VALID_KEY{ 'MOVIES' } = 1;
$VALID_KEY{ 'TITLE' } = 1;


#
# Specialise plugin values.
$VALID_KEY{ 'BUG_REPORT_URL' } = 1;
$VALID_KEY{ 'PLAY_RESULTS' } = 1;
$VALID_KEY{ 'TERMS' } = 1;
$VALID_KEY{ 'RESULTS' } = 1;
$VALID_KEY{ 'SELECT_COMBO' } = 1;
$VALID_KEY{ 'GENRE_COMBO' } = 1;
$VALID_KEY{ 'USER_AGENT' } = 1;
$VALID_KEY{ 'connected_address' } = 1;
$VALID_KEY{ 'REQUEST' } = 1;
$VALID_KEY{ 'host' } = 1;
$VALID_KEY{ 'OSNAME' } = 1;
$VALID_KEY{ 'BUG_CONFIG' } = 1;
$VALID_KEY{ 'MOST_POPULAR_SONGS' } = 1;
$VALID_KEY{ 'MOST_POPULAR_DIRS' } = 1;
$VALID_KEY{ 'MOST_ACTIVE_CLIENTS' } = 1;
$VALID_KEY{ 'MOST_POPULAR_USER_AGENTS' } = 1;


#
#  Do the search
#
my $START = "../templates/";
find({ wanted => \&process, no_chdir => '1' }, $START);


#
#
#
sub process( $ )
{
  my $file = $File::Find::name;

  # Ignore directories
  return if ( -d $file );

  # And images
  return if ( $file =~ /\.jpg$/ );
  return if ( $file =~ /\.gif$/ );
  return if ( $file =~ /\.png$/ );
  return if ( $file =~ /\.ini$/ );

  # And CVS files.
  return if ( $file =~ /CVS/ );

  # Read in the file.
  open( FILY, "<$file" ) or die "Cannot open $file - $!";
  my @LINES  = <FILY>;
  close( FILY );

  #
  # Look for illegal values.
  #
  foreach my $line ( @LINES )
  {
    while( $line =~ /(.*)\$([a-zA-Z-_]+)(.*)/ )
    {
      my $key = $2;
      if (! $VALID_KEY{ $key } ) 
      {
	print "Unknown key $key found in $file\n";
	exit 1;
      }
      $line = $2;
    }
  }
  return 1;
}



exit( 0 );
