/**
  * Start process to install template update
  *
  * @return  void
  */
 public function installPackageAction()
 {
     // Initialize variables
     $joomlaConfig = JFactory::getConfig();
     $packageFile = $joomlaConfig->get('tmp_path') . '/jsn-' . $this->template['id'] . '.zip';
     $packagePath = substr($packageFile, 0, -4);
     $templatePath = JPATH_ROOT . '/templates/' . $this->template['name'];
     // Checking downloaded template package
     if (!is_file($packageFile)) {
         throw new Exception(JText::_('JSN_TPLFW_ERROR_DOWNLOAD_PACKAGE_FILE_NOT_FOUND'));
     }
     // Check if template is copied to another name
     if ($xml = simplexml_load_file($packagePath . '/template/templateDetails.xml')) {
         if (strcasecmp($this->template['name'], trim((string) $xml->name)) != 0) {
             // Update templateDetails.xml with new name
             $content = str_replace((string) $xml->name, $this->template['name'], JFile::read($packagePath . '/template/templateDetails.xml'));
             JFile::write($packagePath . '/template/templateDetails.xml', $content);
         }
     }
     // Get list of files to be updated
     try {
         $update = JSNTplHelper::getFilesBeingUpdated($this->template['name'], $packageFile);
         if (!$update) {
             throw new Exception(JText::_('JSN_TPLFW_ERROR_DOWNLOAD_PACKAGE_FILE_NOT_FOUND'));
         }
     } catch (Exception $e) {
         throw $e;
     }
     // Include template checksum and manifest files
     in_array('template.checksum', $update['edit']) or $update['edit'][] = 'template.checksum';
     in_array('templateDetails.xml', $update['edit']) or $update['edit'][] = 'templateDetails.xml';
     // Import necessary libraries
     jimport('joomla.filesystem.file');
     jimport('joomla.installer.helper');
     // Update the template
     foreach ($update as $action => $files) {
         foreach ($files as $file) {
             if ($action != 'add') {
                 JFile::delete($templatePath . '/' . $file);
             }
             if ($action != 'delete' and JFolder::create(dirname($templatePath . '/' . $file))) {
                 JFile::copy($packagePath . '/template/' . $file, $templatePath . '/' . $file);
             }
         }
     }
     // Move backup file to template directory
     $source = $joomlaConfig->get('tmp_path') . '/' . $this->template['name'] . '_modified_files.zip';
     $target = $templatePath . '/backups/' . date('y-m-d_H-i-s') . '_modified_files.zip';
     if (is_readable($source)) {
         JFile::copy($source, $target);
         // Remove backup file in temporary directory
         filesize($source) != filesize($target) or JFile::delete($source);
     }
     // Clean up temporary data
     JInstallerHelper::cleanupInstall($packageFile, $packagePath);
     // Check if update success
     $messages = JFactory::getApplication()->getMessageQueue();
     if (class_exists('JError')) {
         $messages = array_merge(JError::getErrors(), $messages);
     }
     foreach ($messages as $message) {
         if (is_array($message) and @$message['type'] == 'error' or is_object($message) and (!method_exists($message, 'get') or $message->get('level') == E_ERROR)) {
             $msg = str_replace(JPATH_ROOT, '', is_array($message) ? $message['message'] : $message->getMessage());
             $errors[$msg] = 1;
         }
     }
     if (@count($errors)) {
         throw new Exception('<ul><li>' . implode('</li><li>', array_keys($errors)) . '</li></ul>');
     }
     // Update template version in manifest cache
     $manifest = JSNTplHelper::getManifest($this->template['name'], true);
     $template = JTable::getInstance('extension');
     $template->load(array('type' => 'template', 'element' => $this->template['name']));
     if ($template->extension_id) {
         // Decode manifest cache
         $template->manifest_cache = json_decode($template->manifest_cache);
         // Set new template version
         $template->manifest_cache->version = (string) $manifest->version;
         // Re-encode manifest cache
         $template->manifest_cache = json_encode($template->manifest_cache);
         // Store new data
         $template->store();
     }
     // Update template version in template definition file
     $content = preg_replace('/\\$JoomlaShine_Template_Version = \'[^\']+\';/i', '$JoomlaShine_Template_Version = \'' . (string) $manifest->version . '\';', JFile::read($templatePath . '/template.defines.php'));
     JFile::write($templatePath . '/template.defines.php', $content);
     // Clear backup state
     JFactory::getApplication()->setUserState('jsn-tplfw-backup-done', 0);
     // Clean up compressed files
     $this->_cleanCache();
 }