Rev 4 | Go to most recent revision | Details | 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 | |||
23 | function _rec($directory) { |
||
24 | if (!is_dir($directory)) { |
||
25 | die("Invalid directory path $directory\n"); |
||
26 | } |
||
27 | |||
28 | $sfv_file = $directory.'/'.basename($directory).'.sfv'; |
||
29 | |||
30 | if (file_exists($sfv_file)) { |
||
31 | $existing_files = sfv_get_files($sfv_file); |
||
32 | } else { |
||
33 | $existing_files = array(); |
||
34 | } |
||
35 | |||
36 | foreach (scandir($directory) as $file) { |
||
37 | if ($file === '.') continue; |
||
38 | if ($file === '..') continue; |
||
39 | if (strtolower($file) === 'thumbs.db') continue; |
||
40 | if (substr($file, -4) === '.md5') continue; |
||
41 | if (substr($file, -4) === '.sfv') continue; |
||
42 | |||
43 | $fullpath = $directory . '/' . $file; |
||
44 | if (is_dir($fullpath)) { |
||
45 | _rec($fullpath); |
||
46 | } else { |
||
47 | ### echo "$fullpath\n"; |
||
48 | $dir = pathinfo($fullpath, PATHINFO_DIRNAME); |
||
49 | |||
50 | if (!file_exists($sfv_file)) { |
||
51 | file_put_contents($sfv_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
||
52 | } |
||
53 | |||
54 | if (!in_array($file, $existing_files)) { |
||
55 | $crc32 = strtoupper(crc32_file($fullpath)); |
||
56 | file_put_contents($sfv_file, "$file $crc32\r\n", FILE_APPEND); |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | function sfv_get_files($filename) { |
||
63 | $out = array(); |
||
64 | $lines = file($filename); |
||
65 | foreach ($lines as $line) { |
||
66 | $line = rtrim($line); |
||
67 | if ($line == '') continue; |
||
68 | if (substr($line,0,1) == ';') continue; |
||
69 | $out[] = substr($line, 0, strrpos($line, ' ')); |
||
70 | |||
71 | } |
||
72 | return $out; |
||
73 | } |
||
74 | |||
75 | function swapEndianness($hex) { |
||
76 | return implode('', array_reverse(str_split($hex, 2))); |
||
77 | } |
||
78 | |||
79 | function crc32_file($filename, $rawOutput = false) { |
||
80 | $out = bin2hex(hash_file ('crc32b', $filename , true)); |
||
81 | if (hash('crc32b', 'TEST') == 'b893eaee') { |
||
82 | // hash_file() in PHP 5.2 has the wrong Endianess! |
||
83 | // https://bugs.php.net/bug.php?id=47467 |
||
84 | $out = swapEndianness($out); |
||
85 | } |
||
86 | return $out; |
||
87 | } |
||
88 | |||
89 | # --- |
||
90 | |||
91 | if ($argc != 2) { |
||
92 | echo "Syntax: $argv[0] <directory>\n"; |
||
93 | exit(2); |
||
94 | } |
||
95 | |||
96 | if (!is_dir($argv[1])) { |
||
97 | echo "Directory not found\n"; |
||
98 | exit(1); |
||
99 | } |
||
100 | |||
101 | _rec($argv[1]); |
||
102 | |||
103 | echo "Done.\n"; |