#! /bin/bash

set -e

me=${0%.sh}
me=${me##*/}

(

mkdir -p tmp
cd tmp

rsync --progress -vzrltD --partial --stats \
    ftp.rfc-editor.org::rfc-ed-all/tar/RFC-all.tar.gz .

rsync --progress -vzrltD --partial --stats \
    ftp.rfc-editor.org::rfcs/rfc-index.txt ::rfc-index.xml .

date="$( env - ls -l --full RFC-all.tar.gz | \
         awk '{ print $6, " ", $7, " ", $8; }' | sed 's/\.000*//' )"
date2=$( date -u -d "$date" "+%Y%m%d" )

touch -d "$date" $date2

ln -sf RFC-all.tar.gz doc-rfc_${date2}.orig.tar.gz

URLS="
http://www.rfc-editor.org/categories/rfc-best.html
http://www.rfc-editor.org/categories/rfc-draft.html
http://www.rfc-editor.org/categories/rfc-experimental.html
http://www.rfc-editor.org/categories/rfc-fyi.html
http://www.rfc-editor.org/categories/rfc-historic.html
http://www.rfc-editor.org/categories/rfc-informational.html
http://www.rfc-editor.org/categories/rfc-proposed.html
http://www.rfc-editor.org/categories/rfc-standard.html
http://www.rfc-editor.org/categories/rfc-unknown.html
http://www.rfc-editor.org/queue.html
http://www.rfc-editor.org/rfcxx00.html
http://www.rfc-editor.org/rfcxx00.txt
http://www.rfc-editor.org/
"
wget -N $URLS

# Extract the links to the actual drafts and save their filenames
grep '^[0-9][0-9][0-9][0-9][/-][0-9][0-9][/-][0-9][0-9]' queue.html | \
    sed -re 's|.*<a href="[^">]+">([^<]+)</a>|\1|' > $me.include

# and now sync the respective files
rsync --progress -vzrltD --partial --stats \
    --include-from $me.include --exclude '*' \
    ftp.rfc-editor.org::internet-drafts/ .

) 2>&1 | tee $me.log
