#!/bin/bash
# Replaces links to:
# - Static content (images, CSS, Javascript...)
# - SDL Logo
# - Disable page-wide nofollow
# - Disable nofollow in <a> links (http://www.homelandstupidity.us/2005/05/23/nofollow-revisited/ , http://www.dofollow.fr/)
# - Links to non-existing pages (?WikiWord)
# - Links to pages with parameters (WikiWord?action=edit)
# - UserPreferences (don't trick people into local login)
# - TitleIndex with parameters (does not begin with "/cgi/docwiki.cgi")
# - RecentChanges with parameters (does not begin with "/cgi/docwiki.cgi")
# - Form 'action' field
# - "View" link in the top-right corner, so there's a link to the official wiki page
# - Get rid of search-engine unfriendly '/cgi-bin/' for remaining (i.e. local) links
#   Remain absolute (not relative) to handle Topic/SubTopic1 -> Topic/SubTopic2 links
sed -i -e 's,\(src\|href\)="/docwiki/\([^"]\+\)",\1="http://www.libsdl.org/docwiki/\2",g' \
       -e 's,\(src\|href\)="/images/\([^"]\+\)",\1="http://www.libsdl.org/images/\2",g' \
       -e 's|<meta name="robots" content="index,nofollow">|<meta name="robots" content="all">|g' \
       -e 's,<a\([^>]*\) rel="nofollow"\([^>]*\)>,<a\1\2>,g' \
       -e 's,href="/cgi/docwiki.cgi/\([^"]\+\)">?,href="http://www.libsdl.org/cgi/docwiki.cgi/\1">?,g' \
       -e 's,href="/cgi/docwiki.cgi/\([^"]\+?[^"]\+\)",href="http://www.libsdl.org/cgi/docwiki.cgi/\1",g' \
       -e 's,href="/cgi/docwiki.cgi/UserPreferences\([^"]*\)",href="http://www.libsdl.org/cgi/docwiki.cgi/UserPreferences\1",g' \
       -e 's,href="TitleIndex\([^"]*\)",href="http://www.libsdl.org/cgi/docwiki.cgi/TitleIndex\1",g' \
       -e 's,href="RecentChanges\([^"]*\)",href="http://www.libsdl.org/cgi/docwiki.cgi/RecentChanges\1",g' \
       -e 's,action="/cgi/docwiki.cgi/\([^"]\+\)",action="http://www.libsdl.org/cgi/docwiki.cgi/\1",g' \
       -e 's,<a href="/cgi/docwiki.cgi/\([^"]\+\)" title="View">,<a title="View" href="http://www.libsdl.org/cgi/docwiki.cgi/\1">,g' \
       -e 's,href="/cgi/docwiki.cgi/\([^"]\+\)">,href="/sdl.wiki/\1">,g' \
    ../data/pages.new/*

