Rev 13 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 13 | Rev 15 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #!/usr/bin/php |
1 | #!/usr/bin/php |
2 | <?php |
2 | <?php |
3 | 3 | ||
4 | /* |
4 | /* |
5 | Copyright 2020-2021 Daniel Marschall, ViaThinkSoft |
5 | Copyright 2020-2022 Daniel Marschall, ViaThinkSoft |
6 | 6 | ||
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
9 | You may obtain a copy of the License at |
10 | 10 | ||
Line 18... | Line 18... | ||
18 | */ |
18 | */ |
19 | 19 | ||
20 | // This script generates SFV files |
20 | // This script generates SFV files |
21 | // If there is already an SFV file existing, only new files get appended to the existing SFV files. |
21 | // If there is already an SFV file existing, only new files get appended to the existing SFV files. |
22 | 22 | ||
23 | // TODO: make use of STDERR and return different exit codes |
- | |
24 | - | ||
25 | function _rec($directory) { |
23 | function _rec($directory) { |
26 | if (!is_dir($directory)) { |
24 | if (!is_dir($directory)) { |
27 | die("Invalid directory path $directory\n"); |
25 | fwrite(STDERR, "Invalid directory path '$directory'\n"); |
- | 26 | return false; |
|
28 | } |
27 | } |
29 | 28 | ||
30 | if ((basename($directory) == '.') || (basename($directory) == '..')) { |
29 | if ((basename($directory) == '.') || (basename($directory) == '..')) { |
31 | $sfv_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
30 | $sfv_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
32 | basename(realpath($directory)).'.sfv'; |
31 | basename(realpath($directory)).'.sfv'; |
Line 42... | Line 41... | ||
42 | $existing_files = array(); |
41 | $existing_files = array(); |
43 | } |
42 | } |
44 | 43 | ||
45 | $sd = @scandir($directory); |
44 | $sd = @scandir($directory); |
46 | if ($sd === false) { |
45 | if ($sd === false) { |
47 | echo "Error: Cannot scan directory $directory\n"; |
46 | fwrite(STDERR, "Error: Cannot scan directory $directory\n"); |
48 | return; |
47 | return false; |
49 | } |
48 | } |
50 | 49 | ||
51 | foreach ($sd as $file) { |
50 | foreach ($sd as $file) { |
52 | if ($file === '.') continue; |
51 | if ($file === '.') continue; |
53 | if ($file === '..') continue; |
52 | if ($file === '..') continue; |
- | 53 | if (substr($file,0,1) === '.') continue; |
|
54 | if (strtolower($file) === 'thumbs.db') continue; |
54 | if (strtolower($file) === 'thumbs.db') continue; |
55 | if (strtolower(substr($file, -4)) === '.md5') continue; |
55 | if (strtolower(substr($file, -4)) === '.md5') continue; |
56 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
56 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
57 | 57 | ||
58 | $fullpath = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file; |
58 | $fullpath = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file; |
Line 72... | Line 72... | ||
72 | $crc32 = strtoupper(crc32_file($fullpath)); |
72 | $crc32 = strtoupper(crc32_file($fullpath)); |
73 | file_put_contents($sfv_file, "$file $crc32\r\n", FILE_APPEND); |
73 | file_put_contents($sfv_file, "$file $crc32\r\n", FILE_APPEND); |
74 | } |
74 | } |
75 | } else { |
75 | } else { |
76 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
76 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
77 | echo "Warning: $fullpath is not a regular file!\n"; |
77 | fwrite(STDERR, "Warning: $fullpath is not a regular file!\n"); |
78 | } |
78 | } |
79 | } |
79 | } |
- | 80 | ||
- | 81 | return true; |
|
80 | } |
82 | } |
81 | 83 | ||
82 | function sfv_get_files($filename) { |
84 | function sfv_get_files($filename) { |
83 | $out = array(); |
85 | $out = array(); |
84 | $lines = file($filename); |
86 | $lines = file($filename); |
Line 108... | Line 110... | ||
108 | 110 | ||
109 | # --- |
111 | # --- |
110 | 112 | ||
111 | $show_verbose = false; |
113 | $show_verbose = false; |
112 | $do_recursive = false; |
114 | $do_recursive = false; |
113 | $dir = ''; |
115 | $dirs = array(); |
114 | 116 | ||
115 | for ($i=1; $i<$argc; $i++) { |
117 | for ($i=1; $i<$argc; $i++) { |
116 | if ($argv[$i] == '-v') { |
118 | if ($argv[$i] == '-v') { |
117 | $show_verbose = true; |
119 | $show_verbose = true; |
118 | } else if ($argv[$i] == '-r') { |
120 | } else if ($argv[$i] == '-r') { |
119 | $do_recursive = true; |
121 | $do_recursive = true; |
120 | } else { |
122 | } else { |
121 | $dir = $argv[$i]; |
123 | $dirs[] = $argv[$i]; |
122 | } |
124 | } |
123 | } |
125 | } |
124 | 126 | ||
125 | if (empty($dir)) { |
127 | if (count($dirs) == 0) { |
126 | echo "Syntax: $argv[0] [-v] [-r] <directory>\n"; |
128 | echo "Syntax: $argv[0] [-v] [-r] <directory> [<directory> [...]]\n"; |
127 | exit(2); |
129 | exit(2); |
128 | } |
130 | } |
129 | 131 | ||
- | 132 | $res = 0; |
|
130 | if (!is_dir($dir)) { |
133 | foreach ($dirs as $dir) { |
131 | echo "Directory not found\n"; |
134 | if (!_rec($dir)) $res = 1; |
132 | exit(1); |
- | |
133 | } |
135 | } |
134 | - | ||
135 | _rec($dir); |
- | |
136 | - | ||
137 | if ($show_verbose) echo "Done.\n"; |
136 | if ($show_verbose) echo "Done.\n"; |
- | 137 | exit($res); |