## -*- makefile -*-
##

PCBLIBDIR=		@PCBLIBDIR@
LIBRARYFILENAME=	@LIBRARYFILENAME@

pcblibdir=		$(PCBLIBDIR)
pcblib_SCRIPTS=	${LIBSCRIPTS}
LIBSCRIPTS= \
	CreateLibraryContents.sh \
	CreateLibrary.sh \
	ListLibraryContents.sh \
	QueryLibrary.sh \
	qfp-ui

dist_noinst_SCRIPTS= \
	m4lib_to_newlib.sh

pcblib_DATA= ${LIBCONTENTS} ${DAT_FILES}

LIBCONTENTS= \
	${LIBRARYFILENAME} \
	${LIBRARYFILENAME}.contents


# ######################################################
#
# Process the entire m4lib library and create a newlib
# library from it.
#

NEWLIB=${LIBRARYFILENAME}-newlib

all-local: $(NEWLIB).stamp

# if we have compiled in PNG export capability then
# generate previews of all the footprints.  This has
# the added benefit of doing a PCB syntax check on all of them
if PNG_PREVIEW
PREVIEW=	--png
PREVIEW_MSG=	png-preview=yes
else
PREVIEW=	
PREVIEW_MSG=	png-preview=no
endif

if BUILD_PCBLIB_NEWLIB
NEWLIB_DEPS=	${LIBCONTENTS} ${srcdir}/m4lib_to_newlib.sh
else
NEWLIB_DEPS=
endif

${NEWLIB}.stamp : footprint.pcb ${NEWLIB_DEPS}
	rm -fr ${NEWLIB}
	${SHELL} ${srcdir}/m4lib_to_newlib.sh --contents \
		${LIBRARYFILENAME}.contents --output ${NEWLIB} \
		${PREVIEW} --pcb "${PCB}" --awk "${AWK}"
	echo '${PREVIEW_MSG}' > $@

# Remove any rebuilt footprints
if BUILD_PCBLIB_NEWLIB
distclean-local:
	rm -fr ${NEWLIB}
endif

install-data-local: ${NEWLIB}.stamp
	if test -d ${NEWLIB} ; then \
		tar cf - ${NEWLIB} | (cd $(DESTDIR)$(PCBLIBDIR) && tar -xf -) ; \
	else  \
		cd $(srcdir) && tar -cf - ${NEWLIB} | (cd $(DESTDIR)$(PCBLIBDIR) && tar -xf -) ; \
	fi

uninstall-local:
	chmod -R a+w $(DESTDIR)$(PCBLIBDIR)/${NEWLIB}
	rm -fr $(DESTDIR)$(PCBLIBDIR)/${NEWLIB}

EXTRA_DIST=	${NEWLIB} footprint.pcb ${NEWLIB}.stamp png_diff.sh README-m4libs

if BUILD_PCBLIB_NEWLIB
DISTCLEANFILES=	${NEWLIB}.stamp
else
DISTCLEANFILES=
endif

#
#
# ######################################################

pcblibm4dir=	$(PCBLIBDIR)/m4
pcblibm4_DATA=	$(LIB_FILES)

##pcblibm4_SCRIPTS=	update-pcb
## XXX what was this about?
##(cd $(pcblibm4dir) && sh ./update-pcb)

# note:  only some of the scripts listed in LIBSCRIPTS are generated by autoconf
# and hence need chmod-ing.  The others (ListLIbraryContents.sh in particular)
# is just one of the distribution files and thus will not exist in the build
# directory if the build is not done in the source directory
all-local:
	for f in ${LIBSCRIPTS} ${noinst_SCRIPTS}; do if test -f $$f ; then chmod 755 $$f ; fi ; done

COMMON_FILE= common.m4

M4_DIST_FILES= \
	TTL_74xx_DIL.m4 \
	amphenol.m4 \
	connector.m4 \
	crystal.m4 \
	generic.m4 \
	gtag.m4 \
	jerry.m4 \
	linear.m4 \
	logic.m4 \
	lsi.m4 \
	memory.m4 \
	optical.m4 \
	pci.m4 \
	resistor_0.25W.m4 \
	resistor_adjust.m4 \
	resistor_array.m4 \
	texas_inst_amplifier.m4 \
	texas_inst_voltage_reg.m4 \
	transistor.m4 

# These files are for old footprint libraries and will be
# removed in future releases
OBSOLETE_LIST_DIST_FILES=	\
	TTL_74xx_DIL.list \
	jerry.list \
	linear.list \
	logic.list \
	lsi.list \
	memory.list \
	resistor_0.25W.list \
	resistor_adjust.list \
	resistor_array.list \
	texas_inst_amplifier.list \
	texas_inst_voltage_reg.list \
	transistor.list

LIST_DIST_FILES= \
	amphenol.list \
	connector.list \
	crystal.list \
	generic.list \
	gtag.list \
	optical.list \
	pci.list

