#!/usr/bin/env perl6
use v6.d;

use Grammar::TokenProcessing;

my %*SUB-MAIN-OPTS = :named-anywhere;

#| Replaces token names in files.
multi MAIN(
        Str $dirName where *.IO.d,        #= Directory with files to be processed.
        Str $pairs where *.IO.f,          #= CSV file with replacement pairs. The first column has the original token names; the second column has the new names.
        Str :$file-ext = '.rakumod',      #= Extension(s) pattern of the files to be processed.
        Str :$file-new-ext = '.new',      #= Extension to be added to newly obtained files. If NONE the file content is overwritten.
           ) {

    my @fileNames = Grammar::TokenProcessing::reallyflat(Grammar::TokenProcessing::tree($dirName, $file-ext));

    my @rules = slurp($pairs).lines>>.split(',').map({$_[0]=>$_[1]}).sort({ $_.key });

    for @fileNames -> $f {
        my $content = slurp $f;
        for @rules -> $p {
            $content = $content.subst('<' ~ $p.key ~ '>', '<' ~ $p.value ~ '>'):g;
            $content = $content.subst('<.' ~ $p.key ~ '>', '<.' ~ $p.value ~ '>'):g;
            $content = $content.subst( 'token ' ~ $p.key ~ ' {', 'token ' ~ $p.value ~ ' {');
        }

        if $file-new-ext.chars > 0 && $file-new-ext ne 'NONE'  {
            spurt $f ~ $file-new-ext, $content;
        } elsif $file-new-ext eq 'NONE'  {
            spurt $f, $content;
        } else {
            die 'Do not know how to use the specified new file(s) extension, $file-new-ext';
        }
    }
}