/* REXX */

/*
-----------------------------------------------------------------
  unpacker (v0.1) = public domain : free for any use

  AUTHOR: rugxulo _AT_ gmail

  TESTED: Regina 3.7, BRexx 2.1.9, r4 4.00, ooREXX 4.1.3

  BUG:    Can't use literal '*'+'/' pair in embedded data files.
-----------------------------------------------------------------
*/

/* --- UNPACKER BEGINS --- */

if arg() \= 0 then parse arg onlyfile . ; else onlyfile=''
parse source . . srcfile . ; lineno=0 ; writeln=0

bar = '===' ; prefix='/*' bar ; postfix = bar '*/'
headpost=' begins' postfix ; footpost=' ends' postfix
headlen=length(headpost) ; footlen=length(footpost)

if lines(srcfile)=1 then do while lines(srcfile) \= 0
  call grab
end
else do lines(srcfile)
  call grab
end

exit

grab:
  line=linein(srcfile) ; lineno=lineno+1
  if pos(headpost,line) \= 0 then do
    parse var line ' ' (bar) ' ' outfile (headpost) .
    if onlyfile='' then say outfile
    writeln=1
  end
  else if pos(footpost,line) \= 0 then writeln=0
  if pos(headpost,line)=0 & pos(footpost,line)=0 & writeln then ,
    if onlyfile='' | onlyfile=outfile then ,
      call lineout outfile, line
return

/* --- UNPACKER ENDS --- */

/*
------------------------------------------------------------
*** DATA BEGINS DATA BEGINS DATA BEGINS DATA BEGINS ***

/* these data files = public domain : free for any use */
------------------------------------------------------------

/* === Myfiles.ob2 begins === */
(* public domain, nenies proprajho, free for any use *)
MODULE Myfiles; (* XDS *)
IMPORT StreamFile,IOResult,TextIO,O2Strings,Out;

TYPE File* = POINTER TO RECORD handle:StreamFile.ChanId END;

PROCEDURE Close*(VAR f:File);
BEGIN StreamFile.Close(f.handle)
END Close;

PROCEDURE WriteLn*(f:File);
BEGIN TextIO.WriteLn(f.handle)
END WriteLn;

PROCEDURE WriteString*(f:File; s:ARRAY OF CHAR);
BEGIN TextIO.WriteString(f.handle,s)
END WriteString;

PROCEDURE Open*(name,mode:ARRAY OF CHAR):File;
VAR res:StreamFile.OpenResults; f:File;
BEGIN NEW(f);
  IF mode[0]="r" THEN
    StreamFile.Open(f.handle,name,StreamFile.read,res);
    IF res=StreamFile.opened THEN RETURN f END
  ELSIF mode[0]="w" THEN
    StreamFile.Open(f.handle,name,StreamFile.write,res);
    IF res=StreamFile.fileExists THEN
      Out.String("FAILURE: file exists"); Out.Ln; HALT(254)
    ELSIF res=StreamFile.opened THEN RETURN f
    END
  END;
  RETURN NIL
END Open;

PROCEDURE Eof*(f:File):BOOLEAN;
BEGIN
RETURN IOResult.ReadResult(f.handle) = IOResult.endOfInput
END Eof;

PROCEDURE ReadChar*(f:File; VAR k:CHAR);
BEGIN TextIO.ReadChar(f.handle,k)
END ReadChar;

PROCEDURE readline*(VAR f:File; VAR s:ARRAY OF CHAR):INTEGER;
BEGIN TextIO.ReadRestLine(f.handle,s); TextIO.SkipLine(f.handle);
  IF IOResult.ReadResult(f.handle) # IOResult.endOfInput THEN
    RETURN O2Strings.Length(s)
  ELSE
    RETURN 0
  END
END readline;

END Myfiles.
/* === Myfiles.ob2 ends === */

/* === Myfiles.m begins === */
(* public domain, nenies proprajho, free for any use *)
MODULE Myfiles; (* Oxford *)
IMPORT F := Files;

TYPE File* = F.File;

PROCEDURE Close*(VAR f:File);
BEGIN F.Close(f)
END Close;

PROCEDURE WriteLn*(f:File);
BEGIN F.WriteLn(f)
END WriteLn;

PROCEDURE WriteString*(f:File; s:ARRAY OF CHAR);
BEGIN F.WriteString(f,s)
END WriteString;

PROCEDURE Open*(name,mode:ARRAY OF CHAR):File;
BEGIN RETURN F.Open(name,mode)
END Open;

PROCEDURE Eof*(f:File):BOOLEAN;
BEGIN RETURN F.Eof(f)
END Eof;

PROCEDURE ReadChar*(f:File; VAR k:CHAR);
BEGIN F.ReadChar(f,k)
END ReadChar;

PROCEDURE readline*(VAR myfile:F.File; VAR buf:ARRAY OF CHAR):INTEGER;
CONST EOL=0AX; Tab=9X;
VAR k:CHAR; i:INTEGER;
BEGIN i := 0;
  REPEAT
    F.ReadChar(myfile,k);
    IF (k > EOL) OR (k = Tab) THEN
      buf[i] := k;
      INC(i)
    END
  UNTIL k = EOL;
  buf[i] := 0X;
RETURN i+1
END readline;

END Myfiles.
/* === Myfiles.m ends === */


# --- extract.awk begins ---
#!/usr/bin/awk -f

/[b]egins ===/{
  fname=$3 ; print fname
  while (getline > 0) {
    if ($0 !~ / [e]nds ===/) {
      print > fname
    }
    else {
      close(fname)
      break
    }
  }
}
# --- extract.awk ends ---

------------------------------------------------------------
*** DATA ENDS DATA ENDS DATA ENDS DATA ENDS ***
------------------------------------------------------------
*/

/* EOF */
