REBOL [
  Title:   "Generates run-all-together.red"
	Author:  "Peter W A Wood"
	Version: 0.1.0
	Tabs:	 4
	Rights:  "Copyright (C) 2016 Peter W A Wood. All rights reserved."
	License: "BSD-3 - https://github.com/red/red/blob/origin/BSD-3-License.txt"
]

;;--------------- initialisations 
quick-test-path: to file! clean-path %../../../quick-test/quick-test.red
make-dir/deep: %auto-tests/run-all/auto-tests

;; make the file lists from all-tests.txt
file-list-comp: copy []
file-list-interp: copy []
all-tests: read/lines %all-tests.txt
foreach test all-tests [
	append file-list-comp copy test
	unless any [
		find test "routine"
		find test "evaluation"
	][
		append file-list-interp test
	]
]

;;--------------- functions

;; move file to a new directory and strip out start and end file lines
;; returns the name of the moved file
move-file: func [
	file [string!]
	/local
		new-file
		new-src
][
	new-file: join %auto-tests/run-all/ file
	src: read/lines to file! file
	new-src: copy []
	foreach line src [
		replace line "~~~end-file~~~" ""
		unless any [
			find line "quick-test.red"
			find line "start-file~~~" 
		][
			append new-src line
		]
	]
	write/lines new-file new-src
	replace to string! new-file "auto-tests/" ""
]

;; write test file with header
write-test-header: func [file-out [file!]] [
	write file-out 
;;;;;; start text	
{
Red [
	Title:   "Red auto-generated test"
	Author:  "Peter W A Wood"
	License: "BSD-3 - https://github.com/dockimbel/Red/blob/origin/BSD-3-License.txt"
]

comment ^{
	This file is generated by make-run-all-red.r
	Do not edit this file directly.
^}

#include %../../../../quick-test/quick-test.red

}
;;;;;; end text
]

;;--------------- Main Processing

file-out: %auto-tests/run-all-together.red
write-test-header file-out
write/append file-out {~~~start-file~~~ "run-all-together"^/} 
foreach file file-list-comp [
	write/append file-out rejoin ["#include %" move-file file lf]
]
foreach file file-list-interp [
	src: read/lines to file! file
	until [										;; ignore header
		src: next src
		find first src "~~~start"
	]
	foreach line src [
		unless any [
			find line "quick-test.red"
			find line "start-file~~~"
			find line "end-file~~~" 
		][
			write/append file-out join line lf
		]
	]		
]
write/append file-out {^/~~~end-file~~~^/^/} 