OSDN Git Service

meson 0.60.2: Reserve to translate.
[linuxjm/jm.git] / admin / JM-tl-modify.pl
1 #!/usr/bin/env perl
2 #------------------------------------------------------------
3 # translation_list の手動での更新を行うためのスクリプトです。
4 # 直接編集することなしに、ステータスを更新できます。
5 #
6 # 引き数なしで本コマンドを実行すると、ヘルプが表示されます。
7 #------------------------------------------------------------
8
9 use Getopt::Std;
10
11 my $DEBUG = 1;
12 my %new_status;
13 my $tlist_body = "";
14 my $update_timestamp = 1;
15 my $update_translator = 1;
16 my $backup = 0;
17 my $clear_entry = 0;
18 my $reset_old_translated_ver = 0;
19 my %opts;
20
21 BEGIN{
22     $epath = `dirname $0`; chomp $epath;
23     my $lpath = "$epath/../bin";
24     unshift (@INC, $lpath);
25 }
26
27 use strict 'vars';
28 use JMtl ('line2hash', 'hash2line');
29
30 getopts("tTuUn:e:crb", \%opts);
31 $update_timestamp = 1 if $opts{"t"};
32 $update_timestamp = 0 if $opts{"T"};
33 $update_translator = 1 if $opts{"u"};
34 $update_translator = 0 if $opts{"U"};
35 $backup = 1 if $opts{"b"};
36
37 $clear_entry = 1 if $opts{"c"};
38 $reset_old_translated_ver = 1 if $opts{"r"};
39 $ARGV[2] = "_DUMMY_" if $clear_entry;
40
41 if ($#ARGV < 2) {
42     print STDERR "Usage: JM-tl-modify.pl [OPTIONS] translation_list pagename new_status\n";
43     print STDERR "    pagename = name.[1-9]\n";
44     print STDERR "    new_status = TR DO DP PR RO RR UN\n";
45     print STDERR "\n";
46     print STDERR "Status:\n";
47     print STDERR "  TR : Translation Reservation\n";
48     print STDERR "  DO : Draft Only\n";
49     print STDERR "  DP : Draft & Proof Reservation\n";
50     print STDERR "  PR : Proof Reservation\n";
51     print STDERR "  RO : Release Only\n";
52     print STDERR "  RR : Release and update Reservation\n";
53     print STDERR "  UN : UNassign\n";
54     print STDERR "\n";
55     print STDERR "OPTIONS:\n";
56     print STDERR "    -t : Update timestamp (default)\n";
57     print STDERR "    -T : DO NOT update timestamp\n";
58     print STDERR "    -u : Update Translator info (default)\n";
59     print STDERR "    -U : DO NOT update Translator info\n";
60     print STDERR "    -n NAME : Update name field [JM_USER_NAME]\n";
61     print STDERR "    -e MAIL : Update mail field [JM_USER_MAIL]\n";
62     print STDERR "    -c : Clear entry of specified pagename\n";
63     print STDERR "         (new_status is not required when -c is specified.)\n";
64     print STDERR "    -r : Clear old translated version\n";
65     print STDERR "    -b : Create backup of translation_list\n";
66     exit 0;
67 }
68
69 my $user_name  = $opts{"n"} || $ENV{'JM_USER_NAME'};
70 my $user_mail = $opts{"e"} || $ENV{'JM_USER_MAIL'};
71 if ($update_translator && ($user_name eq "" || $user_mail eq "")) {
72     print STDERR "Translator name or mail not specified.\n";
73     exit 1;
74 }
75
76 my $tlist = $ARGV[0];
77 my $page = $ARGV[1];
78 my $command = $ARGV[2];
79
80 #printf "tlist = %s\n", $tlist;
81 #printf "page = %s\n", $page;
82 #printf "command = %s\n", $command;
83
84 # %new_status への代入
85 # 他のフィールドは使用しないので、初期化せず。
86 $new_status{'stat'} = $command;
87 $new_status{'page'} = $page;
88
89 my $ismatch = 'no';
90
91 open TLO, $tlist or die "cannot open $tlist\n";
92 while (<TLO>) {
93     chomp;
94     my %ti = line2hash($_);
95
96     if ($ti{'kind'} eq 'link' &&
97         "$ti{'lname'}.$ti{'lsec'}" eq $new_status{'page'})
98     {
99         if ($clear_entry) {
100             $ti{'stat'} = '1st_non';
101             my $tll = hash2line(%ti);
102             if ($DEBUG eq "yes") {print "$ismatch MATCH: $tll\n"};
103             $tlist_body .= "$tll\n";
104             next;
105         }
106         if ($new_status{'stat'} =~ /^R/) {
107             $ti{'stat'} = 'up2date';
108             my $tll = hash2line(%ti);
109             if ($DEBUG eq "yes") {print "$ismatch MATCH: $tll\n"};
110             $tlist_body .= "$tll\n";
111             next;
112         }
113     }
114     unless ("$ti{'fname'}.$ti{'sec'}" eq $new_status{'page'}) {
115         $tlist_body .= "$_\n";
116         if ($DEBUG eq "yes") {print "$ismatch : $_\n"};
117         next;
118     }
119
120     $ismatch = 'on';
121
122     if ($ti{'stat'} =~ /^1st/) {
123         $ti{'stat'} = "1st_";
124     } else {
125         $ti{'stat'} = "upd_";
126     }
127   SW1: {
128       if ($new_status{'stat'} =~ /^UN/){$ti{'stat'} .= 'non'; last SW1;}
129       if ($new_status{'stat'} =~ /^TR/){$ti{'stat'} .= 'rsv'; last SW1;}
130       if ($new_status{'stat'} =~ /^DO/){$ti{'stat'} .= 'dft'; last SW1;}
131       if ($new_status{'stat'} =~ /^DP/){$ti{'stat'} .= 'prf'; last SW1;}
132       if ($new_status{'stat'} =~ /^PR/){$ti{'stat'} .= 'prf'; last SW1;}
133       if ($new_status{'stat'} =~ /^RO/){$ti{'stat'} = 'up2date'; last SW1;}
134       if ($new_status{'stat'} =~ /^RR/){$ti{'stat'} = 'up2datR'; last SW1;}
135       if (not $clear_entry) {
136           die "Invalid new status. Valid status is one of TR DO DP PR RO RR.\n";
137       }
138   }
139
140     if ($new_status{'stat'} =~ /^R/){
141         $ti{'rver'} = $ti{'over'};
142         $ti{'dver'} = $ti{'over'};
143 #       $ti{'newsec'} = $ti{'sec'};
144     }
145     if ($reset_old_translated_ver){
146         $ti{'rver'} = $ti{'over'};
147         $ti{'dver'} = $ti{'over'};
148     }
149
150 #    $ti{'tdat'}  = $new_status{'date'};
151 #    $ti{'tmail'} = $new_status{'mail'};
152 #    $ti{'tname'} = $new_status{'name'};
153
154     if ($update_timestamp) {
155         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
156         $year += 1900; $mon += 1;
157         $ti{'tdat'} = sprintf("%04d/%02d/%02d", $year, $mon, $mday);
158     }
159     if ($update_translator) {
160         $ti{'tname'} = $user_name;
161         $ti{'tmail'} = $user_mail;
162     }
163     # If translator name or email is not set, try to set it.
164     if ($ti{'tname'} eq "" || $ti{'tmail'} eq "") {
165         if ($user_name eq "" || $user_mail eq "") {
166             die "[$page] A translator is not specified for the page. " .
167                 "Please set name and mail of the translator.\n";
168             exit 2;
169         } else {
170             print "[!!!] $page: Translator name and mail " .
171                 "are set automatically.\n";
172             $ti{'tname'} = $user_name;
173             $ti{'tmail'} = $user_mail;
174         }
175     }
176     if ($clear_entry) {
177         $ti{'stat'} = '1st_non';
178         $ti{'tdat'} = '';
179         $ti{'tname'} = '';
180         $ti{'tmail'} = '';
181         # Reset versions to the original ver.
182         $ti{'rver'} = $ti{'over'};
183         $ti{'dver'} = $ti{'over'};
184     }
185
186     my $tll = hash2line(%ti);
187
188     if ($DEBUG eq "yes") {print "$ismatch MATCH: $tll\n"};
189     $tlist_body .= "$tll\n";
190 }
191 close TLO;
192 if ($ismatch eq "no") {die "No match in $tlist\n"};
193
194 system "mv -f $tlist $tlist.orig" if $backup;
195 open TLN, "| nkf -w > $tlist" or die "cannot open $tlist.new\n";
196 print TLN $tlist_body;
197 close TLN;
198 print "UPDATE: $tlist ($page)\n";