1 | #!/usr/bin/perl␊ |
2 | ␊ |
3 | # This script converts a human readable file for all the language 'Resources'␊ |
4 | # for the Chameleon package installer in to a simple format ready for merging␊ |
5 | # with pre-defined templates during compilation.␊ |
6 | #␊ |
7 | # The human readable file is a hosted on Google Docs Spreadsheet.␊ |
8 | # To view the readonly spreadsheet, go here:␊ |
9 | # https://docs.google.com/spreadsheet/ccc?key=0Aj0jJ2rdmK_sdFdNbm45NlpNYU1PcjRmOHVXX0FNa3c␊ |
10 | #␊ |
11 | # The spreadsheet is a published file and will be automatically downloaded by this␊ |
12 | # script as a UTF-8, tabbed delimited text file which will be saved in the folder␊ |
13 | # indicated by the two arguments passed.␊ |
14 | #␊ |
15 | # buildpkg.sh calls this with the following defaults:␊ |
16 | # path: ${PKG_BUILD_DIR}␊ |
17 | # filename: ResourcesSourceFile.txt ␊ |
18 | #␊ |
19 | # This script can be called manually by using:␊ |
20 | # ./ConvertResourcesTextFile.pl path filename␊ |
21 | #␊ |
22 | ␊ |
23 | use LWP::Simple;␊ |
24 | ␊ |
25 | if ($#ARGV < 1) {␊ |
26 | print stderr "A destination path and filename is needed\n";␊ |
27 | } else {␊ |
28 | $destination_dir=$ARGV[0];␊ |
29 | $destination_file=$ARGV[1];␊ |
30 | }␊ |
31 | ␊ |
32 | sub set_indent { ␊ |
33 | # Set indent to 4 spaces X the number of fields deep currently at.␊ |
34 | # $_[0] = Headed field number (not including blanks) currently being processed.␊ |
35 | $new_indent=" ";␊ |
36 | for ($l = 0; $l < $_[0]; $l++) {␊ |
37 | $new_indent=$new_indent." ";␊ |
38 | }␊ |
39 | return $new_indent;␊ |
40 | }␊ |
41 | ␊ |
42 | sub adjust_indent { ␊ |
43 | # Pull back indent if Headed fields previous to current one haven't yet been populated.␊ |
44 | # $_[0] = Current indent being used which would have come from function set_indent().␊ |
45 | # $_[1] = Headed field number (not including blanks) currently being processed.␊ |
46 | $new_indent= $_[0];␊ |
47 | for ($p = 0; $p < $_[1]; $p++) {␊ |
48 | if ( $fieldsWithHeadersSet[$p] eq 0 ) {␊ |
49 | $new_indent = substr($new_indent, 0, -4);␊ |
50 | }␊ |
51 | }␊ |
52 | return $new_indent;␊ |
53 | }␊ |
54 | ␊ |
55 | sub calculateIndent {␊ |
56 | # Find number of indents required by matching name of fieldsWithoutLanguage to position in fieldsWithHeaders.␊ |
57 | for($i = 0; $i < scalar(@fieldsWithHeaders); $i++) {␊ |
58 | if ( $fieldsWithHeaders[$i] eq $fieldsWithoutLanguage[$loop] ) {␊ |
59 | $indent=set_indent($i);␊ |
60 | last;␊ |
61 | }␊ |
62 | }␊ |
63 | $indent=adjust_indent($indent,$i); # Are there any empty 'headed' fields preceding this one? if so, adjust.␊ |
64 | }␊ |
65 | ␊ |
66 | #--------------------------------------------------------------------------␊ |
67 | $googlePublishedDoc = 'https://docs.google.com/spreadsheet/pub?key=0Aj0jJ2rdmK_sdFdNbm45NlpNYU1PcjRmOHVXX0FNa3c&single=true&gid=0&output=txt';␊ |
68 | $sourceFileToRead="PackageInstallerResourceText.tsv";␊ |
69 | getstore ($googlePublishedDoc, $destination_dir."/".$sourceFileToRead) or die "Couldn't get master file";␊ |
70 | #--------------------------------------------------------------------------␊ |
71 | ␊ |
72 | open (FILE, "$destination_dir"."/"."$sourceFileToRead");␊ |
73 | open (OUTFILE, ">$destination_dir"."/"."$destination_file");␊ |
74 | ␊ |
75 | print OUTFILE "\n"; # Output blank line. ␊ |
76 | # This is needed so after converting to rtf using textutil in later bash script␊ |
77 | # the rtf description header appears on it's own line. For example: \f0\fs24 \cf0..␊ |
78 | while (<FILE>) {␊ |
79 | $indent = " ";␊ |
80 | chomp;␊ |
81 | s/&/and/g; # Handle '&' for now by converting to 'and'.␊ |
82 | s/&/and/g; # Handle '&' for now by converting to 'and'.␊ |
83 | @fieldsReadIn = split("\t");␊ |
84 | $startFirstField=substr($fieldsReadIn[0],0,1); # First character of first field.␊ |
85 | ␉$startSecondField=substr($fieldsReadIn[1],0,1); # First character of second field.␊ |
86 | ␊ |
87 | if ($startFirstField ne "#" ) { # Ignore lines beginning with a hash.␊ |
88 | if ($startFirstField ne "") { # Check first field for language identifier.␊ |
89 | print OUTFILE "language: $fieldsReadIn[0]\n"; # Output first field - LANGUAGE.␊ |
90 | }␊ |
91 | $headedFieldCount=0;␊ |
92 | if ($startSecondField ne "") { # Check second field for language identifier.␊ |
93 | print OUTFILE " file: $fieldsReadIn[1]\n"; # Output second field - FILE. ␊ |
94 | @fieldsWithHeaders = (); # Clear array which stores only field names with titles and NOT blanks.␊ |
95 | for($i = 1; $i <= scalar(@fieldsReadIn); $i++) { # Loop through each field - ignoring first field [0].␊ |
96 | @fieldsWithoutLanguage[$i]=$fieldsReadIn[$i]; # copy each field in to 'fieldsWithoutLanguage' array.␊ |
97 | if ( $fieldsWithoutLanguage[$i] ne "" ) { # Check for NON-Blank fields headers␊ |
98 | push(@fieldsWithHeaders, $fieldsWithoutLanguage[$i]); # Store in to 'fieldsWithHeaders' array.␊ |
99 | }␊ |
100 | }␊ |
101 | }␊ |
102 | ␊ |
103 | for ($loop = 1; $loop < scalar(@fieldsReadIn); $loop++) {␊ |
104 | if ( $fieldsReadIn[$loop] ne "" && $fieldsWithoutLanguage[$loop] ne "" ) {␊ |
105 | if ( $fieldsReadIn[$loop] ne $fieldsWithoutLanguage[$loop]) { ␊ |
106 | ␉ $headedFieldCount++;␊ |
107 | $indent=calculateIndent;␊ |
108 | print OUTFILE $indent."$fieldsWithoutLanguage[$loop]: $fieldsReadIn[$loop]\n";␊ |
109 | $fieldsWithHeadersSet[$headedFieldCount]=1; # Set value in this array to mark that we've written to it.␊ |
110 | }␊ |
111 | } ␊ |
112 | }␊ |
113 | }␊ |
114 | }␊ |
115 | close (FILE);␊ |
116 | close (OUTFILE);␊ |
117 | unlink("$destination_dir"."/"."$sourceFileToRead"); # Remove downloaded Google Doc file.␊ |
118 | exit; |