# Makefile 

BIN=		bootcdwrite
USRLIBS=	bootcd-run.lib bootcd-check.lib bootcd-hppa.lib \
		bootcd2disk.conf bootcd2disk.lib \
		default.txt bootcd-ia64.lib bootcd-i386.lib
USREXLIBS=	bootcd2disk bootcdflopcp bootcdmodprobe bootcdproberoot \
		S13bootcdflop.sh S12bootcdram.sh bootcdmk2diskconf bootcdbackup
CONFFILES=	bootcdwrite.conf isolinux.cfg syslinux.cfg bootcdbackup.conf 
INITTOP=	runbootcdmodprobe
INITPREMOUNT=	runbootcdproberoot
INITHOOK=       bootcdinitramfshook
INITRAMFSDEST=  $(DESTDIR)/usr/share/initramfs-tools

VERSION=version=$$(sed -ne "s|^bootcd (\([^)]*\)).*|\1|p;q" debian/changelog)
REPLVERSION=($(VERSION); \
  version2=$$(echo "$${version}" | sed "s/\./_/"); \
  sed -e "s|\#\#BOOTCD\.VERSION\#\#|$${version}|" -e "s|\#\#BOOTCD_VERSION\#\#|$${version2}|")
INSERT=( \
    lines=$$(sed -e "$$SEARCH" $< |wc -l); \
    elines=$$(expr $$(cat $< |wc -l) - $$lines + 1); \
    head -n $$lines $<; \
    cat $$FILE; \
    tail -n -$$elines $< \
	)

all: bootcddebootstrap bootcdbackupwizard ${USREXLIBS}

bootcdbackwiz: bootcdbackwiz_extract ../bootcd_?.??_all.deb ../bootcd-mkinitramfs_?.??_all.deb ../bootcd-i386_?.??_all.deb README.bootcdbackwiz_extract
	sh -c '$(VERSION); \
	  mkdir bootcdbackupwizard_$${version}; \
          for i in $^; do \
            cp $$i bootcdbackupwizard_$${version}/; \
          done; \
          tar -cvzf bootcdbackupwizard_$${version}.tar.gz bootcdbackupwizard_$${version}; \
          rm -rf bootcdbackupwizard_$${version}'

clean:
	@rm -f ChangeLog.gz
	@rm -f bootcddebootstrap bootcdbackupwizard bootcdmk2diskconf bootcdbackwiz_extract

install:	all
	@install -m 755 -o root -g root $(BIN) $(DESTDIR)/usr/bin/
	@install -m 644 -o root -g root $(CONFFILES) $(DESTDIR)/etc/bootcd/
	@install -m 644 -o root -g root $(USRLIBS) $(DESTDIR)/usr/share/bootcd/
	@install -m 755 -o root -g root $(USREXLIBS) $(DESTDIR)/usr/share/bootcd/
	@install -m 755 -o root -g root $(INITTOP) $(INITRAMFSDEST)/scripts/init-top/
	@install -m 755 -o root -g root $(INITPREMOUNT) $(INITRAMFSDEST)/scripts/init-premount/
	@install -m 755 -o root -g root $(INITHOOK) $(INITRAMFSDEST)/hooks/

bootcddebootstrap: bootcddebootstrap.src bootcd-run.lib
	/bin/sh -c ' \
	  SEARCH="\|\#\#\# BOOTCD_LIB|q"; \
          FILE="bootcd-run.lib" ; \
          $(INSERT) | \
	  $(REPLVERSION) >$@'
	chmod 755 $@

bootcdbackupwizard: bootcdbackupwizard.src bootcd-run.lib 
	/bin/sh -c ' \
	  SEARCH="\|\#\#\# BOOTCD_LIB|q"; \
          FILE="bootcd-run.lib" ; \
          $(INSERT) | \
	  $(REPLVERSION) >$@'
	chmod 755 $@

bootcdbackwiz_extract: bootcdbackwiz_extract.src ../bootcd_?.??_all.deb ../bootcd-mkinitramfs_?.??_all.deb ../bootcd-i386_?.??_all.deb
	/bin/sh -c ' \
          bootcddeb=$$(cd ..; /bin/ls bootcd_?.??_all.deb); \
	  bootcdmkinitramfsdeb=$$(cd ..; /bin/ls bootcd-mkinitramfs_?.??_all.deb); \
	  bootcdi386deb=$$(cd ..; /bin/ls bootcd-i386_?.??_all.deb);\
	  sed -e "s|\#\#BOOTCDDEB\#\#|$${bootcddeb}|;\
s|\#\#BOOTCDMKINITRAMFSDEB\#\#|$${bootcdmkinitramfsdeb}|;\
s|\#\#BOOTCDI386DEB\#\#|$${bootcdi386deb}|" $< | \
	  $(REPLVERSION) >$@'
	chmod 755 $@

bootcdmk2diskconf:	bootcdmk2diskconf.src bootcd2disk.conf
	/bin/sh -c ' \
	  SEARCH="\|\#\#\# BOOTCD2DISK.CONF|q"; \
          FILE="bootcd2disk.conf"; \
          $(INSERT) | \
	  $(REPLVERSION) >$@'
	chmod 755 $@
	./$@ -checktemplate
