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 MD5 files |
||
21 | // If there is already an MD5 file existing, only new files get appended to the existing MD5 files. |
||
22 | |||
23 | function _rec($directory) { |
||
24 | if (!is_dir($directory)) { |
||
25 | die("Invalid directory path $directory\n"); |
||
26 | } |
||
27 | |||
28 | $md5_file = $directory.'/'.basename($directory).'.md5'; |
||
29 | |||
30 | if (file_exists($md5_file)) { |
||
31 | $existing_files = md5_get_files($md5_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($md5_file)) { |
||
51 | file_put_contents($md5_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
||
52 | } |
||
53 | |||
54 | if (!in_array($file, $existing_files)) { |
||
55 | $md5 = strtolower(md5_file($fullpath)); |
||
56 | file_put_contents($md5_file, "$md5 *$file\r\n", FILE_APPEND); |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | function md5_get_files($filename) { |
||
63 | $out = array(); |
||
64 | $lines = file($filename); |
||
65 | foreach ($lines as $line) { |
||
66 | $line = str_replace("\xEF\xBB\xBF",'',$line); |
||
67 | $line = trim($line); |
||
68 | if ($line == '') continue; |
||
69 | $line = str_replace('*', ' ', $line); |
||
70 | $line = str_replace("\t", ' ', $line); |
||
71 | list($checksum, $origname) = explode(' ', $line, 2); |
||
72 | $origname = dirname($filename) . '/' . trim($origname); |
||
73 | $checksum = trim($checksum); |
||
74 | $out[] = $origname; |
||
75 | } |
||
76 | |||
77 | return $out; |
||
78 | } |
||
79 | |||
80 | # --- |
||
81 | |||
82 | if ($argc != 2) { |
||
83 | echo "Syntax: $argv[0] <directory>\n"; |
||
84 | exit(2); |
||
85 | } |
||
86 | |||
87 | if (!is_dir($argv[1])) { |
||
88 | echo "Directory not found\n"; |
||
89 | exit(1); |
||
90 | } |
||
91 | |||
92 | _rec($argv[1]); |
||
93 | |||
94 | echo "Done.\n"; |