-- Copyright (C) 1984, 1985  by Xerox Corporation. All rights reserved. 
-- ProfileNoDisk.mesa, HGM, 26-Jul-85  2:01:22

DIRECTORY
  Auth USING [Flavor, IdentityHandle, MakeIdentity],
  Heap USING [systemZone],
  NSName USING [NameRecord],
  NSString USING [String, StringFromMesaString],
  Profile USING [Qualification],
  String USING [AppendChar, AppendString, AppendSubString, Length, SubStringDescriptor];

ProfileNoDisk: PROGRAM
  IMPORTS Auth, Heap, NSString, String
  EXPORTS Profile =
  BEGIN

  userName: STRING = "Foo";
  userPassword: STRING = NIL;
  defaultDomain: STRING = "PARC";
  defaultOrganization: STRING = "Xerox";
  defaultRegistry: STRING = "PA";

  simple: Auth.IdentityHandle ← NIL;
  strong: Auth.IdentityHandle ← NIL;

  z: UNCOUNTED ZONE = Heap.systemZone;
  S: PROC [s: LONG STRING] RETURNS [NSString.String] = INLINE {
    RETURN[NSString.StringFromMesaString[s]]};
    
  GetID: PUBLIC PROC [flavor: Auth.Flavor, proc: PROC [id: Auth.IdentityHandle]] =
    BEGIN
    SELECT flavor FROM
      simple => proc[simple];
      strong => proc[strong];
      ENDCASE => ERROR;
    END;

  GetUser: PUBLIC PROC [
     proc: PROC [name, password: LONG STRING], qualification: Profile.Qualification ← none] =
   BEGIN
   proc[userName, userPassword];
   END;

  GetDefaultDomain: PUBLIC PROC [proc: PROC [LONG STRING]] = {proc[defaultDomain]};
  
  GetDefaultOrganization: PUBLIC PROC [proc: PROC [LONG STRING]] = {proc[defaultOrganization]};
  
  Qualify: PUBLIC PROC [
    token, newToken: LONG STRING, qualification: Profile.Qualification] = {
    namePart: CARDINAL ← String.Length[token];
    currentQual: Profile.Qualification ← none;
    octalAddress: BOOLEAN ← TRUE; -- only '0..'7 and '# allowed
    ss: String.SubStringDescriptor;
    chChar: CHARACTER = ':;
    regChar: CHARACTER = '.;
    IF String.Length[token] = 0 THEN RETURN;
    FOR i: CARDINAL IN [0..token.length) DO
       SELECT token[i] FROM
         regChar => {namePart ← i; currentQual ← registry};  -- look for last dot
	 chChar => {namePart ← i; currentQual ← clearinghouse; EXIT}; -- first :
	 IN['0..'7], '# => NULL;
	 ENDCASE => octalAddress ← FALSE;
       ENDLOOP;
    IF currentQual = qualification OR octalAddress THEN {
      String.AppendString[newToken, token]; RETURN};
    ss ← [base: token, offset: 0, length: namePart];
    String.AppendSubString[newToken, @ss];
    SELECT qualification FROM
      none => NULL;
      registry => {
	String.AppendChar[newToken, regChar];
        String.AppendString[newToken, defaultRegistry]; };
      clearinghouse => {
        String.AppendChar[newToken, chChar];
        String.AppendString[newToken, defaultOrganization];
        String.AppendChar[newToken, chChar];
        String.AppendString[newToken, defaultDomain]; };
      ENDCASE};

  Init: PROC = {
    myName: NSName.NameRecord ← [
      org:  S[defaultOrganization],
      domain: S[defaultDomain],
      local: S[userName]];
    strong ← Auth.MakeIdentity[
	    myName: @myName, password: S[userPassword],
	    z: z, style: strong, dontCheck: TRUE];
    simple ← Auth.MakeIdentity[
	    myName: @myName, password: S[userPassword],
	    z: z, style: simple, dontCheck: TRUE];
    };

  Init[];
  END.