#!/usr/bin/env perl
# edit-devede-underscore - "build"-related utility script
# Revision: 080526

#---------------------------------------------------------------------
#                        standard module setup
#---------------------------------------------------------------------

require 5.8.0;
use strict;
use Carp;
use warnings;
                                # Trap warnings
$SIG {__WARN__} = sub { die @_; };

#---------------------------------------------------------------------
#                        load LACSUB module[s]
#---------------------------------------------------------------------

use LACSUB::Base
 ( @LACSUB::Base::EXPORT_OK       );
 
use LACSUB::Filesystem
  ( @LACSUB::Filesystem::EXPORT_OK );

#---------------------------------------------------------------------
#                           basic constants
#---------------------------------------------------------------------

use constant ZERO  => 0;        # Zero
use constant ONE   => 1;        # One

use constant FALSE => 0;        # Boolean FALSE
use constant TRUE  => 1;        # Boolean TRUE

#---------------------------------------------------------------------
#                            main routine
#---------------------------------------------------------------------

sub Main
{
    my $orig_data;              # Copy of original data
    my @list = ();

    &SearchDirTree
    (
        -path      => '.'     ,
        -ref_list  => \@list  ,
        -only_text => TRUE    ,
        -sort      => 'name'
    );

    my $newtext = << 'END';
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

def _(str):
    return str
END
    $newtext =~ s@\s+\z@@s;

    for my $path (@list)
    {
        next if $path !~ m@\.py\z@i;
        $_         = &ReadFile (-path => $path);
        $orig_data = $_;

        s@^#![^\012]+?\s*\n@$newtext\n\n@is;
        next if $_ eq $orig_data;

        &WriteFile
        (
            -path     => $path ,
            -ref_data => \$_
        );

        print "Modified $path\n";
    }

    undef;
}

#---------------------------------------------------------------------
#                            main program
#---------------------------------------------------------------------

&Main();                        # Call the main routine
exit ZERO;                      # Normal exit
