File: erlang-depends.in

package info (click to toggle)
erlang 1%3A27.3.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 225,000 kB
  • sloc: erlang: 1,658,966; ansic: 405,769; cpp: 177,850; xml: 82,435; makefile: 15,031; sh: 14,401; lisp: 9,812; java: 8,603; asm: 6,541; perl: 5,836; python: 5,484; sed: 72
file content (777 lines) | stat: -rwxr-xr-x 20,654 bytes parent folder | download | duplicates (3)
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#! /usr/bin/env escript
%% -*- erlang -*-

-include_lib("kernel/include/file.hrl").

% The current Erlang version
-define(VERSION, "@SOURCE_VERSION@").
-define(ABI_VERSION, "@ABI_VERSION@").
-define(PCRE_VERSION, "@PCRE_VERSION@").

% Since all packages required built application to run may be unavailable
% on build stage it's necessary to list all module-package relationships
% explicitly:
-define(MODULES, [@MODULES@ {"erlang", "erlang-base"}]).

% The erlang packages list (suffixes only)
-define(PACKAGES, [@PACKAGES@]).

% main/1 --
%
%	Generate debhelper substitution variables for Erlang-dependent
%	packages.
%
% Arguments:
%	Options		List of debhelper options (-v, -a, -i, -p*, -N*) or -h.
%
% Result:
%	None.
%
% Side effects:
%	For each of the selected packages the corresponding file
%	debian/package.substvars is created (if necessary) and filled by erlang
%	dependencies. If -h or --help is among arguments then the usage info is
%	printed instead and script is halted.

main(Options) ->
    {Verbosity, TmpDir, Exclude, NewOptions} =
	lists:foldl(fun(Opt, {Sum, Dir, Ex, Opts}) ->
			case Opt of
			    "-h" ->
				usage(),
				halt(2);
			    "--help" ->
				usage(),
				halt(2);
			    "-v" ->
				{Sum + 1, Dir, Ex, Opts};
			    "--verbose" ->
				{Sum + 1, Dir, Ex, Opts};
			    "--ignore=" ++ File ->
				{Sum, Dir, Ex ++ [File], Opts};
			    "-P" ++ D ->
				{Sum, D, Ex, Opts};
			    "--tmpdir=" ++ D ->
				{Sum, D, Ex, Opts};
			    _ ->
				{Sum, Dir, Ex, Opts ++ [Opt]}
			end
		    end, {0, [], [], []}, Options),
    lists:foreach(
      fun({Package, Variables}) ->
	    PkgDir = case TmpDir of
			 [] ->
		    	     filename:join("debian", Package);
			 _ ->
		    	     TmpDir
		     end,
	    delsubstvar(Verbosity, Package, "erlang:Depends"),
	    Variables2 = Variables ++
		case lists:member("erlang:Depends", Variables) of
		    true ->
			Deps = ordsets:to_list(
				 lists:foldl(
				   fun(M, Acc) ->
					case lists:keysearch(M, 1, ?MODULES) of
					    {value, {_, P}} ->
						ordsets:add_element(P, Acc);
					    _ ->
						io:format("WARNING: Module ~s not found~n", [M]),
						Acc
					end
				   end, ordsets:new(),
				   get_remote_modules(Verbosity, PkgDir, Exclude))),
			Dep = join(lists:map(
				     fun(D) ->
					    "${" ++ D ++ ":Depends}"
				     end, Deps), ", "),
			addsubstvar(Verbosity, Package, "erlang:Depends", Dep),
			lists:map(fun(D) ->
					D ++ ":Depends"
				  end, Deps);
		    _ ->
			[]
		end,
	    deladdsubstvar(Verbosity, Package, Variables2, "erlang-abi:Depends",
			   "erlang-abi (= " ++ ?ABI_VERSION ++ ")"),
	    deladdsubstvar(Verbosity, Package, Variables2, "erlang-pcre:Depends",
			   "erlang-pcre (= " ++ ?PCRE_VERSION ++ ")"),
	    deladdsubstvar(Verbosity, Package, Variables2, "erlang-base:Depends",
			   "erlang-base (>= " ++ ?VERSION ++ ")"),
	    lists:foreach(
	      fun(Pkg) ->
		    deladdsubstvar(Verbosity, Package, Variables2, "erlang-" ++ Pkg ++ ":Depends",
				   "erlang-" ++ Pkg ++ " (>= " ++ ?VERSION ++ ")")
	      end, ?PACKAGES)
      end, get_packages(NewOptions)).

