* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / tools / browsercaps-updater / BrowserCapsUpdater.cs
1 //
2 // BrowserCapsUpdater.cs: updates $prefix/etc/mono/browscap.ini file.
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Novell, Inc (http://www.novell.com)
8 //
9
10 using System;
11 using System.IO;
12 using System.Net;
13 using System.Reflection;
14 using System.Text;
15
16 namespace Mono.ASPNET
17 {
18         class Driver
19         {
20                 static string GetFilePath ()
21                 {
22                         Type type = typeof (Environment);
23                         MethodInfo method = type.GetMethod ("GetMachineConfigPath", BindingFlags.Static |
24                                                                                     BindingFlags.NonPublic);
25
26                         if (method == null) {
27                                 Console.WriteLine ("You need to run this under Mono runtime");
28                                 return null;
29                         }
30
31                         string path = (string) method.Invoke (null, null);
32                         return Path.Combine (Path.GetDirectoryName (path), "browscap.ini");
33                 }
34                 
35                 static int Main (string [] args)
36                 {
37                         string path = GetFilePath ();
38                         if (path == null)
39                                 return 1;
40
41                         Updater updater = new Updater (path);
42
43                         if (File.Exists (path)) {
44                                 bool uptodate;
45                                 Console.WriteLine (updater.GetLocalMessage (out uptodate));
46                                 if (uptodate)
47                                         return 0;
48
49                                 Console.WriteLine ("WARNING: your site may be blocked from updating if you abuse.");
50                                 Console.WriteLine ("You're encouraged to browse and understand " +
51                                                    "http://www.GaryKeith.com/browsers");
52
53                                 string r = "NO";
54                                 while (r != "YES") {
55                                         Console.Write ("Do you want to update your file now? (yes/NO) ");
56                                         r = Console.ReadLine ();
57                                         if (r == null)
58                                                 r = "NO";
59                                         else
60                                                 r = r.ToUpper ();
61
62                                         if (r == "NO")
63                                                 return 0;
64                                 }
65                         }
66
67                         try {
68                                 updater.Update ();
69                                 Console.WriteLine ("browscap.ini file provided by Gary J. Keith.");
70                         } catch (Exception e) {
71                                 Console.Error.WriteLine ("Update failed.");
72                                 Console.Error.WriteLine ("Reason: {0}", e.Message);
73                                 return 1;
74                         }
75                         
76                         return 0;
77                 }
78         }
79
80         class Updater
81         {
82                 static string VersionUrl = "http://www.garykeith.com/browsers/version.asp";
83                 static string BrowscapUrl = "http://www.garykeith.com/browsers/stream.asp?BrowsCapINI";
84                 static string UserAgent = "Mono Browser Capabilities Updater 0.1";
85
86                 string filename;
87                 string tempfile;
88                 string local;
89                 int nupdates;
90                 string date;
91                 public Updater (string filename)
92                 {
93                         this.filename = filename;
94                 }
95
96                 public string GetLocalMessage (out bool upToDate)
97                 {
98                         StringBuilder sb = new StringBuilder ();
99                         string localdate = null;
100                         string remotedate = null;
101                         using (StreamReader reader = new StreamReader (File.OpenRead (filename))) {
102                                 string str;
103                                 while ((str = reader.ReadLine ()) != null) {
104                                         if (str.StartsWith ("version="))
105                                                 break;
106                                 }
107
108                                 if (str != null) {
109                                         localdate = str.Substring (8);
110                                         sb.AppendFormat ("Your file is dated {0}\r\n", localdate);
111                                 } else {
112                                         sb.AppendFormat ("Couldn't retrieve date from {0}\r\n", filename);
113                                 }
114                         }
115
116                         remotedate = GetRemoteDate ();
117                         remotedate = remotedate.Replace ("AM ", "");
118                         remotedate = remotedate.Replace ("PM ", "");
119                         remotedate = remotedate.Replace ("/", "-");
120                         sb.AppendFormat ("Remote file is dated {0}", remotedate);
121                         upToDate = (remotedate == localdate);
122                         if (upToDate)
123                                 sb.Append ("\r\nThe file IS up to date.");
124
125                         return sb.ToString ();
126                 }
127
128                 static string GetRemoteDate ()
129                 {
130                         HttpWebRequest request = (HttpWebRequest) WebRequest.Create (VersionUrl);
131                         request.UserAgent = UserAgent;
132                         WebResponse resp = request.GetResponse ();
133                         string str = new StreamReader (resp.GetResponseStream ()).ReadLine ();
134                         resp.Close ();
135                         return str;
136                 }
137
138                 public void Update ()
139                 {
140                         HttpWebRequest request = (HttpWebRequest) WebRequest.Create (BrowscapUrl);
141                         request.UserAgent = UserAgent;
142                         StreamWriter writer = null;
143                         StreamReader reader = null;
144                         Console.Write ("Connecting...");
145                         WebResponse resp = request.GetResponse ();
146                         string tmppath = null;
147                         try {
148                                 tmppath = Path.GetTempFileName ();
149                                 reader = new StreamReader (resp.GetResponseStream ());
150                                 Console.WriteLine (" done");
151                                 writer = new StreamWriter (File.OpenWrite (tmppath));
152                                 Console.WriteLine ("Downloading data to {0}", tmppath);
153                                 string str;
154                                 while ((str = reader.ReadLine ()) != null) {
155                                         writer.WriteLine (str);
156                                 }
157
158                                 writer.Close ();
159                                 writer = null;
160                                 reader.Close ();
161                                 reader = null;
162
163                                 Console.WriteLine ("Removing old {0}", filename);
164                                 File.Delete (filename);
165                                 Console.WriteLine ("Copying {0} to {1}", tmppath, filename);
166                                 File.Copy (tmppath, filename);
167                         } finally {
168                                 try {
169                                         File.Delete (tmppath);
170                                 } catch {}
171
172                                 if (writer != null) {
173                                         try {
174                                                 writer.Close ();
175                                         } catch {}
176                                 }
177
178                                 if (reader != null) {
179                                         try {
180                                                 reader.Close ();
181                                         } catch {}
182                                 }
183                         }
184                 }
185         }
186 }
187