1 |
#!@PERL@ |
2 |
|
3 |
use strict; |
4 |
|
5 |
my $prefix = "@prefix@"; |
6 |
my $exec_prefix = "@exec_prefix@"; |
7 |
my $datarootdir = "@datarootdir@"; |
8 |
my $DATADIR = "@datadir@/@PACKAGE@"; |
9 |
|
10 |
my $CONVERT = "@CONVERT@"; |
11 |
my $IDENTIFY = "@IDENTIFY@"; |
12 |
my $RSYNC = "@RSYNC@"; |
13 |
|
14 |
use Getopt::Long; |
15 |
use IO::AIO; |
16 |
use File::Temp; |
17 |
use Crossfire; |
18 |
|
19 |
sub usage { |
20 |
warn <<EOF; |
21 |
Usage: cfutil [-v] [-q] [--force] |
22 |
[--install-arch path] |
23 |
[--install-maps maps] |
24 |
[--print-statedir] |
25 |
[--print-confdir] |
26 |
[--print-datadir] |
27 |
[--print-libdir] |
28 |
[--print-bindir] |
29 |
EOF |
30 |
exit 1; |
31 |
} |
32 |
|
33 |
my $VERBOSE = 1; |
34 |
my $FORCE; |
35 |
my $TMPDIR = "/tmp/cfutil$$~"; |
36 |
|
37 |
END { system "rm", "-rf", $TMPDIR } |
38 |
|
39 |
mkdir $TMPDIR, 0700 |
40 |
or die "$TMPDIR: $!"; |
41 |
|
42 |
sub inst_maps($) { |
43 |
my (undef, $path) = @_; |
44 |
|
45 |
print "installing '$path' to '$DATADIR/maps'\n\n"; |
46 |
|
47 |
if (!-f "$path/regions") { |
48 |
warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
49 |
exit 1 unless $FORCE; |
50 |
} |
51 |
|
52 |
system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; |
53 |
} |
54 |
|
55 |
{ |
56 |
my @PNG; |
57 |
my @ARC; |
58 |
|
59 |
sub commit_png { |
60 |
my ($name, $data) = @_; |
61 |
#warn "$name: commited\n"; |
62 |
} |
63 |
|
64 |
sub add_png($) { |
65 |
my ($path) = @_; |
66 |
|
67 |
my $png; |
68 |
aio_lstat $path, sub { |
69 |
my ($size, $mtime) = (stat _)[7,9]; |
70 |
|
71 |
aio_load $path, $png, sub { |
72 |
if ($_[0] < 0) { |
73 |
warn "$path: $!, skipping.\n"; |
74 |
return; |
75 |
} |
76 |
|
77 |
unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
78 |
warn "$path: not a recongized png file, skipping.\n"; |
79 |
return; |
80 |
} |
81 |
|
82 |
my ($w, $h) = unpack "NN", $1; |
83 |
|
84 |
(my $face = $path) =~ s/^.*\///; |
85 |
my $T = 32; |
86 |
|
87 |
unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
88 |
warn "$path: weird filename, skipping.\n"; |
89 |
return; |
90 |
} |
91 |
|
92 |
if ($w < $T || $h < $T) { |
93 |
warn "$path: too small ($w $h), skipping.\n"; |
94 |
return; |
95 |
} |
96 |
|
97 |
if ($w % $T || $h % $T) { |
98 |
warn "$path: weird png size ($w $h), skipping.\n"; |
99 |
return; |
100 |
} |
101 |
|
102 |
if ($w > $T || $h > $T) { |
103 |
# split |
104 |
} else { |
105 |
# use as-is |
106 |
commit_png $face, $png; |
107 |
} |
108 |
|
109 |
#warn "$path: $w, $h\n"; |
110 |
}; |
111 |
}; |
112 |
|
113 |
IO::AIO::poll; # reduce file-handle pressure |
114 |
} |
115 |
|
116 |
sub add_arc($$) { |
117 |
my ($dir, $file) = @_; |
118 |
|
119 |
my $arc; |
120 |
aio_load "$dir/$file", $arc, sub { # simply pre-cache, as read_arch wants a file :/ |
121 |
my $arc = read_arch "$dir/$file"; |
122 |
}; |
123 |
} |
124 |
|
125 |
sub add_trs($$) { |
126 |
my ($dir, $file) = @_; |
127 |
} |
128 |
|
129 |
sub collect_arch; |
130 |
sub collect_arch { |
131 |
my ($path) = @_; |
132 |
|
133 |
aioreq_pri 4; |
134 |
aio_scandir $path, 4, sub { |
135 |
my ($dirs, $nondirs) = @_; |
136 |
|
137 |
collect_arch "$path/$_" |
138 |
for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
139 |
|
140 |
for my $file (@$nondirs) { |
141 |
if ($file =~ /\.png$/) { |
142 |
add_png "$path/$file"; |
143 |
} elsif ($file =~ /\.trs$/) { |
144 |
add_trs $path, $file; |
145 |
} elsif ($file =~ /\.arc$/) { |
146 |
add_arc $path, $file; |
147 |
} else { |
148 |
warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
149 |
} |
150 |
} |
151 |
}; |
152 |
} |
153 |
|
154 |
sub inst_arch($) { |
155 |
my (undef, $path) = @_; |
156 |
|
157 |
print "installing '$path' to '$DATADIR'\n\n"; |
158 |
|
159 |
if (!-d "$path/treasures") { |
160 |
warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
161 |
exit 1 unless $FORCE; |
162 |
} |
163 |
|
164 |
collect_arch $path; |
165 |
IO::AIO::flush; |
166 |
|
167 |
die "--install-arch not yet implemented\n"; |
168 |
} |
169 |
} |
170 |
|
171 |
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
172 |
GetOptions ( |
173 |
"verbose|v:+" => \$VERBOSE, |
174 |
"quiet|q" => sub { $VERBOSE = 0 }, |
175 |
"force" => sub { $FORCE = 1 }, |
176 |
"install-arch=s" => \&inst_arch, |
177 |
"install-maps=s" => \&inst_maps, |
178 |
"print-statedir" => sub { print "@pkgstatedir@\n" }, |
179 |
"print-datadir" => sub { print "$DATADIR\n" }, |
180 |
"print-confdir" => sub { print "@pkgconfdir@\n" }, |
181 |
"print-libdir" => sub { print "@libdir@/@PACKAGE@\n" }, |
182 |
"print-bindir" => sub { print "@bindir@/@PACKAGE@\n" }, |
183 |
) or usage; |
184 |
|