###############################################################################
# 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: coreutils 3160 2009-07-02 05:35:22Z gespinasse $
#                                                                             #
###############################################################################

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

include Config

PKG_NAME   = coreutils
VER        = 6.12
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = $(PKG_NAME)-$(VER)
DL_FILE    = $(THISAPP).tar.gz
DL_FROM    = $(URL_GNU)/coreutils
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)

PATCH1     = $(THISAPP)-old_build_kernel-1.patch
PATCH2     = $(THISAPP)-uname-1.patch

###############################################################################
# Top-level Rules
###############################################################################
objects =$(DL_FILE) \
	$(PATCH1) \
	$(PATCH2)

$(DL_FILE)			= $(DL_FROM)/$(DL_FILE)
$(PATCH1)			= $(URL_LFS)/$(PKG_NAME)/$(PATCH1)
$(PATCH2)			= http://svn.cross-lfs.org/svn/repos/patches/$(PKG_NAME)/$(PATCH2)

$(DL_FILE)_MD5			= 2ca9ac69823dbd567b905a9e9f53c4f6
$(PATCH1)_MD5			= 5e8622abe6c6d81901b910383c6fb611
$(PATCH2)_MD5			= d299f910668ab8650e8ecbb6cdc2d5b1

install : $(TARGET)

check : $(patsubst %,$(DIR_CHK)/%,$(objects))

download :$(patsubst %,$(DIR_DL)/%,$(objects))

md5 : $(subst %,%_MD5,$(objects))

###############################################################################
# Downloading, checking, md5sum
###############################################################################

$(patsubst %,$(DIR_CHK)/%,$(objects)) :
	@$(CHECK)

$(patsubst %,$(DIR_DL)/%,$(objects)) :
	@$(LOAD)

$(subst %,%_MD5,$(objects)) :
	@$(MD5)

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

$(TARGET) : $(firstword $(MAKEFILE_LIST)) $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar zxf $(DIR_DL)/$(DL_FILE)

ifeq "$(STAGE)" "toolchain"
	cd $(DIR_APP) && patch -Np1 -i $(DIR_DL)/$(PATCH1)
	cd $(DIR_APP) && ./configure --prefix=/$(TOOLS_DIR)
	cd $(DIR_APP) && make -j $(PARALLELISM)
	cd $(DIR_APP) && make install
	# su is not installed (because suid root)
	# install under a different name to run non-root tests later
	cd $(DIR_APP) && cp -v src/su /$(TOOLS_DIR)/bin/su-tools
endif

ifeq "$(STAGE)" "base"
	cd $(DIR_APP) && patch -Np1 -i $(DIR_DL)/$(PATCH1)
	cd $(DIR_APP) && patch -Np1 -i $(DIR_DL)/$(PATCH2)
	# hostname is used by perl tests a bit later
	cd $(DIR_APP) && ./configure	--prefix=/usr \
					--enable-install-program=hostname \
					--enable-no-install-program=kill,uptime

	cd $(DIR_APP) && make -j $(PARALLELISM)

ifeq "$(RUNNING_TEST)" "yes"
	-cd $(DIR_APP) && make NON_ROOT_USERNAME=nobody check-root &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
	# temporary during tests
	echo "dummy:x:1000:nobody" >> /etc/group
	cd $(DIR_APP) && chown -Rv nobody config.log {gnulib-tests,lib,src}/.deps  >>/dev/null
	# su-tools is not in our actual published 1.9.5 toolchain
	# remove the -f su-tools once toolchain package has been upgraded
	-[ -f /$(TOOLS_DIR)/bin/su-tools ] && cd $(DIR_APP) && su-tools nobody -s /bin/bash -c "make RUN_EXPENSIVE_TESTS=yes check" \
					 2>&1 >> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
	sed -i '/dummy/d' /etc/group
endif

	cd $(DIR_APP) && make install
	mv -v /usr/bin/{cat,chgrp,chmod,chown,cp,date,dd,df,echo} /bin
	mv -v /usr/bin/{false,hostname,ln,ls,mkdir,mknod,mv,pwd,readlink,rm} /bin
	mv -v /usr/bin/{rmdir,stty,sync,true,uname} /bin
	mv -v /usr/bin/chroot /usr/sbin
	mv -v /usr/bin/{head,sleep,nice} /bin
endif

	@rm -rf $(DIR_APP)
	@$(POSTBUILD)
