#!/bin/sh
#
# This thing makes a copy of part of itself to Makefile where it is run
# provided Makefile does not exist

if test a$1 != a-f
then
  if test -e Makefile
  then
    echo Makefile exists here already, cowardly refusing to overwrite it
    echo Delete Makefile first or use -f to over write

    exit 1
  fi
fi

cat <<EOF > Makefile
#
# uClinux development environment top level Makefile
#
# If the directory tree this Makefile expects to find
# is not here, it is created my making a private copy
# of the template installed in /opt/uClinux.  All you
# have to do is copy this Makefile to a fresh directory
# and make a target.
#
# There are a number of targets you can make.
#
#  # make bin		builds the user land binaries
#  # make tree		builds the ROMfs tree
#  # make romfs		builds a ROMfs filesystem
#  # make image		builds a complete binary image
#  # make kernel	builds the uClinux kernel
#  # make clean		removes binaries
#  # make distclean	removes all things built by Makefile
#  # make tarball	creates a .tar.gz of this tree
#
# You can customise which binaries are installed and where in
# the ROMfs image by creating a template file and using 
# TEMPLATE=file.mk  If you don't specify a template file, then
# deftemplate.mk is used.  We recomend starting with a copy of
# this file and customizing it for your application.

TEMPLATE := deftemplate.sh

default: image

# The buildable targets

bin: src dummy
	make -C src

tree: bin romdisk \$(TEMPLATE)
	sh -f \$(TEMPLATE)

romfs: romfs.img

kernel: buildkernel

image: image.bin

clean:

distclean:

tarball:

# The build engine

romfs.img: tree
	genromfs -v -V "ROM Disk" -f romdisk.img -d romdisk 2> romdisk.map

image.bin: kernel romfs.img
	cat linux/linux.bin romdisk.img > image.bin

# The tree management targets

src:
	(cd /opt/uClinux; tar -cpf - src) | tar -xpf -

buildkernel: linux .kernel_stamp

.kernel_stamp:
	make -C linux linux.bin

linux:
ifeq (\$(KERNEL), src)
	echo Makeing a local copy of the kernel source
	(cd /opt/uClinux; tar -cpf - linux) | tar -xpf -
	rm -f .kernel_stamp
else
	echo Setting up a link to the kernel binaries
	ln -sf /opt/uClinux/linux linux
	touch .kernel_stamp
endif

romdisk:
	(cd /opt/uClinux; tar -cpf - romdisk) | tar -xpf -
	rm -rf romdisk/bin/*
	rm -rf romdisk/sbin/*

deftemplate.sh:
	cp /opt/uClinux/deftemplate.sh deftemplate.sh
#

dummy:
EOF

