#!/usr/bin/perl

# Copyright (C) 2010, 2011 Thorsten Kukuk
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# in Version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.

=head1 NAME

bww - calculate "Buchstabenwortwert"

=head1 SYNOPSIS

bww [options] [word ...]

=head1 DESCRIPTION

Calculates the sum of all letters of a word.
a=1, b=2, c=3, ..., z=26, ä=27, ö=28, ü=29, ß=30
All other characters are ignored, for all options.

=head1 OPTIONS

    -b|--bww            Calculate sum of all letters (A=1, B=2, ...)
    -l|--length         Count all characters of input string
    -s|--selbstlaute    Count all "Selbstlaute" (a,e,i,o,u,ä,ö,ü)
    -r|--reverse        Reverse input string
    --bwwrev            Calculate sum of all letters (A=26, B=25, ...)
    --version           Print version number and exit
    --usage             Print usage
    -h|-?|--help	Help

=cut

use strict;
use warnings;
use utf8;
use Encode qw(decode encode);
use Pod::Usage;

#
# process command line arguments
#
use Getopt::Long;
my $help = 0;
my $man = 0;
my $version = 0;
my $usage = 0;
my $length = 0;
my $rev = 0;
my $selbstlaute = 0;
my $bwwopt = 0;
my $bwwrev = 0;
my $bww;

GetOptions('b:1' => \$bwwopt,
	   'bww!' => \$bwwopt,
	   'l:1' => \$length,
	   'length!' => \$length,
	   'r:1' => \$rev,
	   'reverse!' => \$rev,
	   'bwwrev:1' => \$bwwrev,
	   's:1' => \$selbstlaute,
	   'selbstlaute!' => \$selbstlaute,
	   'version' => \$version,
	   'man' => \$man,
	   'usage' => \$usage,
	   'help|h|?' => \$help) or pod2usage(2);
pod2usage(0) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
pod2usage(-exitstatus => 0, -verbose => 0) if $usage;

if ($version) {
  print "bww (geo-tools) 1.23\n";
  exit;
}

if ($bwwopt) {
  $bww = 1;
} elsif ($length == 0 && $rev == 0 && $selbstlaute == 0 && $bwwrev == 0) {
  $bww = 1;
}

binmode(\*STDIN, ":utf8");
binmode(\*STDOUT, ":utf8");

if ($#ARGV >= 0) {
  foreach (@ARGV) {
    my $word = decode("utf-8",$_);
    print "bww($word) = ", bww($word), "\n" if $bww;
    print "bwwrev($word) = ", bwwrev($word), "\n" if $bwwrev;
    print "length($word) = ", countchar($word), "\n" if $length;
    print "selbstlaute($word) = ", countselbstlaute($word), "\n" if $selbstlaute;
    if ($rev) {
      my $rword = reverse ($word);
      print "reverse($word) = $rword\n";
    }
  }
} elsif (($#ARGV < 0) ||($#ARGV == 0 && $ARGV[0] eq "-")) {
  # read from tty
  while (<>) {
    chomp;
    my $word = $_;
    print "bww($word) = ", bww($word), "\n" if $bww;
    print "length($word) = ", countchar($word), "\n" if $length;
    print "selbstlaute($word) = ", countselbstlaute($word), "\n" if $selbstlaute;
    if ($rev) {
      my $rword = reverse ($word);
      print "reverse($word) = $rword\n";
    }
  }
}

exit;

sub bww {
  my @char = split (//, $_[0]);
  my $sum = 0;

  foreach (@char) {
    my $c = lc($_);

    if (ord($c) >= ord('a') && ord($c) <= ord('z')) {
      $sum += ord($c) - ord ('a') + 1;
    } elsif ($c eq 'ä') {
      $sum += 27;
    } elsif ($c eq 'ö') {
      $sum += 28;
    } elsif ($c eq 'ü') {
      $sum += 29;
    } elsif ($c eq 'ß') {
      $sum += 30;
    }
  }
  return $sum;
}

sub bwwrev {
  my @char = split (//, $_[0]);
  my $sum = 0;

  foreach (@char) {
    my $c = lc($_);

    if (ord($c) >= ord('a') && ord($c) <= ord('z')) {
      $sum += 27 - (ord($c) - ord ('a') + 1);
    }
  }
  return $sum;
}

sub countchar {
  my @char = split (//, $_[0]);
  my $sum = 0;

  foreach (@char) {
    my $c = lc($_);

    if (ord($c) >= ord('a') && ord($c) <= ord('z')) {
      $sum++;
    } elsif ($c eq 'ä') {
      $sum++;
    } elsif ($c eq 'ö') {
      $sum++;
    } elsif ($c eq 'ü') {
      $sum++;
    } elsif ($c eq 'ß') {
      $sum++;
    }
  }
  return $sum;
}

sub countselbstlaute {
  my @char = split (//, $_[0]);
  my $sum = 0;

  foreach (@char) {
    my $c = lc($_);

    if ($c eq 'a') {
      $sum++;
    } elsif ($c eq 'e') {
      $sum++;
    } elsif ($c eq 'i') {
      $sum++;
    } elsif ($c eq 'o') {
      $sum++;
    } elsif ($c eq 'u') {
      $sum++;
    } elsif ($c eq 'ä') {
      $sum++;
    } elsif ($c eq 'ö') {
      $sum++;
    } elsif ($c eq 'ü') {
      $sum++;
    }
  }
  return $sum;
}
