File: gift-setup.in

package info (click to toggle)
gift 0.11.8.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 3,632 kB
  • ctags: 2,715
  • sloc: ansic: 21,670; sh: 8,340; perl: 398; makefile: 281
file content (477 lines) | stat: -rw-r--r-- 11,402 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!@PERL@
###############################################################################
##
## $Id: gift-setup.in,v 1.7 2004/11/14 22:44:25 hexwab Exp $
##
## Wrapper for the configuration templates installed in ${datadir}/giFT.
##
## Copyright (C) 2001-2003 giFT project (gift.sourceforge.net)
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by the
## Free Software Foundation; either version 2, or (at your option) any
## later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
###############################################################################

use strict;
use Data::Dumper;
use IO::File;

###############################################################################

my $datadir = '@giftdatadir@';
my $confdir = "$ENV{HOME}/.giFT";

###############################################################################

main();

sub main
{
	return if (!check_uid ());
		
	my $templates = find_templates ($datadir);
	my $configed  = find_configured ($confdir);

	# no available templates, bogus install
	if (scalar @$templates == 0)
	{
		print <<"__EOF__";

 *** ERROR ***

No available configuration templates were found.  This could mean that you
legitimately have none and need to install the giFT daemon properly or that
you have placed them in an alternate data directory than this script is
looking.  The configuration template path being used is:

	$datadir

__EOF__

		return;
	}

	#
	# Configuration modules which will be read from the available templates.
	# This basically creates a new data structure which is a hash of module
	# entries, including ->{path_cfg} and ->{path_tpl}.  This data
	# structure may be further trimmed through the condition below.
	#
	my $modules_hash = build_modules ($configed, $templates);

	#
	# If there have been previously configured modules, we need to ask the
	# user if they'd like us to rebuild from available templates or only
	# add new configuration templates to the confdir.
	#
	if (scalar @$configed > 0)
	{
		print <<"__EOF__";

 *** WARNING ***

Previous configuration has been detected.  This script is capable of
preserving that configuration and only adding from new available templates.
If you choose no, all presently installed configuration modules will be used
in place of any custom configuration you have.  In the future, previous
configuration will be read and used as defaults, but this script does not
currently have such a feature.

__EOF__

 gotos_are_bad:
		print "Would you like to preserve old configuration? [Yn] ";

		chomp (my $yesno = <>); $yesno = 'y' unless $yesno;
		goto gotos_are_bad unless $yesno =~ m/[yn]/i;

		# print a little separator to help the user organize this output
		print_sep();

		if ($yesno =~ m/y/i)
		{
			#
			# Iterate through each configured entry and remove it from the
			# modules hash constructed above.  This is a horribly hackish
			# approach, but hey, it works and this is perl :)
			#
			foreach my $cfg (@$configed)
			{
				delete $modules_hash->{$cfg->{name}};
			}
		}
	}

	#
	# Convert the hash data structure to a list now that we don't need
	# the delete/exists hack.  This will also take care of fudging the
	# order so that gift and ui are before plugins.
	#
	my @modules = get_modules_list ($modules_hash);

	if (scalar @modules == 0)
	{
		print <<'__EOF__';
No configuration modules are available for processing.  This could be because
you have chosen to preserve old configuration and no new modules have been
added since last configuration.

__EOF__

		return;
	}

	# and away we go...
	my $n = process_modules (@modules);
	printf ("%d module(s) successfully processed.\n", $n);
}

###############################################################################

sub print_sep
{
	print "\n", '#' x 78, "\n\n";
}

###############################################################################

#
# Construct a list of all templates that are installed and available.
#
sub find_templates
{
	return find_conf_modules (shift, "*.conf.template");
}

#
# Construct a list of all configuration files that currently exist in the
# supplied config directory.
#
sub find_configured
{
	return find_conf_modules (shift, "*.conf");
}

