]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefTest.pl.in
3e002d9f84307921b1a19a2c9769ba82bc7f1e0e
[lyx.git] / lib / scripts / prefTest.pl.in
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3
4 use strict;
5 use warnings;
6
7 # Syntax: prefTest.pl [<var1>=<Subst1> [<var2>=<Subst> ...]] [[ctest parameters]]
8
9 my $bindir = "@CMAKE_BINARY_DIR@";
10
11 my $userdir = "$bindir/Testing/.lyx";
12
13 my %allowedKeys = (
14   "use_converter_needauth_forbidden" => ["true", "false"],
15   "use_converter_needauth" => ["true", "false"],
16     );
17
18 chdir($bindir);
19
20 # Parse Arguments for strings to substitute
21
22 my %Subst = ();
23
24 my $ctestparams = 0;
25 my @ctestpars = ();
26 for my $arg (@ARGV) {
27   if ($ctestparams) {
28     push(@ctestpars, $arg);
29   }
30   else {
31     if ($arg =~ /^([^=]+)=(.*)$/) {
32       my $key = $1;
33       my $value = $2;
34       my $valid = 0;
35       if (defined($allowedKeys{$key})) {
36         for my $val (@{$allowedKeys{$key}}) {
37           if ($val eq $value) {
38             $valid = 1;
39             last;
40           }
41         }
42       }
43       if ($valid) {
44         $Subst{$key} = [$value, 0];
45       }
46       else {
47         die("invalid key or value specified in \"$arg\"");
48       }
49     }
50     else {
51       $ctestparams = 1;
52       push(@ctestpars, $arg);
53     }
54   }
55 }
56
57 if (%Subst) { # Try to do something only if a substitute is requested
58   if (open(FO, '>', "$userdir/preferences.tmp")) {
59     if (open(FI, "$userdir/preferences")) {
60       while (my $l = <FI>) {
61         for my $k (keys %Subst) {
62           if ($l =~ /^\\$k\b/) {
63             $l = "\\$k $Subst{$k}->[0]\n";
64             $Subst{$k}->[1] = 1;
65           }
66         }
67         print FO $l;
68       }
69     }
70     for my $k (keys %Subst) {
71       if ($Subst{$k}->[1] == 0) {
72         print FO "\\$k $Subst{$k}->[0]\n";
73       }
74     }
75     rename("$userdir/preferences.tmp", "$userdir/preferences");
76   }
77 }
78
79 my $res = 0;
80 if (@ctestpars) {
81   $res = system("ctest", @ctestpars);
82 }
83
84 exit($res);