# @configure_input@

# Makefile for units, a program for units conversion
#
# Copyright (C) 1996, 1997, 1999, 2005, 2006, 2012, 2014, 2017, 2018, 2022, 2024
# Free Software Foundation, Inc
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#    
#
# This program was written by Adrian Mariano (adrianm@gnu.org)
#

@MKS_POSIX@

PACKAGE_VERSION = @PACKAGE_VERSION@

SHELL = /bin/sh
prefix = @MKS_DRIVE@@prefix@
exec_prefix = @exec_prefix@
srcdir = @srcdir@

CC = @CC@

INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
MAKEINFO = makeinfo
TEXI2DVI = texi2dvi
TEXI2PDF = texi2pdf
MKDIR_P = @MKDIR_P@
LDFLAGS = @LDFLAGS@
LIBS= @LIBS@
VPATH = @srcdir@
HAVE_PYTHON = @HAVE_PYTHON@
PYTHON = @PYTHON@
HAVE_MKS = @HAVE_MKS@

datarootdir = @datarootdir@
datadir = @datadir@
sharedstatedir = @sharedstatedir@
bindir = @bindir@
infodir = @infodir@
mandir = @mandir@

transform=@program_transform_name@

DEFS=-DUNITSFILE=\"@UDATADIR@definitions.units\" -DLOCALEMAP=\"@UDATADIR@locale_map.txt\" \
                    @DEFIS@ @DEFS@
CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
OBJECTS = units.@OBJEXT@ parse.tab.@OBJEXT@ getopt.@OBJEXT@ getopt1.@OBJEXT@ @STRFUNC@

.PHONY: currency-update-check dist sig validate-codata


.SUFFIXES:
.SUFFIXES: .c .@OBJEXT@ .rc .res .texinfo .pdf

RC = rc.exe
RES = units.res

.rc.res:
	$(RC) -fo $@ $<

.texinfo.pdf:
	$(TEXI2PDF) $<

.c.@OBJEXT@:
	$(CC) $(DEFS) $(CPPFLAGS) $(CFLAGS) -I$(srcdir) -c $<

# %.@OBJEXT@: %.c
#     $(CC) $(DEFS) $(CFLAGS) -I$(srcdir) -c %.c


DYNAMICFILES = currency.units cpi.units metal_prices.units crypto.units

DISTFILES = README units.info units.txt getopt1.c units.dvi \
   Makefile.in units.c getopt.c getopt.h definitions.units units.texinfo \
   configure.ac configure strfunc.c COPYING install-sh \
   units.man NEWS texi2man INSTALL units.pdf units_cur \
   parse.tab.c parse.y units.h locale_map.txt fdl-1.3.texi elements.units \
   unitsfile.ico unitsprog.ico units.rc icon_ms.png \
   Makefile.OS2 makeobjs.cmd README.OS2 \
   UnitsMKS.texinfo UnitsMKS.pdf setvcvars.sh \
   UnitsWin.texinfo UnitsWin.pdf winmkdirs.bat Makefile.Win dehtml elemcvt.sh \
   Windows codata-check CODATA.map $(DYNAMICFILES)

all: units@EXEEXT@ units.1 units.info units_cur_inst 

units.@OBJEXT@: units.c units.h

parse.tab.c: parse.y units.h
	bison parse.y

parse.tab.@OBJEXT@: parse.tab.c units.h

units@EXEEXT@: $(OBJECTS) @MKS_RES@
	$(CC) $(CFLAGS) $(LDFLAGS)  -o units@EXEEXT@ $(OBJECTS) @MKS_RES@ $(LIBS)

units_cur_inst: units_cur
	sed -e "s*output_dir = ''*output_dir='@CDAT@'*"\
            -e "s@/usr/bin/python@$(PYTHON)@" \
             $(srcdir)/units_cur > units_cur_inst

install-support: definitions.units elements.units units_cur_inst 
	$(MKDIR_P) $(DESTDIR)@UDAT@ $(DESTDIR)$(bindir) $(DESTDIR)@CDAT@
	$(INSTALL_DATA) $(srcdir)/definitions.units $(DESTDIR)@UDAT@definitions.units
	$(INSTALL_DATA) $(srcdir)/elements.units $(DESTDIR)@UDAT@elements.units
	for ufile in cpi.units $(DYNAMICFILES) ; do \
	     rm -f $(DESTDIR)@UDAT@$$ufile; \
	     $(INSTALL_DATA) $(srcdir)/$$ufile $(DESTDIR)@CDAT@$$ufile; \
	     if [ $(DESTDIR)@CDAT@ != $(DESTDIR)@UDAT@ ];then ln -sf @CDAT@$$ufile $(DESTDIR)@UDAT@$$ufile;fi ;\
	   done
	$(INSTALL_DATA) $(srcdir)/locale_map.txt $(DESTDIR)@UDAT@locale_map.txt
	if test $(HAVE_PYTHON) = yes; then \
	  $(INSTALL_PROGRAM) units_cur_inst $(DESTDIR)$(bindir)/`echo units_cur|sed '$(transform)'`;fi
	if test $(HAVE_MKS) = yes; then make install-mks; fi


