##
##  wml::imp::generic - Generic Import Of External Resources
##  Copyright (c) 1997-2001 Ralf S. Engelschall, All Rights Reserved. 
##

#  The <preserve>/<restore> tags with multiple arguments require WML 2.0.3
#use wml::mod::version
<require 2.0.3 />

#use wml::std::tags
#use wml::fmt::verbatim
#use wml::fmt::text
#use wml::fmt::pod
#use wml::fmt::sdf

<protect pass=2>
<:
#
#  determine a way to retrieve the data
#
sub existsprg {
    my ($prg) = @_;
    my ($found, $p);

    $found = 0;
    foreach $p (split(/:/, $ENV{'PATH'})) {
        $found = 1 if (-x "$p/$prg");
    }
    return $found;
}
eval 'use LWP::Simple;';
if (not $@) {
    eval 'sub remote_fetch { my($url) = @_; return LWP::Simple::get($url); }'
}
elsif (&existsprg("lynx")) {
    eval 'sub remote_fetch { my($url) = @_; return `lynx -source $url`; }'
}
elsif (&existsprg("wget")) {
    eval 'sub remote_fetch { my($url) = @_; return `cd $(WML_TMPDIR) && mkdir wget.$$ && cd wget.$$ && wget -q -o/dev/null $url && cat * && cd $(WML_TMPDIR) && rm -f wget.$$`; }'
}
elsif (&existsprg("fetch")) {
    eval 'sub remote_fetch { my($url) = @_; return `fetch $url`; }'
}
else {
    eval 'sub remote_fetch { my($url) = @_; 
                      return "Failed to retrieve $url" .
                             "None of {LWP::Simple::get,Lynx,Wget,Fetch} were found"; }'
}
sub local_fetch {
    my ($file) = @_;
    my ($buf);
    local (*FP);

    open(FP, "<$file") || die;
    local ($/) = undef;
    $buf = <FP>;
    close(FP);

    return $buf;
}
:>
</protect>

<define-tag import>
<preserve src format eperlfilter />
<set-var %attributes />
<perl>
{
    my $format      = '<get-var format />';
    my $src         = '<get-var src />';
    my $eperlfilter = '<get-var eperlfilter />';

    if ($format eq '') {
        $format = 'verbatim';
        $format = 'pod'  if ($src =~ m|\.pod$|);
        $format = 'sdf'  if ($src =~ m|\.sdf$|);
        $format = 'text' if ($src =~ m|\.txt$|);
    }

    my $buf;
    if ($src =~ m|^http://| or $src =~ m|^ftp://|) {
        $buf = &remote_fetch($src);
    }
    else {
        $buf = &local_fetch($src);
    }

    my $tmpfile = "$(WML_TMPDIR)/wml.import.$$.tmp";
    local (*TMP);
    open(TMP, ">$tmpfile");
    print TMP $buf;
    close(TMP);

    if ($format eq 'verbatim') {
        $buf = &wml_fmt_verbatim({ FILE => $tmpfile });
    }
    elsif ($format eq 'pod') {
        $buf = &wml_fmt_pod({ FILE        => $tmpfile, 
                              KEEPINDEX   => 1,
                              EPERLFILTER => $eperlfilter });
    }
    elsif ($format eq 'sdf') {
        $buf = &wml_fmt_pod({ FILE => $tmpfile });
    }
    elsif ($format eq 'text') {
        $buf = &wml_fmt_text({ FILE => $tmpfile });
    }

    unlink($tmpfile);

    <perl:print: $buf />
}
</perl>
<restore  src format eperlfilter />
</define-tag>

##EOF##
