#!/usr/local/bin/konoha
using posix.*;
import "walk.k"

void usage(String filename) {
    OUT << format("""
----------------------------------------------
this is generator for package makefile.
usage:
    %s{0} pkgname pathToPackages (CFLAGS ans LDFLAGS ...)

example:
    math
    %s{0} math ../../package/math -lm

    curl
    %s{0} curl -lcurl 
----------------------------------------------
""",filename);
}
void Array.dump() {
    foreach (String s in this) {
        OUT << s << EOL;
    }
}

String Array.toStr() {
    String ret = "";
    foreach (String s in this) {
        ret += s + " ";
    }
    return ret;
}


void main (String[] args)
{
    if (|args| < 3) {
        usage(args[0]);
        os.exit(1);
    }
    pkgname = args[1];
    path = args[2];
    ldflags = new Array();
    cflags  = new Array();
    oss     = new Array();

    for (i = 0; i < |args|; i++) {
        if (args[i].startsWith("os=") == true) {
            oss = args[i][3..].split(",");
        }
        if (args[i].startsWith("-l") == true) {
            ldflags.add(args[i]);
        }
        if (args[i].startsWith("-L") == true) {
            ldflags.add(args[i]);
        }
        if (args[i].startsWith("-I") == true) {
            cflags.add(args[i]);
        }
        if (args[i].startsWith("-O") == true) {
            cflags.add(args[i]);
        }
        if (args[i].startsWith("-D") == true) {
            cflags.add(args[i]);
        }
        if (args[i].startsWith("-W") == true) {
            cflags.add(args[i]);
        }
    }

    OUT << format("generate %s{0} package makefile",pkgname) << EOL;
    OUT << "chdir path=" + path << EOL;
    System.chDir(path);

    OUT << "searching source file" << EOL;
    String[] array = System.walk(".");
    objs = new Array();
    srcs = new Array();

    foreach (String line in array) {
        if (line.endsWith(".c") == true) {
            OUT << line << EOL;
            obj = line.split("/",0).pop().replace(".c",".o");
            objs.add(obj);
            srcs.add(line);
        }
    }


    foreach (String os in oss) {
        fname = os + ".mk";
        OUT << format("generating %s{0}'s makefile : %s{1}",os,fname) << EOL;
        OutputStream o = new OutputStream(fname,"w");
        o << format("""
# 
# Automagically generated file.
# generated by gensrc/gen_pkgmake.k
# 

CC = gcc
CFLAGS = -O2 -Wall -fmessage-length=0 -fPIC %s{0}
LDLIBS = -lkonoha %s{1}
target = %s{2}
pkgname = %s{3}

library = "$(pkgname)_$(target).dylib"

""", cflags.toStr(), ldflags.toStr(),os,pkgname);

        o.println("objs = " + objs.toStr());
        o.println("");
        o << """

.PHONY: all
all: $(library)

$(library): $(objs)
	$(CC) -dynamiclib -o $@ $^ $(LDLIBS)

""";


        for (i = 0; i < |objs|; i++) {
            o.println(format("%s{0} : %s{1}",objs[i],srcs[i]));
            o.println("	$(CC) $(CFLAGS) -o $@ -c $^");
            o.println("");
        }

        o << """
.PHONY: clean
clean:
	$(RM) -rf $(objs) $(library)
""";

        o.println("");
        o.close();

        // OUT << os + ":end" << EOL;
    }

}
