3 # ================================================================
4 # === ==> -------- HISTORY -------- <== ===
5 # ================================================================
7 # Version Date Maintainer Changes, Additions, Fixes
8 # 0.0.1 2017-03-12 sed, PrydeWorX First basic design
10 # ========================
11 # === Little TODO list ===
12 # ========================
16 use Cwd qw(getcwd abs_path);
21 # ================================================================
22 # === ==> ------ Help Text and Version ----- <== ===
23 # ================================================================
24 Readonly my $VERSION => "0.0.1"; ## Please keep this current!
25 Readonly my $VERSMIN => "-" x length($VERSION);
26 Readonly my $PROGDIR => dirname($0);
27 Readonly my $PROGNAME => basename($0);
28 Readonly my $WORKDIR => getcwd();
29 Readonly my $USAGE_SHORT => "$PROGNAME <--help|path to po directory>";
30 Readonly my $USAGE_LONG => qq#
31 elogind po file cleaner V$VERSION
32 -------------------------$VERSMIN
34 Check all .po files in the given directory and comment out all lines
35 that refer to non-existent files.
43 -h|--help | Print this help text and exit.
46 # ================================================================
47 # === ==> -------- Global variables -------- <== ===
48 # ================================================================
50 my $file_fmt = ""; ## Format string built from the longest file name in generate_file_list().
51 my $main_result = 1; ## Used for parse_args() only, as simple $result is local everywhere.
52 my $po_file_path = ""; ## Path to where the .po files are to be found.
54 my @source_files = (); ## File list generated by generate_file_list()
57 # ================================================================
58 # === ==> -------- Function list -------- <== ===
59 # ================================================================
61 sub generate_file_list; ## Find all relevant files and store them in @wanted_files
62 sub parse_args; ## Parse ARGV for the options we support
63 sub wanted; ## Callback function for File::Find
66 # ================================================================
67 # === ==> -------- Prechecks -------- <== ==
68 # ================================================================
70 $main_result = parse_args(@ARGV);
71 ( (!$main_result) ## Note: Error or --help given, then exit.
72 or ( $show_help and print "$USAGE_LONG" ) )
73 and exit(!$main_result);
74 generate_file_list or exit 1;
77 # ================================================================
78 # === ==> -------- = MAIN PROGRAM = -------- <== ===
79 # ================================================================
81 for my $pofile (@source_files) {
82 printf("$file_fmt: ", $pofile);
85 # --- 1) Load the file ---
86 # ------------------------
88 if (open(my $fIn, "<", $pofile)) {
92 print "ERROR READING: $!\n";
96 # --- 2) Copy @lIn to @lOut, commenting out all parts ---
97 # --- belonging to files that do not exist. ---
98 # -------------------------------------------------------
100 my $in_block = 0; ## 1 if in commented out block
102 my $was_block = 0; ## save in_block, so we know when to add elogind masks
103 for my $line (@lIn) {
106 # in_block switches are done on file identifications lines, which look like
107 # this : "#: ../src/import/org.freedesktop.import1.policy.in.h:2"
108 if ($line =~ m/^#:\s+([^:]+):\d+/) {
109 # Note: There might be two file references, if the transalted text spans
110 # more than one line. The second path is the same as the first, so
111 # it is sufficient not to end the regex with '$' here.
112 my $altfile = substr($1, 0, -2); ## .in files have an extra '.h' attached
113 $was_block = $in_block;
114 $in_block = (-f $po_file_path . "/" . $1) || (-f $po_file_path . "/" . $altfile) ? 0 : 1;
115 $in_block and ++$count;
118 # If the in_block switches, add elogind mask start or end
119 if ($was_block != $in_block) {
121 and push(@lOut, "#endif // 0\n")
122 or push(@lOut, "#if 0 /// UNNEEDED by elgoind");
123 $was_block = $in_block;
126 # If we are in block, comment out the line:
127 $in_block and $line = "# $line";
129 # Now push the line, it is ready.
131 } ## End of line copying
133 # Make sure to end the last block
134 $in_block and push(@lOut, "#endif // 0\n");
136 # --- 3) Overwrite the input file with the adapted text. ---
137 # ----------------------------------------------------------
138 if (open(my $fOut, ">", $pofile)) {
139 for my $line (@lOut) {
140 print $fOut "$line\n";
143 print "$count blocks masked\n";
145 print "ERROR WRITING: $!\n";
147 } ## End of main loop
150 # ===========================
151 # === END OF MAIN PROGRAM ===
152 # ===========================
155 # ================================================================
156 # === ==> ---- Function Implementations ---- <== ===
157 # ================================================================
160 # -----------------------------------------------------------------------
161 # --- Finds all relevant files and store them in @wanted_files ---
162 # --- Returns 1 on success, 0 otherwise. ---
163 # -----------------------------------------------------------------------
164 sub generate_file_list {
166 # Use File::Find to search for .po files:
167 find(\&wanted, "$po_file_path");
171 or print("ERROR: No source files found? Where the hell are we?\n")
174 # Get the maximum file length and build $file_fmt
176 for my $f (@source_files) {
177 length($f) > $mlen and $mlen = length($f);
179 $file_fmt = sprintf("%%-%d%s", $mlen, "s");
185 # -----------------------------------------------------------------------
186 # --- parse the given list for arguments. ---
187 # --- returns 1 on success, 0 otherwise. ---
188 # --- sets global $show_help to 1 if the long help should be printed. ---
189 # -----------------------------------------------------------------------
194 for (my $i = 0; $i < @args; ++$i) {
196 # Check for -h|--help option
197 # -------------------------------------------------------------------------------
198 if ($args[$i] =~ m/^-(?:h|-help)$/) {
202 # Check for unknown options:
203 # -------------------------------------------------------------------------------
204 elsif ($args[$i] =~ m/^-/) {
205 print "ERROR: Unknown option \"$args[$1]\" encountered!\n\nUsage: $USAGE_SHORT\n";
209 # Everything else is considered to the path to the .po files
210 # -------------------------------------------------------------------------------
212 # But only if it is not set, yet:
213 if (length($po_file_path)) {
214 print "ERROR: Superfluous po file path \"$args[$i]\" found!\n\nUsage: $USAGE_SHORT\n";
218 if ( ! -d "$args[$i]") {
219 print "ERROR: po file path \"$args[$i]\" does not exist!\n\nUsage: $USAGE_SHORT\n";
223 $po_file_path = $args[$i];
225 } ## End looping arguments
227 # If we have no upstream path now, show short help.
228 if ($result && !$show_help && !length($po_file_path)) {
229 print "ERROR: Please provide a path to the po files!\n\nUsage: $USAGE_SHORT\n";
234 } ## parse_srgs() end
237 # Callback function for File::Find
239 -f $_ and ($_ =~ m/\.po$/ )
240 and push @source_files, $File::Find::name;