#!/usr/bin/perl

$file = "bible.csv";
open(F, "<$file") or die("ERROR: cannot read file $file: $!");
while(<F>){
	$line = trim($_);
	if($line=~/^\d?\.? ?(\S{1,20} \d+, \d+[a-d]? ?u?\.? ?\d*[a-d]?) (\S.+$)/){
		printVers($stelle, $text);
		$stelle = $1;
		$text = $2;
		printVers($stelle, $text) if($text=~/\d+$/);
	}
	else{
		$text .= " ". $line;
	}
}
close(F);
printVers($stelle, $text);


$stellelast = "";
$textlast = "";
sub printVers{
	my $stelle = shift;
	my $text = shift;
	$text =~ s/;/,:/g;
	$text =~ s/\d+\s*$//;
	println("$stelle;$text") if($stelle=~/\S/ and $stelle ne $stellelast and $text ne $textlast);
	$stellelast = $stelle;
	$textlast = $text;
}

sub println{
	my $str = shift;
	print $str."\n";
}

sub trim{
	my $str = shift;
	$str =~ s/^\s+//gs;
	$str =~ s/\s+$//gs;
	return $str;
}