# config file used by 'run' and 'shell'
CONFIG ?= config/sys.config
# arguments passed to eunit, used by 'check'
EUNIT_ARGS ?=
# directory paths for 'install'
BINDIR ?= /usr/bin
CONFDIR ?= /etc/osmocom
SYSTEMDUNITDIR ?= /lib/systemd/system

all: build

GEN_FILES = include/S1AP-*.hrl src/S1AP-*.erl

src/S1AP-%.erl include/S1AP-%.hrl: asn1/S1AP-%.asn
	rebar3 asn compile

build: $(GEN_FILES)
	rebar3 compile
	rebar3 escriptize

run: build
	ERL_FLAGS='-config $(CONFIG)' _build/default/bin/osmo-s1gw

shell: build
	rebar3 shell --config $(CONFIG)

check: $(GEN_FILES)
	rebar3 eunit $(EUNIT_ARGS)

analyze: $(GEN_FILES)
	rebar3 dialyzer

install: build
	install -Dm0755 _build/default/bin/osmo-s1gw \
		$(DESTDIR)$(BINDIR)/osmo-s1gw
	install -Dm0644 config/sys.config \
		$(DESTDIR)$(CONFDIR)/osmo-s1gw.config
	install -Dm0644 contrib/systemd/osmo-s1gw.service \
		$(DESTDIR)$(SYSTEMDUNITDIR)/osmo-s1gw.service

clean:
# Avoid running rebar3 clean if _build doesn't exist, since it would try
# to fetch deps from the Internet and that may not be avaialble when in
# OBS build.
ifneq ("$(wildcard _build/.*)", "")
	rebar3 asn clean
	rebar3 clean
	rm -rf _build/
else
	rm -f $(GEN_FILES)
endif