/* POWER Request 1.o by Kyle of ARĪSE                            */
/* coded for Pater Pink's Power Board ˇ Call +49-(0)7244-2199   */

/* Edit here */

title     = 'DOORS:ARISE/PowerRequest/PowerRequest.txt'
reqdir    = 'DOORS:ARISE/PowerRequest/Requests'
ansdir    = 'DOORS:ARISE/PowerRequest/Answers'
readdir   = 'DOORS:ARISE/PowerRequest/Read'
workdir   = 'DOORS:ARISE/PowerRequest/Work'
reqamount = 'DOORS:ARISE/PowerRequest/Requests/Amount'

/* No more edits */

parse arg node
options results
address value "AERexxControl"node

signal on syntax

tr=transmit
gu=getuser
qu=query
sm=sendmessage

cls=""
CRLF="0D 0A"x
CR="0D"x
LF="0A"x

NOR = "[0m"
BLA = "[30m"
WHI = "[31m"
GRE = "[32m"
YEL = "[33m"
BLU = "[34m"
PIN = "[35m"
CYA = "[36m"
RED = "[37m"

Version = "1.0"

/* Hier faengt's an! */

	gu 12 ; SYSOP=Result
        gu 100 ; name=result
        gu 104 ; slot=result

        if ~exists(readdir) then address command 'C:makedir "' || readdir || '"'
        read=readdir || "/" || slot

        if ~exists(reqdir) then address command 'C:makedir "' || reqdir || '"'
        reqs=0
        if exists(reqamount) then do
         open(amount,reqamount,'R')
         reqs=readln(amount)
         close(amount)
        end


Main:
        tr cls
        showfile title
        sm RED
        tr Center(BLU"["YEL"V"BLU"]"NOR"iew requests",78)
        tr Center(BLU"["YEL"A"BLU"]"NOR"dd request  ",78)
        tr Center(BLU"["YEL"Q"BLU"]"NOR"uit         ",78)
        tr ""
        sm PIN"Please choose (V/A/Q): "NOR
read1:
        getchar
        char=upper(result)
        if char=CR then call read1

        if char='A' then do
         tr "Add request..."
Add:
         tr ""
         qu PIN"Please enter name of request               : "YEL
         reqfile=result
         qu PIN"Please enter Version No.                   : "YEL
         reqversion=result
rep:
         qu PIN"How many times credits do you want to give : "YEL
         reqcredits=result
         if datatype(reqcredits)~="NUM" then call rep
         tr ""
         sm NOR"Is the above correct(y/N/q)? "
         getchar
         char2=upper(result)
         if char2="Q" then call main
         if char2~="Y" then do
          tr "No..."
          call Add
         end
         tr NOR"Yes..."
         reqs=reqs+1
         request=reqdir || "/" || reqs
         open(req,request,'W')
         writeln(req,slot)
         writeln(req,reqfile)
         writeln(req,reqversion)
         writeln(req,reqcredits)
         writeln(req,name)
         close(req)
         open(amount,reqamount,'W')
         writeln(amount,reqs)
         close(amount)
         tr ""
         tr RED"O.K. your request will be added to the list, and we'll inform"
         tr RED"you, when someone uploaded it..."
         tr ""
         call waitkey
         call Main
        end

/* View requests */

        if char="V" then do
         tr "View requests..."
         if reqs=0 then call main
         do i=1 to reqs
          tr cls
          request=reqdir || "/" || i
          if ~exists(request) then iterate
          ans=ansdir || "/" || i
          if exists(ans) then iterate
          open(req,request,'R')
          reqslot=readln(req)
          reqfile=readln(req)
          reqversion=readln(req)
          reqcredits=readln(req)
          reqby=readln(req)
          tr NOR"Request no  : "RED""i
          tr NOR"Request     : "RED""reqfile
          tr NOR"Version     : "RED""reqversion
          tr NOR"Credits     : "RED""reqcredits"x"
          tr NOR"requested by: "RED""reqby
          close(req)
          tr ""
          sm GRE"Request Options: "YEL"A"CYA","YEL"Q"
          if reqslot=slot then sm CYA","YEL"D"
          sm CYA","YEL"?"CYA","YEL"<CR>"NOR" >: "
          getchar
          opt=upper(result)
          if opt=CR then iterate
          if opt="Q" then break

/* Delete */

          if opt="D"&reqslot=slot then do
           tr ""
           tr ""
           sm NOR"Are you sure to delete this request? (y/N) "
           getchar
           char2=upper(result)
           if char2="Y" then do
            newdir=ansdir || "/" || i
            address command 'c:delete "' || request || '"'
            address command 'c:delete "' || newdir || '" all'
           end
          end

/* Help */

          if opt="?" then do
           tr ""
           tr ""
           tr GRE"A"CYA">"YEL"nswer request"
           tr GRE"Q"CYA">"YEL"uit"
           if slot=reqslot then tr GRE"D"CYA">"YEL"elete request"
           tr GRE"?"YEL"=this info"
           tr GRE"<CR>"YEL"=next"
           i=i-1
           tr ""
           call waitkey
          end

/* Answer */

          if opt="A" then do
           if ~exists(ansdir) then address command 'C:makedir "' || ansdir || '"'
           address command 'C:makedir "' || workdir || '"'
           putustr workdir || "/"
           putuser 138
           address command 'C:list "' || workdir || '" >ram:workdir'
           open(dir,"ram:workdir",'R')
           line=readln(dir)
           l=find(line,'is empty')
           if l~=0 then do
            i=i-1
            iterate i
           end
           credits=0
           do j=1
            fileinfo.j=readln(dir)
            parse var fileinfo.j filename.j filesize.j fileinfo.j
            if filesize.j="files"|filesize.j="file" then break
            filesize.j=compress(filesize.j," ")
            credits=credits+filesize.j
           end
           close(dir)
           address command 'C:delete ram:workdir'
           tr ""
           mulcredits=reqcredits*credits
           tr NOR"You'll get"YEL reqcredits"x"credits" = "RED mulcredits" bytes."
           call waitkey
           gu 117 ; bytes=result
           bytes=bytes+mulcredits
           putustr bytes
           putuser 117
           newdir=ansdir || "/" || i
           if ~exists(newdir) then address command 'C:makedir "' || newdir || '"'
           do k=1 to j
            filename=Workdir || "/" || filename.k
            address command 'C:Copy "' || filename || '" to ' || newdir
           end
           address command 'C:delete "' || workdir || '" all'
           open(req,request,'A')
           writeln(req,name)
           close(req)
          end
         end
         open(r,read,'W')
         i=i-1
         writeln(r,i)
         close(r)
         call main
        end

        if char='Q' then do
         tr "Quit..."
         call exit
        end

        call read1

WaitKey:
        sm BLU"<Return>"
        getchar
        sm CR
        return

Error: ; Syntax: ; IOerr:
	tr CRLF""NOR" An Error Has Occured In The Main Program On Line #"sigl"! Exiting..."
	tr " Please Notify "GRE""SYSOP"."CRLF
	SHUTDOWN
	EXIT

Exit:
	tr crlf
	tr BLU"Call: "PIN"Pater Pink's Power Board"YEL" +49 (0)7244-2199"NOR
	tr ""
	SHUTDOWN
	EXIT
	END