% usage/0 --
%
%	Print a short usage info.
%
% Arguments:
%	None.
%
% Result:
%	ok.
%
% Side effects:
%	Usage is printed to stdout.

usage() ->
    io:format("Usage: erlang-depends [options]~n"
	      "Options:~n"
	      "    -h, --help       Show this help message~n"
	      "    -v, --verbose    Verbose mode~n"
	      "    -a, --arch       Act on architecture dependent packages~n"
	      "    -i, --indep      Act on architecture independent packages~n"
	      "    -ppackage, --package=package~n"
	      "                     Act on package \"package\"~n"
	      "    -Npackage, --no-package=package~n"
	      "                     Do not act on package \"package\"~n"
	      "    -Ptmpdir, --tmpdir=tmpdir~n"
	      "                     Use \"tmpdir\" for package build directory~n").

% get_remote_modules/3 --
%
%	Return a difference between all modules used in the application and
%	the local modules which dependencies are satisfied automagically.
%
% Arguments:
%	Verbosity   Verbosity level (0 - silence, 1 - print action).
%	Dir	    Directory where the local BEAM files are to be searched.
%	Exclude	    Don't take into account files in this list.
%
% Result:
%	The list of remote modules.
%
% Side effects:
%	Application files are taken from filesystem.

get_remote_modules(Verbosity, Dir, Exclude) ->
    Mods = ordsets:to_list(ordsets:subtract(get_modules(Dir, Exclude),
					    get_local_modules(Dir, Exclude))),
    if  Verbosity >= 1 ->
	    lists:foreach(fun(M) ->
				io:format("Remote module: ~s~n", [M])
			  end, Mods);
	true ->
	    ok
    end,
    Mods.

% get_modules/2 --
%
%	Return an ordset of all modules found in the application.
%
% Arguments:
%	Dir	    Directory where the app BEAM files are to be searched.
%	Exclude	    Don't take into account files in this list.
%
% Result:
%	The ordset of all used modules.
%
% Side effects:
%	Application files are taken from filesystem.

get_modules(Dir, Exclude) ->
    Modules = lists:map(fun({M, _F, _A}) ->
			    atom_to_list(M)
			end, get_imports(Dir, Exclude)),
    ordsets:from_list(Modules).

% get_local_modules/2 --
%
%	Return an ordset of local modules found in the application (it is
%	constructed from BEAM filenames).
%
% Arguments:
%	Dir	    Directory where the app BEAM files are to be searched.
%	Exclude	    Don't take into account files in this list.
%
% Result:
%	The ordset of all local modules.
%
% Side effects:
%	Files are taken from filesystem.

get_local_modules(Dir, Exclude) ->
    Files = lists:filter(fun(File) ->
				case lists:member(File, Exclude) of
				    true ->
					false;
				    _ ->
					true
				end
			 end, files(Dir, ".*\\.beam$", true)),
    Basenames = lists:map(fun(File) ->
				filename:basename(File, ".beam")
			  end, Files),
    ordsets:from_list(Basenames).

% get_imports/2 --
%
%	Get imports from all BEAM files in the specified directory and below.
%
% Arguments:
%	Dir	    Directory where to search for BEAM files
%	Exclude	    Don't take into account files in this list.
%
% Results:
%	A list of imports from all found BEAM files.
%
% Side effects:
%	Files are taken from filesystem.

