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