Rev 5 | Rev 12 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | #!/usr/bin/php |
2 | <?php |
||
3 | |||
4 | /* |
||
5 | Copyright 2020 Daniel Marschall, ViaThinkSoft |
||
6 | |||
7 | Licensed under the Apache License, Version 2.0 (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 |
||
10 | |||
11 | http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | |||
13 | Unless required by applicable law or agreed to in writing, software |
||
14 | distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | See the License for the specific language governing permissions and |
||
17 | limitations under the License. |
||
18 | */ |
||
19 | |||
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. |
||
22 | |||
4 | daniel-mar | 23 | // TODO: make use of STDERR and return different exit codes |
24 | |||
2 | daniel-mar | 25 | function _rec($directory) { |
26 | if (!is_dir($directory)) { |
||
27 | die("Invalid directory path $directory\n"); |
||
28 | } |
||
29 | |||
11 | daniel-mar | 30 | if ((basename($directory) == '.') || (basename($directory) == '..')) { |
31 | $sfv_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
||
32 | basename(realpath($directory)).'.sfv'; |
||
2 | daniel-mar | 33 | |
11 | daniel-mar | 34 | } else { |
35 | $sfv_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
||
36 | basename($directory).'.sfv'; |
||
37 | } |
||
38 | |||
2 | daniel-mar | 39 | if (file_exists($sfv_file)) { |
40 | $existing_files = sfv_get_files($sfv_file); |
||
41 | } else { |
||
42 | $existing_files = array(); |
||
43 | } |
||
44 | |||
5 | daniel-mar | 45 | $sd = @scandir($directory); |
46 | if ($sd === false) { |
||
47 | echo "Error: Cannot scan directory $directory\n"; |
||
48 | return; |
||
49 | } |
||
50 | |||
51 | foreach ($sd as $file) { |
||
2 | daniel-mar | 52 | if ($file === '.') continue; |
53 | if ($file === '..') continue; |
||
54 | if (strtolower($file) === 'thumbs.db') continue; |
||
5 | daniel-mar | 55 | if (strtolower(substr($file, -4)) === '.md5') continue; |
56 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
||
2 | daniel-mar | 57 | |
58 | $fullpath = $directory . '/' . $file; |
||
59 | if (is_dir($fullpath)) { |
||
60 | _rec($fullpath); |
||
5 | daniel-mar | 61 | } else if (is_file($fullpath)) { |
4 | daniel-mar | 62 | global $show_verbose; |
63 | if ($show_verbose) echo "$fullpath\n"; |
||
2 | daniel-mar | 64 | $dir = pathinfo($fullpath, PATHINFO_DIRNAME); |
65 | |||
66 | if (!file_exists($sfv_file)) { |
||
67 | file_put_contents($sfv_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
||
68 | } |
||
69 | |||
70 | if (!in_array($file, $existing_files)) { |
||
71 | $crc32 = strtoupper(crc32_file($fullpath)); |
||
72 | file_put_contents($sfv_file, "$file $crc32\r\n", FILE_APPEND); |
||
73 | } |
||
5 | daniel-mar | 74 | } else { |
75 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
||
76 | echo "Warning: $fullpath is not a regular file!\n"; |
||
2 | daniel-mar | 77 | } |
78 | } |
||
79 | } |
||
80 | |||
81 | function sfv_get_files($filename) { |
||
82 | $out = array(); |
||
83 | $lines = file($filename); |
||
84 | foreach ($lines as $line) { |
||
85 | $line = rtrim($line); |
||
86 | if ($line == '') continue; |
||
87 | if (substr($line,0,1) == ';') continue; |
||
88 | $out[] = substr($line, 0, strrpos($line, ' ')); |
||
89 | |||
90 | } |
||
91 | return $out; |
||
92 | } |
||
93 | |||
94 | function swapEndianness($hex) { |
||
95 | return implode('', array_reverse(str_split($hex, 2))); |
||
96 | } |
||
97 | |||
98 | function crc32_file($filename, $rawOutput = false) { |
||
99 | $out = bin2hex(hash_file ('crc32b', $filename , true)); |
||
100 | if (hash('crc32b', 'TEST') == 'b893eaee') { |
||
101 | // hash_file() in PHP 5.2 has the wrong Endianess! |
||
102 | // https://bugs.php.net/bug.php?id=47467 |
||
103 | $out = swapEndianness($out); |
||
104 | } |
||
105 | return $out; |
||
106 | } |
||
107 | |||
108 | # --- |
||
109 | |||
4 | daniel-mar | 110 | $show_verbose = false; |
111 | $dir = ''; |
||
112 | |||
113 | for ($i=1; $i<$argc; $i++) { |
||
114 | if ($argv[$i] == '-v') { |
||
115 | $show_verbose = true; |
||
116 | } else { |
||
117 | $dir = $argv[$i]; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | if (empty($dir)) { |
||
122 | echo "Syntax: $argv[0] [-v] <directory>\n"; |
||
2 | daniel-mar | 123 | exit(2); |
124 | } |
||
125 | |||
4 | daniel-mar | 126 | if (!is_dir($dir)) { |
2 | daniel-mar | 127 | echo "Directory not found\n"; |
128 | exit(1); |
||
129 | } |
||
130 | |||
4 | daniel-mar | 131 | _rec($dir); |
2 | daniel-mar | 132 | |
4 | daniel-mar | 133 | if ($show_verbose) echo "Done.\n"; |