#!/usr/bin/env zsh
SITEMAP_TEMP_FILE=temp.xml

BLOCK_TIP_HEIGHT="$(curl -s https://mempool.space/electrs/mainnet/blocks/tip/height)"
BLOCK_HEIGHT=0
BLOCK_PER_FILE=100

FILE_TOTAL="$(($BLOCK_TIP_HEIGHT / $BLOCK_PER_FILE))"
FILE_START=1
FILE_STOP="${FILE_TOTAL}"
FILE="${FILE_START}"

echo "${BLOCK_TIP_HEIGHT} blocks"
echo "${BLOCK_PER_FILE} blocks per sitemap"
echo "${FILE_TOTAL} total sitemaps"

echo '<?xml version="1.0" encoding="UTF-8"?>' > "${SITEMAP_TEMP_FILE}"
echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' >> "${SITEMAP_TEMP_FILE}"

until [ "${FILE}" = "${FILE_STOP}" ];do

BLOCK_HEIGHT=$((($FILE * $BLOCK_PER_FILE) - 1))
if [ "${BLOCK_HEIGHT}" -gt "${BLOCK_TIP_HEIGHT}" ];then
	BLOCK_HEIGHT="${BLOCK_TIP_HEIGHT}"
fi
BLOCK_HASH=$(curl -s https://mempool.space/electrs/mainnet/block-height/${BLOCK_HEIGHT})
BLOCK=$(curl -s https://mempool.space/electrs/mainnet/block/${BLOCK_HASH})
BLOCK_TIMESTAMP=$(echo "${BLOCK}"|sed -e 's/.*timestamp.://' -e 's/,.*//')

SITEMAP_FILE=$(printf "sitemap/%05d.xml" "${FILE}")
SITEMAP_LASTMOD=$(date -r "${BLOCK_TIMESTAMP}" +"%Y-%m-%dT%H:%M:%SZ")

if [ ! -f "${SITEMAP_FILE}" ] || [ "${BLOCK_HEIGHT}" -gt $(($BLOCK_TIP_HEIGHT - 2000)) ];then

    BLOCK_STOP="$((($BLOCK_HEIGHT - ${BLOCK_PER_FILE}) + 1))"
    echo "generating ${SITEMAP_FILE} for block ${BLOCK_STOP} ~ ${BLOCK_HEIGHT}"

    echo '<?xml version="1.0" encoding="UTF-8"?>' > "${SITEMAP_FILE}"
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' >> "${SITEMAP_FILE}"

    while [ "${BLOCK_HEIGHT}" -ge "${BLOCK_STOP}" ];do

        BLOCK_HASH=$(curl -s https://mempool.space/electrs/mainnet/block-height/${BLOCK_HEIGHT})
        BLOCK=$(curl -s https://mempool.space/electrs/mainnet/block/${BLOCK_HASH})
        BLOCK_TIMESTAMP=$(echo "${BLOCK}"|sed -e 's/.*timestamp.://' -e 's/,.*//')
        BLOCK_LASTMOD=$(date -r "${BLOCK_TIMESTAMP}" +"%Y-%m-%dT%H:%M:%SZ")

        echo '<url>' >> "${SITEMAP_FILE}"
        echo "<loc>https://mempool.space/block/${BLOCK_HASH}</loc>" >> "${SITEMAP_FILE}"
        echo "<lastmod>${BLOCK_LASTMOD}</lastmod>" >> "${SITEMAP_FILE}"
        echo '</url>' >> "${SITEMAP_FILE}"

        ((BLOCK_HEIGHT--))
    done

    echo '</urlset>' >> "${SITEMAP_FILE}"
fi

echo '<sitemap>' >> "${SITEMAP_TEMP_FILE}"
echo "<loc>https://mempool.space/${SITEMAP_FILE}</loc>" >> "${SITEMAP_TEMP_FILE}"
echo "<lastmod>${SITEMAP_LASTMOD}</lastmod>" >> "${SITEMAP_TEMP_FILE}"
echo '</sitemap>' >> "${SITEMAP_TEMP_FILE}"

((FILE++))

done

echo '</sitemapindex>' >> "${SITEMAP_TEMP_FILE}"
mv "${SITEMAP_TEMP_FILE}" "sitemap.xml"
