include ../Config.mk

CHECK_CDL_DIR	= ../libpp_cd/src/check_cdl
CHECK_CDL	= $(CHECK_CDL_DIR)/check_cdl

all: install

$(CHECK_CDL):
	make -C $(CHECK_CDL_DIR)

# get OEM/product specific default values
product_cdl:
	if [ -e "$(OEMDIR)/product.cdl" ]; then \
		cp $(OEMDIR)/product.cdl ./etc; \
	else \
		cp ./product_default.cdl ./etc/product.cdl; \
	fi

$(DESTDIR)/etc/code.cdl: etc/base.cdl product_cdl $(FW_TOPDIR)/include/pp/features.h
	install -d $(DESTDIR)/etc
	filepp -imacros $(FW_TOPDIR)/include/pp/features.h \
		$(WEBS_FILEPP_FLAGS) etc/base.cdl > $(DESTDIR)/etc/code.cdl

$(DESTDIR)/etc/types_n_configs.cdl: etc/types_n_configs.cdl $(FW_TOPDIR)/include/pp/features.h
	install -d $(DESTDIR)/etc
	filepp -imacros $(FW_TOPDIR)/include/pp/features.h \
		$(WEBS_FILEPP_FLAGS) etc/types_n_configs.cdl > $(DESTDIR)/etc/types_n_configs.cdl


ifeq ($(PP_FW_TYPE),production)
cdl_checked:
else
cdl_checked: $(DESTDIR)/etc/code.cdl $(DESTDIR)/etc/types_n_configs.cdl $(CHECK_CDL)
	make -C $(CHECK_CDL_DIR)
	$(CHECK_CDL) $(FW_TOPDIR) install_root/etc/code.cdl include/pp/xdefs.h eric/src/xdefs.c
	touch cdl_checked;
endif

install: cdl_checked $(DESTDIR)/etc/code.cdl $(DESTDIR)/etc/types_n_configs.cdl
# do not copy product / feature specific files to fs-skeleton but to DESTDIR!

# destdir is already created by build_fw.pl!
#	install -d $(DESTDIR)
	tar  -c --exclude-from=EXCLUDES -C common -f - . | ( cd $(DESTDIR); tar xmf -)
        
ifeq ($(PP_FW_TYPE),production)
	install -d $(DESTDIR)/root/firmware
endif
# must be installed unconditional, because it is always used
# at least for final builds
ifneq ($(PP_FEAT_JFFS2_ROOTFS), 1)
	install -d $(DESTDIR)/dev
	install -d $(DESTDIR)/flashdisk
	install -d $(DESTDIR)/var/log
	install -d $(DESTDIR)/var/lock
	install -d $(DESTDIR)/var/nfs
	install -d $(DESTDIR)/var/run
endif

ifneq ($(PP_FEAT_JFFS2_ROOTFS), 1)
	cp -af bin/flash_config.sh $(DESTDIR)/bin
endif
ifeq ($(PP_FW_TYPE),production)
	cp -af bin/flash_firmware.production $(DESTDIR)/bin/flash_firmware.sh
	cp -af bin/flash_firmware_kx101.production $(DESTDIR)/bin/flash_firmware_kx101.sh
endif
	cp -af bin/fldirupdate.sh $(DESTDIR)/bin
ifeq ($(PP_FEAT_JFFS2_ROOTFS), 1)
	cp -af bin/fwupdate.sh $(DESTDIR)/bin
	cp -af bin/fwupdsys.sh $(DESTDIR)/bin
endif
ifeq ($(PP_FW_TYPE),production)
	cp -af bin/oem_switch.production $(DESTDIR)/bin/oem_switch.sh
endif
# bin.debug only for !final FW
ifneq ($(PP_BUILD_TYPE),final)
	cp -af bin.debug $(DESTDIR)
endif

# install correct subset of topologies
ifeq ($(PP_FEAT_IPMI_SERVER),1)
	install -d $(DESTDIR)/etc/bmc
ifeq ($(PP_PRODUCT),smidc)
	# install supermicro specific topologies
	cp -f etc/bmc/supermicro*.top $(DESTDIR)/etc/bmc/
else
ifeq ($(PP_PRODUCT),amddc)
	# install AMD/OPMA specific topologies
	cp -f etc/bmc/AMD_*.top $(DESTDIR)/etc/bmc/
	cp -f etc/bmc/OPMA_*.top $(DESTDIR)/etc/bmc/
	cp -f etc/bmc/NVIDIA_Reference*.top $(DESTDIR)/etc/bmc/
else
ifeq ($(PP_PRODUCT),msidc)
	# install MSI specific topologies
	cp -f etc/bmc/MSI_*.top $(DESTDIR)/etc/bmc/
else
ifeq ($(PP_PRODUCT),pdu)
	cp -f etc/bmc/rpc_*.top $(DESTDIR)/etc/bmc/
	cp -f etc/bmc/rpc_*.inc $(DESTDIR)/etc/bmc/
	./gen_rpc_recep_topo.sh etc/bmc/rpc_rXX.tpl 20 $(DESTDIR)/etc/bmc/
