/* C2Lharc.rexx */

/*
     Format

       C2Lharc files ... [DELETE] [TO <dir>]

   Convert all ARC, ZOO, PAK file into LHARC format. DELETE deletes
   original file. TO <dir> specifies where to place .LZH file. If
   not present, sent to same as original.

*/

signal on failure; signal off error; signal on syntax; signal on break_c
options failat 10
call addlib 'rexxextra.library',-20,-30,0

facility = 'C2Lharc'
retcode = 0
dtemplate = 'FILES/...,TO/K,DELETE/S'
template  = 'DELETE/S,TO/K,FILES/L'
args. = ''

parse arg g_c
do while g_c='?'
  options prompt dtemplate': '  /* this template is      */
  parse pull g_c		/* displayed to the user */
  if g_c='?' then do
    g_s=sourceline(3)
    if pos('/*',g_s)=0 then break; if pos('*/',g_s)>0 then break
    say
    g_s=sourceline(4)
    do i=5 while pos('*/',g_s)=0; say g_s; g_s=sourceline(i); end
    say
    end
  end
interpret Cparse(g_c,template,'args')
if args.ERRCODE > 1 then do; say facility'-E-BADARGS,' args.ERRTEXT; exit 5; end

tdevice = 'USER:'
tdir = facility||pragma('ID')||'Temp'
slog = facility||pragma('ID')||'Sorc:'
dlog = facility||pragma('ID')||'Dest:'
wlog = facility||pragma('ID')||'Work:'
tlog = tdir':'
lharcmd = 'c:lharc -a -r -P-1 m'
mayoverflow = ~args.DELETE & args.TO == ''

'assign' slog '""'
'assign' dlog '"'args.TO'"'
'assign' wlog tdevice
if ~exists(tdevice||tdir) then 'makedir' tdevice||tdir
'assign 'tlog  tdevice||tdir
'pushcd 'tlog
'delete * all'

if mayoverflow then do
  say facility'-W-OVRFLW, Source disk may overflow'
  end

do i = 1 to args.FILES.0
  do j = 1 to filelist(slog||args.FILES.i,'LIST','F')
    sfile = list.j
    retcode = UnDo(sfile)
    if retcode > 0 then do
      say facility'-E-XTRACT, Error extracting' sfile'. Aborting this file'
      end
    else do
      dfile = Fparse(pragma('D'),wlog||'.LZH',sfile)
      'Delete' tlog'#? all quiet'
      ''lharcmd dfile tlog'#?'
      retcode = rc
      if retcode > 0 then do
	say facility'-E-CREATE, Error creating' dfile'. Aborting this file'
	end
      else do
	if args.DELETE then 'Delete' sfile
	'Move' dfile dlog
	end
      end
    end
  end

GetOut:
  'popcd'
  'Assign' wlog
  'Assign' slog
  'Assign' dlog
  'Assign' tlog
  'Delete' tdevice||tdir 'all quiet'
  exit retcode

UnDo: procedure
  parse arg file
  retcode = 0
  cmd. = 'BAD'
  cmd.ARC = 'c:arc x'
  cmd.ZOO = 'c:zoo xO//'
  cmd.PAK = ''
  cmd.LZH = 'c:lharc -a -r -P-1 x'
  cmd.ZIP = 'c:UnZip'
  ftype = upper(fparse(pragma('D'),file,,,'T'))
  doit = cmd.ftype
  if doit ~= 'BAD' then do
    say doit file
    ''doit file
    retcode = rc
    end
   else retcode = 5
  return retcode

break_c:
break_d:
break_e:
break_f:
  say facility'-E-BREAK, Control-C interrupt'
  exit 20
failure:
  say facility'-E-FAIL, Line:' sigl', Error:' rc; retcode = rc; signal GetOut
syntax:
  say facility'-E-SYNTAX, Line:' sigl', Error:' rc; retcode = rc; signal GetOut
error:
  say facility'-E-ERROR, Line:' sigl', Error:' rc; retcode = rc; signal GetOut