INC_FILES= \
	bga.inc \
	amp.inc \
	amphenol.inc \
	aries.inc \
	bourns.inc \
	candk.inc \
	connector.inc \
	cts.inc \
	dil.inc \
	geda.inc \
	johnstech.inc \
	minicircuits.inc \
	misc.inc \
	nichicon.inc \
	optek.inc \
	panasonic.inc \
	pci.inc \
	plcc.inc \
	qfn.inc \
	qfp.inc \
	qfp2.inc \
	qfpdj.inc \
	resistor_adjust.inc \
	rules.inc \
	smt.inc \
	tdk.inc \
	to.inc \
	toko.inc \
	united_chemicon.inc \
	zif.inc 

DAT_FILES= \
	qfp.dat

M4_FILES=	${M4_DIST_FILES} amp.m4 aries.m4 bourns.m4 candk.m4 cts.m4 geda.m4 johnstech.m4 minicircuits.m4 nichicon.m4 \
		optek.m4 panasonic.m4 tdk.m4 toko.m4 united_chemicon.m4
LIST_FILES=	${LIST_DIST_FILES} amp.list aries.list bourns.list candk.list cts.list geda.list johnstech.list minicircuits.list nichicon.list \
		optek.list panasonic.list tdk.list toko.list united_chemicon.list

LIB_FILES= ${COMMON_FILE} ${M4_FILES} ${LIST_FILES} ${INC_FILES} 

EXTRA_DIST+=	ListLibraryContents.sh ${COMMON_FILE} ${M4_DIST_FILES} ${OBSOLETE_LIST_DIST_FILES} \
		${LIST_DIST_FILES} ${INC_FILES} \
		${LIBCONTENTS} \
		${DAT_FILES} gen_list.awk gen_m4.awk 

DISTCLEANFILES+=	${LIBRARYFILENAME} ${LIBRARYFILENAME}.contents amp.m4 amp.list aries.m4 aries.list \
	cts.m4 cts.list bourns.m4 bourns.list \
	candk.m4 candk.list \
	geda.m4 geda.list johnstech.m4 johnstech.list minicircuits.m4 minicircuits.list \
	nichicon.m4 nichicon.list \
	optek.m4 optek.list \
	panasonic.m4 panasonic.list \
	tdk.m4 tdk.list \
	toko.m4 toko.list \
	united_chemicon.m4 united_chemicon.list

${LIBRARYFILENAME}: ${COMMON_FILE} ${INC_FILES} ${DAT_FILES} ${M4_FILES} CreateLibrary.sh
	${SHELL} ./CreateLibrary.sh -I $(srcdir) $@ $(srcdir)/${COMMON_FILE} ${M4_FILES}

${LIBRARYFILENAME}.contents: ${COMMON_FILE} ${INC_FILES} ${DAT_FILES} ${LIST_FILES} CreateLibraryContents.sh
	${SHELL} ./CreateLibraryContents.sh -I $(srcdir) $(srcdir)/${COMMON_FILE} ${LIST_FILES} > $@

amp.list: amp.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=amp $(srcdir)/amp.inc > $@

amp.m4: amp.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=amp $(srcdir)/amp.inc > $@

aries.list: aries.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=aries $(srcdir)/aries.inc > $@

aries.m4: aries.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=aries $(srcdir)/aries.inc > $@

bourns.list: bourns.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=bourns $(srcdir)/bourns.inc > $@

bourns.m4: bourns.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=bourns $(srcdir)/bourns.inc > $@

candk.list: candk.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=candk $(srcdir)/candk.inc > $@

candk.m4: candk.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=candk $(srcdir)/candk.inc > $@

cts.list: cts.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=cts $(srcdir)/cts.inc > $@

cts.m4: cts.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=cts $(srcdir)/cts.inc > $@

johnstech.list: johnstech.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=johnstech $(srcdir)/johnstech.inc > $@

johnstech.m4: johnstech.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=johnstech $(srcdir)/johnstech.inc > $@

minicircuits.list: minicircuits.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=minicircuits $(srcdir)/minicircuits.inc > $@

minicircuits.m4: minicircuits.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=minicircuits $(srcdir)/minicircuits.inc > $@

nichicon.list: nichicon.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=nichicon $(srcdir)/nichicon.inc > $@

nichicon.m4: nichicon.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=nichicon $(srcdir)/nichicon.inc > $@

optek.list: optek.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=optek $(srcdir)/optek.inc > $@

optek.m4: optek.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=optek $(srcdir)/optek.inc > $@

panasonic.list: panasonic.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=panasonic $(srcdir)/panasonic.inc > $@

panasonic.m4: panasonic.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=panasonic $(srcdir)/panasonic.inc > $@

geda.m4: geda.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=geda $(srcdir)/geda.inc > $@

geda.list: geda.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=geda $(srcdir)/geda.inc > $@

tdk.list: tdk.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=tdk $(srcdir)/tdk.inc > $@

tdk.m4: tdk.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=tdk $(srcdir)/tdk.inc > $@

toko.list: toko.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=toko $(srcdir)/toko.inc > $@

toko.m4: toko.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=toko $(srcdir)/toko.inc > $@

united_chemicon.list: united_chemicon.inc gen_list.awk
	${AWK} -f $(srcdir)/gen_list.awk name=united_chemicon $(srcdir)/united_chemicon.inc > $@

united_chemicon.m4: united_chemicon.inc gen_m4.awk
	${AWK} -f $(srcdir)/gen_m4.awk name=united_chemicon $(srcdir)/united_chemicon.inc > $@

