###############################################################################
#
# hfsutils - tools for reading and writing Macintosh HFS volumes
# Copyright (C) 1996 Robert Leslie
#
# 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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###############################################################################

DESTDIR =	/usr/local
INSTDIR =	$(DESTDIR)

BINDEST =	$(DESTDIR)/bin
BININST =	$(INSTDIR)/bin
MANINST =	$(INSTDIR)/man
#LIBINST =	$(INSTDIR)/lib
#INCINST =	$(INSTDIR)/include

BININSTALL =	install -c -m 755
LIBINSTALL =	install -c -m 644
HARDLINK =	ln -f

CC =		gcc
INCLUDES =	-I/usr/include/tcl
XLIBS =		-L/usr/X11R6/lib -lX11
TCLLIBS =	-ltcl7.4 -lm
TKLIBS =	-ltk4.0

LIBS =		#-ldmalloc
DEFINES =	#-DDMALLOC

CFLAGS =	-g -Wall -Ilibhfs $(INCLUDES) $(DEFINES)
LDFLAGS =	-Llibhfs

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

TARGETS =	$(CLITARGETS) $(TCLTARGETS)
CLITARGETS =	hmount hvol hcd hpwd hdir hcat hformat
TCLTARGETS =	hfssh hfs xhfs
SUPPORT =	hfsutil xhfs.c

LIBHFS =	libhfs/libhfs.a
INCHFS =	libhfs/hfs.h

CLIOBJS =	hcwd.o hmount.o hvol.o hcd.o hpwd.o hdir.o hcat.o hformat.o
TCLOBJS =	tclhfs.o crc.o binhex.o copyout.o

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

all :: $(TARGETS)

install :: all
	for file in hfsutil hfssh xhfs; do  \
		$(BININSTALL) $$file $(BININST)/.;  \
	done

	sed -e '1s|^#!.*|#!$(BINDEST)/hfssh|' < hfs.tcl > $(BININST)/hfs
	chmod a+x $(BININST)/hfs

	for file in $(CLITARGETS); do  \
		$(HARDLINK) $(BININST)/hfsutil $(BININST)/$$file;  \
	done
	rm -f $(BININST)/hfsutil

	test -z "$(LIBINST)" || $(LIBINSTALL) $(LIBHFS) $(LIBINST)/.
	test -z "$(INCINST)" || $(LIBINSTALL) $(INCHFS) $(INCINST)/.

	for file in doc/*.1; do  \
		$(LIBINSTALL) $$file $(MANINST)/man1/.;  \
	done

again :: depend clean all

depend ::
	sed -n '1,/^### DEPEND/p' < Makefile > Makefile.new
	gcc -MM $(CFLAGS) *.c >> Makefile.new
	mv Makefile.new Makefile
	cd libhfs && make depend

clean ::
	rm -f $(TARGETS) $(SUPPORT) *.o core
	cd libhfs && make clean

dist :: clean TAGS
	name=$$(basename $$(pwd)); cd .. && tar cvfz $$name.tar.gz $$name

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

TAGS: *.[ch] libhfs/*.[ch]
	etags *.[ch] libhfs/*.[ch]

$(LIBHFS) ::
	cd libhfs && make

$(CLITARGETS): hfsutil
	ln -f hfsutil $@

hfswish: hfswish.o $(LIBHFS) $(TCLOBJS)
	$(CC) $(LDFLAGS) hfswish.o $(TCLOBJS)  \
		$(TKLIBS) $(TCLLIBS) $(XLIBS) -lhfs $(LIBS) -o $@

hfssh: hfssh.o $(LIBHFS) $(TCLOBJS)
	$(CC) $(LDFLAGS) hfssh.o $(TCLOBJS)  \
		$(TCLLIBS) -lhfs $(LIBS) -o $@

hfsutil: hfsutil.o $(LIBHFS) $(CLIOBJS)
	$(CC) $(LDFLAGS) hfsutil.o  \
		$(CLIOBJS)  \
		-lhfs $(LIBS) -o $@

hfs: hfs.tcl hfssh
	cp hfs.tcl $@
	chmod a+x $@

xhfs: xhfs.o hfswish.o $(LIBHFS) $(TCLOBJS)
	$(CC) $(LDFLAGS) hfswish.o xhfs.o $(TCLOBJS)  \
		$(TKLIBS) $(TCLLIBS) $(XLIBS) -lhfs $(LIBS) -o $@

xhfs.c: xhfs.tcl
	( echo "char xhfs[] =";  \
	  sed -e 's|^[ 	]*||' -e '/^#/d' -e '/^$$/d'  \
	      -e 's|\\|\\&|g' -e 's|"|\\&|g' -e 's|.*|"&\\n"|' xhfs.tcl;  \
	  echo ";" ) > $@

### DEPENDENCIES FOLLOW #######################################################
binhex.o: binhex.c binhex.h crc.h
copyout.o: copyout.c libhfs/hfs.h copyout.h binhex.h crc.h
crc.o: crc.c crc.h
hcat.o: hcat.c libhfs/hfs.h hcwd.h hfsutil.h hcat.h
hcd.o: hcd.c libhfs/hfs.h hcwd.h hfsutil.h hcd.h
hcwd.o: hcwd.c hcwd.h
hdir.o: hdir.c libhfs/hfs.h hcwd.h hfsutil.h hdir.h
hformat.o: hformat.c libhfs/hfs.h hcwd.h hfsutil.h hformat.h
hfssh.o: hfssh.c tclhfs.h
hfsutil.o: hfsutil.c libhfs/hfs.h hcwd.h hfsutil.h version.h hmount.h \
 hvol.h hdir.h hcd.h hpwd.h hcat.h hformat.h
hfswish.o: hfswish.c tclhfs.h xhfs.h
hmount.o: hmount.c libhfs/hfs.h hcwd.h hfsutil.h hmount.h
hpwd.o: hpwd.c hcwd.h hpwd.h
hvol.o: hvol.c libhfs/hfs.h hcwd.h hfsutil.h hvol.h
tclhfs.o: tclhfs.c tclhfs.h libhfs/hfs.h copyout.h version.h
xhfs.o: xhfs.c
