# Makefile for package dbtool
# name of the program
PN      = db
SHELL   = /bin/sh
VER     = 0.9.17
DATE    = 2001-01-05

INSTCMD = /usr/bin/install -g root -o root
INSTOPT = --strip
INSTMOD = -m 644

# where db script executable is installed
# user: adjust this to your preferences
PROGDIR = /usr/local/bin

LINKDIR = /var/tmp/db
PCKNAME = dbtool
# HELPDIR is /usr/info or /usr/local/info or /usr/share/info
#         for other dirs pinfo is somewhat picky and will not
#         find the file.
HELPDIR = /usr/share/info
MANDIR  = /usr/share/man
MANSEC  = man1

# Directory for dialog. Standard is /usr/bin for dialog-0.6z. If you
# want patched dialog-0.7 from this distribution to coexist with 
# your existing dialog program you have to select
# /usr/local/bin here and name it db_dialog.
# BINDIR  = /usr/bin
BINDIR  = /usr/local/bin

# Pick a program name for patched dialog; we call it by some new
# name to avoid messing up with the original program which
# you may want to keep for compatibility reasons. Only if
# you have opted to install dialog-0.7 as a replace for your default
# 'dialog' in $(BINDIR), which is usually in /usr/bin, its original 
# name should be used here.
# DIALOG = dialog
DIALOG = db_dialog

# program to fold indented text
FOLD = ifold

# HELPSTUFF = manual.html manual_toc.html manual_db.html man_db_toc.html db.rc
HELPSTUFF = dbtool.info.gz mysql.stpd.info.gz

DIRS      = dialog-0.7 bin docu print scripts

# db, dbtool.lsm and README are copied separately via sed
#   and are not included in DISTFILES
DISTFILES = CHANGES COPYING INSTALL Makefile TODO dbtool.man \
            dialog-0.6z.patch $(DIRS)

# -----------------------------------------------------------------
VIRTUALHELPDIR =

all:
	@echo "Can make $(PN) | proper | clean | "
	@echo "         install | uninstall | "
	@echo "         distrib | floppy | update | patchfile"

help: all

.PHONY: proper $(DIRS)
proper: WIPE = proper
proper: $(DIRS)
	rm -f *~
	rm -f DEADJOE

.PHONY: clean
clean: WIPE = clean
clean: $(DIRS)
	rm -f *~ DEADJOE
	rm -f db.tmp
	rm -rf $(PCKNAME)-$(VER)
	rm -f dialog-0.7.tar.gz

$(DIRS):
	(cd $@ && make $(WIPE))

dialog-0.7/dialog:
	cd dialog-0.7 && make depend
	cd dialog-0.7 && make

dialog: dialog-0.7/dialog

/bin/ifold:
	cd bin && make ifold

/bin/csv:
	cd bin && make csv

ifold: /bin/ifold

csv: /bin/csv

db: dialog ifold csv manuals
	cd .. && ln -s ../$(PCKNAME)-$(VER) ../$(PCKNAME) \
          >/dev/null 2>&1; exit 0

.PHONY: manuals
manuals: docuversion docu/dbtool.info.gz docu/mysql.stpd.info.gz

docu/dbtool.info.gz:
	cd docu && make dbtool.info.gz

docu/mysql.stpd.info.gz:
	cd docu && make mysql.stpd.info.gz

.PHONY: docuversion
docuversion:
	cd docu && cat db_incl.texi | sed -e "s/version .*/version $(VER)/" \
          > db_incl.tmp ; cp db_incl.tmp db_incl.texi ; rm -f db_incl.tmp

install:
	@if test "$$EUID" != "0"; then \
           echo "-----> ERROR: must be root to install!" ; \
           echo "enter: su -c 'make install'"; exit 1 ; fi
	sed -e "\|^DLGPRG=|s|=.*$$|=$(BINDIR)/$(DIALOG)|" \
            -e "\|^FLDPRG=|s|=.*$$|=$(BINDIR)/$(FOLD)|" \
            -e "\|^MYSQLHELPDIR|s|=.*$$|=|" \
            -e "\|^DBMANHELPDIR|s|=.*$$|=|" \
            -e "\|^LINK_DIR|s|=.*$$|=$(LINKDIR)|" <$(PN) >$(PN).tmp
	$(INSTCMD) --directory $(HELPDIR)
	$(INSTCMD) -m 777 --directory $(LINKDIR)
	touch $(LINKDIR)/db.linklist
	chmod 666 $(LINKDIR)/db.linklist
	$(INSTCMD) $(INSTMOD) print/massmail.sty $(LINKDIR)/massmail.sty
	$(INSTCMD) $(INSTMOD) print/german.sty $(LINKDIR)/german.sty
	$(INSTCMD) $(INSTMOD) print/ngerman.sty $(LINKDIR)/ngerman.sty
	$(INSTCMD) $(INSTMOD) print/umlaut.tex $(LINKDIR)/umlaut.tex
	$(INSTCMD) $(INSTMOD) print/verbatim.hdr.tex $(LINKDIR)/verbatim.hdr.tex
	$(INSTCMD) $(INSTMOD) print/verbatim.end.tex $(LINKDIR)/verbatim.end.tex
	$(INSTCMD) $(INSTMOD) docu/db.rc $(LINKDIR)/db.rc
	$(INSTCMD) $(PN).tmp $(PROGDIR)/$(PN)
	$(INSTCMD) $(INSTOPT) dialog-0.7/dialog $(BINDIR)/$(DIALOG)
	$(INSTCMD) $(INSTOPT) bin/ifold $(BINDIR)/$(FOLD)
	$(INSTCMD) $(INSTMOD) docu/dbtool.info.gz $(HELPDIR)
	$(INSTCMD) $(INSTMOD) docu/mysql.stpd.info.gz $(HELPDIR)
	cat dbtool.man | sed -e "s/xxVERSIONxx/$(VER)/g" \
                             -e "s/xxDATExx/$(DATE)/g" > $(PCKNAME).1
	gzip -f9 $(PCKNAME).1
	$(INSTCMD) -m 644 $(PCKNAME).1.gz $(MANDIR)/$(MANSEC)
	rm -f $(PN).tmp $(PCKNAME).1.gz