get_imports(Dir, Exclude) ->
    Files = lists:filter(fun(File) ->
				case lists:member(File, Exclude) of
				    true ->
					false;
				    _ ->
					true
				end
			 end, files(Dir, ".*\\.beam$", true)),
    lists:foldl(fun(File, Acc) ->
			case beam_lib:chunks(File, [imports]) of
			    {ok, {_, [{imports, List}]}} ->
				Acc ++ List;
			    _ ->
				Acc
			end
		end, [], Files).

% split/2 --
%
%	Split string into a list of tokens using the specified delimiter
%
% Arguments:
%	String	    String to split
%	Delimiter   Character (delimiter)
%
% Results:
%	A list of strings.
%
% Side effects:
%	None.

split(String, Delimiter) ->
    split(String, Delimiter, none, []).

split([], _Delimiter, Current, Res) ->
    case Current of
	none ->
	    lists:reverse(Res);
	_ ->
	    lists:reverse([lists:reverse(Current) | Res])
    end;

split([Delimiter | Tail], Delimiter, Current, Res) ->
    case Current of
	none ->
	    split(Tail, Delimiter, [], [[] | Res]);
	_ ->
	    split(Tail, Delimiter, [], [lists:reverse(Current) | Res])
    end;

split([Char | Tail], Delimiter, Current, Res) ->
    case Current of
	none ->
	    split(Tail, Delimiter, [Char], Res);
	_ ->
	    split(Tail, Delimiter, [Char | Current], Res)
    end.

% join/2 --
%
%	Join string list into a single string using the specified delimiter
%
% Arguments:
%	List	    List of strings to join
%	Delimiter   Character or character list (delimiter)
%
% Results:
%	A string.
%
% Side effects:
%	None.

join(List, Delimiter) ->
    join(List, Delimiter, []).

join([], _Delimiter, Res) ->
    lists:flatten(lists:reverse(Res));

join([String | Tail], Delimiter, []) ->
    join(Tail, Delimiter, [String]);

join([String | Tail], Delimiter, Res) ->
    join(Tail, Delimiter, [String, Delimiter | Res]).

% delsubstvar/3 --
%
%	Removes substitution variable from a substvar file for a given
%	package in debian/ directory.
%
% Arguments:
%	Verbosity   Verbosity level (0 - silence, 1 - print action)
%	Package	    Name of a package (file Package ++ ".substvars" is used).
%	Substvar    Name of a substitution variable to delete.
%
% Results:
%	ok or halt.
%
% Side effects:
%	File "debian/" ++ Package ++ ".substvars" is overwritten. The
%	specified variable is deleted. Program is halted in case of error.

delsubstvar(Verbosity, Package, Substvar) ->
    SubstvarFile = filename:join("debian", Package ++ ".substvars"),
    if  Verbosity >= 1 ->
	    io:format("Deleting substvar ~s from file ~s~n", [Substvar, SubstvarFile]);
	true ->
	    ok
    end,
    case file:read_file(SubstvarFile) of
	{ok, BinData} ->
	    StrData = binary_to_list(BinData),

	    % Remove the trailing newline if any

	    Len = string:len(StrData),
	    StrData2 = case Len - string:rstr(StrData, "\n") of
			    0 when Len > 0 ->
				string:left(StrData, Len - 1);
			    _ ->
				StrData
		       end,
	    Tokens = split(StrData2, $\n),
	    NewTokens = lists:filter(
			    fun(S) ->
				    case string:str(S, Substvar ++ "=") of
					1 ->
					    false;
					_ ->
					    true
				    end
			    end, Tokens),
	    case file:write_file(SubstvarFile, join(NewTokens, $\n) ++ "\n") of
		ok ->
		    ok;
		{error, Error} ->
		    io:format("ERROR: Can't write ~s: ~s~n", [SubstvarFile, Error]),
		    halt(1)
	    end;
	{error, enoent} ->
	    ok;
	{error, Error} ->
	    io:format("ERROR: Can't read ~s: ~s~n", [SubstvarFile, Error]),
	    halt(1)
    end.

