/* LHAControl written 04.94 by Karsten Wysocki */

call addlib("rexxsupport.library",0,-30,0)
call addlib("apig.library",0,-30,0)

call SET_APIG_GLOBALS()

portname = "LHAControl"
portlist = Showlist('p')
if find(portlist,portname) > 0 then exit
p = openport(portname)

request = "freq = ALLOCFILEREQUEST();
           filename = REQUESTFILE(freq,multi,save,hail,dir,file,0,nofile,
                                  null(),-10,-2,200,211,'222022'x);
           call FREEFILEREQUEST(freq);
           pos = index(filename,'/');
           if pos = 0 then pos = index(filename,':');
              else do;
                     do forever;
                     newpos = index(filename,'/',pos+1);
                     if newpos > 0 then pos = newpos;
                     else leave;
                     end;
                   end;
           dir  = left(filename,pos);
           file = delstr(filename,1,pos);"

TRUE  = 1
FALSE = 0

LH5 = " -2"
LH4 = " -1"
LH1 = " -0"

recoursiv = " -r"
emptydirs = " -e"
paths     = " -x"
touch     = " -E"
newfiles  = " -G"

archiv   = "a"
freshen  = "f"
extract  = "e"
fullpath = "x"

archivswitches = recoursiv || emptydirs || LH5
archivcommand = archiv
extractswitches = ""
extractcommand = fullpath

multi   = 1
arcdir  = "SYS:"
extdir  = "RAM:"
nofile  = FALSE
filename = ""

archive  = 10
extract  = 20
contents = 30
options  = 40

scr          = LOCKPUBSCREEN("Workbench")
scrvinfo     = GETVISUALINFO(scr)
scrfont      = GETVALUE(scr,40,4,'p')
glistpointer = ALLOCVEC(4,MEMF_CLEAR)
conxgad      = CREATECONTEXT(glistpointer)
previousgadget = conxgad

makegadget = "call SETNEWGADGET(newgadx,scrvinfo,scrfont,lft,top,wdt,hgt,
                                gadname,PLACETEXT_IN,gadnr,null());
              previousgadget = CREATEGADGET(BUTTON_KIND,previousgadget,
                                            newgadx,TAG_DONE,0)"

gadname = "Archive"
gadnr   =  archive
lft     = 3
top     = 3
wdt     = 92
hgt     = 12
newgadx = MAKENEWGADGET(scrvinfo,scrfont,lft,top,wdt,hgt,gadname,
                        ,PLACETEXT_IN,gadnr,null())
previousgadget = CREATEGADGET(BUTTON_KIND,previousgadget,newgadx,TAG_DONE,0)

gadname = "Extract"
gadnr   =  extract
lft     =  97
interpret makegadget

gadname = "Contents"
gadnr   =  contents
lft     = 3
top     = 18
interpret makegadget

gadname = "Options"
gadnr   =  options
lft     =  97
interpret makegadget


wintitle = "LHA Control"
winidcmp = CLOSEWINDOW+GADGETUP+GADGETDOWN+MOUSEMOVE
winflags = WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH+GIMMEZEROZERO+ACTIVATE

w1  = OPENWINDOW(portname,0,211,200,45,2,4,winidcmp,winflags,wintitle,
                 ,scr,0,null(),null(),conxgad)
call GT_REFRESHWINDOW(w1,null())
rpw1 = GETWINDOWRASTPORT(w1)


