#! /usr/bin/env tclsh

set sourceDirectory [file dirname [file normalize [info script]]]

lappend auto_path [file join $sourceDirectory lib]

set template [file join $sourceDirectory xvfs.c.rvt]

package require minirivet

set mode "run"
if {[lindex $argv 0] == "--dump-tcl"} {
	set mode "dump-tcl"
}

switch -- $mode {
	"run" {
		::minirivet::parse $template
	}
	"dump-tcl" {
		set xvfs_tcl [file join $sourceDirectory lib xvfs xvfs.tcl]
		set xvfs_core_h [file join $sourceDirectory xvfs-core.h]
		set xvfs_core_c [file join $sourceDirectory xvfs-core.c]

		set cleanup {
			"#include <xvfs-core.h>" ""
			"#include <xvfs-core.c>" ""
		}

		set core_header_data ""
		append core_header_data [string map $cleanup [read [open $xvfs_core_h]]] "\n"
		append core_header_data [string map $cleanup [read [open $xvfs_core_c]]] "\n"

		puts "#! /usr/bin/env tclsh"
		puts ""
		puts [read [open $xvfs_tcl]]
		puts ""
		puts [list puts -nonewline $core_header_data]
		puts ""
		puts [string map $cleanup [::minirivet::parseStringToCode [read [open $template]]]]
	}
	default {
		puts stderr "error: Invalid mode: $mode"
		exit 1
	}
}
