1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ciuse strict;
10e1051a39Sopenharmony_ciuse warnings;
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ciuse lib ".";
13e1051a39Sopenharmony_ciuse Getopt::Std;
14e1051a39Sopenharmony_ciuse Pod::Html;
15e1051a39Sopenharmony_ciuse File::Spec::Functions qw(:DEFAULT rel2abs);
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci# Options.
18e1051a39Sopenharmony_ciour($opt_i);    # -i INFILE
19e1051a39Sopenharmony_ciour($opt_o);    # -o OUTFILE
20e1051a39Sopenharmony_ciour($opt_t);    # -t TITLE
21e1051a39Sopenharmony_ciour($opt_r);    # -r PODROOT
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_cigetopts('i:o:t:r:');
24e1051a39Sopenharmony_cidie "-i flag missing" unless $opt_i;
25e1051a39Sopenharmony_cidie "-o flag missing" unless $opt_o;
26e1051a39Sopenharmony_cidie "-t flag missing" unless $opt_t;
27e1051a39Sopenharmony_cidie "-r flag missing" unless $opt_r;
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci# We originally used realpath() here, but the Windows implementation appears
30e1051a39Sopenharmony_ci# to require that the directory or file exist to be able to process the input,
31e1051a39Sopenharmony_ci# so we use rel2abs() instead, which only processes the string without
32e1051a39Sopenharmony_ci# looking further.
33e1051a39Sopenharmony_ci$opt_i = rel2abs($opt_i) or die "Can't convert to real path: $!";
34e1051a39Sopenharmony_ci$opt_o = rel2abs($opt_o) or die "Can't convert to real path: $!";
35e1051a39Sopenharmony_ci$opt_r = rel2abs($opt_r) or die "Can't convert to real path: $!";
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cipod2html
38e1051a39Sopenharmony_ci    "--infile=$opt_i",
39e1051a39Sopenharmony_ci    "--outfile=$opt_o",
40e1051a39Sopenharmony_ci    "--title=$opt_t",
41e1051a39Sopenharmony_ci    "--podroot=$opt_r",
42e1051a39Sopenharmony_ci    "--podpath=man1:man3:man5:man7",
43e1051a39Sopenharmony_ci    "--htmldir=..";
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ci# Read in contents.
46e1051a39Sopenharmony_ciopen F, "<$opt_o"
47e1051a39Sopenharmony_ci    or die "Can't read $opt_o, $!";
48e1051a39Sopenharmony_cimy $contents = '';
49e1051a39Sopenharmony_ci{
50e1051a39Sopenharmony_ci    local $/ = undef;
51e1051a39Sopenharmony_ci    $contents = <F>;
52e1051a39Sopenharmony_ci}
53e1051a39Sopenharmony_ciclose F;
54e1051a39Sopenharmony_ciunlink $opt_o;
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci$contents =~
57e1051a39Sopenharmony_ci    s|href="http://man\.he\.net/(man\d/[^"]+)(?:\.html)?"|href="../$1.html"|g;
58e1051a39Sopenharmony_ciopen F, ">$opt_o"
59e1051a39Sopenharmony_ci    or die "Can't write $opt_o, $!";
60e1051a39Sopenharmony_ciprint F $contents;
61e1051a39Sopenharmony_ciclose F;
62