3aa3303d7910f6b45cae6072d73fc9a4bb73d38b
[jalview.git] / utils / patchGt.pl
1 #!/bin/perl
2 #*******************************************************************************
3 # Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
4 # Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 #
6 # This file is part of Jalview.
7 #
8 # Jalview is free software: you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License 
10 # as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 #
12 # Jalview is distributed in the hope that it will be useful, but 
13 # WITHOUT ANY WARRANTY; without even the implied warranty 
14 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15 # PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
18 #*******************************************************************************
19
20 use strict;
21 use Env qw($GTID);
22
23 defined($GTID) or $GTID="UA-9060947-1";
24 my $SCRIPT = <<FOO;
25 <body>
26 <script type="text/javascript">
27     var gaJsHost = (("https:" == document.location.protocol) ?
28         "https://ssl." : "http://www.");
29     document.write(unescape("%3Cscript src=\'" + gaJsHost +
30         "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
31 </script>
32 <script type="text/javascript">
33 try{
34     var pageTracker = _gat._getTracker("'$GTID'");
35     pageTracker._trackPageview();
36 } catch(err) {}
37 </script>
38 FOO
39                 
40 while (scalar @ARGV)
41 {
42     my $f=shift @ARGV;
43     if (-f $f) {
44         if (system("grep","-v","-q",'"'.$GTID.'"',$f)) {
45             if (open OF,">$f.".$GTID) {
46                 if (open IF,"$f") {
47                 while (<IF>) {
48                     if ($_=~m!<body>!) {
49                         $_=~s!<body>!$SCRIPT!;
50                     } else {
51 #                       $_=~s!href="([~"]+)"!href="$1" onclick="
52                     }
53                     print OF $_;
54                 }
55                 close(IF);
56                 close(OF);
57                 rename($f,$f.".old.".$GTID) or die("Couldn't rename $f to $f".".old.".$GTID,$@);
58                 rename($f.".$GTID",$f) or die("Couldn't rename $f.".$GTID." to $f",$@);
59                 unlink($f.".old.".$GTID) or die("Couldn't delete ".$f.".old.".$GTID,$@);
60                 } else {
61                     warn("Can't open $f for reading.",$@);
62                 }
63             } else {
64                 warn("Couldn't open new edited file $f.$GTID",$@);
65             }
66         }
67     }
68 }
69