1 | #!/usr/bin/perl |
---|
2 | #c:\usr\perl\bin\perl.exe |
---|
3 | |
---|
4 | use Cwd 'abs_path'; |
---|
5 | use File::Basename; |
---|
6 | use File::HomeDir; |
---|
7 | use File::Spec::Functions; |
---|
8 | use POSIX; |
---|
9 | |
---|
10 | my $sys_home = catdir(dirname(abs_path($0)), ".."); |
---|
11 | my $usr_home = catdir(home(), '.utt'); |
---|
12 | |
---|
13 | prepareUttUsrHome($usr_home); |
---|
14 | conf_utt(catfile($usr_home, 'utt.conf'), $sys_home); |
---|
15 | |
---|
16 | conf_cor(catfile($usr_home, 'cor.conf'), $sys_home); |
---|
17 | conf_compiledic(catfile($usr_home, 'compiledic.conf'), $sys_home); |
---|
18 | conf_grp(catfile($usr_home, 'grp.conf'), $sys_home); |
---|
19 | conf_gue(catfile($usr_home, 'gue.conf'), $sys_home); |
---|
20 | conf_lem(catfile($usr_home, 'lem.conf'), $sys_home); |
---|
21 | conf_ser(catfile($usr_home, 'ser.conf'), $sys_home); |
---|
22 | |
---|
23 | print "UTT user configuration created in $usr_home\n"; |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | #Kasuje stare configi i tworzy katalog .utt |
---|
28 | sub prepareUttUsrHome() { |
---|
29 | my $dir = shift; |
---|
30 | |
---|
31 | print "Preparing user configuration.\n"; |
---|
32 | |
---|
33 | if(-d $dir) { |
---|
34 | print "Old configuration detected. "; |
---|
35 | my $cnt = unlink <$dir/*>; |
---|
36 | print "($cnt files deleted)\n"; |
---|
37 | } |
---|
38 | else { |
---|
39 | print "Creating directory $dir\n"; |
---|
40 | if(1 != mkdir $dir) { |
---|
41 | die "Unable to create UTT user configuration!\n"; |
---|
42 | } |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | # Tworzy naglowek dla wszystkich plikow konfiguracyjnych |
---|
47 | sub makeConfigHeader() { |
---|
48 | return "# ************************************************************\n". |
---|
49 | "# * This file was created automatically during installation. *\n". |
---|
50 | "# * If you don't need do not change it. *\n". |
---|
51 | "# * *\n". |
---|
52 | "# * UAM Text Tools *\n". |
---|
53 | "# * Adam Mickiewicz University, Poland *\n". |
---|
54 | "# * http://utt.amu.edu.pl *\n". |
---|
55 | "# ************************************************************\n". |
---|
56 | "#\n". |
---|
57 | "# All lines must looks like:\n". |
---|
58 | "# parameter_name [=] value\n". |
---|
59 | "#\n\n"; |
---|
60 | } |
---|
61 | |
---|
62 | sub conf_compiledic() { |
---|
63 | my $compiledic_file = shift; |
---|
64 | my $utthome = shift; |
---|
65 | open(FILE, ">$compiledic_file"); |
---|
66 | |
---|
67 | print FILE makeConfigHeader(); |
---|
68 | my $hm = home(); |
---|
69 | my $lang=`grep language $hm/.utt/utt.conf | cut -d= -f 2`; |
---|
70 | chomp $lang; |
---|
71 | # powinno byc $utthome/share/utt/pl_PL.ISO-8859-2/pl_PL.ISO-8892-2.sym |
---|
72 | # ale to bez sensu |
---|
73 | print FILE "symbols=", abs_path("$utthome/share/utt"); |
---|
74 | print FILE "/$lang/$lang.sym\n"; |
---|
75 | |
---|
76 | close FILE; |
---|
77 | } |
---|
78 | |
---|
79 | sub conf_grp() { |
---|
80 | my $grp_file = shift; |
---|
81 | my $utthome = shift; |
---|
82 | open(FILE, ">$grp_file"); |
---|
83 | |
---|
84 | print FILE makeConfigHeader(); |
---|
85 | print FILE "macros=", abs_path("$utthome/lib/utt/terms.m4"), "\n"; |
---|
86 | |
---|
87 | close FILE; |
---|
88 | } |
---|
89 | |
---|
90 | sub conf_cor() { |
---|
91 | my $cor_file = shift; |
---|
92 | my $utthome = shift; |
---|
93 | open(FILE, ">$cor_file"); |
---|
94 | |
---|
95 | print FILE makeConfigHeader(); |
---|
96 | print FILE "dictionary-home=", abs_path("$utthome/share/utt"), "\n"; |
---|
97 | |
---|
98 | close FILE; |
---|
99 | } |
---|
100 | |
---|
101 | sub conf_grp() { |
---|
102 | my $grp_file = shift; |
---|
103 | my $utthome = shift; |
---|
104 | open(FILE, ">$grp_file"); |
---|
105 | |
---|
106 | print FILE makeConfigHeader(); |
---|
107 | print FILE "macros=", abs_path("$utthome/lib/utt/terms.m4"), "\n"; |
---|
108 | |
---|
109 | close FILE; |
---|
110 | } |
---|
111 | |
---|
112 | sub conf_gue() { |
---|
113 | my $gue_file = shift; |
---|
114 | my $utthome = shift; |
---|
115 | open(FILE, ">$gue_file"); |
---|
116 | |
---|
117 | print FILE makeConfigHeader(); |
---|
118 | |
---|
119 | close FILE; |
---|
120 | } |
---|
121 | |
---|
122 | sub conf_lem() { |
---|
123 | my $lem_file = shift; |
---|
124 | my $utthome = shift; |
---|
125 | open(FILE, ">$lem_file"); |
---|
126 | |
---|
127 | print FILE makeConfigHeader(); |
---|
128 | print FILE "dictionary-home=", abs_path("$utthome/share/utt"), "\n"; |
---|
129 | |
---|
130 | close FILE; |
---|
131 | } |
---|
132 | |
---|
133 | sub conf_ser() { |
---|
134 | my $ser_file = shift; |
---|
135 | my $utthome = shift; |
---|
136 | open(FILE, ">$ser_file"); |
---|
137 | |
---|
138 | print FILE makeConfigHeader(); |
---|
139 | print FILE "macros=", abs_path("$utthome/lib/utt/terms.m4"), "\n"; |
---|
140 | print FILE "flex-template=", abs_path("$utthome/lib/utt/ser.l.template"), "\n"; |
---|
141 | |
---|
142 | close FILE; |
---|
143 | } |
---|
144 | |
---|
145 | sub conf_utt() { |
---|
146 | my $utt_file = shift; |
---|
147 | my $utthome = shift; |
---|
148 | |
---|
149 | my $lang = getUserLanguage(abs_path(catdir($utthome, "share/utt"))); |
---|
150 | |
---|
151 | open(FILE, ">$utt_file"); |
---|
152 | |
---|
153 | print FILE makeConfigHeader(); |
---|
154 | print FILE "# user locale (dictionary)\n"; |
---|
155 | print FILE "language=", $lang, "\n"; |
---|
156 | |
---|
157 | close FILE; |
---|
158 | } |
---|
159 | |
---|
160 | sub getUserLanguage() { |
---|
161 | my $dict_dir = shift; |
---|
162 | |
---|
163 | print "Setup dictionaries.\n"; |
---|
164 | |
---|
165 | opendir(DIR, $dict_dir) || die "Install dictionary first!\n"; |
---|
166 | @files = grep(/[^\.{1,2}]/ && -d "$dict_dir/$_", readdir(DIR)); |
---|
167 | closedir DIR; |
---|
168 | my $cnt = scalar @files; |
---|
169 | print "$cnt dictionary(ies) found.\n"; |
---|
170 | if(0 == $cnt) { |
---|
171 | die("Install dictionary first!\n"); |
---|
172 | } |
---|
173 | |
---|
174 | foreach my $file (@files) { |
---|
175 | print "Do you select dictionary $file [yes/no]? "; |
---|
176 | my $ans = <STDIN>; |
---|
177 | if($ans =~ /y(es)?/i) { |
---|
178 | return $file; |
---|
179 | } |
---|
180 | } |
---|
181 | die "No dictionary selected - setup failed!\n"; |
---|
182 | } |
---|
183 | |
---|
184 | # Szuka locali u uzytkownika |
---|
185 | sub findLocale() { |
---|
186 | $cur_locale = setlocale(LC_CTYPE); |
---|
187 | |
---|
188 | # we replace Latinx to ISO-8859-x |
---|
189 | $cur_locale =~ s/(.+?)Latin(.+?)/$1ISO\-8859\-$2/g; |
---|
190 | |
---|
191 | if($cur_locale =~ /\w+_\w+\.\S+/) { |
---|
192 | $best_locale = $cur_locale; |
---|
193 | } |
---|
194 | elsif($cur_locale =~ /\w+_\w+/) { |
---|
195 | $best_locale = $cur_locale.".UTF-8"; |
---|
196 | } |
---|
197 | else { |
---|
198 | $best_locale = toupper($cur_locale).'_'.tolower($cur_locale).'.UTF-8'; |
---|
199 | } |
---|
200 | return $best_locale; |
---|
201 | } |
---|