install: units@EXEEXT@ install-doc install-support
	$(MKDIR_P) $(DESTDIR)$(bindir) 
	$(INSTALL_PROGRAM) units@EXEEXT@ $(DESTDIR)$(bindir)/`echo units@EXEEXT@|sed '$(transform)'`

install-strip: units@EXEEXT@ install-doc install-support
	$(MKDIR_P) $(DESTDIR)$(bindir)
	$(INSTALL_PROGRAM) -s units@EXEEXT@ $(DESTDIR)$(bindir)/`echo units@EXEEXT@|sed '$(transform)'`

install-doc: install-man install-info

install-man: units.1
	$(MKDIR_P) $(DESTDIR)$(mandir)/man1
	$(INSTALL_DATA) units.1 $(DESTDIR)$(mandir)/man1/`echo units|sed '$(transform)'`.1

# file association and DefaultIcon
# assume that if we're running the MKS Toolkit, we're running the Korn
# shell, and can use pattern replacement
install-mks:
	$(INSTALL_DATA) $(srcdir)/unitsfile.ico $(DESTDIR)@UDAT@unitsfile.ico
	$(INSTALL_DATA) $(srcdir)/unitsprog.ico $(DESTDIR)@UDAT@unitsprog.ico
	assoc .units Units.DataFile
	vidir="$${ROOTDIR//\//\\}"; \
	    ftype Units.DataFile "\"$$vidir"'\mksnt\viw.exe" -Uc8p8 "%1"'
	registry -s -k "HKCR\\Units.DataFile" -v 'GNU units data file'
	iconfile=$(DESTDIR)@UDAT@unitsfile.ico; \
	    registry -s -k "HKCR\\Units.DataFile\\DefaultIcon" -n '' -v "$${iconfile//\//\\}"

units.man: units.texinfo texi2man
	./texi2man units.texinfo > units.man

elements.units:
	curl 'https://physics.nist.gov/cgi-bin/Compositions/stand_alone.pl?ele=&ascii=ascii2&isotype=all' \
	    | ./elemcvt.sh > elements.units

units.1: units.man
	sed s!@DATAFILE@!@UDAT@definitions.units! $(srcdir)/units.man > units.1

install-info: units.info
	$(POST_INSTALL)
	$(MKDIR_P) $(DESTDIR)$(infodir)
	$(INSTALL_DATA) $(srcdir)/units.info $(DESTDIR)$(infodir)
# Run install-info only if it exists.
# Use `if' instead of just prepending `-' to the
# line so we notice real errors from install-info.
# We use `$(SHELL) -c' because some shells do not
# fail gracefully when there is an unknown command.
	if test -f $(DESTDIR)$(infodir)/dir; then \
	  if $(SHELL) -c 'install-info --version' >/dev/null 2>&1; then \
	    install-info --dir-file=$(DESTDIR)$(infodir)/dir \
	               $(DESTDIR)$(infodir)/units.info; \
	  else true; fi \
	else true; fi

Makefile: Makefile.in config.status
	./config.status

uninstall: 
	-rm -f $(DESTDIR)$(bindir)/`echo units@EXEEXT@|sed '$(transform)'` \
	       $(DESTDIR)$(mandir)/man1/`echo units|sed '$(transform)'`.1 \
	       $(DESTDIR)$(infodir)/units.info \
	       $(DESTDIR)@UDAT@definitions.units \
	       $(DESTDIR)@UDAT@elements.units \
	       $(DESTDIR)@CDAT@currency.units \
	       $(DESTDIR)@UDAT@currency.units \
	       $(DESTDIR)@CDAT@cpi.units \
	       $(DESTDIR)@UDAT@cpi.units \
	       $(DESTDIR)@UDAT@locale_map.txt \
	       $(DESTDIR)@UDAT@unitsfile.ico \
	       $(DESTDIR)@UDAT@unitsprog.ico \
	       $(DESTDIR)$(bindir)/`echo units_cur|sed '$(transform)'`
	-rmdir $(DESTDIR)@UDAT@
	@if test -f $(DESTDIR)$(infodir)/dir; then \
	  echo You may need to remove units.info from $(DESTDIR)$(infodir)/dir; \
	else true; fi

