
import "md5", "autoclean"

var :content => "1\n"

task :a => "f1.t"

file "f1.t" do |t|
    write_content(t.name)
end

gen Directory, "d1.t"

gen Directory, "base.t", "s/s" => ["a.t", "f1.t"] do |t|
    fn = t.name + "/t"
    puts "copying to #{fn}"
    open fn, "w" do |f|
        f.write(File.read(t.source))
        f.write(File.read("f1.t"))
    end; sys.cd "sub1"
end

if var["subfile"]
    import "subfile"
    gen SubFile, "d2.t/f" => "f1.t" do |t|
        sys.cp t.source, t.name
    end
end

file "f2.t" => ["base.t/s/s"] do |t|
    sys.cp "#{t.source}/t", t.name
end

gen SourceNode, "c1.t"

gen SourceNode, "c2.t" => ["c1.t", "c3.t"]

file "f3.t" => "c1.t" do |t|
    var[:content] = File.read(t.source)
    write_content(t.name)
end

file "f4.t" => ["f3.t", "c2.t"] do |t|
    var[:content] = t.deps.inject("") { |c, fn| c + File.read(fn) }
    write_content(t.name)
end

gen SourceNode, "c4.t" => "c2.t"

file "f5.t" => "c2.t" do |t|
    sys.cp t.source, t.name
end

gen SourceNode, "c5.t" => ["sub1/c1.t", "c6.t"]

file "f6.t" => "c5.t" do |t|
    write_content(t.name)
end

gen Rule, ".r.t" => ".t" do |t|
    sys.cp t.source, t.name
end

gen Rule, ".r.t" => ".tt" do |t|
    sys.cp t.source, t.name
end

gen SourceNode, "c7.t" => ["f1.t", "c8.t"]

file "f7.t" => "c7.t" do |t|
    sys.cp t.source, t.name
    sys.cd "sub1"
end

desc "copy f1.t from sub1 to f8.t"
file "f8.t" => "sub1/f1.t" do |t|
    sys.cp t.source, t.name
end

subdirs "sub1"

def write_content(fn)
    puts "writing #{fn}"
    open fn, "w" do |f|
        f.write var[:content]
    end
end

gen AutoClean