% addsubstvar/4 --
%
%	Adds a dependency to a substitution variable in a substvar file
%	for a given package in debian/ directory.
%
% Arguments:
%	Verbosity   Verbosity level (0 - silence, 1 - print action)
%	Package	    Name of a package (file Package ++ ".substvars" is used).
%	Substvar    Name of a substitution variable to add/change.
%	Dependency  An added substitution dependency string.
%
% Results:
%	ok or halt.
%
% Side effects:
%	File "debian/" ++ Package ++ ".substvars" is overwritten. The specified
%	depandency string is added to the variable. Program is halted in case
%	of error.

addsubstvar(Verbosity, Package, Substvar, Dependency) ->
    SubstvarFile = filename:join("debian", Package ++ ".substvars"),
    if  Verbosity >= 1 ->
	    io:format("Adding value ~s to substvar ~s in file ~s~n",
		      [Substvar, Dependency, SubstvarFile]);
	true ->
	    ok
    end,
    case file:read_file(SubstvarFile) of
	{ok, BinData} ->
	    StrData = binary_to_list(BinData),

	    % Remove the trailing newline if any

	    Len = string:len(StrData),
	    StrData2 = case Len - string:rstr(StrData, "\n") of
			    0 when Len > 0 ->
				string:left(StrData, Len - 1);
			    _ ->
				StrData
		       end,
	    Tokens = split(StrData2, $\n),
	    {NewTokens, Found} = lists:mapfoldl(
				    fun(S, F) ->
					    case string:str(S, Substvar ++ "=") of
						1 ->
						    {S ++ ", " ++ Dependency, true};
						_ ->
						    {S, F}
					    end
				    end, false, Tokens),
	    NewTokens2 = if Found ->
				NewTokens;
			    true ->
				[Substvar ++ "=" ++ Dependency | NewTokens]
			 end,
	    case file:write_file(SubstvarFile, join(NewTokens2, $\n) ++ "\n") of
		ok ->
		    ok;
		{error, Error} ->
		    io:format("Can't write ~s: ~s~n", [SubstvarFile, Error]),
		    halt(1)
	    end;
	{error, enoent} ->
	    case file:write_file(SubstvarFile, [Substvar, "=", Dependency, "\n"]) of
		ok ->
		    ok;
		{error, Error} ->
		    io:format("Can't write ~s: ~s~n", [SubstvarFile, Error]),
		    halt(1)
	    end;
	{error, Error} ->
	    io:format("Can't read ~s: ~s~n", [SubstvarFile, Error]),
	    halt(1)
    end.

% deladdsubstvar/5 --
%
%	Delete a substitution variable from a substvar file and add a
%	dependency to it if this substvar is present in a specified list of
%	variables for a given package in debian/ directory.
%
% Arguments:
%	Verbosity   Verbosity level (0 - silence, 1 - print action)
%	Package	    Name of a package (file Package ++ ".substvars" is used).
%	Variables   List of requested variables for the package.
%	Substvar    Name of a substitution variable to add/change.
%	Dependency  An added substitution dependency string.
%
% Results:
%	ok or halt.
%
% Side effects:
%	File "debian/" ++ Package ++ ".substvars" is overwritten. The specified
%	dependency string is either deleted or replaced. Program is halted in
%	case of error.

deladdsubstvar(Verbosity, Package, Variables, Substvar, Dependency) ->
    delsubstvar(Verbosity, Package, Substvar),
    case lists:member(Substvar, Variables) of
	true ->
	    addsubstvar(Verbosity, Package, Substvar, Dependency);
	_ ->
	    ok
    end.

% get_packages/1 --
%
%	Parses debhelper command line options and debian/control file and
%	returns packages with requested substvars to act on.
%
% Arguments:
%	ArgList	    Dephelper options (only -a, -i, -p, -N options are
%		    recognised).
%
% Results:
%	{ok, [{Package,Vars}]} to work on or error message and halt if
%	debian/control is unreadable or unknown option is specified.
%
% Side effects:
%	Program is halted in case of error, packages info is taken from
%	an external file.

