forked from poisa/finalizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.php
74 lines (62 loc) · 2.14 KB
/
restore.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
<?php
function restoreMxlFiles($jsonFilePath, $force = false, $interactive = false)
{
if (!file_exists($jsonFilePath)) {
throw new Exception("The JSON file does not exist.");
}
$files = json_decode(file_get_contents($jsonFilePath), true);
if (!$files) {
throw new Exception("Failed to parse the JSON file.");
}
foreach ($files as $slug => $originalPath) {
$originalFilename = pathinfo($originalPath, PATHINFO_FILENAME);
$mxlFilePath = __DIR__.DIRECTORY_SEPARATOR.'scores'.DIRECTORY_SEPARATOR.$slug.'.mxl';
$destinationDir = dirname($originalPath);
$destinationPath = $destinationDir.DIRECTORY_SEPARATOR.$originalFilename.'.mxl';
if (!file_exists($mxlFilePath)) {
echo "MXL file for $slug not found.\n";
continue;
}
if (file_exists($destinationPath)) {
if ($force) {
moveFile($mxlFilePath, $destinationPath);
} elseif ($interactive) {
promptAndMoveFile($mxlFilePath, $destinationPath);
} else {
echo "Skipping file $destinationPath exists.\n";
}
} else {
moveFile($mxlFilePath, $destinationPath);
}
}
}
function moveFile($source, $destination)
{
if (!rename($source, $destination)) {
echo "Failed to move $source to $destination\n";
} else {
echo "Moved $source to $destination\n";
}
}
function promptAndMoveFile($source, $destination)
{
echo "File $destination already exists. Overwrite? (y/N): ";
$handle = fopen("php://stdin", "r");
$response = trim(fgets($handle));
fclose($handle);
if (strtolower($response) === 'y') {
moveFile($source, $destination);
} else {
echo "Skipping $source\n";
}
}
try {
$jsonFilePath = __DIR__.DIRECTORY_SEPARATOR.'scores.json';
$force = in_array('--force', $argv);
$interactive = in_array('--interactive', $argv);
restoreMxlFiles($jsonFilePath, $force, $interactive);
echo "MXL files restored successfully.\n";
} catch (Exception $e) {
echo "Error: ".$e->getMessage()."\n";
exit(1);
}