/
upgradedb.3.0a8-to-3.0a9.02/
upgradedb.3.0a8-to-3.0a9.02/files/
upgradedb.3.0a8-to-3.0a9.02/scripts/
@mojo on
@core $robot
@program $sys.startup() +access=driver -e
  arg args;
  var opt, str, obj, f, ver, firsttime, doinit;
  
  set_heartbeat(0);
  
  // clean db?
  if ("-clean" in args) {
      catch any {
          dblog("** Cleaning Database..");
          (> .clean_database() <);
          dblog("** Done.");
          if (dict_contains(startup, 'time))
              startup = dict_del(startup, 'time);
      } with {
          .log($parse_lib.traceback(traceback()));
      }
      doinit = "-init" in args;
  } else {
      doinit = "-init" in args || !dict_contains(startup, 'time);
  }
  
  // init?
  if (doinit) {
      catch any {
          dblog("** Initializing Database..");
          (> .init_database() <);
          dblog("** Done.");
      } with {
          .log($parse_lib.traceback(traceback()));
      }
  }
  
  // done?
  if ("-quit" in args) {
      dblog("** Shutting down.");
      .shutdown();
      return;
  }
  
  // make core?
  if ((opt = find opt in (args) where (opt.match_begin("-makecore=")))) {
      ver = regexp(args[opt], "-MAKECORE=(.*)$")[1];
      dblog("** Calling .make_core(\"" + ver + "\")..");
      .make_core(ver);
      return;
  }
  
  // Standard startup..
  startup = startup.add('time, time());
  backup = backup.add('next, time() + backup['interval]);
  catch any {
      // get back the heartbeat
      set_heartbeat(startup['heartbeat_interval]);
  
      // Bind functions for security
      for f in (bindings) {
          catch any
              bind_function(@f);
          with
              dblog("** Unable to bind function " + f[1] + "() to " + f[2]);
      }
  
      // tell objects who should know, that we are online
      if (type(args) != 'list)
          args = [];
      for obj in (startup['objects]) {
          .log("Calling " + obj + ".startup()");
          catch any
              (> obj.startup(@args) <);
          with
              .log($parse_lib.traceback(traceback()));
      }
  } with {
      .log("** Startup ERROR at " + ctime() + ":");
      .log(toliteral(traceback()));
      .log($parse_lib.traceback(traceback(), -1, ""));
  }
.
@program $sys.clean_database() +access=private -e
  var obj, p, c, cmd, other;
  
  // cleanup some of $root's messiness
  for obj in ($root.descendants()) {
      (| obj.clean_root() |);
      refresh();
  }
  other = $command_cache.children().setremove($user_interfaces).mmap('descendants).flatten().compress();
  
  // purge all command caches
  for obj in ($has_commands.descendants()) {
      (| obj.purge_caches() |);
      refresh();
  }
  
  // check user info (move'em home etc)
  for obj in ($user.descendants()) {
      if ((| obj.home() != obj.location() |))
          (| obj.move_to(obj.home()) |);
      refresh();
  }
  
  // validate all locations and location content's
  for obj in ($physical.descendants()) {
      (| obj.validate_contents() |);
      if (obj.has_ancestor($located)) {
          if (!valid(obj.location()) || !(obj in obj.location().contents()))
              obj.move_to((| obj.home() |) || $lost_and_found);
      }
      refresh();
  }
.
@program $sys.init_database() +access=private -e
  var obj, p, c, cmd, other;
  
  // get back caches
  other = $command_cache.children().setremove($user_interfaces).mmap('descendants).flatten().compress();
  for obj in (other) {
      (| obj.rehash_caches() |);
      refresh();
  }
  
  // create location caches
  for obj in ((| $location.descendants() |) || []) {
      for p in (obj.contents()) {
          (| obj.add_object_to_remote_cache(p) |);
          refresh();
      }
      refresh();
  }
.
@program $lag_watcher.shutdown() -e=0 +access=pub
  // Called by $sys.shutdown();
  (> .perms(sender(), 'manager) <);
  $heart.del_heartbeat();
  last_time = 0;
  lags = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
.