function selection(id)
{
  select = document.getElementById(id);
  value = select.options[select.selectedIndex].value;

  // These should never hit since we control the "value" attributes
  // But just in case someone decides to mess around with us....
  value.replace(/&/g, '&amp;');
  value.replace(/</g, '&lt;');
  value.replace(/>/g, '&gt;');

  return value;
}


function update()
{
  distro = selection('distro');
  tool = selection('tool');
  base = document.URL.replace(/[^\/]*$/, 'rpm');

  splits = distro.split('-');
  name = splits[0];
  version = splits[1];
  arch = splits[2];

  step = 'choose a supported distribution and RPM updating tool.';
  steps = '';

  switch (distro)
    {
    case 'fedora-1-i386':
    case 'fedora-2-i386':
      switch (tool)
	{
	case 'apt-get':
	  step = 'save the following line in a new file named <q><kbd>/etc/apt/sources.list.d/sampler.list</kbd></q>:';
	  steps = '<pre class="file">rpm ' + base + ' ' + distro + ' apps tools</pre>';
	  break;

	case 'smart':
	  step = 'save the following lines in a new file named <q><kbd>/etc/smart/channels/cbi.channel</kbd></q>:';
	  longname = 'Fedora Core ' + version + ' (' + arch + ')';
	  base += '/' + distro + '/';
	  lines = new Array(
	    '[cbi-apps]',
	    'name = CBI instrumented applications for ' + longname,
	    'baseurl = ' + base + 'RPMS.apps/',
	    'type = rpm-md',
	    'priority = 10',
	    '',
	    '[cbi-tools]',
	    'name = CBI support tools for ' + longname,
	    'baseurl = ' + base + 'RPMS.tools/',
	    'type = rpm-md',
	    'priority = 10'
	  );
	  steps = '<pre class="file">' + lines.join('\n') + '</pre>';
	  break;

	case 'up2date':
	  step = 'add the following lines to the end of <q><kbd>/etc/sysconfig/rhn/sources</kbd></q>:';
	  base += '/' + name + '-' + version + '-$ARCH/';
	  lines = new Array(
	    'yum cbi-apps  ' + base + 'RPMS.apps/',
	    'yum cbi-tools ' + base + 'RPMS.tools/'
	  );
	  steps = '<pre class="file">' + lines.join('\n') + '</pre>';
	  break;

	case 'PackageKit':
	case 'yum':
	  step = 'add the following lines to the end of <q><kbd>/etc/yum.conf</kbd></q>:';
	  base += '/fedora-$releasever-$basearch/',
	  lines = new Array(
	    '[cbi-apps]',
	    'name = CBI instrumented applications for Fedora Core $releasever ($basearch)',
	    'baseurl = ' + base + 'RPMS.apps/',
	    'gpgcheck = 1',
	    'gpgkey = http://www.cs.wisc.edu/cbi/downloads/public-key.asc',
	    '',
	    '[cbi-tools]',
	    'name = CBI support tools for Fedora Core $releasever ($basearch)',
	    'baseurl = ' + base + 'RPMS.tools/',
	    'gpgcheck = 1',
	    'gpgkey = http://www.cs.wisc.edu/cbi/downloads/public-key.asc'
	  );
	  steps = '<pre class="file">' + lines.join('\n') + '</pre>';
	  break;
	}
      break;

    default:
      releases = {
	'fedora-4-i386': '7',
	'fedora-5-i386': '7',
	'fedora-6-i386': '7',
	'fedora-7-i386': '8',
	'fedora-8-i386': '9',
	'fedora-9-i386': '10',
	'fedora-10-i386': '10',
	'fedora-11-i386': '10',
	'fedora-12-i386': '10'
      };
      release = releases[distro];

      cpus = {
	'fedora-4-i386': 'i386',
	'fedora-5-i386': 'i386',
	'fedora-6-i386': 'i386',
	'fedora-7-i386': 'i386',
	'fedora-8-i386': 'i386',
	'fedora-9-i386': 'i386',
	'fedora-10-i386': 'i386',
	'fedora-11-i386': 'i586',
	'fedora-12-i386': 'i686'
      };
      cpu = cpus[distro];

      url = base + '/' + distro + '/RPMS.tools/cbi-package-config-' + version + '-' + release + '.' + cpu + '.rpm';
      step = 'download and install the <a href="' + url + '">CBI package configuration RPM</a>.  The next time you run <tt>' + tool + '</tt>, it will include packages from the CBI package repository.';
      steps = '';
    }

  document.getElementById('step').innerHTML = step;
  document.getElementById('steps').innerHTML = steps;
}
