#!/bin/bash

# Copyright (C) 2015  Stefano Zacchiroli <zack@upsilon.cc>
# License: GNU General Public License version 3, or any later version

# Depends: rsync, wget

die_usage () {
    echo "Usage: export-category URL DESTDIR CATEGORY.."
    exit 2
}

if [ -z "$1" -o -z "$2" -o -z "$3" ] ; then
    die_usage
fi
wiki_url="$1" ; shift 1
destdir="$1" ; shift 1
categories="$@"

export_tmp=$(mktemp -td tmp.mw_export.XXXXXXXXXX)
url_list=$(mktemp -t tmp.mw_urls.XXXXXXXXXX)
trap "rm -rf $url_list" EXIT

mw-ls-category --list-urls --include-roots --no-ssl-verify \
    "$wiki_url" $categories > "$url_list"

wget \
    --span-hosts \
    --adjust-extension \
    --page-requisites \
    --convert-links \
    --no-check-certificate \
    --quiet \
    --directory-prefix "$export_tmp" \
    --input-file "$url_list"

echo 'Options +Indexes' > "${export_tmp}/.htaccess"

rsync -au --delete --quiet "${export_tmp}/" "${destdir}/"
