/*  $VER: JoinConfs.br 1.0 (27.4.97)
 *
 *  Arexx script for joining confs. 
 *
 *  Script by: Eivind Nordseth, Ultima Thule Software.
 */

	options results
/*	trace results */

	parse arg argument
	template = 'BBSNAME/A,OLDCONF/A,NEWCONF/A'

	if(argument = '' | argument = '?') then
	do
		say '$VER: JoinConfs.br 3.2 (23.4.97)'
		say 'Template:' template
		say 'This arexx script is ment to be used when a conference changes'
		say 'name, and you have both the old and new conference in your'
		say 'database. It will add all messages in the new name conference'
		say 'to the old name conf. Afterwards is the the old name conference'
		say 'renamed to the name of the new name conference.'
		exit
	end

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

	address BBSREAD

	/* Bit numbers for message flags */

	MDB_READ          =  0   /* Message is read. */
	MDB_REPLIED       =  1   /* Message is replied. */
	MDB_PRIVATE       =  2   /* Message is private. */
	MDB_TO_USER       =  3   /* Message is to the user. */
	MDB_FROM_USER     =  4   /* Message is from the user. */
	MDB_DELETED       =  5   /* Message is deleted. */
	MDB_UNRECOVERABLE =  6   /* Message is can not be undeleted. */
	MDB_KEEP          =  7   /* Keep message. Message will not be deleted during conference packing. */
	MDB_TO_ALL        =  8   /* Message is to all. (has no reciever) */
	MDB_XPK_TEXT      =  9   /* Message text is Xpk'ed. (Private flag) */
	MDB_MARKED        = 10   /* Message is marked.  */
	MDB_URGENT        = 11   /* Message is urgent.	*/
	MDB_IMPORTANT     = 12   /* Message is important. */
	MDB_SUPERMARKED   = 13   /* Message will not be unmarked as long as this flag is set. */

	rc = 0
	signal on ERROR 

	READARGS template ARGS CMDLINE argument

	GETCONFDATA ARGS.BBSNAME ARGS.OLDCONF OLDCONFDATA

	GETCONFDATA ARGS.BBSNAME ARGS.NEWCONF NEWCONFDATA

	do i= NEWCONFDATA.FIRSTMSG to NEWCONFDATA.LASTMSG

		drop MSGTAGS.

		READBRMESSAGE ARGS.BBSNAME ARGS.NEWCONF i headstem MSGTAGS textstem MSGTAGS datastem MSGDATA

		if ~bittst(MSGDATA.FLAGS,MDB_DELETED) then
		do
			exargs = ''
			if ~bittst(MSGDATA.FLAGS,MDB_MARKED)   then exargs = exargs || ' DONTMARKMESSAGE' 
			if bittst(MSGDATA.FLAGS,MDB_PRIVATE)   then exargs = exargs || ' PRIVATE'
			if bittst(MSGDATA.FLAGS,MDB_READ)      then exargs = exargs || ' READ'
			if bittst(MSGDATA.FLAGS,MDB_URGENT)    then exargs = exargs || ' URGENT'
			if bittst(MSGDATA.FLAGS,MDB_IMPORTANT) then exargs = exargs || ' IMPORTANT'
		
			say 'Writing message' i ' in' NEWCONFDATA.NAME 'to' OLDCONFDATA.NAME

			WRITEBRMESSAGE ARGS.BBSNAME ARGS.OLDCONF stem MSGTAGS exargs
			
			msgnr = result

			exargs = ''
	
			if bittst(MSGDATA.FLAGS,MDB_KEEP)    then exargs = exargs || ' SETKEEP'
			if bittst(MSGDATA.FLAGS,MDB_REPLIED) then exargs = exargs || ' SETREPLIED'

			hazelevel = MsgHazeLevel(MSGDATA.FLAGS)
			if hazelevel ~= 0 then exargs = exargs || ' HAZELEVEL ' || hazelevel

			if exargs ~= '' & msgnr ~= -1 then UPDATEBRMESSAGE ARGS.BBSNAME ARGS.OLDCONF msgnr exargs
		end
	end

	CONFIGCONF ARGS.BBSNAME ARGS.NEWCONF DELETECONF

	CONFIGCONF ARGS.BBSNAME ARGS.OLDCONF NEWNAME NEWCONFDATA.NAME BBSCONFNR OLDCONFDATA.BBSCONFNR

	say 'All done!'

	exit

ERROR:
	if(rc ~= 0) then 
	do
		say 'Error' rc 'in line' SIGL ':' BBSREAD.LASTERROR
		exit
	end
	exit


MsgHazeLevel: PROCEDURE
ARG msgflags

	MDB_HAZE_BIT0     = 24   /* Message haze level bit 0. */
	MDB_HAZE_BIT1     = 25   /* Message haze level bit 1. */

	hazelevel = 0

	if bittst(msgflags, MDB_HAZE_BIT0) then hazelevel = hazelevel + 1
	if bittst(msgflags, MDB_HAZE_BIT1) then hazelevel = hazelevel + 2

	return hazelevel
