/*
** $VER: dofile.rexx 3.4 (15.8.97) Rolf Rotvel
**
** Uses datatypes.library & rexxreqtools.library
*/

defdir = 'txt:'                                 /* Defdir for filerequester */
multicmd = 'sys:utilities/multiview screen'     /* Path (& options) to Multiview */

/*
** End cfg.
*/

/* Load libraries */
call addlib('rexxsupport.library', 0, -30, 0)
call addlib('rexxreqtools.library', 0, -30, 0)
call addlib('datatypes.library', 0, -30)

/* Get version info from $VER line */
version = subword(sourceline(2), 3, 2) 

/* If no arg then open filerequester */
if arg() = 0 then do
    /* Check defdir */
    if ~exists(defdir) | defdir = '' then defdir = pragma('d')
    /* Get file or dir */
    txt = gettxt(defdir)
end
else txt = strip(arg(1), 'b', '"')  /* Get args and remove any quotes */

/* Is it a lone .info file? */
if ~exists(txt) then do
    txt2 = txt||'.info'
    if exists(txt2) then txt = txt2
    else do
        errtxt = 'File error'
        call syntax()
    end
end

/* Get (data)type of file */
errtxt = 'datatypes.library error'
type = getfiletype(txt)

/* Open filerequester if it's a directory? */
if type = 'DIRECTORY' then type = getfiletype(gettxt(txt))

/* Get command associated with filetype */
cmd = getclip('dofile.'||type)

/* Check ascii & binary files for file extensions */
select 
    when type = 'ASCII' then do
        cmd = getcmd(txt)
        /* Let Multiview do the job on ascii files if no cmd */
        if cmd = '' then cmd = multicmd
    end
    when type = 'BINARY' then cmd = getcmd(txt)
    otherwise do     
        /* It's a known datatype so Multiview should work */        
        if cmd = '' then cmd = multicmd
    end
end

errtxt = 'Couldn''t process'
if cmd = '' then call syntax()

if right(cmd, 2) = '->' then do
    /* Concatate file with last word of command */
    parse var cmd cmd '->' .
    errtxt = cmd||' error'
    w = words(cmd)
    cmd = subword(cmd, 1, w - 1)||' "'||subword(cmd, w, 1)||txt||'"'
end
else do
    errtxt = cmd||' error'
    cmd = cmd||' "'||txt||'"'
end

say realtype||' -> '||cmd
signal on failure
address command cmd
exit


GETCMD: procedure expose cmd type
arg path
newcmd = ''

/* Extract file extension from path */
parse value substr(path, max(pos(':', path), lastpos('/', path)) + 1) with base '.' suff

if suff ~= '' then do   /* Filename has an extension */
    newcmd = getclip('dofile..'||suff)
    if newcmd ~= '' then type = '.'||suff
    else do             /* No match > check base for MOD. type names */
        newcmd = getclip('dofile..'||base)
        if newcmd ~= '' then type = '.'||base
    end
    if newcmd ~= '' then cmd = newcmd
end
return cmd


/* Get argument from filereq - exit if none */
GETTXT:
txt = rtfilerequest(arg(1),, version,, 'rt_screentofront=true')
if txt = '' then exit
return strip(txt, 'b', '"')


GETFILETYPE: 
/* Error handling for datatypes.library v40.6 */
signal on syntax
realtype = examinedt(arg(1),, var)
signal off syntax

/* Error handling for datatypes.library v45.3 */
if datatype(realtype, 'w') then call syntax()

return upper(strip(realtype, 't', '0'x))


SYNTAX:
call rtezrequest(errtxt||':'||'0a'x||txt,, version)
exit


FAILURE:
exit