sub find_conf_modules
{
	my $dir = shift;
	my $glob = shift;

	my @modules = ();

	#
	# Quick and dirty hack to access all template files in the supplied data
	# dir and the first depth underneath.  I would prefer to avoid using
	# File::Find, if possible.
	#
	foreach my $path ((glob ("$dir/$glob"), glob ("$dir/*/$glob")))
	{
		# just making sure...
		die "Cannot read $path: $!" unless -f $path && -r $path;

		#
		# Expose only the module name, that is the configuration object
		# name, instead of the path and file extension.  For example,
		# if given /usr/local/share/giFT/OpenFT/OpenFT.conf.template,
		# 'OpenFT' should be added as the module.
		#
		my @pathelem = split ('/', $path); my $file = pop @pathelem;
		my ($module) = $file =~ m/^(\w+)/;

		push @modules, { 'name' => $module, 'path' => $path };
	}

	return \@modules;
}

###############################################################################

sub build_modules
{
	my $cfg = shift;
	my $tpl = shift;

	my $modules = {};

	foreach my $template (@$tpl)
	{
		my $module = construct_module ($modules, $template->{name});
		$module->{path_tpl} = $template->{path};

		# try to guess the configuration path, in case no real config
		# module is present by that name...
		$module->{path_cfg} = guess_config_path ($module->{name});
	}

	foreach my $config (@$cfg)
	{
		#
		# Only templates are allowed to construct new module elements, because
		# we dont want to present configuration when no template exists
		# for parsing...
		#
		next unless exists $modules->{$config->{name}};

		# add the path_cfg member to the module object
		my $module = $modules->{$config->{name}};
		$module->{path_cfg} = $config->{path};
	}

	return $modules;
}

# simple short-hand to reduce code duplication
sub construct_module
{
	my $modules = shift;
	my $name    = shift;

	$modules->{$name} = {} unless exists $modules->{$name};

	$modules->{$name}->{name} = $name;
	return $modules->{$name};
}

sub guess_config_path
{
	my $name = shift;

	return "$confdir/giftd.conf" if ($name eq 'giftd');
	return "$confdir/$name/$name.conf";
}

###############################################################################

sub get_modules_list
{
	my $hash = shift;
	my @modules = ();

	# fudge the order
	push @modules, delete $hash->{'giftd'} if exists $hash->{'giftd'};
	push @modules, delete $hash->{'ui'} if exists $hash->{'ui'};

	push @modules, values %$hash;

	return @modules;
}

###############################################################################

sub process_modules
{
	my @modules = @_;

	# number of successfully processed modules
	my $ret = 0;

	foreach my $module (@modules)
	{
		# process this module atomically and flush all configuration
		$ret += process_module ($module);
	}

	return $ret;
}

sub process_module
{
	my $module = shift;

	printf ("Configuring %s (%s):\n", $module->{name}, $module->{path_tpl});
	print_sep();

	my $cfg_ents = parse_template ($module->{path_tpl});
	my $cfg_tbl  = {};

	foreach my $ent (@$cfg_ents)
	{
		my $dont_ask = 0;

		# WARNING: This is an absolutely terrible hack!  This functionality
		# needs to exist in the config template itself!
		if ($module->{name} eq 'OpenFT')
		{
			# if the user has configured as something other than a search
			# node, do not present relevant configuration switches to them.
			$dont_ask = 1 if ($ent->{key} =~ m%^/search/% &&
			                  !($cfg_tbl->{'/main/class'} & 0x2));
		}

		if ($ent->{default} =~ m/^random \((\d+), (\d+)\)/)
		{
			# create a random number and pretend the default value was that
			$ent->{value} = get_random_port ($1, $2);
		}

		my $resp;

		unless ($dont_ask)
		{
			printf ("%s\n\n", $ent->{label}) if $ent->{label};
			printf ("%s [%s] ", $ent->{key}, $ent->{value});

			$resp = <>; chomp $resp;
		}

		$resp = $ent->{value} unless $resp;
		$cfg_tbl->{$ent->{key}} = $resp;

		print_sep() unless $dont_ask;
	}

	write_config ($cfg_tbl, $module->{path_cfg}, $module->{path_tpl});

	1;
}

