-- File: CreateDB.mesa
-- Contents: creates a database with name given by command line

DIRECTORY DBView, UserExec, UserProfile, Rope, IOStream, MessageWindow;

CreateDB: PROGRAM IMPORTS DBView, UserExec, Rope, IOStream, UserProfile, MessageWindow =

BEGIN


ROPE: TYPE = Rope.Ref;
databasePathName: PUBLIC ROPE;
dbT: DBView.Transaction;
welcomeText: ROPE;


StartSquirrel: PROC =
  BEGIN
	r: ROPE; ch: CHAR;
	h: IOStream.Handle← UserExec.GetExecHandle[].commandLineStream;
	rdbName: ROPE← UserProfile.String[key: "RemoteDatabaseName", default: "<CedarDB>PDB"];
	databasePathName← UserProfile.String[key: "LocalDatabaseName", default: "[Local]DB"];
	IF (r← h.GetToken[]) # NIL THEN
	   SELECT ch← r.Fetch[0] FROM
	    '/ => IF (Rope.Lower[h.GetChar[]] = 'r) THEN databasePathName← rdbName;
	    '[, '< => databasePathName← Rope.Concat[r, h.GetRope[]];
	   ENDCASE => IF Rope.Letter[ch] THEN databasePathName← Rope.Concat["[Local]", r];
  END;

OpenUpDB: PROC =
  -- Does OpenDatabase call, sets up transaction
  BEGIN
  ENABLE DBView.IllegalDatabaseName, DBView.ProtectionViolation, DBView.ServerNotFound =>
	  {MessageWindow.Blink[]; MessageWindow.Append["Can't open database!"]; CONTINUE};
  [dbT, welcomeText]← DBView.OpenDatabase[databaseName: databasePathName, version: NewOnly];
  END;


StartSquirrel[];
OpenUpDB[];
DBView.CloseDatabase[];

END.