#!/usr/bin/perl

use strict;
use warnings;

use Biblio::SIF::Patron;
use Getopt::Long
    qw(:config posix_default gnu_compat require_order bundling no_ignore_case);

my ($term, $remove_bom);

GetOptions(
    'B|remove-utf8-bom' => \$remove_bom,
    't|terminator=s' => \$term,
    'z' => sub { $term = "\x00\n" },
    'Z' => sub { $term = "\x0a\x00\x0a" },
    '0' => sub { $term = "\x00" },
    'n' => sub { $term = "\n" },
    'N' => sub { $term = "\n\n" },
    'crlf' => sub { $term = "\x0d\x0a" },
);

my $iter = Biblio::SIF::Patron->iterator(
    @ARGV ? shift @ARGV : \*STDIN,
    'terminator' => $term,
    'chomp' => 1,
);

while (defined (my $p = $iter->())) {
    $p =~ s/\A\xef\xbb\xbf// if $remove_bom && $. == 1;
    print "$p\x00\x0a";
}