sub write_config
{
	my $cfg_tbl  = shift;
	my $path_cfg = shift;
	my $path_tpl = shift;

	my ($path_cfg_dir) = $path_cfg =~ m/^(.*)\/[^\/]+$/;
	mkdir ($path_cfg_dir, 0755) unless (-d $path_cfg_dir);
	
	my $fcfg = new IO::File;
	my $ftpl = new IO::File;

	open ($fcfg, ">$path_cfg") or
	  die "Cannot open $path_cfg: $!";

	open ($ftpl, $path_tpl) or
	  die "Cannot open $path_tpl: $!";

	my $cfg_hdr;

	while (<$ftpl>)
	{
		$cfg_hdr = $1 if m/^\[(\w+)\]$/;

		if (m/^\#?(\w+) =\s?(.*)$/)
		{
			die unless defined $cfg_hdr;
			printf $fcfg ("%s = %s\n", $1, $cfg_tbl->{"/$cfg_hdr/$1"});
		}
		else
		{
			print $fcfg $_;
		}
	}

	close $fcfg;
	close $ftpl;

	printf ("Wrote %s.\n", $path_cfg);
}

###############################################################################

sub parse_template
{
	my $template = shift;
	my @cfg_tbl = ();
	my $cfg_ent = {};
	my $cfg_hdr = undef;
	
	my $fhandle = new IO::File;

	open ($fhandle, $template) or
	  die "Cannot open $template: $!";

	#
	# Special flag used to switch on reading of the leading config switch
	# documentation.  This is switched on as soon as a blank comment line
	# is observed, which by convention begins the comment.  It is turned off
	# once the actual configuration switch is seen.
	#
	my $reading = 0;

	while (<$fhandle>)
	{
		$cfg_hdr = $1 if m/^\[(\w+)\]$/;

		# determine when to begin processing a single config switch
		$reading = 1 if m/^\#\s*$/ or m/^\w+ =/;
		next unless $reading;

		if (m/^\#\s(.*)$/ or m/^\#(\s*)$/)
		{
			$cfg_ent->{label} .= "$1\n";
			$cfg_ent->{default} = $1 if ($1 =~ m/^Default: (.*)$/);
		}
		else
		{
			if (m/^\#?(\w+) =\s?(.*)$/)
			{
				$cfg_ent->{key} = "/$cfg_hdr/$1";
				$cfg_ent->{value} = $2;

				# tidy up
				$cfg_ent->{label} =~ s/^\s*//;
				$cfg_ent->{label} =~ s/\s*$//;
				$cfg_ent->{default} = undef if ($cfg_ent->{default} eq 'none');

				push @cfg_tbl, $cfg_ent;
			}
			else
			{
				print "shouldnt happen, bogus template, something!\n\n$_\n\n";
				print Dumper $cfg_ent;
			}

			$cfg_ent = {};
			$reading = 0;
		}
	}

	close $fhandle;

	return \@cfg_tbl;
}

###############################################################################

sub get_random_port
{
	my ($min, $max) = @_;

	# TODO: should we do any checks on the value before we spit it back out?
	my $num = int (rand (($max - $min)) + $min);

	return $num;
}

###############################################################################

sub check_uid
{
	return 1 if $<;

	print <<"__EOF__";

 *** WARNING ***

You are running this script as root.  Instead, you should run it as the
user you intend to run giFT as (setup files are created in the user's
home directory).  If you intend to run giFT as root (not advised), or
if your system does not support multiple users, you may continue at
your own risk.

__EOF__

	my $yesno;
	do {
		print "Do you want to continue anyway? [yN] ";
	} while (($yesno = <>) !~ m/^[yn]?$/i);

	return ($yesno =~ m/y/i);
}