else
	# default case, install everything, convenience only
	cp -f etc/bmc/*.top $(DESTDIR)/etc/bmc/
endif
endif
endif
endif
endif

ifeq ($(PP_BUILD_TYPE),final)
ifeq ($(PP_FEAT_JFFS2_ROOTFS), 1)
	cp -af etc/fstab.final.jffs $(DESTDIR)/etc/fstab
else
	cp -af etc/fstab.final.ramfs $(DESTDIR)/etc/fstab
endif
else
ifeq ($(PP_FEAT_JFFS2_ROOTFS), 1)
	cp -af etc/fstab.devel.jffs $(DESTDIR)/etc/fstab
else
	cp -af etc/fstab.devel.ramfs $(DESTDIR)/etc/fstab
endif
endif
ifeq ($(PP_BUILD_TYPE),final)
	cp -af etc/initscript.final $(DESTDIR)/etc/initscript
else
	cp -af etc/initscript.devel $(DESTDIR)/etc/initscript
endif
ifeq ($(PP_FW_TYPE),production)
	cp -af etc/inittab.production $(DESTDIR)/etc/inittab
else
ifeq ($(PP_BUILD_TYPE),final)
	cp -af $(OEMDIR)/inittab.final $(DESTDIR)/etc/inittab
else
	cp -af $(OEMDIR)/inittab.devel $(DESTDIR)/etc/inittab
endif
endif
ifeq ($(PP_FW_TYPE),production)
	cp -af etc/issue.production $(DESTDIR)/etc/issue
else
	if [ -f etc/issue.$(PP_PRODUCT) ]; then \
	    cp -af etc/issue.$(PP_PRODUCT) $(DESTDIR)/etc/issue; \
	fi # else we use default issue from common
endif
ifeq ($(PP_FW_TYPE),production)
	cp -af etc/passwd.production $(DESTDIR)/etc/passwd
else
ifeq ($(PP_BUILD_TYPE),final)
	cp -af etc/passwd.final $(DESTDIR)/etc/passwd
else
	cp -af etc/passwd.devel $(DESTDIR)/etc/passwd
endif
endif
# Temporary HACK for ICP
ifeq ($(PP_PRODUCT),icpmmd)
ifeq ($(PP_BUILD_TYPE),final)
	cp -af $(OEMDIR)/passwd.final $(DESTDIR)/etc/passwd
endif
endif
# Temporary HACK for Intel RMM2
ifeq ($(PP_PRODUCT),asmidc)
ifeq ($(PP_OEM),intel)
ifeq ($(PP_BUILD_TYPE),final)
	cp -af $(OEMDIR)/passwd.final $(DESTDIR)/etc/passwd
endif
endif
endif
ifeq ($(PP_SUBBOARD),kimamdg4)
	cp -af bin/bond_info.sh $(DESTDIR)/bin/
endif
ifeq ($(PP_FW_TYPE),production)
	cp -af etc/rc.sysinit.production $(DESTDIR)/etc/rc.sysinit
else
 ifeq ($(PP_FEAT_JFFS2_ROOTFS), 1)
	cp -af etc/rc.sysinit.jffs $(DESTDIR)/etc/rc.sysinit
 else
	cp -af etc/rc.sysinit.ramfs $(DESTDIR)/etc/rc.sysinit

  ifeq ($(PP_BUILD_TYPE),final)
	cp -af var/log/messages $(DESTDIR)/var/log
	cp -af var/run/utmp $(DESTDIR)/var/run
  endif
 endif
endif
ifeq ($(PP_FEAT_LOCAL_VIDEO_X_CONSOLE), 1)
	cp -arf etc/X11 $(DESTDIR)/etc
endif

# copy stuff to devel/final root directory
# link some stuff
	rm -f $(DESTDIR)/sbin
	ln -s bin $(DESTDIR)/sbin
	rm -f $(DESTDIR)/usr/bin
	ln -s ../bin $(DESTDIR)/usr/bin
	rm -f $(DESTDIR)/usr/sbin
	ln -s ../sbin $(DESTDIR)/usr/sbin
# post-setup devel/final stuff
ifneq ($(PP_FEAT_JFFS2_ROOTFS), 1)
ifeq ($(PP_BUILD_TYPE),final)
	rm -f $(DESTDIR)/var/log/mgetty.log.ttyS0
	ln -s /dev/null $(DESTDIR)/var/log/mgetty.log.ttyS0
	rm -f $(DESTDIR)/var/log/wtmp
	ln -s /dev/null $(DESTDIR)/var/log/wtmp
	rm -f $(DESTDIR)/var/log/snmpd.log
	ln -s /dev/null $(DESTDIR)/var/log/snmpd.log
endif
endif

clean:
	rm -f etc/product.cdl cdl_checked
	make -C $(CHECK_CDL_DIR) clean
ifeq ($(PP_FW_TYPE),production)
	rm bin/flash_firmware.sh bin/oem_switch.sh
endif
