normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / link_reporter_v0.5.txt
1 
2 // huffware script: link reporter, by fred huffhines.
3 //
4 // a simple helper script that reports which link number this prim has.
5 //
6 //   this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
7 //   do not use it in objects without fully realizing you are implicitly accepting that license.
8 //
9
10 //////////////
11 // huffware script: auto-retire, by fred huffhines, version 2.8.
12 // distributed under BSD-like license.
13 //   !!  keep in mind that this code must be *copied* into another
14 //   !!  script that you wish to add auto-retirement capability to.
15 // when a script has auto_retire in it, it can be dropped into an
16 // object and the most recent version of the script will destroy
17 // all older versions.
18 //
19 // the version numbers are embedded into the script names themselves.
20 // the notation for versions uses a letter 'v', followed by two numbers
21 // in the form "major.minor".
22 // major and minor versions are implicitly considered as a floating point
23 // number that increases with each newer version of the script.  thus,
24 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
25 // and "hazmap v3.2" is a more recent version.
26 //
27 // example usage of the auto-retirement script:
28 //     default {
29 //         state_entry() {
30 //            auto_retire();  // make sure newest addition is only version of script.
31 //        }
32 //     }
33 // this script is partly based on the self-upgrading scripts from markov brodsky
34 // and jippen faddoul.
35 //////////////
36 auto_retire() {
37     string self = llGetScriptName();  // the name of this script.
38     list split = compute_basename_and_version(self);
39     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
40     string basename = llList2String(split, 0);  // script name with no version attached.
41     string version_string = llList2String(split, 1);  // the version found.
42     integer posn;
43     // find any scripts that match the basename.  they are variants of this script.
44     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
45         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
46         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
47             // found a basic match at least.
48             list inv_split = compute_basename_and_version(curr_script);
49             if (llGetListLength(inv_split) == 2) {
50                 // see if this script is more ancient.
51                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
52                 // must make sure that the retiring script is completely the identical basename;
53                 // just matching in the front doesn't make it a relative.
54                 if ( (llList2String(inv_split, 0) == basename)
55                     && ((float)inv_version_string < (float)version_string) ) {
56                     // remove script with same name from inventory that has inferior version.
57                     llRemoveInventory(curr_script);
58                 }
59             }
60         }
61     }
62 }
63 //
64 // separates the base script name and version number.  used by auto_retire.
65 list compute_basename_and_version(string to_chop_up)
66 {
67     // minimum script name is 2 characters plus a version.
68     integer space_v_posn;
69     // find the last useful space and 'v' combo.
70     for (space_v_posn = llStringLength(to_chop_up) - 3;
71         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
72         space_v_posn--) {
73         // look for space and v but do nothing else.
74     }
75     if (space_v_posn < 2) return [];  // no space found.
76     // now we zoom through the stuff after our beloved v character and find any evil
77     // space characters, which are most likely from SL having found a duplicate item
78     // name and not so helpfully renamed it for us.
79     integer indy;
80     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
81         if (llGetSubString(to_chop_up, indy, indy) == " ") {
82             // found one; zap it.  since we're going backwards we don't need to
83             // adjust the loop at all.
84             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
85         }
86     }
87     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
88     // ditch the space character for our numerical check.
89     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
90     // strip out a 'v' if there is one.
91     if (llGetSubString(chop_suffix, 0, 0) == "v")
92         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
93     // if valid floating point number and greater than zero, that works for our version.
94     string basename = to_chop_up;  // script name with no version attached.
95     if ((float)chop_suffix > 0.0) {
96         // this is a big success right here.
97         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
98         return [ basename, chop_suffix ];
99     }
100     // seems like we found nothing useful.
101     return [];
102 }
103 //
104 //////////////
105
106 default
107 {
108     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
109     on_rez(integer parm) { state rerun; }
110 }
111 state rerun { state_entry() { state default; } }
112
113 state real_default
114 {
115     state_entry()
116     {
117         auto_retire();
118         llSay(0, "This link is #" + (string)llGetLinkNumber());
119     }
120     
121     on_rez(integer parm) { llResetScript(); }
122 }