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