<?php

$version = '2.1.29';

function copy_directory($baseDirectory, $directory, $version)
{
    shell_exec("rm -rf src/assets/elfinder/{$directory}");
    shell_exec("cp -rpf {$baseDirectory}/elFinder-{$version}/{$directory} src/assets/");
}

$filename = tempnam(sys_get_temp_dir(), uniqid());
$directory = sys_get_temp_dir() . "/" . uniqid('php', true);
mkdir($directory);

$file = fopen($filename, 'w+');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://github.com/Studio-42/elFinder/archive/{$version}.tar.gz");
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);

shell_exec("tar -xzf {$filename} --directory {$directory}");

copy_directory($directory, 'css', $version);
copy_directory($directory, 'img', $version);
copy_directory($directory, 'js', $version);
copy_directory($directory, 'sounds', $version);
shell_exec("cp -rpf {$directory}/elFinder-{$version}/php src/");
copy("{$directory}/elFinder-{$version}/main.default.js", 'src/assets/main.default.js');