#!/usr/bin/perl

use warnings;
use strict;
use Bencode qw(bencode bdecode);

if (@ARGV < 2) {
  die "USAGE: $0 INFILE OUTFILE\n";
}

open INFILE, "<", $ARGV[0];
open OUTFILE, ">", $ARGV[1];

my $orig;
{
  local $/;
  $orig = <INFILE>;
}

my $decoded = bdecode $orig;

for (@{$decoded->{'info'}->{'files'}}) {
  $_->{'path'} = $_->{'path.utf-8'};
}
$decoded->{'info'}->{'name'} = $decoded->{'info'}->{'name.utf-8'};
$decoded->{'comment'} = $decoded->{'comment.utf-8'}
  if exists $decoded->{'comment.utf-8'};

print OUTFILE bencode($decoded);
