REBOL [
    Title: "Script packager"
    Purpose: {Package up one or more script (text) files into
    a REBOL script that will unpack them.}
]

;; [---------------------------------------------------------------------------]
;; [ This script encapsulates an idea seen a lot in REBOL documentation,       ]
;; [ where the documentor wants to present a script that is so large it        ]
;; [ would make the documeation hard to work with.  The plan is to compress    ]
;; [ the script and show the compressed form in the documentation, with an     ]
;; [ invitation to the reader to copy out the compressed script and            ]
;; [ decompress it on his own computer.                                        ]
;; [                                                                           ]
;; [ The program asks for one or more file names for files that are assumed    ]
;; [ to be REBOL scripts, but could be any text files.  It will compress       ]
;; [ each and generate a REBOL script which, if run, will uncompress the       ]
;; [ embedded compressed scripts and write them to disk.                       ]
;; [                                                                           ]
;; [ This is a less-sophisticated implementaton of the idess present in the    ]
;; [ script build-pack.r on the rebol.org web site.                            ]
;; [---------------------------------------------------------------------------]

PACKAGE-STRING: ""

PACKAGE-HEADER: {REBOL [
    Title: "Script unpacker"
]
}

PACKAGE-FOOTER: {
alert "Done."
}

SCRIPT-HEADER-MODEL: {
write %%OUTPUT-NAME%% decompress 
%%SCRIPT-STRING%%
}
SCRIPT-HEADER: ""

if not FILE-LIST: request-file [
    alert "No files selected"
    quit
]

append PACKAGE-STRING PACKAGE-HEADER

system/options/binary-base: 64
foreach NAME FILE-LIST [
    SCRIPT-HEADER: copy SCRIPT-HEADER-MODEL
    replace SCRIPT-HEADER "%%OUTPUT-NAME%%" mold second split-path NAME
    replace SCRIPT-HEADER "%%SCRIPT-STRING%%" mold compress read NAME
    append PACKAGE-STRING SCRIPT-HEADER
]

append PACKAGE-STRING PACKAGE-FOOTER

write %package.r PACKAGE-STRING

alert "Done."