#!/pro/bin/perl

use 5.014002;
use warnings;

our $VERSION = "0.01 - 20230921";
our $CMD = $0 =~ s{.*/}{}r;

sub usage {
    my $err = shift and select STDERR;
    say "usage: $CMD [in] [out]";
    exit $err;
    } # usage

use CSV;
use File::Temp   qw( tempfile       );
use Getopt::Long qw(:config bundling);
GetOptions (
    "help|?"		=> sub { usage (0); },
    "V|version"		=> sub { say "$CMD [$VERSION]"; exit 0; },

    "v|verbose:1"	=> \(my $opt_v = 0),
    ) or usage (1);

@ARGV && -s $ARGV[0] and open STDIN,  "<:encoding(utf-8)", shift;
@ARGV && -s $ARGV[0] and die "$ARGV[0] already exists\n";
@ARGV                and open STDOUT, ">:encoding(utf-8)", shift;

my ($nfh, $nfn) = tempfile ();
print $nfh <STDIN>;
close $nfh;

open my $fh, "-|", "nroff", "-mandoc", $nfn;
print map {
       s{(?:\x{02dc}|\xcb\x9c		)}{~}grx	# ~
    =~ s{(?:\x{02c6}|\xcb\x86		)}{^}grx	# ^
    =~ s{(?:\x{2018}|\xe2\x80\x98
	   |\x{2019}|\xe2\x80\x99	)}{'}grx	# '
    =~ s{(?:\x{201c}|\xe2\x80\x9c
	   |\x{201d}|\xe2\x80\x9d	)}{"}grx	# "
    =~ s{(?:\x{2212}|\xe2\x88\x92
	   |\x{2010}|\xe2\x80\x90	)}{-}grx	# -
    =~ s{(?:\e\[|\x9b)[0-9;]*m}		  {}grx		# colors
    } <$fh>;
close $fh;
