<?php

require_once 'app/Mage.php';
umask(0);
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$writeConnection = $resource->getConnection('core_write');


#### START security patch lpapadopoulos@amdtelecom.net */
Mage::init(); // 1.5+
Mage::app(); // (pretty much anything) below 1.5
Mage::getSingleton('core/session', array('name'=>'adminhtml'));
//verify if the user is logged in to the backend
if(Mage::getSingleton('admin/session')->isLoggedIn()){
  //echo "Admin Logged in";
}
else
{
  die();
}
#### END security patch lpapadopoulos@amdtelecom.net */
$tableName = $resource->getTableName('core_config_data');

$query1 = "DELETE FROM ".$tableName." WHERE path = 'routeesection/routeegroup/routee_username'";
$writeConnection->query($query1);

$query2 = "DELETE FROM ".$tableName." WHERE path = 'routeesection/routeegroup/routee_pwd'";
$writeConnection->query($query2);

$query3 = "DELETE FROM ".$tableName." WHERE path = 'routeesection/routeegroup/routee_mmid'";
$writeConnection->query($query3);

$query4 = "DELETE FROM ".$tableName." WHERE path = 'routeesection/routeegroup/routee_bulkimport'";
$writeConnection->query($query4);


/*  #### START uninstall patch lpapadopoulos */
$uuid = Mage::getStoreConfig('routeesection/routeegroup/routee_mmid');

$eventdata = array();
$eventdata['data'] = [];
$eventdata['uuid'] = $uuid;
$eventdata['event'] = "Uninstall";

$stockdata = Mage::helper('core')->jsonEncode($eventdata);

$url = "https://idata.routee.net/api/event";

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $stockdata);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_TIMEOUT, 30);
$response = curl_exec($c);
curl_close($c);

#Mage::log("plugin uninstalled", null, "myroutee.log");
#### END uninstall patch lpapadopoulos */

unlink('app/code/local/Ambient/Routee/Block/Adminhtml/System/Config/Form/Button.php');
unlink('app/code/local/Ambient/Routee/Block/Adminhtml/System/Config/Form/Buttonproduct.php');
unlink('app/code/local/Ambient/Routee/Block/Index.php');
unlink('app/code/local/Ambient/Routee/Helper/Data.php');
unlink('app/code/local/Ambient/Routee/Model/Observer.php');
unlink('app/code/local/Ambient/Routee/controllers/Adminhtml/RouteeController.php');
unlink('app/code/local/Ambient/Routee/controllers/IndexController.php');
unlink('app/code/local/Ambient/Routee/etc/adminhtml.xml');
unlink('app/code/local/Ambient/Routee/etc/config.xml');
unlink('app/code/local/Ambient/Routee/etc/system.xml');
unlink('app/design/adminhtml/default/default/template/routee/system/config/button.phtml');
unlink('app/design/adminhtml/default/default/template/routee/system/config/button_product.phtml');
unlink('app/etc/modules/Ambient_Routee.xml');

rmdir('app/code/local/Ambient/Routee/Block/Adminhtml/System/Config/Form');
rmdir('app/code/local/Ambient/Routee/Block/Adminhtml/System/Config');
rmdir('app/code/local/Ambient/Routee/Block/Adminhtml/System');
rmdir('app/code/local/Ambient/Routee/Block/Adminhtml');
rmdir('app/code/local/Ambient/Routee/Block');
rmdir('app/code/local/Ambient/Routee/Helper');
rmdir('app/code/local/Ambient/Routee/Model');
rmdir('app/code/local/Ambient/Routee/controllers/Adminhtml');
rmdir('app/code/local/Ambient/Routee/controllers');
rmdir('app/code/local/Ambient/Routee/etc');
rmdir('app/code/local/Ambient/Routee');
rmdir('app/design/adminhtml/default/default/template/routee/system/config');
rmdir('app/design/adminhtml/default/default/template/routee/system');
rmdir('app/design/adminhtml/default/default/template/routee');

unlink('uninstall_routee.php');

echo "Done Uninstall module";
