# NOTE: The examples need an installed HsShellScript in order to
# build. You can't compile them from the source distribution without
# installing first.

# Place where the "install" target puts the compiled scripts. Adapt as needed.
TARGET		= ~/bin

CFLAGS 	        = -hidir build -odir build -ibuild -package hsshellscript
LIBS		= -package hsshellscript
HC	        = ghc

MAIN 		= $(notdir $(basename $(wildcard src/[abcdefghijklmnopqrstuvwxyz]*.hs)))
NONMAIN		= $(notdir $(basename $(wildcard src/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*.hs)))
MAIN_O	  	= $(foreach f, $(MAIN), build/$(f).o)
NONMAIN_O	= $(foreach f, $(NONMAIN), build/$(f).o)
BIN   		= $(foreach f, $(MAIN), build/$(f))


0 :: bin

bin :: $(BIN)

clean :
	rm -f build/* src/*~ Makefile~
	make depend

install :: $(BIN)
	cp -fv $^ $(TARGET)

build/% : build/%.o $(NONMAIN_O)
	$(HC) -o $@ $^ -odir build $(LIBS)
	strip "$@"
#	upx -q "$@"

build/%.hi : build/%.o
	@:

# The "-ohi" is needed only for make. It makes the .hi file name match the
# .hs file name even for Main modules. For the Main module, the .hi file name
# doesn't need to match the module name, because it isn't imported. For
# other modules, module name and file name match. Thus, every *.hs has a
# *.hi with the same name.
build/%.o : src/%.hs
	$(HC) $(CFLAGS) -ohi build/$*.hi -c -o $@ $<


depend ::
	rm -f build/depend
	for dn in $(MAIN) $(NONMAIN) ; do touch build/$$dn.hi ; done
	for dn in $(MAIN) $(NONMAIN) ; do \
		ghc -M src/"$$dn".hs $(CFLAGS) -optdep-f -optdep build/depend.tmp ;\
		sed "s|^build/Main.o : |build/$$dn.o : |" < build/depend.tmp > build/depend.tmp.1 ;\
		cat build/depend.tmp.1 >> build/depend ;\
		sed "s/.o : /.hi : /" < build/depend.tmp.1 >> build/depend ;\
		rm build/depend.tmp build/depend.tmp.1 ;\
	done

-include build/depend
-include build/depend1
