#   Example2Name.pl
#   ---------------
#   This script renames example scripts to their approprate names.
#   It reads the comment header at the beginning of the script
#   and looks for the script name such as:
#       DumpShares
#   Then proceeds to name the file DumpShares.pl.
#   This is used to convert example scripts their descriptive names.
#   This was used for the book:
#       Win32 Perl Scripting: Administrator's Handbook
#
#   2000.10.28 roth
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";

use Getopt::Long;

Configure( \%Config );
if( $Config{help} )
{
  Syntax();
  exit( 0 );
}

foreach my $Dir ( @{$Config{dirs}} )
{
  ProcessDir( $Dir );
}

sub ProcessDir
{
    my( $Dir ) = @_;
    my @Dirs;

    if( opendir( DIR, $Dir ) )
    {
        while( my $File = readdir( DIR ) )
        {
            my $Path = "$Dir/$File";

            next if( "." eq $File || ".." eq $File );

            if( -d $Path )
            {
                push( @Dirs, $Path );
            }
            else
            {
                ProcessFile( $Dir, $File );
            }
        }
        closedir( DIR );

        foreach my $Path ( sort( @Dirs ) )
        {
            ProcessDir( $Path ) if( $Config{recurse} );
        }
    }
}

sub ProcessFile
{
    my( $Dir, $File ) = @_;
    $Path = "$Dir/$File";

    return unless( $Path =~ /\.pl$/i );

    if( open( FILE, "< $Path" ) )
    {
        my $Name;
        
        print "$Path ";
        while( my $Line = <FILE> )
        {
            if( ( $Name ) = ( $Line =~ /^#\s*(\w*?\.pl)\s*$/i ) )
            {
                last;
            }
        }
        close( FILE );

        if( "" ne $Name )
        {
            if( lc( $Name ) ne lc( $File ) )
            {
                print "=> $Name";
                rename( $Path, "$Dir/$Name" ) unless( $Config{no_rename} );
            }
        }
        print "\n";
    }
}

sub Configure
{
  my( $Config ) = @_;

  Getopt::Long::Configure( "prefix_pattern=(-|\/)" );
  $Result = GetOptions( $Config, 
                          qw(
                              recurse|r
                              no_rename|n
                              help|?
                          )
                      );
  $Config->{help} = 1 if( ! $Result );
  push( @{$Config->{dirs}}, @ARGV );
        
  if( ! scalar @{$Config->{dirs}} )
  {
    $Config->{help} = 1;
  }
}

sub Syntax
{
  my( $Script ) = ( $0 =~ /([^\\\/]*?)$/ );
  my( $Line ) = "-" x length( $Script );

  print <<EOT;

$Script
$Line
Renames example scripts to descriptive names.
This is used for the example scripts from the book
"Win32 Perl Scripting: The Administrator's Handbook" by
Dave Roth.

Syntax:
    perl $Script [-r] [-n] <Dir> [Dir2 [, Dir3 ...]]
        -r..........Recurse into subdirectories.
        -n..........Do not rename (only display names).
        Dir.........Directory of example files to rename.
EOT
}

