2 // BrowserCapsUpdater.cs: updates $prefix/etc/mono/browscap.ini file.
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Novell, Inc (http://www.novell.com)
13 using System.Reflection;
20 static string GetFilePath ()
22 Type type = typeof (Environment);
23 MethodInfo method = type.GetMethod ("GetMachineConfigPath", BindingFlags.Static |
24 BindingFlags.NonPublic);
27 Console.WriteLine ("You need to run this under Mono runtime");
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 ();
41 Updater updater = new Updater (path);
43 if (File.Exists (path)) {
45 Console.WriteLine (updater.GetLocalMessage (out uptodate));
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");
55 Console.Write ("Do you want to update your file now? (yes/NO) ");
56 r = Console.ReadLine ();
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);
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";
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))) {
103 while ((str = reader.ReadLine ()) != null) {
104 if (str.StartsWith ("version="))
109 localdate = str.Substring (8);
110 sb.AppendFormat ("Your file is dated {0}\r\n", localdate);
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);
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 ();
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;
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);
154 while ((str = reader.ReadLine ()) != null) {
155 writer.WriteLine (str);
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);
169 File.Delete (tmppath);
172 if (writer != null) {
178 if (reader != null) {