<?php

// NOTE: the core client fetches download.php?xml=1 every so often;
// don't break this!!!
//
// WARNING: it's na test version write i XHTML 1.1!!!
// This code could have some errors and bugs!!!

require_once("docutil.php");
require_once("versions.inc");
require_once("../html/inc/translation.inc");

$client_info = $_SERVER['HTTP_USER_AGENT'];

function latest_version($p) {
    foreach ($p['versions'] as $i=>$v) {
        if (!$dev && is_dev($v)) continue;
        return $v;
    }
    foreach ($p['versions'] as $i=>$v) {
        return $v;
    }
}

function download_link($pname) {
    global $platforms;
    global $url_base;
    $p = $platforms[$pname];
    $v = latest_version($p);
    $file = $v['file'];
    $long_name = $p['name'];
    $num = $v['num'];
    $path = "dl/$file";
    $url = $url_base.$file;
    $dlink = "<a href=\"$url\">$file</a>";
    $s = number_format(filesize($path)/1000000, 2);

    echo "
        <table style=\"border: 4px; padding: 10px\"><tr><td style=\"background-color: #ccccff\">
        <a href=\"$url\" style=\"font-size: 18px; text-decoration: underline\">".tr(DL_DOWNLOAD)."</a>
        <br />".
        sprintf(tr(DL_VERSION_LNAME_SIZE), $num, $long_name, $s )."
        </td></tr></table>
    ";
    if ($pname == 'linux') {
        echo "
            <p>Note: BOINC may be available as a package for for your particular Linux distribution
		(Gentoo, Fedora, Debian, Ubuntu); check this first before downloading from this page.</p>
            <p>Note: 5.9.1+ requires GLIBC 2.4 or better to run. This
		change was required for the benchmarks to be similar
		to the other platforms.</p>
        ";
    }
}

function link_row($pname) {
    echo "<tr><td>";
    if ($pname=='win') {
        echo "<img src=\"images/ico-win.png\" alt=\"Windows\"> <span style=\"font-weight: bold;\">Windows</span>";
    } else if ($pname=='mac') {
        echo "<img src=\"images/ico-osx-uni.png\" alt=\"Mac OS X\"> <span style=\"font-weight: bold;\">Mac OS X</span>";
    } else if ($pname=='linux') {
        echo "<img src=\"images/ico-tux.png\" alt=\"Linux\"> <span style=\"font-weight: bold;\">Linux/x86</span>";
    }
    echo "</td><td>";
    download_link($pname);
    echo "</td></tr>
    ";
}

$apps = array(
    array('classic.jpg', 180, 143),
    array('cpdn_200.jpg', 200, 147),
    array('eah_200.png', 200, 150),
    array('rosetta_at_home.jpg', 200, 150),
    array('qah.200x150.png', 200, 150),
);

function show_pictures() {
    global $apps;
    shuffle($apps);
    $a0 = $apps[0];
    $a1 = $apps[1];
    $f0 = $a0[0];
    $f1 = $a1[0];
    echo "
        <div style=\"max-height: 300px\">
        <img src=\"images/mgrwork.png\"><br />
        <div style=\"position:relative; top:-80px; left:30px\">
            <img src=\"images/$f0\"><br />
        </div>
        <div style=\"position:relative; top:-160px; left:70px\">
            <img src=\"images/$f1\"><br />
        </div>
        </div>
    ";
}

function show_download($pname) {
    echo "
        <table style=\"padding: 10px\"><tr><td style=\"vertical-align: top;\">
        ".tr(DL_WHATS_BOINC)
    ;
    if ($pname) {
        download_link($pname);
    } else {
        echo "<table style=\"padding: 8px\">
        ";
        link_row('win');
        link_row('winx64');
        link_row('mac');
        link_row('linux');
        echo "</table>
        ";
    }
    echo "
        <p>
        <a href=\"system_requirements.php\"><span style=\"white-space: nowrap\">".tr(DL_SYSTEMREQ)."</span></a>
        | <a href=\"release_notes.php\"><span style=\"white-space: nowrap\">".tr(DL_RELNOTES)."</span></a>
        | <a href=\"troubleshoot.php\"><span style=\"white-space: nowrap\">".tr(DL_TROUBLE)."</span></a>
    ";
    if ($pname) {
        //echo " | <a href=\"download.php?all_platforms=1\"><span style=\"white-space: nowrap\">".tr(DL_OTHERSYS)."</span></a>
        echo " | <a href=\"download_all.php\"><span style=\"white-space: nowrap\">".tr(DL_ALLVERSIONS)."</span></a>
        ";
    } else {
        echo " | <a href=\"download_all.php\"><span style=\"white-space: nowrap\">".tr(DL_ALLVERSIONS)."</span></a></p>
        <p>".tr(DL_IF_OTHERTYPES)."</p>
        <ul>
        <li> ".sprintf(tr(DL_MAKEYOUROWN),"<a href=anonymous_platform.php>","</a>")."</li>
        <li> ".sprintf(tr(DL_DL_FROM3RDP),"<a href=download_other.php>","</a>")."</li>
        </ul>
        ";
    }
    echo "
        <p>
        </td><td style=\"vertical-align: top;\">
    ";
    show_pictures();
    echo "
        </td>
        </tr></table>
        <hr />
        <span style=\"font-size: 10px\">
        ".tr(DL_MIRRORS)."
        </span>
    ";
}

if ($_GET['xml']) {
    $args = strstr($_SERVER['REQUEST_URI'], '?');
    Header("Location: download_all.php$args");
    exit();
}

page_head(tr(DL_DOWNLOAD_TITLE));

if ($_GET['all_platforms']) {
    show_download(null);
} else if (strstr($client_info, 'Windows')) {
    if (strstr($client_info, 'Win64')||strstr($client_info, 'WOW64')) {
        show_download('winx64');
    } else {
        show_download('win');
    }
} else if (strstr($client_info, 'Mac')) {
    show_download('mac');
} else if (strstr($client_info, 'Linux')) {
    show_download('linux');
} else {
    show_download(null);
}
page_tail(true);

?>
