#!/bin/sh

echo 'Content-Type: text/html; charset=UTF-8'
echo

# stat -c %Y IP
# date +%s

cat <<_END1_
__HTML5__
        <meta name="robots" content="follow, noindex"/>
        __TITLE__
        <style>
            #itsup
            {
                color: #000000;
                background-color: #22ee22;
                font-size: 125%;
                padding: 3em;
            }
        </style>
    </head>
    <body>
__NAVIGATION__
        <main><div id="content">
            __H1__
            <p>
                It's not always up or the IP address might be different,
                so I'll (automatically) link to the right place below.
                It's supposed to be on <a target="_blank" rel="noopener"
                href="http://thinkpad.oskog97.com/">thinkpad.oskog97.com</a>
                but... :-/
            </p>
_END1_

stale_time=$(($(date +%s) - $(stat -c %Y IP)))
if [ $stale_time -lt 600 ]; then
    IP=$(cat IP)
    A=$(host thinkpad.oskog97.com | cut -d' ' -f4)
    if [ "$A" = "$IP" ]; then
        DNS_status="The thinkpad subdomain's A-record matches the IP address"
        link="http://thinkpad.oskog97.com/"
    else
        DNS_status="<strong>The thinkpad subdomain needs to be updated!</strong>"
        link="http://$IP/"
    fi
    cat <<_END2_
            <div id="itsup">
                <h2>It's up!</h2>
                <p>IP: ${IP}</p>
                <p>${DNS_status}</p>
                <p><a href="$link" target="_blank" rel="noopener">Visit its website</a></p>
            </div>
_END2_
else
    link="https://web.archive.org/web/http://thinkpad.oskog97.com/"
    echo "            <p><a href='$link' rel='noopener' target='_blank'>Visit its website on archive.org</a></p>"
fi

cat <<_END3_
        </div></main>
__FOOTER__
    </body>
</html>
_END3_