* @category   AW
 * @package    AW_FBIntegrator
 * @version    2.2.0
 * @copyright  Copyright (c) 2010-2012 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/AW-LICENSE.txt
 */
function getStoreConfig($path)
{
    $coll = Mage::getModel('core/config_data')->getCollection();
    $coll->getSelect()->where('path=?', $path);
    foreach ($coll as $i) {
        return $i->getValue();
    }
}
$text = getStoreConfig('fbintegrator/facebook/post_link_text');
$link = getStoreConfig('fbintegrator/facebook/post_link');
$template = $text . $link;
$installer = $this;
$installer->startSetup();
$template = $text . $link;
if ($template != '') {
    $upData = array('scope' => 'default', 'scope_id' => '0', 'path' => 'fbintegrator/wall/post_link_template', 'value' => $template);
    $configData = Mage::getModel('core/config_data')->load('fbintegrator/wall/post_link_template', 'path');
    if ($configData->getConfigId()) {
        $configData->setValue($template);
    } else {
        $configData->setData($upData);
    }
    $configData->save();
}
$installer->endSetup();
        return $i->getValue();
    }
}
$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */
$statusProcessed = AW_Helpdeskultimate_Model_Mysql4_Popmessage_Collection::STATUS_PROCESSED;
$installer->startSetup();
$installer->run("\n\nALTER TABLE {$this->getTable('helpdeskultimate/ticket')} ADD `store_id` INT( 4 ) NOT NULL AFTER `uid` ;\nALTER TABLE {$this->getTable('helpdeskultimate/ticket')} ADD INDEX ( `store_id` ) ;\nALTER TABLE {$this->getTable('helpdeskultimate/ticket')} ADD `priority` ENUM( 'urgent', 'asap', 'todo', 'iftime', 'postponed' ) NOT NULL DEFAULT 'todo' AFTER `status`;\nALTER TABLE {$this->getTable('helpdeskultimate/ticket')} ADD INDEX ( `priority` ) ;\n\nALTER TABLE {$this->getTable('helpdeskultimate/message')} ADD INDEX ( `ticket_id` );  \nALTER TABLE {$this->getTable('helpdeskultimate/message')} ADD CONSTRAINT `FK_MSG_TICKET_ID` FOREIGN KEY (`ticket_id`) REFERENCES {$this->getTable('helpdeskultimate/ticket')} (`id`) ON DELETE CASCADE;\n\nALTER TABLE {$this->getTable('helpdeskultimate/popmessage')} ADD `gateway_id` INT( 5 ) NOT NULL AFTER `uid` ;\nALTER TABLE {$this->getTable('helpdeskultimate/popmessage')} ADD INDEX ( `gateway_id` ) ;\nALTER TABLE {$this->getTable('helpdeskultimate/popmessage')} ADD `content_type` VARCHAR( 32 ) NOT NULL DEFAULT 'text/plain', ADD `attachment` BLOB NOT NULL , ADD `attachment_name` VARCHAR( 255 ) NOT NULL ;\n\nUPDATE {$this->getTable('helpdeskultimate/popmessage')} SET `status` = '{$statusProcessed}';\n\nALTER TABLE {$this->getTable('helpdeskultimate/department')} ADD `gateways` TEXT NOT NULL ;\n\nCREATE TABLE IF NOT EXISTS {$this->getTable('helpdeskultimate/gateway')} (\n  `id` int(11) NOT NULL auto_increment,\n  `title` varchar(255) NOT NULL,\n  `email` varchar(64) NOT NULL,\n  `protocol` enum('pop3','imap') NOT NULL default 'pop3',\n  `host` varchar(255) NOT NULL,\n  `port` int(4) NOT NULL,\n  `login` varchar(255) NOT NULL,\n  `password` varchar(255) NOT NULL,\n  `secure` enum('ssl','tls','none') NOT NULL default 'none',\n  `delete_message` tinyint(1) NOT NULL default '0',\n  `is_active` tinyint(1) NOT NULL default '1',\n  `create_tickets` tinyint(1) NOT NULL default '1',\n  PRIMARY KEY  (`id`),\n  KEY `is_active` (`is_active`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\n\nCREATE TABLE IF NOT EXISTS {$this->getTable('helpdeskultimate/proto')} (\n  `id` int(11) NOT NULL auto_increment,\n  `subject` varchar(255) NOT NULL,\n  `from` varchar(255) NOT NULL,\n  `to` varchar(255) NOT NULL,\n  `gateway_id` int(11) NOT NULL,\n  `content` text NOT NULL,\n  `content_type` varchar(32) NOT NULL,\n  `status` enum('pending','processed','failed') NOT NULL default 'pending',\n  `priority` enum('urgent','asap','todo','iftime','postponed') NOT NULL default 'todo',\n  `filename` varchar(255) NOT NULL,\n  `store_id` tinyint(4) NOT NULL,\n  `department_id` int(6) NOT NULL default '0',\n  `order_id` varchar(64) NOT NULL,\n  `source` varchar(64) NOT NULL,\n  PRIMARY KEY  (`id`),\n  KEY `source` (`source`)\n) ENGINE=InnoDB  DEFAULT CHARSET=utf8;\n\n\nCREATE TABLE IF NOT EXISTS {$this->getTable('helpdeskultimate/ticket_flat')} (\n  `item_id` int(11) NOT NULL auto_increment,\n  `ticket_id` int(11) unsigned NOT NULL,\n  `last_reply` datetime default NULL,\n  `last_customer_reply` datetime default NULL,\n  `last_department_reply` datetime default NULL,\n  `total_replies` int(5) NOT NULL,\n  PRIMARY KEY  (`item_id`),\n  KEY `last_reply` (`last_reply`,`total_replies`),\n  KEY `FK_FLAT_TICKET_ID` (`ticket_id`),\n  KEY `last_customer_reply` (`last_customer_reply`,`last_department_reply`)\n) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=338 ;\n\nALTER TABLE {$this->getTable('helpdeskultimate/ticket_flat')}\n  ADD CONSTRAINT `FK_FLAT_TICKET_ID` FOREIGN KEY (`ticket_id`) REFERENCES {$this->getTable('helpdeskultimate/ticket')} (`id`) ON DELETE CASCADE;\n");
$installer->endSetup();
$data1 = array('title' => 'General gateway', 'email' => getStoreConfig('helpdeskultimate/imap/email'), 'protocol' => getStoreConfig('helpdeskultimate/imap/type'), 'host' => getStoreConfig('helpdeskultimate/imap/server'), 'port' => getStoreConfig('helpdeskultimate/imap/port'), 'login' => getStoreConfig('helpdeskultimate/imap/login'), 'password' => getStoreConfig('helpdeskultimate/imap/password'), 'secure' => getStoreConfig('helpdeskultimate/imap/ssl'), 'delete_message' => 0, 'is_active' => 1, 'create_tickets' => 1);
// Convert gateway
$newGw = Mage::getModel('helpdeskultimate/gateway');
if ($data1['email']) {
    $newGw->setData($data1)->save();
}
$data2 = array('name' => getStoreConfig('helpdeskultimate/generaldep/name'), 'contact' => getStoreConfig('helpdeskultimate/generaldep/contact'), 'email' => getStoreConfig('helpdeskultimate/generaldep/contact'), 'notify' => getStoreConfig('helpdeskultimate/generaldep/notify'), 'sender' => getStoreConfig('helpdeskultimate/generaldep/sender'), 'to_admin_new_email' => 'helpdeskultimate_to_admin_new_email', 'to_admin_reply_email' => 'helpdeskultimate_to_admin_reply_email', 'to_admin_reassign_email' => 'helpdeskultimate_to_admin_reassign_email', 'to_customer_reply_email' => 'helpdeskultimate_to_customer_reply_email', 'to_customer_new_email' => 'helpdeskultimate_to_customer_new_email', 'new_from_admin_to_customer' => 'helpdeskultimate_new_from_admin_to_customer', 'enabled' => 1, 'primary_store_id' => '', 'gateways' => '');
// Convert general support department
$newDep = Mage::getModel('helpdeskultimate/department');
if ($data2['email']) {
    $newDep->setData($data2)->save();
}
$generalId = (int) $newDep->getId();
// Start converting tickets now
foreach (Mage::getModel('helpdeskultimate/ticket')->getCollection() as $ticket) {
    if (!$ticket->getDepartmentId()) {
        $ticket->setDepartmentId($generalId);
    }
    $ticket->save();
}
$path = Mage::getBaseDir('media') . DS . 'helpdeskultimate';
if (file_exists($path) || !file_exists($path) && @mkdir($path)) {