#!/bin/bash

##############################################################################
#
# 1) Navigate To: https://de.wikitolearn.org/Spezial:Exportieren
#
# 2) Paste this line into the text box: Course:Einstieg_in_die_Programmierung_mit_Python
#
# And check those checkboxes:
#    [x] Nur die aktuelle Version der Seite exportieren
#    [x] Inklusive Vorlagen
#    [x] Als XML-Datei speichern
#
# Click the button "Seite exportieren".
#
# Save XML file.
# --> e.g. WikiToLearn+-+collaborative+textbooks-20161221213127.xml 17 KB
#
#
# 3) Run this script with the downloaded XML as parameter
#    $ ./backup-script.sh WikiToLearn+-+collaborative+textbooks-20161221213127.xml
#
#
# 4) The result is a list of page URLs. Paste them into the textbox from step 2)
#    Click the Export button again.
#
#    The result is an XML file that contains the data of the whole course.
#    --> e.g. WikiToLearn+-+collaborative+textbooks-20161221214958.xml 171 KB
#             (latest revision without history)
#
# See also https://en.wikipedia.org/wiki/Help:Export
#
##############################################################################

COURSE_PREFIX=Course:Einstieg_in_die_Programmierung_mit_Python

ROOT_XML=$1 # give via first argument

echo "Wikimedia XML export of $COURSE_PREFIX: $ROOT_XML"

# Example link:
#    [[Course:Einstieg_in_die_Programmierung_mit_Python/Arbeitsblätter/Binäres Zahlensystem|Binäres Zahlensystem]]
# Would like to extract with the following command:
#      Course:Einstieg_in_die_Programmierung_mit_Python/Arbeitsblätter/Binäres Zahlensystem

# see mostly here: http://www.grymoire.com/Unix/Sed.html#uh-4a
# and party here:  http://stackoverflow.com/questions/19940935/extract-email-addresses-from-text-file-using-regex-with-bash-or-command-line
sed "s/^.*\[\[\($COURSE_PREFIX.*\)|.*\]\].*$/\1/" $ROOT_XML | grep "$COURSE_PREFIX"