#!/usr/bin/env perl

# Copyright 2016-2017 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

# Internal script for generate-documentation to create asciidoc from POD
# in os-autoinst

use strict;
use warnings;

my $adoc;

eval 'use Pod::AsciiDoctor';
eval {
    # make sure compile-all does not barf here
    $adoc = Pod::AsciiDoctor->new();
};

if ($@ || !$adoc) {
    print "Make sure to install Pod::AsciiDoctor if you meant to run this script\n";
    die $@;
}

my $data_dir = "./src/";
opendir(DIR, $data_dir) or die("Cannot read directories: $data_dir");
my @files = grep { /\.pm$/ } readdir DIR;

foreach my $current_file (@files) {

    open(my $ifh, '<', $data_dir . $current_file) or die("Cannot open $current_file");
    $current_file =~ s/^(.*)\.pm$/$1.asciidoc/;
    open(my $ofh, ">", $current_file) or die("Cannot open $current_file");

    print "Transforming $current_file\n";
    $adoc->append("include::header.asciidoc[]");
    $adoc->append("\n");
    $adoc->parse_from_filehandle($ifh);

    print $ofh $adoc->adoc();
    close($ofh);
    close($ifh);

}
