
# need to define CP for freeswan install
TOPDIR = $(shell cd ..;pwd)
include ${TOPDIR}/brecis/Makefile.brecis

DONOTCOPY := $(shell cat BOOT.FILES${TYPE_SYSTEM}.EXCLUDE 2>/dev/null || true)
TARX := $(shell if [ -s BOOT.FILES${TYPE_SYSTEM}.EXCLUDE ]; then echo "-X BOOT.FILES${TYPE_SYSTEM}.EXCLUDE" ; else echo ""; fi)
TAR := $(shell if [ -x /usr/local/bin/gtar ]; then echo /usr/local/bin/gtar; else echo tar; fi)
#-----------------------------------------------------------------------------
SYMBOLIC_LINKS = etc.big/dhcpc etc.big/resolv.conf etc.big/localtime
#-----------------------------------------------------------------------------
all:
	../genromfs/genromfs -v -f romdisk.img  -d root/ 
#-----------------------------------------------------------------------------
test:
	echo DONOTCOPY=${DONOTCOPY}
	echo TAR=${TAR}
	echo TARX=${TARX}
#-----------------------------------------------------------------------------
etc.big/dhcpc:
	ln -sf /var/dhcpc etc.big/dhcpc
etc.big/dhcp-brecis.renew:
	ln -sf /var/dhcp-brecis.bound etc.big/dhcp-brecis.renew
etc.big/resolv.conf:
	ln -sf /var/dhcpc/resolv.conf etc.big/resolv.conf
etc.big/localtime:
	ln -sf /var/zoneinfo/CST6CDT etc.big/localtime
#-----------------------------------------------------------------------------
# romdisk.img used from compilescripts directory to remake romdisk.img file.
romdisk.img:: ../genromfs/genromfs ${SYMBOLIC_LINKS}
	(cd ../genromfs; ${MAKE})
#-----------------------------------------------------------------------------
romdisk.img:: root.add2${NFS}
	../genromfs/genromfs -v -f romdisk.img  -d root/ 
	chmod 644 romdisk.img
#-----------------------------------------------------------------------------
new::
	- mv romdisk.tgz romdisk.tgz.save
	(cd root; GZIP=-9 ${TAR} zcf ../romdisk.tgz .)
#-----------------------------------------------------------------------------
root::
	rm -rf root
	mkdir root
	(cd root; ${TAR} -xzpf ../romdisk.tgz)
	if [ -x fixipaddr ] ; then sh ./fixipaddr ; fi
#-----------------------------------------------------------------------------
root.add0:: root
	if [ -d local${TYPE_SYSTEM} ]; then mkdir root/local; ${TAR} cf - ${TARX} -C local${TYPE_SYSTEM} . | ${TAR} xf - -C root/local; fi
	if [ -d htdocs${TYPE_SYSTEM} ]; then mkdir root/htdocs; ${TAR} cf - ${TARX} -C htdocs${TYPE_SYSTEM} . | ${TAR} xf - -C root/htdocs; fi
	if [ -d etc${TYPE_SYSTEM} ]; then mkdir root/etc; ${TAR} cf - ${TARX} -C etc${TYPE_SYSTEM} . | ${TAR} xf - -C root/etc; fi
	if [ -d share${TYPE_SYSTEM} ]; then mkdir root/share; ${TAR} cf - ${TARX} -C share${TYPE_SYSTEM} . | ${TAR} xf - -C root/share; fi
#-----------------------------------------------------------------------------
# Copy all executable userland files, if BOOT.FILES.type_system does not exist.
root.add1${NFS}${TYPE_SYSTEM}:: root.add0
	- if [ "${NFS}" == "NFS" -a -e BOOT.FILES${TYPE_SYSTEM} ]; then \
	    ./NFS.create ; \
	  else \
	    if [ "${TYPE_SYSTEM}" == "" -o ! -e BOOT.FILES${TYPE_SYSTEM} ]; then \
	      if [ ! -e BOOT.FILES${TYPE_SYSTEM} ] ; then echo "exists" ; fi ; \
	      ${TAR} cf - -C ../uC-src/bin ${TARX} . | ${TAR} xf - -C root/bin ; \
	      if [ -d ../brecis/bin ]; then ${TAR} cf - -C ../brecis/bin ${TARX} . | ${TAR} xf - -C root/bin; fi ; \
	      ${TAR} cf - ${TARX} -C ../uC-src/sbin . | ${TAR} xf - -C root/sbin ; \
	      if [ -d ../uC-src/share ]; then mkdir root/share; ${TAR} cf - -C ../uC-src/share ${TARX} . | ${TAR} xf - -C root/share; fi ; \
	      cp -p ../brecis/secutil/sectest root/bin/sectest ; \
	      cp -p ../brecis/secutil/secutil root/bin/secutil ; \
	      if [ -d ../uC-src/etc/pcmcia ]; then mkdir -p root/etc/pcmcia; \
		${TAR} cf - -C ../uC-src/etc/pcmcia ${TARX} . | ${TAR} xf - -C root/etc/pcmcia; \
              fi ; \
	    fi ; \
	  fi

	# add by hook 20030711 for freeswan ipsec  
	if [ "${TYPE_SYSTEM}" = ".ids" ];\
	then \
		(cd ../uC-src/real/freeswan && sh freeswan_install.sh) ; \
    fi

	if [ "${TYPE_SYSTEM}" = ".freeswan" \
		-o "${TYPE_SYSTEM}" = ".big"  \
		-o "${TYPE_SYSTEM}" = ".fpga" ] ; \
	then \
		(cd ../uC-src/real/freeswan && sh freeswan_install.sh) ; \
		rm -f root/bin/sh ; \
		if [ "${TYPE_SYSTEM}" = ".big" ] ; then \
			ln -sf bash root/bin/sh ;\
		else \
			ln -sf newbusybox root/bin/sh ;\
		fi ;\
		rm -f root/bin/logger ; \
		ln -s newbusybox root/bin/logger ;\
		rm -f root/bin/egrep ; \
		ln -s newbusybox root/bin/egrep ;\
		rm -f root/bin/ls ; \
		ln -s newbusybox root/bin/ls ;\
		rm -f root/bin/sed ; \
		ln -s newbusybox root/bin/sed ;\
		rm -f root/bin/tr ; \
		ln -s newbusybox root/bin/tr ;\
	fi
