#!/usr/bin/perl

# Coded by Trizen
# Email: echo dHJpemVueEBnbWFpbC5jb20K | base64 -d
# Website: http://trizen.go.ro

sub help {
    print "
                               Wordlist Master
                                                         coded by Trizen
                                                         

 Usage: $0 [options] [file1] [file2] [file3] [...]

Available options:

 -rd,  --rm_dups                   : remove duplicate lines
 -up,  --uppercase                 : all characters to uppercase
 -low, --lowercase                 : all characters to lowercase
 -sort                             : sort list (from a to z)
 -cut=<value>                      : cuts and keeps 'n' characters from line
 -keep_if_less=<value>             : keep line if it's less than 'n' characters
 -keep_if_more=<value>             : keep line it's more than 'n' characters
 -count_lines                      : prints counted lines by schema: <line_nr line>
 
 -rm_if_contains=<string>             : removes lines which contains 'string'
 -regex_rm_if_contains=<regex>        : same as above, but 'regex' match
 -replace=<string1>-with=<string2>    : replace in line 'string1' with 'string2'
 -regex_replace=<regex>-with=<string> : same as above, but 'regex' match
";
    print ' --r2n, --n2r                         : converts \\r\\n lines to \\n and viceversa';
    print qq[
 --delete_file(s)                     : delete original file (default: rename it to "\$file~")
  
  Examples:
   script -replace=A-with=a wordlist.txt
   script -regex-replace="^\\d"-with="#" wordlist.txt
   script -keep_if_less=20 -low -r2n wordlist.txt
];
    exit;
}
unless ($ARGV[0]) {
    &help;
}
$all_args = join('#', @ARGV);
foreach $arg (@ARGV) {
    if ($arg eq '-h') {
        &help;
    }
    if ($arg =~ /^[-]+(rm[-_]?dups|rd)$/) {
        $rmdups = 1;
        next;
    }
    if ($arg =~ /^[-]+(uppercase|up)$/) {
        $uppercase = 1;
        next;
    }
    elsif ($arg =~ /^[-]+(lowercase|low)$/) {
        $lowercase = 1;
        next;
    }
    if ($arg =~ /^[-]+keep_if_less=([\d]+)$/) {
        $if_less = $1;
        next;
    }
    if ($arg =~ /^[-]+keep_if_more=([\d]+)$/) {
        $if_more = $1;
        next;
    }
    if ($arg =~ /^[-]+cut=([\d]+)$/) {
        $cut_chars = $1;
        next;
    }
    if ($arg =~ /^[-]+replace=(.+)[_-]{1}with=(.*)$/) {
        $search = quotemeta $1;
        $replace = $2;
        next;
    }
    elsif ($arg =~ /^[-]+regex[_-]{1}replace=(.+)[-_]{1}with=(.*)$/) {
        $search = $1;
        $replace = $2;
        next;
    }
    if ($arg =~ /^[-]+rm_if_contains=(.+)/) {
        $contains = quotemeta $1;
        next;
    }
    elsif ($arg =~ /^[-]+regex_rm_if_contains=(.+)/) {
        $contains = $1;
        next;
    }
    if ($arg =~ /^[-]+r2n/) {
        $r2n = 1;
        next;
    }
    elsif ($arg =~ /^[-]+n2r/) {
        $n2r = 1;
        next;
    }
    if ($arg =~ /^[-]+delete[-_]?file[s]?$/) {
        $delete_file = 1;
        next;
    }
    if ($arg =~ /^[-]+sort$/) {
        $sort = 1;
        next;
    }
    if ($arg =~ /^[-]+count[-_]{1}lines$/) {
        $count_lines = 1;
        next;
    }
    unless ($replace) {
        $replace = '';
    }
}
foreach $arg (@ARGV) {
    unless ($arg =~ /^-/) {
        &Open_file;
    }
}
sub Open_file {
    next unless open FILE, $arg;
    @file = <FILE>;
    close FILE;
    if ($delete_file) {
        die $! unless unlink $arg;
    }
    else {
        die $! unless rename $arg, "$arg~";
    }
    die $! unless open WRITE, ">$arg";
    if ($rmdups) {
        @file = sort  @file;
        &Remove_duplicate_lines;
        next;
    }
    if ($sort) {
        @file = sort  @file;
    }
    $n = 0;
    foreach $_ (@file) {
        if ($r2n and not $r_done) {
            $r = '';
            $r_done = 1;
        }
        elsif ($n2r and not $n_done) {
            $r = 1;
            $n_done = 1;
        }
        unless ($r or $r2n) {
            if ($_ =~ /\r\n$/) {
                $r = 1;
            }
            else {
                $r = '';
            }
        }
        chomp($line = $_);
        if ($contains) {
            next if $line =~ /$contains/;
        }
        if ($uppercase) {
            $line = "\U$line\E";
        }
        elsif ($lowercase) {
            $line = "\L$line\E";
        }
        if ($cut_chars) {
            if ($line =~ /^([\w\W]{$cut_chars})/) {
                $line = $1;
            }
        }
        if ($if_less) {
            next if length $line >= $if_less;
        }
        if ($if_more) {
            next if length $line <= $if_more;
        }
        if ($search) {
            $line =~ s[$search][$replace]g;
        }
        if ($r) {
            $line = "$line\r\n";
        }
        else {
            if ($line =~ /\r$/) {
                $line =~ s/\r$//;
            }
            $line = "$line\n";
        }
        if ($count_lines) {
            ++$n;
            print WRITE "$n ";
        }
        print WRITE $line;
    }
    close WRITE;
}
sub Remove_duplicate_lines {
    die $! unless open WRITE, ">$arg";
    foreach $line (@file) {
        next if $line eq $lastline;
        print WRITE $line;
        $lastline = $line;
    }
    close WRITE;
}

