# DIET-PC Makefile

# Copyright (C) 2003-2005 Paul A. Whittaker <whitpa@users.sourceforge.net>

# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Whether or not to use UPX to compress executables when constructing an
# initramfs (compressed CPIO archive).  This may save you some RAM, but you
# can't use ldd on UPX-packed executables (to diagnose shared library problems).
USE_UPX=no

# Root directory of the TFTP hierarchy (as per tftpd directory argument).
TFTPROOT=/var/lib/tftpboot

# CD/DVD recorder device.  My CDROM test unit can read CD-RWs, so I like to
# test ISO boot images on CDRWs first (hence the "blank=fast" option).
CDR_DEV=/dev/scd0
CDR_SPEED=16
CDR_EXTRAOPTS=blank=fast driveropts=burnproof

# Disk device to install to.  This must be a block device containing an
# uncompressed FAT12 or FAT16 filesystem, or an ext2 or ext3 filesystem.
# Typically you would specify the pathname to a removable media writer device
# here, rather than a local fixed disk.
#
# This Makefile will not partition the disk, set the active partition, create
# the filesystem, or make you coffee.  Do all of this separately prior to
# running "make install-{fat,ext2}[grub|lilo]" (you can skip the coffee if you
# wish :-).
DISK_DEV=/dev/hda1

# Whether or not to install a boot loader in the Master Boot Record of the
# disk.  This option is only relevant when using FAT or ext2 boot loaders.
# MBR installation is preferable if the disk in question will only ever be used
# for DIET-PC purposes (ie. it does not contain any other operating system).
# Otherwise say no, unless you're sure that you understand the consequences.
USE_MBR=yes

# Expert-only modification beyond this point.

ARCH_LISTS=$(shell ipkg -V 0 print_architecture | awk '{printf "%s/Packages\n", $$2}')

ifeq ($(USE_UPX), yes)
    UPX=--upx
else
    UPX=
endif

ALT_FORMATS=cpio.gz e2compr ext2.gz ext3.gz ext4.gz squashfs
ALT_ROOTS=$(shell echo $(ALT_FORMATS) | tr ' ' '\n' | sed 's/^/root./')

#WHOLEDISK_DEV=$(shell echo $(DISK_DEV) | tr -d '[0-9]')
#ifeq ($(DISK_DEV), $(WHOLEDISK_DEV))
#    GRUB_ROOT=(hd7)
#else
#    GRUB_ROOT=(hd7,`(echo $(DISK_DEV) | tr -cd '[0-9]\n'; echo '1 - p') | dc`)
#endif

SUDO=
ifneq ($(shell id -u), 0)
    SUDO = sudo
endif

# The root.cpio target ought to have proper dependencies, but it is very
# difficult to detect whether or not anything relevant to the final state of
# the image has changed, so unless building a specific target, we'll just
# assume the worst and rebuild from scratch each time.
all: clean root.cpio

clean:
	rm -f root.cpio usr.cpio usrlocal.cpio

distclean: clean
	rm -rf kbuild mdcache

$(ARCH_LISTS):
	dpc-pull

ipkg-tree: $(ARCH_LISTS)
	dpc-unpack

ipks: ipkg-tree
	dpc-pack

mdcache: $(ARCH_LISTS)
	dpc-mdcache refresh

kbuild/Kconfig: mdcache
	dpc-mdxlate

config: kbuild/Kconfig
	dpc-configure
menuconfig: config

root.cpio: config
	$(SUDO) dpc-assemble $(UPX)

$(ALT_ROOTS): root.cpio
	dpc-convert root.img $(shell echo $@ | cut -f2- -d.)