clean mostlyclean: texclean
	-rm -f *.@OBJEXT@ *.res units@EXEEXT@ units.dvi units.1 distname .chk units_cur_inst currency.units.test cpi.units.test
	-rm -rf wwwold wwwnew $(CODATA_FILE) $(NATURAL_ENERGY)

distclean: clean
	-rm -f config.* Makefile TAGS Windows/Makefile

maintainer-clean: clean
	-rm -f units.txt units.info units.pdf units.dvi \
	       UnitsMKS.pdf UnitsWin.pdf \
               units-*.tar.gz parse.tab.c currency.units cpi.units

# get rid of the TeX auxilliary files
texclean:
	-rm -f units.log UnitsMKS.log UnitsWin.log \
	    *.aux *.cp *.fn *.ky *.op *.pg *.toc *.tp *.vr

currency-update-check:
	@echo "Trying to update currency.units and cpi.units" 
	-$(srcdir)/units_cur currency.units.test cpi.units.test
	@echo "--------------------------------"
	@if [ ! -s currency.units.test ]; then echo "Currency update failed"; else echo "Currency update appears OK"; fi
	@if [ ! -s cpi.units.test ]; then echo "CPI update failed"; else echo "CPI update appears OK"; fi
	@rm -f currency.units.test cpi.units.test

KEY=927D02FA733C3D902D21CAC02D649F2B7B4C8179

sig:
	echo units-`sed -n -e '/\#.*VERSION/s/.*"\(.*\)"/\1/gp' \
	    $(srcdir)/units.c`.tar.gz > distname
	echo 'version: 1.2' > `cat distname`.directive
	echo 'directory: units' >> `cat distname`.directive
	echo 'filename: '`cat distname` >> `cat distname`.directive
	gpg --clearsign --local-user $(KEY) `cat distname`.directive
	gpg -b --local-user $(KEY) `cat distname`
	-rm -f `cat distname`.directive distname


dist: $(DISTFILES) units
	@echo units-`sed -n -e '/\#.*VERSION/s/.*"\(.*\)"/\1/gp' \
	    $(srcdir)/units.c` > distname
	@./units_cur
	@echo Updated currencies, running check:
	@for system in si emu esu gaussian gauss hlu natural \
	               natural-gauss hartree planck planck-red ; do \
		echo $$system;  ./units -f definitions.units -c -u $$system; done
	@echo ''
	@chmod g+rw,o+r *
	-@if [ -d `cat distname` ]; then rm -r `cat distname`; fi
	-@if [ -e `cat distname`.tar.gz ]; then rm `cat distname`.tar.gz; fi
	@mkdir `cat distname`
	@dst=`cat distname`; for f in $(DISTFILES); do \
	   ln $(srcdir)/$$f $$dst/$$f >/dev/null 2>&1 || { \
	          cp -pr $(srcdir)/$$f $$dst/$$f ; } \
	done
	@chmod 777 `cat distname`
	@tar czf `cat distname`.tar.gz `cat distname`
	@echo `cat distname`.tar.gz created
	-@if [ `cat distname` != units-${PACKAGE_VERSION} ]; then echo "Package version (${PACKAGE_VERSION}) doesn't match code version.";fi
	-@rm -rf `cat distname` distname

dvi: units.dvi

pdf: units.pdf winpdf

winpdf: UnitsWin.pdf UnitsMKS.pdf

units.dvi: units.texinfo
	$(TEXI2DVI) $(srcdir)/units.texinfo

info: units.info

units.info: units.texinfo
	$(MAKEINFO) $(srcdir)/units.texinfo

units.txt: units.1
	nroff -Tascii -man $(srcdir)/units.1 | col -b > units.txt

doc: units.dvi units.info units.txt units.pdf UnitsMKS.pdf UnitsWin.pdf

check: all
	@echo Checking units
	@./units -f $(srcdir)/definitions.units \
	      '(((square(kiloinch)+2.84m2) /0.5) meters^2)^(1|4)' m \
	    | sed -n -e 's/	\* //p' > .chk
	@if [ "`cat .chk`" = 6 ]; then echo Units seems to work; \
	   else echo Something is wrong: units failed the check: ;cat .chk; fi
	@rm -f .chk

configure: configure.ac
	autoconf

TAGS: units.c
	etags $(srcdir)/units.c $(srcdir)/parse.y

tags: units.c
	ctags $(srcdir)/units.c

smalldist: units.c units.h parse.y parse.tab.c
	echo units-`sed -n -e '/\#.*VERSION/s/.*"\(.*\)"/\1/gp' \
	    $(srcdir)/units.c` > distname
	-rm -r `cat distname` `cat distname`.tar `cat distname`.tar.gz
	tar cf `cat distname`.tar units.c units.h  parse.y  parse.tab.c\
	   getopt1.c getopt.c getopt.h
	gzip `cat distname`.tar


