<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__)
    .' is not allowed.' );

/**
 *
 * @package  MediaLibrary
 * @copyright 2021 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Rob de Cleen(rob@decleen.com);
 * Homepage: https://ordasoft.com
 * @version: 4.6 Free
 * @license GNU General Public license version 2 or later; see LICENSE.txt
 *
 */


include_once("../components/com_medialibrary/compat.joomla1.5.php");

global $mosConfig_absolute_path;
require_once ($mosConfig_absolute_path.
    "/administrator/components/com_medialibrary/admin.medialibrary.class.conf.php");
if(version_compare(JVERSION, '3.0', 'lt')) {
 $db = $GLOBALS['database'];
} else {
  $db = JFactory::getDBO();
}
 
function copydir($src,$dst){
  $dir = opendir($src);
     while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if(!copy($src ."/". $file,$dst ."/". $file)){
              echo "copy error! Please copy folders
                ..path_to_joomla/components/com_medialibrary/covers and 
                ..path_to_joomla/components/com_medialibrary/emedia to 
                ..path_to_joomla/tmp/com_medialibrary/ manualy if you want to save electronic documents and 
                picture of media";
            }
        }
     }
}
 
 if($medialibrary_configuration['update'] == 'true'){
   @mkdir($mosConfig_absolute_path."/tmp/com_medialibrary/");
   @mkdir($mosConfig_absolute_path."/tmp/com_medialibrary/covers/");
   @mkdir($mosConfig_absolute_path."/tmp/com_medialibrary/emedia/");
     $dst=$mosConfig_absolute_path."/tmp/com_medialibrary/covers";
     $src=$mosConfig_absolute_path."/components/com_medialibrary/covers";
     copydir($src,$dst);
     $dst=$mosConfig_absolute_path."/tmp/com_medialibrary/emedia";
     $src=$mosConfig_absolute_path."/components/com_medialibrary/emedia";
     copydir($src,$dst);
     //echo 'Sorry, copy do not successful';exit;
   
   
   echo 'database saved';
} else {
   echo 'no update';
   $db->SetQuery("DROP TABLE if exists #__medialibrary");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_categories");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_lend");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_lend_request");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_const");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_const_languages");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_languages");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_review");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_book");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_video");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_music");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_game");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_main_categories");
   $db->query();
   $db->SetQuery("DROP TABLE if exists #__medialibrary_version");
   $db->query();
   
  $db->SetQuery("DROP TABLE #__medialibrary_video_source");
  $db->query();
  $db->SetQuery("DROP TABLE #__medialibrary_track_source");
  $db->query();
  $db->SetQuery("DROP TABLE #__medialibrary_audio_source");
  $db->query();
  $db->SetQuery("DROP TABLE #__medialibrary_mime_types");
  $db->query();
  $db->SetQuery("DROP TABLE #__medialibrary_emedia_files");
  $db->query();
  $db->SetQuery("DROP TABLE #__medialibrary_people");
  $db->query();
  $db->SetQuery("DROP TABLE #__medialibrary_people_connect");
  $db->query();
}



function com_uninstall()
{
echo "Uninstalled! ";
}