get_packages(ArgList) ->
    ControlFile = filename:join("debian", "control"),
    case file:read_file(ControlFile) of
	{ok, BinData} ->
	    {Arches, Excluded, Explicit} =
		lists:foldl(fun(Arg, {A1, E1, E2}) ->
				case Arg of
				    % Only the last -a or -i option takes
				    % effect

				    "-a" ->
					{arch, E1, E2};
				    "--arch" ->
					{arch, E1, E2};
				    "-i" ->
					{indep, E1, E2};
				    "--indep" ->
					{indep, E1, E2};
				    "-s" ->
					io:format("Options -s and --same-arch aren't supported yet~n"),
					halt(1);
				    "--same-arch" ->
					io:format("Options -s and --same-arch aren't supported yet~n"),
					halt(1);
				    "-p" ++ Package ->
					{A1, E1, E2 ++ [Package]};
				    "--package=" ++ Package ->
					{A1, E1, E2 ++ [Package]};
				    "-N" ++ Package ->
					{A1, E1 ++ [Package], E2};
				    "--no-package=" ++ Package ->
					{A1, E1 ++ [Package], E2};
				    _ ->
					io:format("Unknown option ~s~n", [Arg]),
					halt(1)
				end
			    end, {all, [], []}, ArgList),

	    % Join up continuation lines

	    StrData = re:replace(BinData, "\\n( |\\t)", "\\1", [global, {return, list}]),

	    % Add an extra empty line for the case if debian/config doesn't have a trailing LF

	    Tokens = split(StrData, $\n) ++ [[]],
	    {_Arch, _Package, _Vars, Packages, AllPackages} =
		lists:foldl(fun(Line, {A, P, Vs, Ps, APs}) ->
				case Line of
				    "Package: " ++ Pkg ->
					{A, string:strip(Pkg), Vs, Ps, APs};
				    "Architecture: " ++ Arc ->
					{string:strip(Arc), P, Vs, Ps, APs};
				    "Pre-Depends: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "Depends: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "Recommends: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "Suggests: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "Enhances: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "Breaks: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "Conflicts: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "Provides: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "Replaces: " ++ Deps ->
					{A, P, Vs ++ find_vars(Deps), Ps, APs};
				    "" ->
					case P of
					    "" ->
						% Two LF in a row or the end of a source package

						{"", "", [], Ps, APs};
					    _ ->
						case lists:member(P, Excluded) of
						    true ->
							% The package is excluded by -Npackage

							{"", "", [], Ps, APs};
						    _ ->
							case lists:member(P, Explicit) of
							    true ->
								% The package is explicitly required

								{"", "", [], Ps ++ [{P, Vs}], APs};
							    _ ->
								case A of
								    "all" ->
									case Arches of
									    indep ->
										% Arch independent packages
										% are requested

										{"", "", [], Ps ++ [{P, Vs}], APs};
									    all ->
										{"", "", [], Ps, APs ++ [{P, Vs}]};
									    _ ->
										{"", "", [], Ps, APs}
									end;
								    _ ->
									case Arches of
									    arch ->
										% Arch dependent packages
										% are requested

										{"", "", [], Ps ++ [{P, Vs}], APs};
									    all ->
										{"", "", [], Ps, APs ++ [{P, Vs}]};
									    _ ->
										{"", "", [], Ps, APs}
									end
								end
							end
						end
					end;
				    _ ->
					{A, P, Vs, Ps, APs}
				end
			    end, {"", "", [], [], []}, Tokens),
	    case {Arches, Packages} of
		{all, []} ->
		    % There aren't explicitly requested packages

		    AllPackages;
		_ ->
		    Packages
	    end;
	{error, Error} ->
	    io:format("Can't read ~s: ~s~n", [ControlFile, Error]),
	    halt(1)
    end.

