// ==UserScript==
// @name          SF Auto-Mirror Links
// @description   Transform Sourceforge download links to a preferred mirror
// @namespace     http://www.gozer.org/mozilla/greasemonkey/
// @include       http://*.sourceforge.net/*
// @include       http://sourceforge.net/*
// ==/UserScript==

// http://www.mozdev.org/bugs/show_bug.cgi?id=12838

function GM_Sourceforge_getPref(prefName, defaultValue)
{
  var retval;

  try {
    retval = GM_getValue(prefName, defaultValue);
  } catch(e) {
    retval = defaultValue;
  }

  if(retval == undefined)
    return(null);

  return(retval);
}

function GM_Sourceforge_setPref(prefName, value)
{
  try {
    GM_setValue(prefName, value);
  } catch(e) { }
}

// ----------------------------------------------------------------------------

var GM_Sourceforge_mirrorList = [  // As of Tue Jan 17 17:46:26 CST 2006
    'ufpr',                        // BR
    'voxel',                       // US
    'nchc',                        // TW
    'easynews',                    // US
    'peterhost',                   // ru
    'kent',                        // UK
    'internap',                    // US
    'heanet',                      // IE
    'surfnet',                     // NL
    'optusnet',                    // AU
    'citkit',                      // RU
    'switch',                      // CH
    'ovh',                         // FR
    'puzzle',                      // CH
    'keihanna',                    // JP
    'jaist',                       // JP
    'mesh'                         // DE
  ];

var maxLinks = GM_Sourceforge_getPref('maxLinks');

if(maxLinks == null)
  GM_Sourceforge_setPref('maxLinks', (maxLinks = 1500));

// avoid pages with a large amount of links
if(maxLinks > 0 && document.links.length > maxLinks) {
  /*GM_log('Skipping ' + document.location.href + ' ('
                     + document.links.length + ' links)');*/
  return;
}

// if the user wants the script to apply to all pages, they should edit the
// include list in the greasemonkey ui. it's just too awkward to have both
// gm's includes and the script toggle.
/*var sfonly = GM_Sourceforge_getPref('sfonly');

if(sfonly == null)
  GM_Sourceforge_setPref('sfonly', (sfonly = true));

if(sfonly == true)
  if(!(document.location.href.indexOf('sourceforge.net/') != -1))
    return;*/

var mirror = GM_Sourceforge_getPref('mirror');

if(mirror == null) {
  var i = Math.round(Math.random() * (GM_Sourceforge_mirrorList.length - 1));
  GM_Sourceforge_setPref('mirror', (mirror = GM_Sourceforge_mirrorList[i]));
}

for(var i = 0; i < document.links.length; i++) {
  var href = document.links[i].getAttribute('href');
      
  // http://prdownloads.sourceforge.net/...?downloadfiles
  if(href.indexOf('/prdownloads.sourceforge.net/') != -1) {
    var q = href.indexOf('?');

    if(q != -1)  // strip query string
      href = href.substring(0, q);

    href = href.replace('prdownloads', mirror + '.dl');

    document.links[i].setAttribute('href', href);
  }
}
