#!/usr/bin/perl

print "Content-type: text/html\n\n";

use POSIX;
use CGI::Carp "fatalsToBrowser";

my $StripNumber = 1;

use LWP::Simple;

# https://www.webmasterworld.com/perl/5018940.htm
# use HTTP::Tiny;
# sub get {
#    my ($url) = @_;
#    return HTTP::Tiny->new->get($url)->{content};
# }

# https://stackoverflow.com/a/63796968/1424877
# use WWW::Curl::Simple;
# sub get {
#     my ($url) = @_;
#     my $curl = WWW::Curl::Simple->new();
#     return $curl->get($url)->content;
# }

#sub get {
#    my ($url) = @_;
#    return `curl "$url"`
#}

# Simplified from the HTML::Entities package.
sub num_entity {
    return '&lt;' if ($_ eq '<');
    return '&gt;' if ($_ eq '>');
    return '&quot;' if ($_ eq '"');
    return '&amp;' if ($_ eq '&');
    my $result = sprintf "&#x%X;", ord($_[0]);
    return $result;
}

sub encode_entities {
   my $ref;
   if (defined wantarray) {
       my $x = $_[0];
       $ref = \$x;       # copy
   } else {
       $ref = \$_[0];   # modify in-place
   }
   # Encode control chars, high-bit chars, and <&>'"
   $$ref =~ s/([^\n\r\t !\#\$%\(-;=?-~])/num_entity($1)/ge;
}

sub unpack_args {
    my $cgi_args = join('&', @_);
    @_ = split(/&/, $cgi_args);

    foreach my $i (@_) {
        my $varname;
        my $vardata;
        ($varname, $vardata) = split(/=/, $i);
        if ($varname eq "q") {
            $StripNumber = int($vardata);
        }
    }
}

sub htmlize($) {
    my ($foo) = @_;
    $foo =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr(hex($1))/ge;
    $foo =~ s/&quot;/"/gs;
    $foo =~ s/&lt;/</gs;
    $foo =~ s/&gt;/>/gs;
    $foo =~ s/&amp;/&/gs;
    $foo =~ s/\n/<br>/gs;
    return $foo;
}

sub get_page {
    # get the original page from Qwantz
    my $qwantz_url = "https://www.qwantz.com/index.php?comic=${StripNumber}";
    $content = get($qwantz_url);

    # read in whole file, not just one line or paragraph
    undef $/;

    # Get the date.

    $content =~ m/<title>Dinosaur Comics - (.*?)<\/title>/sg;
    my $comicdate = $1;

    # Get the RSS title.

    my $rsstitle = "";
    if ($content =~ m/<span class="rss-title">(.*?)<\/span>/sg) {
        $rsstitle = htmlize($1);
    }

    print <<END
<div align="right"><small>$comicdate</small></div>
<div style="width:30%;float:right" align="right"><small>$rsstitle</small></div>
<h1>Daily Dinosaur Comic</h1>
END
;

    # Get the image(s?) and title text.
    while (
        $content =~ m/<img src=".*comics\/([^"]+)"[^<]+title="([^"]*)"/sg
    ) {
        my $imagepath = $1;
        my $titletext = htmlize($2);
        last if ($imagepath eq "");
        print <<END
<p> from <a href="${qwantz_url}">www.qwantz.com</a>
<img src="https://www.qwantz.com/comics/${imagepath}"/>
<p> ${titletext}

END
;
    }


    # Get the contact e-mail subject joke.
    # The first several comics have no e-mail subject line.
    # For an example of an e-mail body, see comic #250.

    $content =~ m/mailto:ryan\@qwantz.com\?subject=([^"]*)"/sg;
    my $emailtext = $1;
    my $replytext = "";

    # LWP::Simple::get() encodes "&body=" as "%26body%3D"
    # for some reason. Decode percent-sign hex values here.

    $emailtext =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr(hex($1))/ge;

    my $numfields = ($emailtext =~ /([^\&]*)\&body=(.*)/s);
    print "<!-- emtext=\"$emailtext\" numfields=$numfields -->";
    if ($numfields >= 1 && !($2 eq "")) {
        $emailtext = htmlize($1);
        $replytext = htmlize($2);
    } else {
        $emailtext = htmlize($emailtext);
    }

    if ($replytext eq "") {
        print "<p><b>Subject:</b> ${emailtext}\n\n";
    } else {
        print "<p><b>Subject:</b> ${emailtext} <i>${replytext}</i>\n\n";
    }
}




my $query;
if (!defined $ENV{'QUERY_STRING'}) {
    $query = "";
} else {
    $query = $ENV{'QUERY_STRING'};
}
my @args = split('&', $query);
unpack_args(@args);

print <<END
<html>
<head>
<title>Daily Dinosaur Mirror</title>
</head>
<body>
END
;
get_page();

$prev = $StripNumber - 1;
$next = $StripNumber + 1;
# These strips are missing for some reason?
if ($StripNumber == 1372) { $next = 1379; }
if ($StripNumber == 1379) { $prev = 1372; }

if ($prev > 0) {
    print <<END
<p><p>
<a href="dino.cgi?q=${prev}">previous</a> &mdash; &mdash; 
&mdash; <a href="dino.cgi?q=${next}">next</a>
END
;
} else {
    print <<END
<p><p>
first strip &mdash; &mdash; &mdash; <a href="dino.cgi?q=${next}">next</a>
END
;
}

print <<END
</body>
</html>
END
;
exit 0;