###############################################################################
# This file is part of the IPCop Firewall.                                    #
#                                                                             #
# IPCop 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.                                         #
#                                                                             #
# IPCop 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 IPCop; if not, write to the Free Software                        #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA    #
#                                                                             #
# Makefiles are based on LFSMake, which is                                    #
# Copyright (C) 2002 Rod Roard <rod@sunsetsystems.com>                        #
#                                                                             #
# Modifications by:                                                           #
# ??-12-2003 Mark Wormgoor < mark@wormgoor.com>                               #
#          - Modified Makefile for IPCop build                                #
#                                                                             #
# $Id: stage2 2450 2009-02-09 01:47:46Z gespinasse $
#                                                                             #
###############################################################################

###############################################################################
# Definitions
###############################################################################

include Config

PKG_NAME   = stage2
HOST_ARCH  = all
OTHER_SRC  = no

THISAPP    = stage2
TARGET     = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)

###############################################################################
# Top-level Rules
###############################################################################

install : $(TARGET)

check :

download :

md5 :

###############################################################################
# Installation Details
###############################################################################

$(TARGET) : $(firstword $(MAKEFILE_LIST))
	@$(PREBUILD)

	# Create directories
	-mkdir -p /{bin,boot,dev,home,lib,lib/firmware,mnt,proc} 
	-mkdir -p /{root,sbin,sys,tmp,usr/local,var,opt}
	-for dirname in /usr /usr/local; do \
		mkdir -p $$dirname/{bin,include,lib,sbin,share,src}; \
		ln -sfn share/{man,doc,info} $$dirname; \
		mkdir -p $$dirname/share/{dict,doc,info,locale,man}; \
		mkdir -p $$dirname/share/{nls,misc,terminfo,zoneinfo}; \
		mkdir -p $$dirname/share/man/man{1,2,3,4,5,6,7,8}; \
	done

	# Check for some important links and create them if they don't exist
	if [ ! -e /bin/bash ]; then ln -sfn /$(TOOLS_DIR)/bin/bash /bin/bash && ln -sf bash /bin/sh; fi
	if [ ! -e /bin/cat ]; then ln -sfn /$(TOOLS_DIR)/bin/cat /bin/cat; fi
	if [ ! -e /bin/pwd ]; then ln -sfn /$(TOOLS_DIR)/bin/pwd /bin/pwd; fi
	if [ ! -e /bin/stty ]; then ln -sfn /$(TOOLS_DIR)/bin/stty /bin/stty; fi
	if [ ! -e /lib/libgcc_s.so.1 ]; then \
		ln -sfn /$(TOOLS_DIR)/lib/libgcc_s.so.1 /lib/libgcc_s.so.1 && \
		ln -sfn libgcc_s.so.1 /lib/libgcc_s.so; \
	fi

	-mkdir -p /var/{lock/subsys,log,mail,run,spool}
	-mkdir -p /var/{tmp,opt,cache,lib/misc,local,empty}

	# This trick is necessary in order to create two devices in the target /dev, rather than in the host /dev
	mkdir -p /dev1
	mount --move /dev /dev1

	# Make /dev/null and /dev/console
	cd /dev && rm -f null console
	cd /dev && mknod -m 0666 null c 1 3
	cd /dev && mknod -m 0600 console c 5 1

	# Now move the target /dev to point back to the host /dev
	mount --move /dev1 /dev
	rm -fr /dev1
	
	# Permissions
	chmod 0750 /root
	chmod 1777 /tmp /var/tmp
	chmod 0711 /var/empty
	
	# Config files
	for i in $(DIR_SRC)/config/etc/*; do \
		[ -f $$i ] && install -m 0644 --owner=0 --group=0 $$i /etc; \
	done
	# HDD logrotate will be default
	ln -sf /etc/logrotate.conf.HDD /etc/logrotate.conf

	# Everyone says /etc/mtab -> /proc/mounts is better :-)
	ln -sf /proc/mounts /etc/mtab

	# Enable the serial line console for sparcs
ifeq "$(MACHINE)" "sparc"
	sed -i "s,^#7,7,g" /etc/inittab
endif

	echo "$(NAME) v$(VERSION) for $(MACHINE) - $(SLOGAN) (\l)" > /etc/issue

	# Nobody user
	-mkdir -p /home/nobody
	chown -R nobody:nobody /home/nobody

	# Patches directory
	-mkdir -m 1775 -p /var/patches
	chown -R root:nobody /var/patches

	# we need our diff helper before POSTBUILD
	cp $(DIR_SRC)/src/scripts/install-diff.pl /usr/local/bin
	@$(POSTBUILD)