#	if [ -x ../brecis/init/init ] ; then cp ../brecis/init/init root/sbin/init ; fi
#	if [ -x ../brecis/init/fdtest ] ; then cp ../brecis/init/fdtest root/bin/fdtest ; fi
	if [ -d ../brecis/lib/zsp ]; then ${TAR} cf - -C ../brecis/lib ${TARX} zsp | ${TAR} xf - -C root/lib; fi;
#-----------------------------------------------------------------------------

UTS_RELEASE := $(shell grep UTS_RELEASE ../linux/include/linux/version.h | awk ' { print $$3 } ')

root.add2${NFS}: root.add1${NFS}${TYPE_SYSTEM}
	-(cd ../linux ; INSTALL_MOD_PATH=`pwd`/../romdisk/root ${MAKE} modules_install)
	-for i in modules.dep modules.generic_string \
		modules.ieee1394map modules.isapnpmap modules.parportmap \
		modules.pcimap modules.usbmap modules.pnpbiosmap ; \
	do \
		rm -f root/lib/modules/${UTS_RELEASE}/$$i ; \
		cp -f modules${TYPE_SYSTEM}/$$i root/lib/modules/${UTS_RELEASE}/ ; \
	done
#		ln -sf /var/$$i root/lib/modules/${UTS_RELEASE}/$$i ; \
#	done

# fix problem with modules that were output by the assembler, not the
# linker, by running them all through ld -r

	find root/lib/modules -name \*.o -type f -print | \
	while read pth ; do \
		nm=`basename $$pth .o` ; dir=`dirname $$pth` ;\
		tmpnam=$${dir}/$${nm}_tmp.o ; \
		mv $$pth $$tmpnam ; \
		mips-ld -r -o $$pth $$tmpnam ; \
		rm -f $$tmpnam ; \
	done

#ensure busybox links are installed if nothing else is.
	if [ -f root/bin/newbusybox ] ; then \
		for i in `sort ../uC-src/real/busybox/busybox.links | uniq` ; do \
			appdir=`dirname $$i`; \
			cmd=`basename $$i` ; \
			if [ "$$appdir" = "/sbin" ] ; then \
				[ -f root/sbin/$$cmd ] || ln -sf ../bin/newbusybox root/sbin/$$cmd ; \
			else \
				[ -f root/bin/$$cmd ] || ln -sf newbusybox root/bin/$$cmd ; \
			fi ; \
		done ; \
	fi

# and do the same for old busybox 
#  note -- older busybox did not discriminate between bin and sbin, so
#  only link if neither exists.
	if [ -f root/bin/busybox ] ; then \
		for i in `sort ../uC-src/busybox/busybox.links | uniq` ; do \
			[ -f root/bin/$$i ] || [ -f root/sbin/$$i ] || \
				ln  root/bin/busybox root/bin/$$i ; \
		done ; \
	fi


#-----------------------------------------------------------------------------
# Only copy these specific files, no symbolic links for "nfs".
#
# Bash dislikes "for j in ;" which results from an empty DONOTCOPY.
# But it handles "for j in $emptyvar ;" just fine.  Thus, I hope nobody
# ever defines EmPtYvAr in their environment...

# line 164, change by hook 20030602
# copy uC-src/bin & sbin to romdisk include some symblic-links
#         cp ../uC-src/$$i root/$$i ; \

root.add1${TYPE_SYSTEM}:: root.add0
	if [ "${TYPE_SYSTEM}" != "" -a -e BOOT.FILES${TYPE_SYSTEM} ]; then \
	    for i in `cat BOOT.FILES${TYPE_SYSTEM}` ; do \
		k=1; \
		for j in $$EmPtYvAr ${DONOTCOPY} ; do \
		  if \[ "$$i" = "$$j" ] ; then k=0; fi; \
		done; \
		if [ "$$k" = "1" ]; then \
		  cp -f -d ../uC-src/$$i root/$$i ; \
		fi; \
	    done ; \
	fi
#-----------------------------------------------------------------------------
# After adding a device node to MAKEDEV, here is how you generate a new
# romdisk.tgz:
update_devs:
	${MAKE} clean
	${MAKE} root
	cp MAKEDEV root/dev/MAKEDEV
	(cd root/dev ; ./MAKEDEV)
	${MAKE} new
#-----------------------------------------------------------------------------
clean::
	rm -f romdisk.img
	rm -rf root
	rm -f ${SYMBOLIC_LINKS}
