OSDN Git Service

Import iptables from https://scm.osdn.net/gitroot/linuxjm/iptables.git
[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 %opts;
19
20 BEGIN{
21     $epath = `dirname $0`; chomp $epath;
22     my $lpath = "$epath/../bin";
23     unshift (@INC, $lpath);
24 }
25
26 use strict 'vars';
27 use JMtl ('line2hash', 'hash2line');
28
29 getopts("tTuUn:e:cb", \%opts);
30 $update_timestamp = 1 if $opts{"t"};
31 $update_timestamp = 0 if $opts{"T"};
32 $update_translator = 1 if $opts{"u"};
33 $update_translator = 0 if $opts{"U"};
34 $backup = 1 if $opts{"b"};
35
36 $clear_entry = 1 if $opts{"c"};
37 $ARGV[2] = "_DUMMY_" if $clear_entry;
38
39 if ($#ARGV < 2) {
40     print STDERR "Usage: JM-tl-modify.pl [OPTIONS] translation_list pagename new_status\n";
41     print STDERR "    pagename = name.[1-9]\n";
42     print STDERR "    new_status = TR DO DP PR RO RR\n";
43     print STDERR "\n";
44     print STDERR "OPTIONS:\n";
45     print STDERR "    -t : Update timestamp (default)\n";
46     print STDERR "    -T : DO NOT update timestamp\n";
47     print STDERR "    -u : Update Translator info (default)\n";
48     print STDERR "    -U : DO NOT update Translator info\n";
49     print STDERR "    -n NAME : Update name field [JM_USER_NAME]\n";
50     print STDERR "    -e MAIL : Update mail field [JM_USER_MAIL]\n";
51     print STDERR "    -c : Clear entry of specified pagename\n";
52     print STDERR "         (new_status is not required when -c is specified.)\n";
53     print STDERR "    -b : Create backup of translation_list\n";
54     exit 0;
55 }
56
57 my $user_name  = $opts{"n"} || $ENV{'JM_USER_NAME'};
58 my $user_mail = $opts{"e"} || $ENV{'JM_USER_MAIL'};
59 if ($update_translator && ($user_name eq "" || $user_mail eq "")) {
60     print STDERR "Translator name or mail not specified.\n";
61     exit 1;
62 }
63
64 my $tlist = $ARGV[0];
65 my $page = $ARGV[1];
66 my $command = $ARGV[2];
67
68 #printf "tlist = %s\n", $tlist;
69 #printf "page = %s\n", $page;
70 #printf "command = %s\n", $command;
71
72 # %new_status への代入
73 # 他のフィールドは使用しないので、初期化せず。
74 $new_status{'stat'} = $command;
75 $new_status{'page'} = $page;
76
77 my $ismatch = 'no';
78
79 open TLO, $tlist or die "cannot open $tlist\n";
80 while (<TLO>) {
81     chomp;
82     my %ti = line2hash($_);
83
84     if ($ti{'kind'} eq 'link' &&
85         "$ti{'lname'}.$ti{'lsec'}" eq $new_status{'page'})
86     {
87         if ($clear_entry) {
88             $ti{'stat'} = '1st_non';
89             my $tll = hash2line(%ti);
90             if ($DEBUG eq "yes") {print "$ismatch MATCH: $tll\n"};
91             $tlist_body .= "$tll\n";
92             next;
93         }
94         if ($new_status{'stat'} =~ /^R/) {
95             $ti{'stat'} = 'up2date';
96             my $tll = hash2line(%ti);
97             if ($DEBUG eq "yes") {print "$ismatch MATCH: $tll\n"};
98             $tlist_body .= "$tll\n";
99             next;
100         }
101     }
102     unless ("$ti{'fname'}.$ti{'sec'}" eq $new_status{'page'}) {
103         $tlist_body .= "$_\n";
104         if ($DEBUG eq "yes") {print "$ismatch : $_\n"};
105         next;
106     }
107
108     $ismatch = 'on';
109
110     if ($ti{'stat'} =~ /^1st/) {
111         $ti{'stat'} = "1st_";
112     } else {
113         $ti{'stat'} = "upd_";
114     }
115   SW1: {
116       if ($new_status{'stat'} =~ /^TR/){$ti{'stat'} .= 'rsv'; last SW1;}
117       if ($new_status{'stat'} =~ /^DO/){$ti{'stat'} .= 'dft'; last SW1;}
118       if ($new_status{'stat'} =~ /^DP/){$ti{'stat'} .= 'prf'; last SW1;}
119       if ($new_status{'stat'} =~ /^PR/){$ti{'stat'} .= 'prf'; last SW1;}
120       if ($new_status{'stat'} =~ /^RO/){$ti{'stat'} = 'up2date'; last SW1;}
121       if ($new_status{'stat'} =~ /^RR/){$ti{'stat'} = 'up2datR'; last SW1;}
122       if (not $clear_entry) {
123           die "Invalid new status. Valid status is one of TR DO DP PR RO RR.\n";
124       }
125   }
126
127     if ($new_status{'stat'} =~ /^R/){
128         $ti{'rver'} = $ti{'over'};
129         $ti{'dver'} = $ti{'over'};
130 #       $ti{'newsec'} = $ti{'sec'};
131     }
132
133 #    $ti{'tdat'}  = $new_status{'date'};
134 #    $ti{'tmail'} = $new_status{'mail'};
135 #    $ti{'tname'} = $new_status{'name'};
136
137     if ($update_timestamp) {
138         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
139         $year += 1900; $mon += 1;
140         $ti{'tdat'} = sprintf("%04d/%02d/%02d", $year, $mon, $mday);
141     }
142     if ($update_translator) {
143         $ti{'tname'} = $user_name;
144         $ti{'tmail'} = $user_mail;
145     }
146     # If translator name or email is not set, try to set it.
147     if ($ti{'tname'} eq "" || $ti{'tmail'} eq "") {
148         if ($user_name eq "" || $user_mail eq "") {
149             die "[$page] A translator is not specified for the page. " .
150                 "Please set name and mail of the translator.\n";
151             exit 2;
152         } else {
153             print "[!!!] $page: Translator name and mail " .
154                 "are set automatically.\n";
155             $ti{'tname'} = $user_name;
156             $ti{'tmail'} = $user_mail;
157         }
158     }
159     if ($clear_entry) {
160         $ti{'stat'} = '1st_non';
161         $ti{'tdat'} = '';
162         $ti{'tname'} = '';
163         $ti{'tmail'} = '';
164         # Reset versions to the original ver.
165         $ti{'rver'} = $ti{'over'};
166         $ti{'dver'} = $ti{'over'};
167     }
168
169     my $tll = hash2line(%ti);
170
171     if ($DEBUG eq "yes") {print "$ismatch MATCH: $tll\n"};
172     $tlist_body .= "$tll\n";
173 }
174 close TLO;
175 if ($ismatch eq "no") {die "No match in $tlist\n"};
176
177 system "mv -f $tlist $tlist.orig" if $backup;
178 open TLN, "| nkf -w > $tlist" or die "cannot open $tlist.new\n";
179 print TLN $tlist_body;
180 close TLN;
181 print "UPDATE: $tlist ($page)\n";