/*  $VER: SaveUserlist.thor 1.2PH (23.06.97)
 *
 *  Save the user database from a system to a file.
 *
 *  Can also parse into a system a file previously saved
 *  by this script.
 *  
 *  Script by: Petter Nilsen, Ultima Thule Software.
 */

/*trace results*/

UDB_DELETED       = 0  /* User is deleted */

options results

thorport = address()

if ~show('p', 'BBSREAD') then do
	address command
		"run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead"
		"WaitForPort BBSREAD"
end

address(thorport)

REQUESTNOTIFY TEXT '"Parse an old list into the userdatabase ?"' BT '" _Yes | _No "'
if(rc ~= 0) then
do
	say THOR.LASTERROR
	exit
end

if(result = 1) then 
do
	GETGLOBALCONFIG stem GLOBALCONFIG

	REQUESTFILE TITLE '"Select userlist to parse:"' ID '"'GLOBALCONFIG.SAVEDIR'"' FP
	if(rc ~= 0 | result = "") then exit
	else file = result

	CURRENTSYSTEM stem CURRENT
	if(rc = 30) then
	do
		say THOR.LASTERROR
		exit
	end
	else
	if(rc = 1) then
	do
		address(BBSREAD)
		GETBBSLIST BBSLIST
		if(rc ~= 0) then
		do
			say BBSREAD.LASTERROR
			exit
		end
		address(thorport)
		REQUESTLIST instem BBSLIST title '"Select system to parse into:"' SIZEGADGET
		if(rc ~= 0) then exit
		else CURRENT.BBSNAME = result
	end

	address(BBSREAD)

	GETBBSDATA '"'CURRENT.BBSNAME'"' stem BBSDATA
	if(rc ~= 0) then 
	do
		say BBSREAD.LASTERROR
		exit
	end

	if(~open(fh, file, 'R')) then 
	do
		say "Failed to open file: "file
		exit
	end

	do while(~eof(fh))
		line = readln(fh)
		if(line = '#') then
		do
			drop USER.
			line = readln(fh)
			if(left(line, 6) = "Name: ") then
			do
				USER.NAME = substr(line, 7)
				say "Name:" USER.NAME
				line = readln(fh)
			end
			if(left(line, 9) = "Address: ") then
			do
				USER.ADDRESS = substr(line, 10)
				say "Address:" USER.ADDRESS
				line = readln(fh)
			end
			if(left(line, 7) = "Alias: ") then
			do
				USER.ALIAS = substr(line, 8)
				say "Alias:" USER.ALIAS
				line = readln(fh)
			end
			if(left(line, 9) = "Comment: ") then
			do
				USER.COMMENT.1 = substr(line, 10)
				say "Comment:" USER.COMMENT.1
				USER.COMMENT.COUNT = 1
/*				line = readln(fh) */
			end
			WRITEBRUSER '"'CURRENT.BBSNAME'"' stem USER
			if(rc ~= 0) then 
			do
				say BBSREAD.LASTERROR
				exit
			end
			say "User written successfully."
			say '--------------------------'
		end
	end
	call close(fh)
end
else
do
	GETGLOBALCONFIG stem GLOBALCONFIG

	REQUESTFILE TITLE '"Save userlist to file:"' ID '"'GLOBALCONFIG.SAVEDIR'"' FP
	if(rc ~= 0 | result = "") then exit
	else file = result

	CURRENTSYSTEM stem CURRENT
	if(rc = 30) then
	do
		say THOR.LASTERROR
		exit
	end
	else
	if(rc = 1) then
	do
		address(BBSREAD)
		GETBBSLIST BBSLIST
		if(rc ~= 0) then
		do
			say BBSREAD.LASTERROR
			exit
		end
		address(thorport)
		REQUESTLIST instem BBSLIST title '"Select system to save from:"' SIZEGADGET
		if(rc ~= 0) then exit
		else CURRENT.BBSNAME = result
	end

	address(BBSREAD)

	GETBBSDATA '"'CURRENT.BBSNAME'"' stem BBSDATA
	if(rc ~= 0) then 
	do
		say BBSREAD.LASTERROR
		exit
	end
	
	if(~open(fh, file, 'W')) then 
	do
		say "Failed to open file: "file
		exit
	end

	if(BBSDATA.FIRSTUSER < 1 | BBSDATA.LASTUSER < 1) then exit
	else
	do
		address(thorport)
		OPENPROGRESS TITLE '"' || 'Saving users from ' || CURRENT.BBSNAME || '"' TOTAL BBSDATA.LASTUSER-BBSDATA.FIRSTUSER+1 AT '"_Abort"'
		if(rc ~= 0) then exit

		window = result

		do n = BBSDATA.FIRSTUSER to BBSDATA.LASTUSER

			address(thorport)
			UPDATEPROGRESS REQ window CURRENT n-BBSDATA.FIRSTUSER+1 PT '"' || 'Saving user #' || n || '"'

			address(BBSREAD)
			drop USERDATA.

			READBRUSER bbsname '"'CURRENT.BBSNAME'"' usernr n datastem USERDATA
			if(rc ~= 0) then 
			do
				say BBSREAD.LASTERROR
				call close(fh)
				address(thorport)
				CLOSEPROGRESS REQ window
				exit
			end

			if ~bittst(USERDATA.FLAGS, UDB_DELETED) then
			do
				drop USERTAGS.
				READBRUSER bbsname '"'CURRENT.BBSNAME'"' usernr n tagsstem USERTAGS
				if(rc ~= 0) then
				do
					say BBSREAD.LASTERROR
					address(thorport)
					CLOSEPROGRESS REQ window
					call close(fh)
					exit
				end

				call writeln(fh, '#')
				call writeln(fh, 'Name: ' || USERTAGS.NAME)
				if(symbol("USERTAGS.ADDRESS") = "VAR") then do 
					call writeln(fh, 'Address: '|| USERTAGS.ADDRESS)
				end
				else do
					call writeln(fh, 'Address: ')
				end
				if(symbol("USERTAGS.ALIAS") = "VAR") then do 
					call writeln(fh, 'Alias: '|| USERTAGS.ALIAS)
				end
				else do
					call writeln(fh, 'Alias: ')
				end
				if(symbol("USERTAGS.COMMENT.COUNT") = "VAR") then do 
					call writeln(fh, 'Comment: '|| USERTAGS.COMMENT.1)
				end
				else do
					call writeln(fh, 'Comment: ')
				end
			end
		end
		address(thorport)
		CLOSEPROGRESS REQ window
	end

	call close(fh)
end
exit