.PHONY: uninstall
uninstall:
	@if test "$$EUID" != "0"; then \
           echo "-----> must be root to uninstall!" ; exit 1 ; fi
	rm -f $(HELPDIR)/dbtool.info.gz
	rm -f $(HELPDIR)/mysql.stpd.info.gz
	rm -f $(MANDIR)/$(MANSEC)/$(PCKNAME).1.gz
	rm -f $(LINKDIR)/db.rc
	rm -f $(PROGDIR)/$(DIALOG)
	rm -f $(PROGDIR)/$(PN)
	rm -f $(LINKDIR)/db.log
	rm -f $(LINKDIR)/db.linklist
	rm -f $(LINKDIR)/massmail.sty
	rm -f $(LINKDIR)/german.sty
	rm -f $(LINKDIR)/ngerman.sty
	rm -f $(LINKDIR)/umlaut.tex
	rm -f $(LINKDIR)/verbatim.hdr.tex
	rm -f $(LINKDIR)/verbatim.end.tex
	find /home -name '.db.rc' -exec rm -f '{}' \;
	rmdir $(HELPDIR)
	rmdir $(LINKDIR)

#
# These targets are for package development only.
# File README.in is not part of the dbtool distribution.
#

.PHONY: distclean
distclean: clean
	rm -f db.tmp README

.PHONY: distrib
distrib: distclean
	rm -f dialog-0.7.tar.gz
	cd .. && tar -cf - $(PCKNAME) | gzip -9 > $(PCKNAME).tgz

README: README.in
	cat README.in | sed -e "s/xxVERSIONxx/$(VER)/g" > README

# the following two targets assume that you have a directory
# /floppy to be used as mount point and an entry in fstab
# saying how to mount the floppy drive with permission for users.
# it my look like: /etc/fstab
# /dev/fd0    /floppy    msdos    rw,noauto,user    0    0
.PHONY: floppy
floppy: distrib
	umount /floppy >/dev/null 2>&1 ; exit 0
	mount /floppy
	cp -a ../$(PCKNAME).tgz /floppy
	sync
	sleep 1
	sync
	umount /floppy


update:
	mkdir -p db-unpack
	umount /floppy >/dev/null 2>&1 ; exit 0
	mount /floppy
	cp /floppy/$(PCKNAME).tgz db-unpack/$(PCKNAME).tgz
	umount /floppy
	cd db-unpack && tar xzf $(PCKNAME).tgz
	sync
	cp -fr db-unpack/$(PCKNAME)/* .
	rm -fr db-unpack


patchfile: 
	rm -f *~ DEADJOE dialog-0.6z.patch
	cd dialog-0.7 && make proper
	@echo -e " To apply this patch, change to the directory\n\
 below your original distributions 'dialog-0.6z/' directory,\n\
 put the patchfile there and enter:\n\n\
       patch -p5 < dialog-0.6z.patch\n\n\
 Then rename the directory: mv dialog-0.6z dialog-0.7\n\n\
 Do not try to patch dialog-0.7 in the '$(PCKNAME)' distribution;\n\
 it already contains the modified code!\n\n" > dialog-0.6z.patch
	diff -C 2 -P -r -x dialog -x .depend \
        /usr/src/packages/SOURCES/dialog-0.6z \
        dialog-0.7 >>dialog-0.6z.patch ; exit 0


upload: distclean patchfile README
	mkdir -p $(PCKNAME)-$(VER)
	cp -r $(DISTFILES) $(PCKNAME)-$(VER)
	cat $(PN) | sed -e "/DB_VERSION=/s/'[0-9.]*/'$(VER)/" \
          -e "/DB_VERSION=/s/[0-9-]*'$$/$(DATE)'/" \
          > $(PCKNAME)-$(VER)/$(PN)
	chmod 755 $(PCKNAME)-$(VER)/$(PN)
	cat $(PCKNAME).lsm | sed -e "s/^Version:.*/Version:	$(VER)/" \
          -e "s/xxVERSIONxx/$(VER)/" \
          -e "s/^Entered-date:.*/Entered-date:	$(DATE)/" \
          > $(PCKNAME)-$(VER)/$(PCKNAME)-$(VER).lsm
	cat README.in | sed -e "s/xxVERSIONxx/$(VER)/g" \
          > $(PCKNAME)-$(VER)/README
	tar -cf - $(PCKNAME)-$(VER) | gzip -9 \
          > $(PCKNAME)-$(VER).tar.gz
	mv $(PCKNAME)-$(VER).tar.gz $(PCKNAME)-$(VER)/$(PCKNAME)-$(VER).tar.gz
