1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import java.security.*;
7    import java.io.*;
8    import java.text.*;
9    import java.util.*;
10   
11   /** This class is used to keep track of which version of carto is running.
12    *It has only static fields and can niether be subclassed nor instantiated.
13    */
14   public final class Version implements java.io.Serializable{
15   
16     /** Versions of carto with different serialversion
17      *cannot read each others files. New serialversions
18      *must be used sparingly if at all after any users
19      *have the program. Even then, conversion programs would
20      *have to be written. */
21     private static final long serialVersionUID = -4851269449106793339L;
22   
23     public int majorversion = 0;
24   
25     public int minorversion = 8;
26   
27     /**The revision is automatically incremented each time the the program is uploaded */
28     public int rev = 0;
29   
30     public Version() {
31       try {
32         read(getClass().getResource("version.txt").openStream());
33       } catch (IOException e) {ErrorLog.exception(e,"Trouble reading version info from jar");
34       } catch (NumberFormatException e) {ErrorLog.exception(e,"bad version info in jar");
35       }
36     }
37     public Version(String name) {
38       try {
39         read(new FileInputStream(name));
40       } catch (IOException e) {ErrorLog.exception(e,"Trouble reading version info from "+name);
41       } catch (NumberFormatException e) {ErrorLog.exception(e,"bad version info in "+name);
42       }
43     }
44   
45     public void read(InputStream instr) throws IOException,NumberFormatException {
46       BufferedReader in = new BufferedReader(new InputStreamReader(instr));
47       in.readLine();
48       in.readLine();
49       majorversion = Integer.parseInt(in.readLine());
50       in.readLine();
51       minorversion = Integer.parseInt(in.readLine());
52       in.readLine();
53       rev = Integer.parseInt(in.readLine());
54       in.readLine();
55       date = in.readLine();
56     }
57   
58     public static String date = "1/6/02 1:44 PM";
59     
60     /** Returns the current version as a string. */
61     public String getVersion() {
62   //    if (branch.length()>0)
63   //      return(""+majorversion+"."+minorversion+"."+rev+"."+branch);
64   //    else
65         return(""+majorversion+"."+minorversion+"."+rev);
66     }
67   
68     static public long getSUID() {
69       return(serialVersionUID);
70     }
71   
72     public String getAbout() {
73       return(
74         "Copyright (C) 2002 Ralph Hartley\n"+
75         "Carto may be obtained from http://www.psc-cavers.org/carto/\n"+
76         "Carto is free software, you may redistribute under the\n"+
77         "terms of the Gnu Public Licence. For details see the file gpl.txt\n"+
78         "Carto comes with ABSOLUTELY NO WARRANTY\n"+
79         "Please report bugs at http://www.psc-cavers.org/bugs/\n"+
80         "or by email to hartley@aic.nrl.navy.mil\n"+
81         "May contain code from the FreeHEP project, http://java.freehep.org/\n\n"+
82   
83         "Carto version: "+getVersion()+
84         "\nUploaded on: "+date+
85         "\nRunID: " + Persist.current.getRunID()+
86         "\nJava runtime version: " + System.getProperty("java.version") +"\n");
87     }
88   
89     static public void main(String[] args) {
90       Version current = new Version();
91       if (args.length==0)
92         System.out.println(current.getVersion());
93       else try {
94         BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
95         PrintStream out = new PrintStream(new FileOutputStream(args[1]));
96         if (args.length>2 && args[2].charAt(0)=='r') current.rev++;
97         if (args.length>2 && args[2].charAt(0)=='i') {
98           current.minorversion++;
99           current.rev=0;
100        }
101        if (args.length>2 && args[2].charAt(0)=='m') {
102          current.majorversion++;
103          current.rev=current.minorversion=0;
104        }
105        for (String line = in.readLine();
106             line!=null;
107             line = in.readLine()) {
108          line = replace(line,"@@"+"@",current.getVersion());
109  
110          line = replace(line,"%%%"+"m",""+current.majorversion);
111          line = replace(line,"%%%"+"i",""+current.minorversion);
112          line = replace(line,"%%%"+"r",""+current.rev);
113          line = replace(line,"%%%"+"d",
114                         DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT).format(new Date()));
115  
116          line = replace(line,"##"+"#",current.date);
117          out.println(line);
118        }        
119      } catch (IOException e) {}
120    }
121  
122    static String replace(String line,String pattern,String newvalue) {
123      for (int place=line.indexOf(pattern);
124           place != -1;
125           place = line.indexOf(pattern,place)) 
126        line = line.substring(0,place) + newvalue +line.substring(place+pattern.length());
127      return(line);
128    }
129  }
130