
/*
-----------------------------------------------------------------------------
UU-Retriever

This script written by Jan Van Overbeke

This script needs UUDecode V37.22 (or later if arguments are compatible).

Any remarks to Jan Van Overbeke, Fido: 2:292/603.29
Let me know if your script ends faulty, or you have trouble with this script
Giving me a clear description of what you did and what compu you have
-----------------------------------------------------------------------------
*/

/* ---      This is a file-intensive script.      --- */
/* ---  I learned that open(), close(), writeln() --- */
/* ---          Must be perceded by CALL          --- */
/* ---  Like: call writeln(outputter)             --- */
/* ---  and:  temp=readln(inputter)               --- */
/* -------------------------------------------------- */

/* ---               Never use '0a'x               --- */
/* --- That would get your requesters into trouble --- */
/* --------------------------------------------------- */
CR='0d'x
options results
address SPOT
call addlib('rexxsupport.library',0,-30)

/* --- Put up a credits-requester --- */
/* ---------------------------------- */
tell='UU-Retriever'CR'Version 1.0'CR||CR
tell=tell'Retrieves a file from UUEncoded messages'CR
tell=tell'Multiple messages when created by UU-Writer'CR
tell=tell'Compatible with every other if 1 message'CR||CR
tell=tell'UU-Retriever & UU-Writer: Written by'CR
tell=tell'Jan Van Overbeke, 2:292/603.29'
'requestnotify PROMPT "'tell'" CENTER'

/* ---     Do some checking first.    --- */
/* ---   checking is very important   --- */
/* --- so everyone can use the script --- */
/* -------------------------------------- */
if ~exists('c:UUDecode') then do
    'requestnotify PROMPT "Could not find c:UUDecode (V37.22)"'
    exit
    end
if ~exists('c:delete') then do
    'requestnotify PROMPT "Could not find c:Delete"'
    exit
    end
if ~exists('c:join') then do
    'requestnotify PROMPT "Could not find c:Join"'
    exit
    end
if ~exists('c:Rename') then do
    'requestnotify PROMPT "Could not find c:Rename"'
    exit
    end

/* --- The area list is NOT the right place --- */
/* ---          to use this script          --- */
/* -------------------------------------------- */
'isarealist'
if RC=0 then do
    'requestnotify PROMPT "Cannot UU-Retrieve in area-list!"'
    exit
    end

/* --- Let's see if the message we're in is UU-Written. --- */
/* -------------------------------------------------------- */
saveascii 't:TEMP-ENCODED' overwrite noheader noorigin
call open(inputter,'t:TEMP-ENCODED','R')
goon=1
do while goon
    readed=readln(inputter)
    if eof(inputter) then do
        'requestnotify PROMPT "Seems like this message is not UU-written."'
        call close(inputter)
        address command 'c:delete >nil: t:part1'
        exit
        end
    if pos('begin',readed)~=0 & datatype(word(readed,2))='NUM' then do
        goon=0
        file=word(readed,3)
        end
    end
call close(inputter)

/* --- The message is UU-Written =:*] --- */
/* ---  Now get info out of SUBJECT   --- */
/* -------------------------------------- */
'getsubject'
original=word(result,1)
result=word(result,2)
splitnr=right(result,length(result)-lastpos('/',result))
if datatype(splitnr)~='NUM' then splitnr=1

/* ---     Let's check if all messages are there.    --- */
/* ---    And meanwhile save them into a BIG file    --- */
/* --- like first command, SPOT will append the text --- */
/* ----------------------------------------------------- */
if splitnr>1 then do
    do t=2 to splitnr
        'nextmessage'
        'getsubject'
        if result~=original' 't'/'splitnr then do
            tell="Problem while reading messages!"CR
            tell=tell"Conflict between subjects"CR
            tell=tell"Subjects are wrong, or a message is missing"
            'requestnotify PROMPT "'tell'" CENTER'
            address command 'c:delete >nil: t:TEMP-ENCODED'
            exit
            end
        saveascii 't:TEMP-ENCODED' noheader
        end
    end

/* --- Request a path&file to save to --- */
/* -------------------------------------- */
help=lastpos('/',original)
if help=0 then help=pos(':',original)
if help=0 then path='spot:'
          else path=delstr(original,help+1)
if ~exists(path) then path='spot:'
goon=1
do while goon
    'requestfile TITLE "Pick a file 2 save" PATH "'path'" FILE "'file'"'
    if RC=5 then exit
    filename=result
    goon=0
    if exists(filename) then do
        'requestresponse PROMPT "Overwrite?" GADGETS "Yes|No"'
        if RC=1 then address command 'c:delete >nil: "'filename'"'
                else goon=1
        end
    end
help=lastpos('/',filename)
if help=0 then help=pos(':',filename)
if help=0 then do
            path='spot:'
            file2=filename
            end
      else do
          path=delstr(filename,help+1)
          file2=delstr(filename,1,help)
          end

/* ---  Get TEMP-ENCODED back to an original UU-Encoded file  --- */
/* --- The encoded part begins after the unpair (->nmr) lines --- */
/* -------------------------------------------------------------- */
if splitnr>1 then do
call open(inputter,'t:TEMP-ENCODED','R')
call open(outputter,'t:FULLY-ENCODED','W')
call writeln(outputter,'')
numolines=0
do while ~eof(inputter)
    line=readln(inputter)
    if pos('-----------------------',line)>0
        then numolines=numolines+1
        else do
            if ~(numolines/2=trunc(numolines/2)) & line ~=''
                then call writeln(outputter,line)
            end
    end
call writeln(outputter,'')
call close(inputter)
call close(outputter)
address command 'c:delete >nil: t:TEMP-ENCODED'
end
else address command 'c:rename >nil: t:TEMP-ENCODED as t:FULLY-ENCODED'

/* --- Start UU-Decoding here --- */
/* --- Routine taken from:    --- */
/* --- ++ Francois Helsen     --- */
/* --- ++ David Nouls         --- */
/* ---                        --- */
/* --- Dunno what it means    --- */
/* ------------------------------ */
olddir=pragma('d',path)
address command 'c:UUDecode >nil: from t:FULLY-ENCODED'
call pragma('d',olddir)
address command 'c:delete >nil: t:FULLY-ENCODED'
if (path||file~=path||file2) then
     address command 'c:rename >nil: "'path||file'" as "'path||file2'"'

/* --- Finished. --- */
/* ----------------- */
tell='Finished.'
'requestnotify PROMPT "'tell'" CENTER'
exit





