PKG_NAME=opkg
PKG_VERSION=0.1.8
PKG_URL=opkg.googlecode.com/files

PKG_EXT=tar.gz
PKG_UNPACK=tar -xzf
PKG_CONFIG_OPTS=--prefix=$(TOOL_PATH) \
	--disable-curl \
	--disable-gpg \
	--with-opkgetcdir=etc \
	--with-opkglockfile=var/opkg.lock

PKG_DEPS=

all: .built_

.built_: .prepared_
	$(MAKE) -C ${PKG_NAME}-${PKG_VERSION}
	touch $@

.prepared_: .configured_
	touch $@

prepare: .prepared_

configure: .configured_

.configured_: .deps_checked_ ${PKG_NAME}-${PKG_VERSION}.${PKG_EXT}
	${PKG_UNPACK} ${PKG_NAME}-${PKG_VERSION}.${PKG_EXT}
	cd ${PKG_NAME}-${PKG_VERSION} && \
		./configure ${PKG_CONFIG_OPTS}
	touch $@

install: .installed_

.installed_: .built_
	$(MAKE) -C ${PKG_NAME}-${PKG_VERSION} install
	touch $@

deps: .deps_checked_

.deps_checked_:
	@echo "Building ${PKG_NAME} dependencies..."
	@for d in ${PKG_DEPS}; do \
		$(MAKE) -C ../$${d}; \
		$(MAKE) -C ../$${d} install; \
	done;
	@echo "done with ${PKG_NAME} dependencies"
	touch $@

dl: ${PKG_NAME}-${PKG_VERSION}.${PKG_EXT}

${PKG_NAME}-${PKG_VERSION}.${PKG_EXT}:
	wget ${PKG_URL}/${PKG_NAME}-${PKG_VERSION}.${PKG_EXT}

uninstall:
	$(MAKE) -C ${PKG_NAME}-${PKG_VERSION} uninstall

clean:
	rm -f .prepared_ .configured_ .built_ .installed_ .deps_checked_

dirclean: clean
	rm -rf ${PKG_NAME}-${PKG_VERSION}

distclean: dirclean
	rm -f ${PKG_NAME}-${PKG_VERSION}.${PKG_EXT}

.PHONY: dl clean prepare configure install deps all
