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