(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / tools / browsercaps-updater / BrowserCapsUpdater.cs
blob843bfe2232e57fd7948c5806d480f5a42f22ebc0
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 //
10 using System;
11 using System.IO;
12 using System.Net;
13 using System.Reflection;
14 using System.Text;
16 namespace Mono.ASPNET
18 class Driver
20 static string GetFilePath ()
22 Type type = typeof (Environment);
23 MethodInfo method = type.GetMethod ("GetMachineConfigPath", BindingFlags.Static |
24 BindingFlags.NonPublic);
26 if (method == null) {
27 Console.WriteLine ("You need to run this under Mono runtime");
28 return null;
31 string path = (string) method.Invoke (null, null);
32 return Path.Combine (Path.GetDirectoryName (path), "browscap.ini");
35 static int Main (string [] args)
37 string path = GetFilePath ();
38 if (path == null)
39 return 1;
41 Updater updater = new Updater (path);
43 if (File.Exists (path)) {
44 bool uptodate;
45 Console.WriteLine (updater.GetLocalMessage (out uptodate));
46 if (uptodate)
47 return 0;
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");
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 ();
62 if (r == "NO")
63 return 0;
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;
76 return 0;
80 class Updater
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";
86 string filename;
87 string tempfile;
88 string local;
89 int nupdates;
90 string date;
91 public Updater (string filename)
93 this.filename = filename;
96 public string GetLocalMessage (out bool upToDate)
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;
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);
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.");
125 return sb.ToString ();
128 static string GetRemoteDate ()
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;
138 public void Update ()
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);
158 writer.Close ();
159 writer = null;
160 reader.Close ();
161 reader = null;
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 {}
172 if (writer != null) {
173 try {
174 writer.Close ();
175 } catch {}
178 if (reader != null) {
179 try {
180 reader.Close ();
181 } catch {}