/* Chatop Chat-Exit - by Dreamer/Supplex/1oo% */

options results ; signal on SYNTAX ; /*signal on ERROR ; signal on IOERR*/
tr=transmit ; se=sendstring ; gc=getchar ; gu=getuser ; mg=maygetchar
a='rexxsupport.library';if ~show('l',a) then if ~addlib(a,0,-30) then exit

/******************************** CONFIG ***********************************/

Path="Pfiles:1oo/Chatop/"
variation=2

/* Variaton: 1 = One Line; 2 = only after new entry; 3 = always! */

/***************************************************************************/

/********* Getting Getusers *********/

getuser 1; username=result
getuser 23; port=result
bbsidentify sysop;sysop=result

/********* Reading Old Date *********/

ChTime=getclip(port'ChTime')

if ChTime="" then exit

Time=word(ChTime,3)

hour=left(Time,2)
minutes=substr(Time,4,2)
seconds=right(Time,2)

/********** getting actual Date **********/

address command "date >pipe:datum"

open(date,'pipe:datum','R')
ActTime=readln(date)
close(date)

Datum=word(ActTime,2)
NewTime=word(ActTime,3)

Newhour=left(NewTime,2)
Newminutes=substr(NewTime,4,2)
Newseconds=right(NewTime,2)

/********** calculating Chat-Time **********/

if word(ChTime,1) ~= word(ActTime,1)
	then 
		ChatSecs=(23-hour)*3060+(60-Minutes)*60+60-seconds+(Newhour*3060)+(NewMinutes*60)+NewSeconds
	else
		ChatSecs=(Newhour-hour)*3060+(Newminutes-Minutes)*60+NewSeconds-Seconds

/********** reading Datafile ***********/

open(dat,path'Chatop.dat','R')
do x=1 to 10
	chatop.x=readln(dat)
end
close(dat)

/********** reorganize **********/

if word(Chatop.10,1) > ChatSecs then do
	if variation<3 then do
		ChatMins=ChatSecs % 60
		ChatSecs=right((ChatSecs // 60),2,"0")
		tr "n1c7     1oo% CHATOP c2-- c6"sysop" SAYS:n1c2**** c6SoRRY, DuDE! c7"chatmins":"chatsecs"c6 MiNZ AiN'T ENuFF TO ENTeR DA TOP 1o CHaTTERZ!"
		setclip(port"ChTime")
		exit
		end
	if variation=3 then Showtop
end

do x=1 to 10
	ClockSec=word(chatop.x,1)
	if ClockSec > ChatSecs then NewChatop.x = Chatop.x
	else break
end

NewChatop.x = ChatSecs" "datum" "Username
postition=0; Position=x

do x=x+1 to 10
	y=x-1
	NewChatop.x=Chatop.y
end
changed=1

/************ write changed Dat-File ************/

open(dat,path'Chatop.dat','W')
do x=1 to 10
	writeln(dat,NewChatop.x)
end
close(dat)

/*********** view endurable best chatters **********/

ShowTop:

if Variation=1 then do
	tr "n1c7     1oo% CHATOP c2-- c6"sysop" SAYS:n1c2**** c7YeEH! c6YA ReACHED c7RaNK "position" c6iN DA TOP 1o CHaTTERZ LoG!"
	setclip(port"ChTime")
	exit
end

if changed=1 then do x=1 to 10
	Chatop.x=NewChatop.x
end

tr "f1*0"path"sys.chatop.msk}f0!8}"

do x=1 to 10
	ChatMins=word(Chatop.x,1) % 60
	ChatSecs=word(Chatop.x,1) // 60
	select
		when x=1 then se "c7"
		when x=2 then se "c6"
		otherwise se "c2"
		end
	if position=x then se "z4"
	se ">22}"||right(Chatmins,3)||":"right(Chatsecs,2,"0")||"   "
	se word(Chatop.x,2)"   "
	tr " "||substr(Chatop.x,wordindex(Chatop.x,3),24," ")||" z0"
end

tr "!2}"
if variation=2 then getchar

/********** fini **********/

setclip(port"ChTime")
exit

SYNTAX:;ERROR:;IOERR:;e1=' c9Errorcf: ca'rc' cf(ca'errortext(rc)'cf)'
e2='  c9Linecf: ce'left(sigl,4)'c9Filecf:';gu 1311992;a=result
gu 1311960;b=result;c='cb"ce'a||b'cb"';e2=e2' 'c;tr e1;tr e2;logentry e1
logentry e2;e=sourceline(sigl);do while e~='';e3='c9Sourcecf: cd'
e3=e3||left(e,37);tr e3;logentry e3;e=substr(e,38);end;bufferflush;exit
