#!/bin/make -f

CC?=cc
CFLAGS+=-Wall -Wmissing-declarations -Wstrict-prototypes -I$(SLANG_INCDIR) $(KANJI)
SOFLAGS:=-shared -fPIC -Wl,-export-dynamic
LDFLAGS:=-s
LIBS:=-lm -ltermcap -L$(SLANG_LIBDIR) -lslang

pwd:=$(shell pwd)

# You might want to change these install-dirs:
PREFIX:=/usr/local
bin_dir:=$(PREFIX)/bin
lib_dir:=$(PREFIX)/lib

# Uncomment one of the next 2, depending whether you plan to
# make install, or just run slirc from it's build directory:
CFG_DIR:=$(lib_dir)/slirc
#CFG_DIR:=$(pwd)

script_dir:=$(CFG_DIR)/scripts
script_path:=$(script_dir)

# -- YOU SHOULDN'T NEED TO EDIT BELOW HERE --
VSDIR:=$(notdir $(pwd))
VSNUM:=$(VSDIR:slirc-%=%)
CFLAGS1:=-DVERSION=\"$(VSNUM)\" -DCFG_DIR=\"$(CFG_DIR)\" -DSCRIPTPATH=\"$(script_path)\"
TESTBINS:=vf
MODULES:=vfile-module.so utils-module.so# cheats-module.so
OBJS2:=objs/lru.o objs/slload.o objs/windoze.o

vpath %.o objs
vpath % tests

all: slirc $(MODULES)
	@./check_slang

tests: $(TESTBINS) $(MODULES)

install: slirc $(MODULES)
ifeq ($(CFG_DIR),$(pwd))
	@echo "Running from local dir, no global install"
else
	[ -d $(bin_dir) ] || mkdir -p $(bin_dir)
	[ -d $(lib_dir) ] || mkdir -p $(lib_dir)
	[ -d $(CFG_DIR) ] || mkdir -p $(CFG_DIR)
	[ -d $(script_dir) ] || mkdir -p $(script_dir)
	install -p -o root -g wheel -m755 slirc $(bin_dir)
	install -p -o root -g wheel -m755 $(MODULES) $(CFG_DIR)
	install -p -o root -g wheel -m644 scripts/*.sl $(script_dir)
endif

slirc: slirc.o lru.o slload.o windoze.o
	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

tests/vf: vf.c
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS)

objs/slbot.o: slirc.c slirc.h Makefile
	$(CC) $(CFLAGS) $(CFLAGS1) -DSLBOT -o $@ -c $<

objs/slirc.o: slirc.c slirc.h Makefile
	$(CC) $(CFLAGS) $(CFLAGS1) -o $@ -c $<

$(OBJS2): objs/%.o: %.c slirc.h
	$(CC) $(CFLAGS) -o $@ -c $<

$(MODULES): %.so: %.c
	$(CC) $(CFLAGS) $(SOFLAGS) -o $@ $< $(LIBS)
	cd tests; ln -sf ../$@ $@

dist-core:
	cd ..; sed -e "s/^/$(VSDIR)\//g" $(VSDIR)/Core| \
	cpio -o -Hustar|gzip -9 -c >$(VSDIR).tgz

clean:
	rm -f slirc objs/*.o $(MODULES)
	(cd tests && rm -f $(TESTBINS) $(MODULES))


syntax highlighted by Code2HTML, v. 0.9.1