% find_vars/1 --
%
%	Find substitution variables in dependencies line and return list of
%	their names.
%
% Arguments:
%	Dependencies	Dependencies line from debian/control
%
% Result:
%	List of substvars names (inside ${}).
%
% Side effects:
%	None.

find_vars(Dependencies) ->
    case re:run(Dependencies, "\\$\\{(\\S*)\\}", [global, {capture, all_but_first, list}]) of
	{match, Captured} ->
	    lists:foldl(fun(M, Acc) ->
			    Acc ++ M
			end, [], Captured);
	_ ->
	    []
    end.

% files/3 --
%
%	Return all files in a directory which names match a given expression.
%	(This procedure is borrowed from http://erlang.org/examples/examples-2.0.html
%	and a bit modified.)
%
% Arguments:
%	Dir	    Directory from which to search
%	Re	    Regular expression for filename matching
%	Recursive   Whether to search for files recursively
%
% Result:
%	A list of filenames.
%
% Side effects:
%	File and directory names are taken from filesystem.

files(Dir, Re, Recursive) ->
    find_files(Dir, Re, Recursive, []).

% find_files/4 --
%
%	Find all files in a directory which names match a given expression,
%	prepend them to a given list of files and return the total files list.
%	(This procedure is borrowed from http://erlang.org/examples/examples-2.0.html
%	and a bit modified.)
%
% Arguments:
%	Dir	    Directory from which to search
%	Re	    Regular expression for filename matching
%	Recursive   Whether to search for files recursively
%	L	    List of already found files
%
% Result:
%	A list of filenames.
%
% Side effects:
%	File and directory names are taken from filesystem.

find_files(Dir, Re, Recursive, L) ->
    case file:list_dir(Dir) of
	{ok, Files} ->
	    find_files(Files, Dir, Re, Recursive, L);
	{error, _} ->
	    L
    end.

% find_files/5 --
%
%	Check all specified filenames if they match a given expressions and
%	prepend matched ones to a given list of filenames. If the file type is
%	directory and search is recursive then descend into it and add found
%	filenames to the result too. Return the total files list.
%	(This procedure is borrowed from http://erlang.org/examples/examples-2.0.html
%	and a bit modified.)
%
% Arguments:
%	_FL	    List of files in directory Dir
%	Dir	    Directory from which to search
%	Re	    Regular expression for filename matching
%	Recursive   Whether to search for files recursively
%	L	    List of already found files
%
% Result:
%	A list of filenames.
%
% Side effects:
%	File and directory names are taken from filesystem.

find_files(_FL = [File | T], Dir, Re, Recursive, L) ->
    FullName = filename:join(Dir, File),
    case file_type(FullName) of
	regular ->
	    case re:run(FullName, Re) of
		{match, _}  ->
		    find_files(T, Dir, Re, Recursive, [FullName | L]);
		_ ->
		    find_files(T, Dir, Re, Recursive, L)
	    end;
	directory ->
	    case Recursive of
		true ->
		    L1 = find_files(FullName, Re, Recursive, L),
		    find_files(T, Dir, Re, Recursive, L1);
		false ->
		    find_files(T, Dir, Re, Recursive, L)
	    end;
	error ->
	    find_files(T, Dir, Re, Recursive, L)
    end;

find_files([], _, _, _, L) ->
    L.

% file_type/1 --
%
%	Return file type (regular, directory, or error) for a given filename.
%	(This procedure is borrowed from http://erlang.org/examples/examples-2.0.html
%	and a bit modified.)
%
% Arguments:
%	File	    Filename to get type.
%
% Result:
%	A file type.
%
% Side effects:
%	File info is taken from the filesystem.

file_type(File) ->
    case file:read_file_info(File) of
	{ok, FileInfo} ->
	    case FileInfo#file_info.type of
		regular ->
		    regular;
		directory ->
		    directory;
		_ ->
		    error
	    end;
	_ ->
	    error
    end.

% vim:ft=erlang