###############################################################################
# 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 4114 2010-01-13 21:24:27Z owes $
#                                                                             #
###############################################################################

###############################################################################
# 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)

	# Change ownership of directories already created by make.sh
	# use 0:0 as etc is not yet installed
	chown 0:0 /usr /usr/src
	# Create directories not already made by make.sh
	-mkdir -p /{boot,etc,home,lib/firmware,mnt}
	-mkdir -p /{root,sbin,tmp,usr/local,var}
	-for dirname in /usr /usr/local; do \
		mkdir -p $$dirname/{bin,include,lib,sbin,share}; \
		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
	# bash links are made inside make.sh as we can't reach that point without
	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,cache,lib/misc,local,empty}

	# Permissions
	chmod 0750 /root
	chmod 1777 /tmp /var/tmp
	chmod 0711 /var/empty
	
	# Config files
	cp -fR $(DIR_SRC)/config/etc/* /etc
	chown -R 0:0 /etc
	chmod -R 644 /etc
	find /etc -type d -exec chmod 755 {} \;
	# HDD logrotate will be default
	ln -sf /etc/logrotate.conf.HDD /etc/logrotate.conf

	# Use mtab as a file not a symlink to /proc/mounts
	rm -f /etc/mtab
	touch /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

	# fix setuid programs in case of direct toolchain build
	# to workaround mount 'only root can do that' error even when run as root
	# tar remove setuid bit
	chmod u-s /$(TOOLS_DIR)/bin/{u,}mount

	# 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

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