updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / arena / arena-launcher.pl
blob9fa8bf501a4982164f5fad4f6d1600d6d4f5ee7b
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
5 #=================================================================================
6 # Arena Launcher
7 #=================================================================================
9 # This launcher should work, but I cannot guarantee that,
10 # it was written on Arch Linux and not tried anywhere else
11 # if you want to use it on other system, adjust configuration
12 # below and keep your fingers crossed!
14 # Run it with "--help" option to get help.
16 # Remember that you use it at your own risk :-)
18 #=================================================================================
19 # License
20 #=================================================================================
21 # Copyright (C) 2011 by Andrzej Giniewicz <gginiu@gmail.com>
23 # Permission is hereby granted, free of charge, to any person obtaining a copy
24 # of this software and associated documentation files (the "Software"), to deal
25 # in the Software without restriction, including without limitation the rights
26 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 # copies of the Software, and to permit persons to whom the Software is
28 # furnished to do so, subject to the following conditions:
30 # The above copyright notice and this permission notice shall be included in
31 # all copies or substantial portions of the Software.
33 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 # THE SOFTWARE.
41 #=================================================================================
42 # Configuration variables
43 #=================================================================================
44 my $user_group = 'games';
45 my $arena_path = "/usr/share/games/arena";
46 my $license = "/usr/share/licenses/arena/license";
47 my $dosbox = "/usr/bin/dosbox";
48 my $dosbox_config = "arena.conf";
49 my $arena_dir = "ARENA";
50 my $license_lock = "terms-accepted";
51 my $passwords_file = "passwords.txt";
53 #=================================================================================
54 # Declarations and description of available functions
55 #=================================================================================
57 # check if terms of use were accepted
58 # no arguments
59 # returns boolean
60 sub terms_accepted;
62 # get terms of use
63 # no arguments
64 # returns array of lines
65 sub get_terms;
67 # accept terms of use
68 # no arguments
69 # no return value
70 sub accept_terms;
72 # run Arena, requires that terms of use are already accepted
73 # no arguments
74 # no return value
75 sub run_arena;
77 # run sound setup utility
78 # no arguments
79 # no return value
80 sub run_setup;
82 #=================================================================================
83 # Gory details :-)
84 #=================================================================================
86 use File::Copy qw(copy move);
87 use File::Find qw(find);
88 use File::Path qw(remove_tree);
89 use File::Spec::Functions qw(catfile);
90 use List::Util qw(min max);
92 my $gid = getgrnam($user_group);
94 sub terms_accepted
96 return ( -e catfile($arena_path, $license_lock) );
99 sub accept_terms
101 my $file = catfile($arena_path, $license_lock);
102 open(FILE, ">$file") or die "Cannot create license lock";
103 close(FILE);
104 chmod 0664, $file;
105 chown -1, $gid, $file;
108 sub get_terms
110 open(FILE, "<$license") or die "Cannot open license";
111 my @text = <FILE>;
112 close(FILE);
113 return @text;
116 sub get_passwords
118 my $file = catfile($arena_path, $passwords_file);
119 open(FILE, "<$file") or die "Cannot open passwords file";
120 my @text = <FILE>;
121 close(FILE);
122 return @text;
125 sub fix_dirs;
126 sub fix_dirs
128 my $path = shift;
129 chmod 0775, $path;
130 chown -1, $gid, $path;
131 opendir(DIR, $path) or die "Cannot access target directory";
132 my @files = readdir(DIR);
133 closedir(DIR);
134 @files = grep(!/\./, @files);
135 foreach my $file (@files) {
136 my $full = catfile($path, $file);
137 if ( -d $full) {
138 fix_dirs $full;
139 } else {
140 chmod 0664, $full;
141 chown -1, $gid, $full;
146 sub run_dosbox
148 my ($app, $exit) = @_;
149 terms_accepted or die "Terms of usage not accepted";
150 my $run = catfile($arena_path, $arena_dir, $app);
151 ( -e $run) or die "Cannot find requested application";
152 my $cfg = catfile($arena_path, $dosbox_config);
153 ( -e $cfg) or die "Cannot find dosbox config file";
154 if ($exit) {
155 system($dosbox." ".$run." -exit -conf ".$cfg);
156 } else {
157 system($dosbox." ".$run." -conf ".$cfg);
159 fix_dirs catfile($arena_path, $arena_dir);
162 sub run_arena
164 run_dosbox "ARENA.BAT", 1;
167 sub run_setup
169 run_dosbox "INSTALL.EXE", 1;
172 #=================================================================================
173 # Command line interface, options parsing
174 #=================================================================================
176 use Getopt::Long;
178 my $opt_run_arena=1;
179 my $opt_force_run_arena=0;
180 my $opt_help=0;
181 my $opt_accept_terms=0;
182 my $opt_run_setup=0;
183 my $opt_show_passwords=0;
185 Getopt::Long::Configure('pass_through');
186 GetOptions (
187 'help' => \$opt_help,
188 'accept-terms' => \$opt_accept_terms,
189 'run-arena' => \$opt_force_run_arena,
190 'run-setup' => \$opt_run_setup,
191 'show-passwords' => \$opt_show_passwords,
194 if ($#ARGV >= 0) {
195 foreach my $arg (@ARGV) {
196 print "Unknown option: $arg\n";
198 $opt_help = 1;
201 #=================================================================================
202 # Command line interface, commands
203 #=================================================================================
205 if ($opt_help) {
206 print
208 The Elder Scrolls: Arena launcher
210 usage:
211 arena [options]
213 available conflicting options:
215 --run-setup run the sound setup utility
216 --run-arena when any option is specified Arena will not be started by
217 launcher. This options forces start of game when all other
218 tasks are finished
219 --show-passwords show the spell costs, answers to copy-protection questions
221 --accept-terms accept Arena terms of use
223 --help display this help message
225 exit;
228 if ($opt_accept_terms) {
229 $opt_run_arena=0;
230 accept_terms;
233 if ($opt_run_setup) {
234 $opt_run_arena=0;
235 run_setup;
238 if ($opt_show_passwords) {
239 $opt_run_arena=0;
240 foreach (get_passwords) { print $_ }
243 $opt_run_arena or $opt_force_run_arena or exit;
245 if ( ! terms_accepted ) {
246 foreach (get_terms) { print $_ }
247 my $ans = 0;
248 until ($ans =~ /yes|no/) {
249 print "Do you accept the license? (yes/no) ";
250 $ans = <>;
251 ($ans =~ /yes|no/) or print "Please answer with \"yes\" or \"no\"\n";
253 if ($ans =~ /yes/) {
254 accept_terms
255 } else {
256 print "You should uninstall Arena at once!\n";
257 exit
261 run_arena;