<?php

/* 
 * Copyright (c) 2011 James Turner <james@calminferno.net> 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

$dirIterator = new RecursiveDirectoryIterator('.');
$iterator    = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
    // ignore special certain files
    $ignores = array(
        './prep/ftp.html',
        './graphics/jesus-cartoon.html',
        './licenses/dsl.html',
        './licenses/old-licenses/fdl-1.2.html',
        './licenses/old-licenses/gpl-2.0.html',
        './licenses/agpl-3.0.html',
        './licenses/fdl-1.3.html',
        './licenses/gpl-3.0.html',
        './licenses/lgpl-3.0.html',
        './licenses/autoconf-exception-3.0.html',
        './licenses/gcc-exception-3.0.html',
        './licenses/gcc-exception-3.1.html',
        './philosophy/copyright-versus-community-2000.html',
        './philosophy/enforcing-gpl.html',
        './philosophy/freedom-or-power.html',
        './philosophy/gpl-american-dream.html',
        './philosophy/moglen-harvard-speech-2004.html',
        './philosophy/nit-india.html',
        './philosophy/nonsoftware-copyleft.html',
        './philosophy/patent-practice-panel.html',
        './philosophy/self-interest.html',
        './philosophy/software-libre-commercial-viability.html',
        './philosophy/vaccination.html',
        './philosophy/why-audio-format-matters.html',
        './philosophy/sco/questioning-sco.html',
        './philosophy/sco/sco-preemption.html',
        './philosophy/sco/sco-without-fear.html',
        './press/2001-06-28-USENIX.html',
    );

    if (in_array($file, $ignores)) {
        continue;
    }

    // ignore everything under software except at the root level
    if (substr($file, 0, 10) == './software') {
        if (!preg_match('#^./software/[a-zA-Z0-9-]+\.html$#', $file)) {
            continue;
        }
    }

    // ignore everything that's not an html file
    if (substr($file, -5) != '.html') {
        continue;
    } 

    // ignore full translation directories
    if (preg_match('/wwwes|wwwin/', $file)) {
        continue;
    }

    // ignore all translation html files
    if (preg_match('/\.([a-zA-Z-]{1,2}|zh-[a-z]+|pt-[a-z]+)\.html/', $file)) {
        continue;
    }

    $contents = file_get_contents($file);

    if (strpos($contents, 'Verbatim copying') === false &&
        strpos($contents, 'Permission is granted to anyone to make or distribute verbatim copies of') === false) { 
        continue;
    }

    $replace = <<<END
This page is licensed under a <a rel="license"
href="http://creativecommons.org/licenses/by-nd/3.0/us/">Creative
Commons Attribution-NoDerivs 3.0 United States License</a>.
$2
END;

    $pattern  = '#(Verbatim copying.*)(</p>|</P>|<p>|<P>|<HR>)#Us';
    $contents = preg_replace($pattern, $replace, $contents);

    $pattern  = '#(Permission is granted to anyone to make or distribute verbatim copies of.*)(</p>|</P>|<p>|<P>|<HR>|</BLOCKQUOTE>)#Us';
    $contents = preg_replace($pattern, $replace, $contents);

    file_put_contents($file, $contents);
}