-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.php
103 lines (84 loc) · 3.84 KB
/
installer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
// Set the error reporting level to hide notices and warnings
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
if (posix_geteuid() !== 0)
{
echo "\033[31mThis script require sudo(super user) privilege\033[0m" . PHP_EOL. PHP_EOL;
exit();
}
if(realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "Initializing..." . PHP_EOL;
echo "Processing directory..." . PHP_EOL;
}
//copy folder to target folder, must be on top before calling
/**
* @throws Exception
*/
if(!function_exists('processFileSystem'))
{
function processFileSystem($source, $destDir)
{
// Define the script name
global $currentDirectory, $scriptName, $binDir;
// Running with sudo, get the home directory of the user who invoked sudo
$loggedInUser = $_SERVER['SUDO_USER'];
// Create the destination directory if it doesn't exist
if (!is_dir($destDir)) {
if(!mkdir($destDir, 0755, true)){
throw new Exception('An error occurred while making directory.' . PHP_EOL);
}
chown($destDir, $loggedInUser); //update ownership of file
}
// Get a list of all files and directories in the source directory
$items = scandir($source);
if(!empty($items)){
foreach ($items as $item) {
if ($item[0] !== '.' || $item === '.git') { //filter everything starts with . except .git
$destItem = $destDir . '/' . basename($item);
if (is_dir($source . DIRECTORY_SEPARATOR .$item)) {
// If it's a directory, recursively copy its contents
processFileSystem($source . DIRECTORY_SEPARATOR .$item, $destItem);
} else {
// If it's a file, copy it to the destination
if (!copy($source . DIRECTORY_SEPARATOR .$item, $destItem)) {
throw new Exception('File could not copy.' . PHP_EOL);
}
//move the destination to /bin if its from bin dir
if ($source === $currentDirectory. "/bin") {
$destItem = $binDir .'/'. $scriptName;
if (!copy($source . DIRECTORY_SEPARATOR .$item, $destItem)) {
throw new Exception('File could not copy.' . PHP_EOL);
}
}
}
chown($destItem, $loggedInUser); //update ownership of file
chmod($destItem, 0755); //update permission of file
}
}
}
}
}
try{
global $currentDirectory;
global $dependencyDirectory;
global $binDir;
global $scriptName;
// Define the current directory
$currentDirectory = __DIR__; // Current directory where your script is located
// Define the target directory
$dependencyDirectory = '/usr/local/lib/devironment';
$binDir = '/usr/local/bin';
$scriptName = 'devenv';
processFileSystem($currentDirectory, $dependencyDirectory); //process files to their appropriate destination
if(realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])){
echo "\033[32mInstallation is completed. run '$scriptName' to execute\033[0m". PHP_EOL. PHP_EOL;
}else{
echo json_encode(['status' => 1, 'message' => "Installation completed. run '$scriptName' to execute"]);
}
} catch (Exception $e) {
if(realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "An Error Occurred: " .$e->getMessage(). PHP_EOL;
}else{
echo json_encode(['status' => 0, 'message' => "An Error Occurred: " .$e->getMessage()]);
}
}