
import "var/strings", "var/booleans", "command", "autoclean"

@ruby = Env::RUBY_EXE
@sh_echo = "#{sys.sp Env::RUBY_EXE} -e \"puts ARGV.join(' ')\""
@sh_puts = "#{sys.sp Env::RUBY_EXE} -e \"puts ARGV\""
@sh_cat = "#{sys.sp Env::RUBY_EXE} -e \"print ARGF.read\""

desc "Build a.t"
gen Command, "a.t" => ["b.t", "c.t"] do |t|
    "#@sh_echo #{sys.sp t.prerequisites} > #{t.name}"
end
gen Command, "f_a.t" do |t|
    sys "#@sh_echo #{sys.escape "I will fail."} > #{t.name}"
end

gen Command, "a2.t", ["b.t", "c.t"],
    "$[sh_puts] $(<) > $(>)"

var :btxt => "b"
var :be, :Bool

gen Command, "b.t", "$[sh_echo] $(btxt) > $(>)"

if var[:be]
    enhance "b.t" => "d.t" do |t|
        import "sys/more"
        sys.write_to_file(t.name,
            File.read(t.name) + File.read(t.source))
    end
end

gen Command, "c.t", sys["d???.t"], "$[sh_echo] $(<) > $(>)"

gen Command, "with space/a.t",
    ["b.t", "with space/b.t"],
    "$[sh_puts] $(<) > $(>)"

var :fargs => "/I$(p_ath1)"
var :p_ath1 => "a bc"
gen Command, "f.t", "$[sh_echo] $[fargs] > $[>]"

var :eargs => "/I$(epath)"
var :epath => "a b/c/"
gen Command, "e.t", "$[sh_echo] $[eargs] > ${>}"

var :gargs => "/I${gpath}"
var :gpath => "a b/c/"
gen Command, "g.t", "$[sh_echo] $[gargs] > $(>)"

var :h1 => 1
var :h2 => 2
gen Command, "h.t", <<end
$[sh_echo] ${h1} > $(>)1
$[sh_echo] ${h2} > $(>)2
$[sh_cat] $(>)1 $(>)2 > $(>)
end

var :rargs => "$(prerequisites) $(source) > $(name)"
var :rcmd => "$[sh_echo] " + var[:rargs]
gen Rule, :out => [:in1, :in2] do |name, sources|
    gen Command, name, sources, var[:rcmd]
end

gen Directory, "a.in1"

var :rc_dep => "puts 'a'"
gen Command, "dep1.t", "$(ruby) -e \"$[rc_dep]\" > $(>)"

gen Command, "t1.t", "dep1.t", "$[sh_echo] making t1 > $(>)"
gen Command, "t2.t", "dep1.t", "$[sh_echo] making t2 > $(>)"

gen Command, "sub1.t/a", "#@sh_echo ${>} > $(>)"
gen Command, "sub2.t/a", "$[sh_echo] ${>} > $(>)"
gen Directory, "sub2.t"

task :sub3 do
    puts "task sub3"
end
gen Command, "sub3/a", "$[sh_echo] ${>} > $(>)"

gen Command, "x.t", '[#$[sh_puts] ${a}#] ${b} > $(>)'

gen Command, "delay.t", "$[sh_echo] ${foo} > $(>)"

gen Command, "p1.t", '$[sh_puts] ${p1} > $(>)'
gen Command, "p2.t", '$[sh_puts] ${p2} > $(>)'
gen Command, "p3.t", '$[sh_puts] ${p2} > $(>)'

var :foo => "foo value"
task :change_foo do
    var[:foo] = "changed"
end

var[:p1] = lambda { |n| "#{n.full_name} $[foo]" }
var[:p2] = lambda { var[:foo] << "." }

if var[:inc_foo]
    var[:p2].call
end

@h = {:a => "b"}
gen Command, "hash.t", "$[sh_puts] ${h} > $(>)"

gen AutoClean
