#! /usr/bin/env orca

ORCA [
	Title:		"Consonant build system"
	Version:	0.2.0
	Author:		"Kaj de Vos"
	Rights: {
		Copyright (c) 2006-2008,2010 Kaj de Vos
		License: GPL 3 or any later version
	}
	Purpose:	"Builds the Syllable operating systems, their native applications and their ports."
	Example:	"construct help"

	Tabs:		4
]

recycle/off  ; FIXME: ORCA bug


log: context [  ; Singleton logger object
	open: func [] [
	]
	close: does [
	]

	header: func [header] [
		prin newline
		print header
	]
	detail: func [detail] [
;		print detail
	]
	action: func [action] [
		prin newline
		print action
		prin newline
	]
	output: func [output] [
		print output
	]
	error: func [error] [
		prin newline
		print ["Error:" error]
	]
	warning: func [warning] [
		prin newline
		print ["Warning:" warning]
	]
	failure: func [module] [
		prin newline
		print ["Package" module "failed!"]
	]
]

action: func ["Returns the output of a shell command, or false if unsuccessful."
	command "Shell command"
	/local status stdout
][
	log/action command

	either 0 = status: call/wait/output command stdout: copy "" [
;		FIXME?: ORCA call/output bug (on Syllable?):
		log/output stdout
		stdout
	][
		log/error status
		log/output stdout
		no
	]
]


header: system/script/header

either any [
	not args: system/script/args
	find ["help" "-h" "--help" "-help" "-?" "?"] command: args/1
][
	print "Usage: construct <command> [<parameters>]"
	print "<command>:"
	print "    help, -h, --help, -help, -?, (?)"
	print "        Show this help information"
	print "    version"
	print "        Show version information"
	print "    distro <system> <version> <architecture>"
	print "        Package up a system distribution"
;	print "    iso [<file base name> [<label> [<image directory>]]]"
;	print "        Build an ISO CD image file for a system distribution"
	print "Example:"
	print ["   " select header 'example]
][
	either command = "version" [
		print [select header 'title  select header 'version]
		print select header 'rights
	][
		log/open

		switch/default command [
			"distro" [
				call/wait/output "uname" result: copy ""
				Linux?: result = "Linux^/"

				unless all [
					name: rejoin [args/2 "-" args/3]

					any [change-dir %stage/image (
						log/error "Staging area not found."
						quit/return 11
					)]

					any [not exists? %system/development/
						all [
							log/header "Packaging development files"

							change-dir %system

							; Pack but don't compress
							action rejoin [
								"zip -rqy0 ../../../"
								file: rejoin [name "-development." args/4 ".zip"]
								" development"
							]
							action "sync"

							change-dir %../../..

							; Compress
							action join "xz -e9 " file
							action "sync"

							change-dir %stage/image

							log/header "Removing development files"
							action "rm --recursive system/development/"
						]
					]

					log/header "Packaging system"

					; Pack
					action join either Linux?
							; Zip doesn't restore character devices:
							[join "tar -cf ../../" file: rejoin [name "." args/4 ".tar"]]
							; TAr doesn't store extended attributes:
							[join "zip -rqy0 ../../" file: rejoin [name "." args/4 ".zip"]]
;						Wildcard doesn't work:
						" ."

					; Compress
					any [not Linux?
						all [
							change-dir %../..
							action join "xz -e9 " file
						]
					]
				][
					log/error "Packaging not completed."
					quit/return 10
				]
			]

			"iso" [
				log/header "Building ISO image"

;				FIXME: ORCA CALL bug: command too long:
				unless action rejoin ["mkisofs"
					" -iso-level 3 --allow-leading-dots -R -V '" args/3 "'"
					" -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table"
					" -o " args/2 ".iso"
					" " any [args/4 %stage/image]
				][
					log/error "ISO image not created."
					quit/return 20
				]
			]
		][
			log/error "Command unknown."
			quit/return 1
		]
		action "sync"
	]
]
