*	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
require_once "functions.inc";
require_once "guiconfig.inc";
require_once 'notices.inc';
include_once "includes/functions.inc.php";
if ($_REQUEST['getupdatestatus']) {
    require_once "pkg-utils.inc";
    $system_version = get_system_pkg_version();
    if ($system_version === false) {
        print gettext("<i>Unable to check for updates</i>");
        exit;
    }
    if (!is_array($system_version) || !isset($system_version['version']) || !isset($system_version['installed_version'])) {
        print gettext("<i>Error in version information</i>");
        exit;
    }
    $version_compare = pkg_version_compare($system_version['installed_version'], $system_version['version']);
    switch ($version_compare) {
        case '<':
            ?>
		<div>
			<?php 
            echo gettext("Version ");
//		next_log_line = Send log file entries that come after this line number
//
// JSON items returned
//		log:
//		exitcode:
//		data:{current:, total}
//
// Todo:
//		Respect next_log_line and append log to output window rather than writing it
if ($_REQUEST['ajax']) {
    $response = "";
    $code = 0;
    // If this is an ajax call to get the installed and newst versions, call that function,
    // JSON encode the result, print it and exit
    if ($_REQUEST['getversion']) {
        $firmwareversions = get_system_pkg_version(true);
        print json_encode($firmwareversions);
        exit;
    }
    // Check to see if our process is still running
    $pidfile = $g['varrun_path'] . '/' . $g['product_name'] . '-upgrade.pid';
    $running = "running";
    // When we do a reinstallall, it is technically possible that we might catch the system in-between
    // packages, hence the de-bounce here
    if (!isvalidpid($pidfile)) {
        usleep(100000);
        if (!isvalidpid($pidfile)) {
            $running = "stopped";
            // The log files may not be complete when the process terminates so we need wait until we see the
            // exit status (__RC=x)
            waitfor_string_in_file($_REQUEST['logfilename'] . '.txt', "__RC=", 10);
                } else {
                    $headline = gettext("Reinstall package");
                }
                break;
            case 'delete':
                $headline = gettext("Remove package");
                break;
            default:
                $headline = gettext("Install package");
                break;
        }
    }
}
if ($_GET && $_GET['id'] == "firmware") {
    $firmwareupdate = true;
    $firmwareversion = get_system_pkg_version();
}
$pgtitle = array(gettext("System"), gettext("Package Manager"), $headline);
include "head.inc";
$tab_array = array();
$tab_array[] = array(gettext("Available packages"), false, "pkg_mgr.php");
$tab_array[] = array(gettext("Installed packages"), false, "pkg_mgr_installed.php");
if ($firmwareupdate) {
    $tab_array[] = array(gettext("System update"), true, "");
} else {
    $tab_array[] = array(gettext("Package Installer"), true, "");
}
if ($firmwareupdate) {
    $tab_array[] = array(gettext("Update Settings"), false, "system_update_settings.php");
}
display_top_tabs($tab_array);