exitme = 0
do forever
   x = waitpkt(portname)
   do forever
      msg = getpkt(portname)
      if msg = '0000 0000'x then leave
      msgclass  = getarg(msg,0)
      optmsg = delstr(msgclass,11)
      if optmsg = "LHAOptions" then
         do
           optmsg = delstr(msgclass,1,11)
           if optmsg ~= "END"
              then do
                   opts = optmsg
                   trenn = index(opts,'|')
                   extractswitches = delstr(opts,trenn)
                   opts = delstr(opts,1,trenn)
                   trenn = index(opts,'|')
                   extractcommand = delstr(opts,trenn)
                   opts = delstr(opts,1,trenn)
                   trenn = index(opts,'|')
                   archivswitches = delstr(opts,trenn)
                   opts = delstr(opts,1,trenn)
                   archivcommand = opts
                   end
         end
      if msgclass = DIE then exitme = 1
      if msgclass = CLOSEWINDOW then exitme = 1
      if msgclass = GADGETUP then
         do
           gadid  = getarg(msg,9)
           select
                 when gadid = archive then
                      do
                      multi = 1
                      save = FALSE
                      nofile = FALSE
                      file = "#?";dir = arcdir
                      hail = "Select Dir or File(s) to archive"
                      interpret request
                      if filename ~= null()
                         then do
                              arcdir = dir
                              filespec = '22'x || filename || '22'x
                              dirinfo = (delstr(dir,length(dir))) || '.info'
                          if exists(dirinfo)
                             then filespec = filespec || '2022'x || dirinfo || '22'x
                              multi = 0
                              save = TRUE
                              nofile = FALSE
                              file = "";dir = "RAM:"
                              hail = "Select Archive-Dir and Filename"
                              interpret request
                              if file ~= "" & filename ~= null()
                                 then do
                                      call archive()
                                      end
                                 else do
                                      call noarchive()
                                      end
                              end
                      end
                 when gadid = extract then
                      do
                        multi = 0
                        save = FALSE
                        nofile = FALSE
                        file = ""; dir = extdir
                        hail = "Select Archive to extract"
                        interpret request
                        if filename ~= null() & file ~= ""
                           then do
                                archivname = filename;archivout = compress(archivname)
                                extdir = dir
                                x = easyrequest(w1,"LHA Control Request",
                                               ,'Extract to 'extdir'?',
                                               ,'Yes|Filerequester|Cancel')
                                if x = 2
                                   then do
                                        multi = 0
                                        save = TRUE
                                        nofile = FALSE
                                        file = ""; dir = extdir
                                        hail = "Select Dir to extract Archive"
                                        interpret request
                                        if filename ~= null()
                                           then do
                                                call extract()
                                                end
                                        end
                                 if x = 1 then call extractcurrent()
                                end
                      end
                 when gadid = contents then
                      do
                        multi = 0
                        save = FALSE
                        nofile = FALSE
                        file = ""; dir = "RAM:"
                        hail = "Select Archive to list contents"
                        interpret request
                        if filename ~= null() & file ~= ""
                           then do
                                call contents()
                                end
                      end
                 when gadid = options then
                      do
                        portlist = Showlist('p')
                        if find(portlist,"LHAOptions") = 0
                           then do
                                opts = extractswitches '|' extractcommand '|' archivswitches '|' archivcommand
                                call setclip('LHAopts',opts)
                                ADDRESS COMMAND 'run >NIL: rx LHAOptions'
                                end
                      end
                 otherwise nop
           end
         end
      x = reply(msg,0)
   end
   if exitme = 1 then leave
end

call CLOSEWINDOW(w1)
call UNLOCKPUBSCREEN(null(),scr)
call FREEVISUALINFO(scrvinfo)
call FREEGADGETS(conxgad)
call FREETHIS(newgadx)
call FREEVEC(glistpointer)

portlist = Showlist('p')
if find(portlist,"LHAOptions") > 0 then ADDRESS 'LHAOptions' DIE

exit





/*-------------------------------------------------------------------------*/
/*                    Unterprogramm archive                                */
/*-------------------------------------------------------------------------*/

archive:
   archivname = filename
    archivout = compress(archivname)
            x = 'run lha >CON:0/9/600/101/ARCHIVE...'archivout'.lha(lzh)/AUTO/WAIT '
  lha_archive = x || archivswitches archivcommand '22'x || archivname || '22'x filespec
  address command lha_archive
return




/*-------------------------------------------------------------------------*/
/*                    Unterprogramm noarchive                              */
/*-------------------------------------------------------------------------*/

noarchive:
  x = easyrequest(w1,"LHA Control Request",'No Archive-name given, operation canceled','OK')
return




/*-------------------------------------------------------------------------*/
/*                    Unterprogramm extract                                */
/*-------------------------------------------------------------------------*/

extract:
       extdir = dir
    extdirout = compress(extdir)
            x = 'run lha >CON:0/110/600/101/EXTRACT...'archivout'>>>'extdirout'/AUTO/WAIT '
  lha_extract = x || extractswitches extractcommand '22'x || archivname || '222022'x || extdir || '22'x
  address command lha_extract
return




/*-------------------------------------------------------------------------*/
/*                    Unterprogramm extractcurrent                         */
/*-------------------------------------------------------------------------*/

extractcurrent:
            x = 'run lha >CON:0/110/600/101/EXTRACT...'archivout'/AUTO/WAIT '
    lha_excur = x || extractswitches extractcommand '22'x || archivname || '222022'x || extdir || '22'x
    address command lha_excur
return



/*-------------------------------------------------------------------------*/
/*                    Unterprogramm contents                               */
/*-------------------------------------------------------------------------*/

contents:
    archivout = compress(filename)
            x = 'run lha >CON:0/9/600/202/ARCHIVECONTENTS...'archivout'/AUTO/WAIT v '
    lha_conts = x || '22'x || filename || '22'x
    address command lha_conts
return