#bork <commando>
use utf8;
sub bork
{
    my ($msg, $dbh, $kanaal, $server) = @_, my $return = '', my ($command);
    ($command, $msg) = split(/ /, $msg, 2);
    $command = lc $command;

    if ($command ne 'bork' && defined(&{$command}))
    {
        $msg = &{$command}($msg, $dbh, $kanaal, $server);
        $msg =~ s/\b(\w+)\b/bork_word($1);/eg;
        $msg =~ s/([\.!?])$/$1 Bork Bork Bork!/mg;
        return $msg;
    }
    else
    { return "Geen geldig commando. Zie: help bork"; }
}

sub bork_help
{
    return "Laat Furbie Zweeds-Engels praten: bork <commando>\nBv.: bork datum";
}

sub bork_word
{
    my ($word) = @_, my $buff = '', my $i_seen = 0;
    my ($begin, $letter, $isLast), my $count = 0, my $len = length($word);

    if (lc $word eq 'bork') { return $word; }

    while ($count < $len)
    {
        $isLast = $count == ($len-1);
        $letter = substr($word, $count, 1);

        if ($count == 0)
        {
            if ($letter eq 'e') { $buff .= 'i'; $count++; next; }
            elsif ($letter eq 'E') { $buff .= 'I'; $count++; next; }
            elsif ($letter eq 'o') { $buff .= 'å'; $count++; next; }
            elsif ($letter eq 'O') { $buff .= 'Å'; $count++; next; }
        }
        else
        {
            if ($letter eq 'e')
            {
                if (!$isLast && substr($word, $count+1, 1) eq 'w') { $buff .= 'å'; $count += 2; next; }
                elsif ($isLast) { $buff .= 'e-a'; $count++; next; }
            }
            elsif ($letter eq 'f')
            { $buff .= 'ff'; $count++; next; }
            elsif ($letter eq 'i')
            {
                if (!$isLast && substr($word, $count+1, 1) eq 'r') { $buff .= 'ur'; $count += 2; next; }
                elsif (!$i_seen) { $buff .= 'ee'; $count++; $i_seen = 1; next; }
            }
            elsif ($letter eq 'o')
            {
                if (!$isLast && substr($word, $count+1, 1) eq 'w') { $buff .= 'å'; $count += 2; next; }
                else { $buff .= 'u'; $count++; next; }
            }
            elsif ($count <= $len-4 && $letter eq 't' && substr($word, $count+1, 3) eq 'ion')
            { $buff .= "shun"; $count += 4; next; }
            elsif ($letter eq 'u') { $buff .= 'å'; $count++; next; }
        }

        if ($letter eq 'A')
        {
            if (!$isLast && substr($word, $count+1, 1) eq 'n') { $buff .= "Un"; $count += 2; next; }
            elsif (!$isLast && substr($word, $count+1, 1) eq 'u') { $buff .= "Å"; $count += 2; next; }
            elsif (!$isLast) { $buff .= "E"; $count++; next; }
        }
        elsif ($letter eq 'a')
        {
            if (!$isLast && substr($word, $count+1, 1) eq 'n') { $buff .= "un"; $count += 2; next; }
            elsif (!$isLast && substr($word, $count+1, 1) eq 'u') { $buff .= "å"; $count += 2; next; }
            elsif (!$isLast) { $buff .= "e"; $count++; next; }
        }
        elsif ($letter eq 'e')
        {
            if (!$isLast && substr($word, $count+1, 1) eq 'n' && $count == $len - 2) { $buff .= 'ee'; $count += 2; next; }

        }
        elsif ($letter eq 't')
        {
            if ($count == $len-2 && substr($word, $count+1, 1) eq 'h')
            { $buff .= 't'; $count += 2; next; }
            elsif ($count <= $len-3 && substr($word, $count+1, 2) eq 'he')
            { $buff .= 'zee'; $count += 3; next; }
        }
        elsif ($letter eq 'T')
        {
            if ($count <= $len-3 && substr($word, $count+1, 2) eq 'he')
            { $buff .= 'Zee'; $count += 3; next; }
        }
        elsif ($letter eq 'v') { $buff .= 'f'; $count++; next; }
        elsif ($letter eq 'V') { $buff .= 'F'; $count++; next; }
        elsif ($letter eq 'w') { $buff .= 'v'; $count++; next; }
        elsif ($letter eq 'W') { $buff .= 'V'; $count++; next; }

        $buff .= $letter; $count++; next;
    }

    return $buff;
}

1;
