#!/usr/bin/env ruby
$:.unshift('vendor/ruby-mediawiki/lib')
require 'mediawiki/dotfile'
@wiki = MediaWiki.dotfile(nil,'entropia')

roh = @wiki.article("Vorlage:GPN7:Fahrplan").text

def cleanse (t)
  t.gsub(/\[\[.*\|(.*)\]\]/, '\1')
end

uhrzeit = nil
titel1 = nil
titel2 = nil
titel3 = nil
tag = 1

roh.split("\n").each do |line|

  if line =~ /^\| *(.*:[0-9]+) *$/
    if uhrzeit
      puts "#{tag} #{uhrzeit} ÜBERALL  #{titel1}" if titel1
      puts "#{tag} #{uhrzeit} NIRGENDS  #{titel2}" if titel2
      puts "#{tag} #{uhrzeit} IRGENDWO    #{titel3}" if titel3
    end

    if uhrzeit && $1 < uhrzeit
      tag += 1
    end

    uhrzeit = $1
    titel1 = nil
    titel2 = nil
    titel3 = nil
  elsif line =~ /^\|(?: *(?:valign=top|align=center) *\|)? *('''(.*)'''|.*(GOTO).*)\s*$/

    # yum
    if $3 == "GOTO"

      # *frickl*
      if line =~ /colspan=2/
        titel1 = ""
      end
      t = ""
    else
      t = $2
    end

    if titel1 == nil
      titel1 = cleanse(t)
    elsif titel2 == nil
      titel2 = cleanse(t)
    else
      titel3 = cleanse(t)
    end
  end

end