#
# This section attempts to update the manual on the GNU web server
# If all works correctly, it will add/remove files from the CVS tree as required.
# It assumes there are only files in the manual and manual/html_node directories. 
#

www: doc
	-rm -r wwwold wwwnew
	mkdir wwwnew
	./mygendocs.sh --email adrianm@gnu.org -o wwwnew/units/manual units "GNU Units Manual"
	mkdir wwwold
	cd wwwold;CVS_RSH=ssh cvs -z3 -d:ext:adrianm@cvs.savannah.gnu.org:/webcvs/units co units
	cd wwwold/units/manual;ls > /tmp/wwwunits.listold
	cd wwwnew/units/manual;ls > /tmp/wwwunits.listnew
	cd wwwold/units/manual/html_node;ls > /tmp/wwwunits.hlistold
	cd wwwnew/units/manual/html_node;ls > /tmp/wwwunits.hlistnew
	-grep -vf /tmp/wwwunits.listold /tmp/wwwunits.listnew > /tmp/wwwunits.add
	-grep -vf /tmp/wwwunits.listnew /tmp/wwwunits.listold | grep -v CVS > /tmp/wwwunits.rm
	-grep -vf /tmp/wwwunits.hlistold /tmp/wwwunits.hlistnew > /tmp/wwwunits.html.add
	-grep -vf /tmp/wwwunits.hlistnew /tmp/wwwunits.hlistold | grep -v CVS > /tmp/wwwunits.html.rm
	rsync -Cr --del -v wwwnew/units/manual wwwold/units/
	@if [ -s /tmp/wwwunits.add ]; then cd wwwold/units/manual; cvs add `cat /tmp/wwwunits.add`; fi
	@if [ -s /tmp/wwwunits.rm ]; then cd wwwold/units/manual; cvs remove `cat /tmp/wwwunits.rm`; fi
	@if [ -s /tmp/wwwunits.html.add ]; then cd wwwold/units/manual/html_node; cvs add `cat /tmp/wwwunits.html.add`; fi
	@if [ -s /tmp/wwwunits.html.rm ]; then cd wwwold/units/manual/html_node; cvs remove `cat /tmp/wwwunits.html.rm`; fi
	cd wwwold/units; cvs commit
	-rm /tmp/wwwunits.hlistnew /tmp/wwwunits.hlistold /tmp/wwwunits.listnew /tmp/wwwunits.listold
	-rm /tmp/wwwunits.html.rm /tmp/wwwunits.html.add /tmp/wwwunits.add /tmp/wwwunits.rm
	-rm -r wwwold wwwnew

# This was above but had issues

# @if [ -s /tmp/wwwunits.add ]; then cd wwwold/units/manual; cvs add `cat /tmp/wwwunits.add`; \
#        cvs commit `cat /tmp/wwwunits.add`; fi
# @if [ -s /tmp/wwwunits.rm ]; then cd wwwold/units/manual; cvs remove `cat /tmp/wwwunits.rm`; \
#        cvs commit `cat /tmp/wwwunits.rm`; fi
# @if [ -s /tmp/wwwunits.html.add ]; then cd wwwold/units/manual/html_node; \
#        cvs add `cat /tmp/wwwunits.add`; cvs add `cat /tmp/wwwunits.add`; fi
# @if [ -s /tmp/wwwunits.html.rm ]; then cd wwwold/units/manual/html_node; \



#these definitions may be overridden on make's command-line:
DEFINITION_FILE = definitions.units
MAP_FILE = CODATA.map
CODATA_FILE = allascii.txt
NATURAL_ENERGY = natural_energy.units


validate-codata: codata-check $(CODATA_FILE) $(MAP_FILE) $(DEFINITION_FILE) $(NATURAL_ENERGY)
	./codata-check -c $(CODATA_FILE) -m $(MAP_FILE) \
	               -U $(DEFINITION_FILE) -u $(NATURAL_ENERGY) -v

$(CODATA_FILE):
	wget -O $@ https://physics.nist.gov/cuu/Constants/Table/allascii.txt

$(NATURAL_ENERGY):
	@# harmonize the definition of natural_energy with CODATA's
	@# by using a hack/work-around/overrride file:
	echo '+natural_energy  m_e c^2' > $@

#
# Not very portable code to compile into library (written for Linux)
#

libunits.a: $(OBJECTS)
	ar -r libunits.a $^

libunits.so: $(OBJECTS)
	$(CC) $(LDFLAGS) -shared -o $@ $^

# Tell versions [3.59,3.63) of GNU make not to export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
