#!/usr/bin/env perl

# Replace malformed data by U+FFFD or '?' (depending on the encoding)
# and remove the control characters from U+007F to U+009F. Useful for
# MUA's like Mutt, before calling the editor.

use strict;
use Encode;

my ($proc) = '$Id: mkprintable 38604 2010-08-12 13:00:18Z vinc17/ypig $'
  =~ /^.Id: (\S+) / or die;

@ARGV or die <<EOF;
Usage: $proc <encoding> [ <file> ... ]
       perl -i /path/to/$proc <encoding> <file> ...
EOF

my $encoding = shift;

while (<>)
  {
    $_ = decode($encoding, $_);
    tr/\x{7F}-\x{9F}//d;
    print encode($encoding, $_);
  }