-- File: FTPSample.mesa, Edit: HGM November 8, 1979  3:40 PM

-- Copyright  Xerox Corporation 1979, 1980

DIRECTORY
  FTPDefs:		FROM "FTPDefs"			USING [
      FTPCloseConnection, FTPCreateUser, FTPDestroyUser, FTPError, FTPFinalize,
      FTPInitialize, FTPOpenConnection, FTPSetCredentials, FTPStoreFile, FTPUser,
      PupCommunicationPrimitives, AltoFilePrimitives],
  ImageDefs:		FROM "ImageDefs"		USING [StopMesa],
  IODefs:			FROM "IODefs"			USING [WriteLine],
  OsStaticDefs:	FROM "OsStaticDefs"	USING [OsStatics],
  StringDefs:		FROM "StringDefs"		USING [BcplToMesaString];
  
FTPSample: PROGRAM
  -- import list
    IMPORTS FTPDefs, ImageDefs, IODefs, StringDefs
  = BEGIN OPEN FTPDefs, ImageDefs, IODefs, OsStaticDefs, StringDefs;

-- variables
ftpInitialized: BOOLEAN ← FALSE;
ftpuser, copy: FTPUser ← NIL;
user: STRING ← [40];
password: STRING ← [40];

-- intercept errors
BEGIN ENABLE
  BEGIN
  FTPError =>
    BEGIN
    IF message # NIL THEN WriteLine[message];
    CONTINUE;
    END;
  UNWIND =>
    BEGIN
    IF ftpuser # NIL THEN FTPDestroyUser[ftpuser];
    IF ftpInitialized THEN FTPFinalize[];
    END;
  END;

-- initialize ftp
FTPInitialize[];  ftpInitialized ← TRUE;

-- create ftp user
ftpuser ← FTPCreateUser[AltoFilePrimitives[], PupCommunicationPrimitives[]];

-- set credentials to login user and password
BcplToMesaString[OsStatics↑.UserName, user];
BcplToMesaString[OsStatics↑.UserPassword, password];
FTPSetCredentials[ftpuser, primary, user, password];

-- open connection, store self, and close connection
FTPOpenConnection[ftpuser, "Iris"L, files, NIL];
[] ← FTPStoreFile[ftpuser, "FTPCSample.BCD"L, "FTPCSample.BCD"L, binary];
FTPCloseConnection[ftpuser];

-- destroy ftp user
copy ← ftpuser;  ftpuser ← NIL;  FTPDestroyUser[copy];

-- finalize ftp
ftpInitialized ← FALSE;  FTPFinalize[];
END; -- enable

-- return to exec
StopMesa[];

END. -- of FTPSample