/*
 *
 * Types a file Specified by the User....
 *
 * (c) 1994 by K.P. van Beem (2:280/464.2, patrick.van.beem@aobh.xs4all.nl)
 *
 * Written by Ray Abram to Supplement the DOpus Twin Package
 *   - file is temporally stored to T:
 *     - Thus ensure that there is enough free RAM to hold the data
 *
 *   - Program also attempts to recognize the following file types...
 *   -    *.guide    - an Amiga Guide File  - Run AMIGAGUIDE
 *   -    *.anim     - an Animation File    - Run VT   (ViewTek)
 *   -    *.IFF      - an IFF picture       - Run VT   (ViewTek)
 *   -    *.GIF      - a  GIF picture       - Run VT   (ViewTek)
 *   -    *.jpg      - a  Jpegged Picture   - Run VT   (ViewTek)
 *   -    *.info     - an Icon File         - Run DOPus's internal IconInfo
 *   -    *.8svx     - a Sample File        - Run DOPus's internal Play
 *   -    MOD.*      - a MOD file           - Run DOPus's internal PlayST
 *   -    *.MOD      - a MOD file           - Run DOPus's internal PlayST
 *      -> note: when Twin is Running, Modules play a bit ODD !!
 *   -    NOT KNOWN  - any other file       - Run DOPus's internal ANSIREADer
 *
 *    - if you double click on a Cache Dir file, then the file is loaded into
 *      the active window...
 */

DOpusPort   = 'DOPUS.1'

if ~show(l,"rexxsupport.library") then
    call addlib("rexxsupport.library",0,-30,0)
if showlist('Ports', DOpusPort) = 0 then do
   say 'Directory Opus Arexx port not found. Aborting.'
   call CleanUp
end

address 'DOPUS.1'
options results

/*get the filename sent to me */
parse arg FilePath
if FilePath="" then do
   TopText "You have to specify a File. !!"
   exit
end

if words(File) > 1 then do
   Request "Spaces in a Filename are not Allowed !!"
end

/* see if the requested file is a cache dir  (scan for ';'*/
Filename = CutOutFilename(FilePath)
FName    = Filename

Found = False
pos =  length(Filename)
do while pos > 0
   if substr(Filename,pos,1) = '=' then
      Found = True
   pos = pos - 1
end

if Found = True then do
   Request "Do you want to Load Cache Dir :-" Filename
   if result = 0 then
      call CleanUp
   else do
      Filename = ConvertFrom(Filename)
      ClearWin
      address AREXX "rexx:dopus/ReadDir.rexx" Filename
      call EndBit
   end
end

/* ensure user wants to do this */
Request "Do you want to Process :-" Filename
if result = 0 then
   call CleanUp

else do
   Busy on

   ToDir = 'T:' || FName

   /* setup DOpus window and tell user what's happening */
   TopText "Reading in" FilePath "..."

   /*tell TWIN to copy the File...  and then put something into the Queue:*/
Trace ?r

   Path = CutOutPath(FilePath)
   SD   = EnterDir(Path)
   address command 'echo >PPipe: copy' SD || FName ToDir
   address command 'echo >PPipe: help'

   if open(PipeList, "QUEUE:Twin", 'R') then do
      call WaitForAFile
      close(PipeList)
   end

   /* get the last file type */
   FileEnd  =upper(right(FilePath,3))
   FileStart=upper(left(Filename ,3))

/********************************************************************
 *  do the recognition ...                                          *
 *     - add more "when"'s if you want before the "otherwise"...    *
 ********************************************************************/

   select
      when FileStart = "MOD" then
        PLAYST ToDir
      otherwise
         select
            when FileEnd = "IDE" then do
               DopusToBack
               address command 'AMIGAGUIDE ' ToDir
               DopusToFront
            end
            when FileEnd = "IFF" then
              address command 'VT ' ToDir
            when FileEnd = "NIM" then
              address command 'VT ' ToDir
            when FileEnd = "GIF" then
              address command 'VT ' ToDir
            when FileEnd = "JPG" then
              address command 'VT ' ToDir
            when FileEnd = "ZVX" then
              PLAY ToDir
            when FileEnd = "MOD" then
              PLAYST ToDir
            when FileEnd = "NFO" then
              ICONINFO ToDir
            otherwise ANSIREAD ToDir
         end
      end

   /* and now delete the temp file */
   address command 'echo >PPipe: delete ' ToDir
   address command 'echo >PPipe: help'
   if open(PipeList, "QUEUE:Twin", 'R') then do
      test = ''
      /* deleted is for a deleted file */
      do while (test ~= "deleted")
        junk = readln(PipeList)
        test = right(junk , 7, '')
      end
      close(PipeList)
   end

end


/* that's all folks !! */
TopText "Ready"

/*---------------------------------------------------------------------------*/

CleanUp: /* Remove any files and exit */
   Busy off
   exit
return
/*--------------------------------------------------------------------------*/

Quote: procedure /* add quotes to string */
   parse arg string
return '"'||string||'"'
/*---------------------------------------------------------------------------*/

CutOutFilename: procedure
   parse arg string
   bit = ''
   pos = length(string)
   count = 0
   do while((bit ~= ":") & (bit ~= "/"))
     bit = substr(string, pos, 1)
     pos = pos - 1
     count = count + 1
   end

return right(string, count-1)

CutOutPath: procedure
   parse arg string
   bit = ''
   pos = length(string)
   count = 0
   do while((bit ~= ":") & (bit ~= "/"))
     bit = substr(string, pos, 1)
     pos = pos - 1
     count = count + 1
   end

return left(string, length(string) - count + 1)
/*---------------------------------------------------------------------------*/

WaitForAFile:
   test = ''
   /* received is for a normal copy,   sent is for a copy as */
   do while ((test ~= "received") & (test ~= "   sent ") & (test ~= " copied ") & (test ~= "Error: D") & (test ~= "TWIN> Er"))
     junk = readln(PipeList)
     test = left(junk , 8, '')
   end

   /* now see if the file stopped due to an error */
   if ((test = "Error: De") | (test = "TWIN> Er")) then do
      request "An Error occured during the Copying... (Disk Full ?)"
      close(PipeList)
      call EndBit
   end

return

/*--------------------------------------------------------------------------*/
/* convert a file name from df0=ray\was\here  ->  df0:ray/was/here (un-cached filename)*/
ConvertFrom: procedure

   parse arg Dev '=' Path

   do until (t2 = '')
       parse var Path t1 '\' t2
       Path = t1 || '/' || t2
   end

   if left(dev,1) = '`' then
      dev = '~' || right(dev,length(dev)-1)

return Dev || ':' || left(Path,length(Path)-1)

/*--------------------------------------------------------------------------*/

/* Cd into the sent Directory path
   - this is needed, as TWIN has a command parameter limit of 30 characters,
      and as we all know AmigaDos file & paths can easily go over this limit !!*/

EnterDir: procedure

   parse arg Dev ':' Path

   /* get the 1st character to address the local or remote machine correctly */
   if left(Dev,1) = '~' then
      sbit = '~'
     else
      sbit = ''

   /* does the passed name have a DEVICE in it ? */
   if Dev ~= '' then
      address command 'echo >PPipe: cd' Dev || ':'

   do until (t2 = '')
      parse var Path t1 '/' t2
      path = t2
      if t1 ~= '' then
         address command 'echo >PPipe: cd' sbit || t1
   end

return sbit
