public function autoupgradePlugin(Plugin $plugin)
    {

        if(empty($plugin) || !$plugin->isInstalled() || !$plugin->isEnabled())
            return;

        $xml = $this->loadXML($plugin->Path);

        if(empty($xml))
            return;

        if($plugin->Version != ($newversion = strval($xml->info->version))) {
            $plugin->NewVersion = $newversion;
            return;
        }

        if(!$this->TransactionManager->isTransactionInProgress())
            $this->TransactionManager->begin();

        $xmlFile = $plugin->Path.'/plugin.xml';
        $ts = $this->DateFactory->newLocalDate(filemtime($xmlFile));
        $md5 = md5_file($xmlFile);

        $changed = $this->processAspects($plugin, $log);

        if($plugin->Md5 !== $md5)
        {
            $plugin->Md5 = $md5;
            $this->processCMSNavItems($plugin, $log, $xml);
            $this->processElements($plugin, $log, $xml);

            $changed = true;
        }

        // rerun element schemas
        if($changed) {

            $plugin->AutoUpgraded = true;

            if(!$this->TransactionManager->isTransactionInProgress())
                $this->TransactionManager->begin();

//            foreach($this->ElementService->findAll()->getResults() as $element)
//                $this->NodeService->createDBSchema($element);

            $plugin = $this->PluginService->edit($plugin);
        }

    }