#!/bin/bash
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/scripts/Build-Tools
# ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
# 
# This program 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. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

config=default
stagelevel=9
cleanup=0

while [ "$1" ] ; do
    case "$1" in
	-cleanup)  cleanup=1 ; shift ;;
	-cfg)      config=$2 ; shift ; shift ;;
	-[0-9])    options="$options $1" ; stagelevel=${1#-} ; shift ;;
	*)         echo "Usage: $0 [ -cfg config ] [ -0 | -9 ] \\"
	           echo "       ${0//?/ } [ -cleanup ]" ; exit 1 ;;
    esac
done

. ./scripts/parse-config
. ./scripts/functions

var_remove PATH ":" $base/build/$ROCKCFG_ID/$toolsdir/bin
var_remove PATH ":" $base/build/$ROCKCFG_ID/$toolsdir/wrapper

mkdir -p build/$ROCKCFG_ID/$toolsdir/bin
mkdir -p build/$ROCKCFG_ID/$toolsdir/wrapper
mkdir -p build/$ROCKCFG_ID/$toolsdir/lib

if [ $toolsdir = tools.cross ]; then
	mkdir -p build/$ROCKCFG_ID/$toolsdir/{var/adm,etc,share,include}
	[ -L build/$ROCKCFG_ID/$toolsdir/usr ] ||
		ln -s . build/$ROCKCFG_ID/$toolsdir/usr
	[ -L build/$ROCKCFG_ID/$toolsdir/sbin ] ||
		ln -s bin build/$ROCKCFG_ID/$toolsdir/sbin
	[ -L build/$ROCKCFG_ID/$toolsdir/var/adm/logs ] ||
		ln -s ../../../root/var/adm/logs \
		      build/$ROCKCFG_ID/$toolsdir/var/adm/logs
	mkdir -p build/$ROCKCFG_ID/root/var/adm/logs
fi

x="$( bash scripts/xfind.sh config/$config/. package/. scripts/. \
	misc/. architecture/. target/. ! -name '*.cache' \
	-newer build/$ROCKCFG_ID/$toolsdir/.lastupdate \
	-printf 'Found new/updated file: %p\n' 2> /dev/null )"

if [ "$x" ] ; then
	echo_header Found $( echo "$x" | wc -l ) new/updated \
	            files in source tree:
	echo "$x" | head -3 | sed 's,/\./,/,g' |
		while read y ; do echo_status $y ; done
	[ "$( echo "$x" | tail +4 )" ] &&
	    echo_status "** list truncated to save space on the terminal **"

	if [ "$ROCK_DEBUG_TOOLS_SKIP" = 1 ]; then
		echo_error "Found \$ROCK_DEBUG_TOOLS_SKIP ..."
	else
		rm -f build/$ROCKCFG_ID/$toolsdir/.lastupdate

		if echo "$x" | grep -qv " config/$config/" ; then
			echo_header Re-running configuration:
			while read line ; do
				echo_status $line
			done < <( ./scripts/Config -cfg $config -oldconfig )
		fi
	fi
fi

if [ $cleanup = 1 ] ; then
	echo_header "Removing old stuff in $toolsdir/ directory ..."

	rm -f build/$ROCKCFG_ID/$toolsdir/wrapper/*

	rm -f build/$ROCKCFG_ID/$toolsdir/bin/getdu
	rm -f build/$ROCKCFG_ID/$toolsdir/bin/getfiles
	rm -f build/$ROCKCFG_ID/$toolsdir/bin/fl_wrparse
	rm -f build/$ROCKCFG_ID/$toolsdir/bin/fl_stparse

	rm -f build/$ROCKCFG_ID/$toolsdir/lib/fl_wrapper.so
	rm -f build/$ROCKCFG_ID/$toolsdir/lib/parse-config
	rm -f build/$ROCKCFG_ID/$toolsdir/.lastupdate
fi

if [ ! -f build/$ROCKCFG_ID/$toolsdir/.lastupdate ] ; then

	echo_header "Building tools in build/.../$toolsdir:"

	# CC, CXX, F77 and KCC Wrapper
	#
	wrappers="cmd_wrapper" ; wrappervars=""
	echo_status "Building $toolsdir/wrapper/cmd_wrapper."
	$BUILDCC -Wall -O2 misc/tools-source/cmd_wrapper.c \
		  -DENVPREFIX='"CMD"' -DMYNAME='"cmd_wrapper"' \
	          -o build/$ROCKCFG_ID/$toolsdir/wrapper/tmp.$$ || exit 1
	mv build/$ROCKCFG_ID/$toolsdir/wrapper/tmp.$$ \
	   build/$ROCKCFG_ID/$toolsdir/wrapper/cmd_wrapper
	#
	build_wrapper() {
	    local x="$( echo ${1//,/ } )" y z
	    wrappervars="$wrappervars $x"
	    while shift ; [ $# != 0 ]
	    do
		wrappers="$wrappers $1"
		tmp=`mktemp -p build/$ROCKCFG_ID/$toolsdir/wrapper`
		{
			echo '#!/bin/bash'

			for y in $x ; do
				echo -n "[ \"\$${y}_WRAPPER_DEBUG\" = 1 ] && "
				echo "export CMD_WRAPPER_DEBUG=1"
			done

			for y in $x ; do
				echo -n "[ \"\$${y}_WRAPPER_BYPASS\" = 1 ] && "
				echo "export CMD_WRAPPER_BYPASS=1"
			done

			z=""; echo -n 'export CMD_WRAPPER_APPEND_PO="'
			for y in $x; do
				echo -n "${z}\$${y}_WRAPPER_APPEND_PO"; z=" "
			done ; echo '"'

			z=""; echo -n 'export CMD_WRAPPER_OTHERS="'
			for y in $x; do
				echo -n "${z}\$${y}_WRAPPER_OTHERS"; z=":"
			done ; echo '"'

			z=""; echo -n 'export CMD_WRAPPER_INSERT="'
			for y in $x; do
				echo -n "${z}\$${y}_WRAPPER_INSERT"; z=" "
			done ; echo '"'

			z=""; echo -n 'export CMD_WRAPPER_REMOVE="'
			for y in $x; do
				echo -n "${z}\$${y}_WRAPPER_REMOVE"; z=" "
			done ; echo '"'

			z=""; echo -n 'export CMD_WRAPPER_APPEND="'
			for y in $x; do
				echo -n "${z}\$${y}_WRAPPER_APPEND"; z=" "
			done ; echo '"'

			z=""; echo -n 'export CMD_WRAPPER_FILTER="'
			for y in $x; do
				echo -n "${z}\$${y}_WRAPPER_FILTER"; z="|"
			done ; echo '"'

			echo 'exec cmd_wrapper $( basename $0 ) "$@"'
		} > $tmp
		chmod +rx $tmp
		mv $tmp "build/$ROCKCFG_ID/$toolsdir/wrapper/$1"
	    done
	}
	#
	while read script ; do . $script
	done < <( bash scripts/xfind.sh package/. target/$target/. misc/. -name 'wrappers.in' )
	#
	echo_status "Building $toolsdir/wrapper/strip_wrapper."
	cat << 'EOT' > build/$ROCKCFG_ID/$toolsdir/wrapper/strip_wrapper.$$
#!/bin/sh
if [ "$STRIP_WRAPPER_NOLOOP" = 1 ] ; then
	echo "Aaaaaeik! Strip wrapper is looping!" >&2
	exit 1
fi
if [ "$ROCKCFG_OPT" = 0 ] ; then
	PATH=${PATH//$STRIP_WRAPPER_MYPATH:/}
	export STRIP_WRAPPER_NOLOOP=1
	exec "`basename $0`" "$@"
fi
exit 0
EOT
	chmod +x build/$ROCKCFG_ID/$toolsdir/wrapper/strip_wrapper.$$
	mv build/$ROCKCFG_ID/$toolsdir/wrapper/strip_wrapper.$$ \
	   build/$ROCKCFG_ID/$toolsdir/wrapper/strip_wrapper
	wrappers="$wrappers strip_wrapper"
	#
	for x in strip $arch_target-strip ; do
		[ $crossnative = cross -a $x = strip ] && continue
		if [ -x "`type -p $x`" ] ; then
			ln -sf strip_wrapper \
				build/$ROCKCFG_ID/$toolsdir/wrapper/$x
			wrappers="$wrappers $x"
		fi
	done
	#
	echo_status "Building $toolsdir/wrapper/install_wrapper."
	cp misc/tools-source/install_wrapper.sh build/$ROCKCFG_ID/$toolsdir/wrapper/install_wrapper
	chmod +x build/$ROCKCFG_ID/$toolsdir/wrapper/install_wrapper
	ln -sf install_wrapper build/$ROCKCFG_ID/$toolsdir/wrapper/install
	ln -sf install_wrapper build/$ROCKCFG_ID/$toolsdir/wrapper/cp
	ln -sf install_wrapper build/$ROCKCFG_ID/$toolsdir/wrapper/ln
	ln -sf install_wrapper build/$ROCKCFG_ID/$toolsdir/wrapper/mv
	wrappers="$wrappers install_wrapper install cp ln mv"
	#
	for x in $( cd build/$ROCKCFG_ID/$toolsdir/wrapper/ ; echo * )
	do
		z=0
		for y in $wrappers ; do
			[ "$x" = "$y" ] && z=1
		done
		if [ "$z" = 0 ] ; then
			rm -f build/$ROCKCFG_ID/$toolsdir/wrapper/$x
		fi
	done

	# Various small tools
	#
	for x in getdu getfiles fl_wrparse fl_stparse ; do
		echo_status "Building $toolsdir/bin/$x."
		$BUILDCC -Wall -O2 misc/tools-source/$x.c \
		          -o build/$ROCKCFG_ID/$toolsdir/bin/$x.$$ || exit 1
		mv build/$ROCKCFG_ID/$toolsdir/bin/$x.$$ \
		   build/$ROCKCFG_ID/$toolsdir/bin/$x
	done

	# Flist Wrapper Library
	#
	if [ "$ROCKCFG_FLIST" = "flwrapper" ] ; then
		echo_status "Building $toolsdir/lib/fl_wrapper.so."
		$BUILDCC -Wall -O2 -ldl -shared misc/tools-source/fl_wrapper.c \
	        	  -o build/$ROCKCFG_ID/$toolsdir/lib/fl_wrapper.so.$$ || exit 1
		mv build/$ROCKCFG_ID/$toolsdir/lib/fl_wrapper.so.$$ \
		   build/$ROCKCFG_ID/$toolsdir/lib/fl_wrapper.so

		echo_status  "Testing $toolsdir/lib/fl_wrapper.so."
		if ! sh -c "LD_PRELOAD=\"\$LD_PRELOAD:build/\$ROCKCFG_ID/`
			`$toolsdir/lib/fl_wrapper.so\" date ; `
			`exit $?" >/dev/null 2>&1
		then
			echo_error "Detected a problem with the flist" \
			           "wrapper lib on your system."
			echo_error "Please specify another flist-creation" \
			           "system in your build configuration."
			rm -f build/$ROCKCFG_ID/$toolsdir/lib/fl_wrapper.so
			echo ; exit 1
		fi
	fi

	# Create Parse-Config Collection
	#
	echo_status "Creating $toolsdir/lib/parse-config."
	bash scripts/xfind.sh \
	     architecture/$arch/. package/. target/$target/. misc/ -type f \
	     '(' -name 'parse-config' -o -name 'parse-config-*' ')' \
	     '(' ! -name 'parse-config-x' -o ! -name 'parse-config-*-x' ')' \
	     -printf '%f\t%p\n' | sort | awk '{ $1="."; print; }' \
			> build/$ROCKCFG_ID/$toolsdir/lib/parse-config.$$
	mv build/$ROCKCFG_ID/$toolsdir/lib/parse-config.$$ \
	   build/$ROCKCFG_ID/$toolsdir/lib/parse-config

	# Create pkg_*_(pre_post).conf files
	#

	rm -f build/$ROCKCFG_ID/$toolsdir/lib/pkg_*_{pre,post}.conf
	echo_status "Creating $toolsdir/lib/pkg_*_{pre,post}.conf"
	for x in `grep "^X" config/$config/packages | cut -d ' ' -f 4,5 | tr ' ' '/'`; do
	   for y in  `echo package/$x/pkg_*_{pre,post}.conf`; do
	      if [ -f $y ]; then
		 echo " . $y" >> build/$ROCKCFG_ID/$toolsdir/lib/${y##*/}
	      fi
	   done
	done

	echo -e "${wrappervars// /\n}" | grep . | sort -u |
	while read x ; do
		echo "export ${x}_WRAPPER_DEBUG=0	${x}_WRAPPER_BYPASS=0"
		echo "export ${x}_WRAPPER_OTHERS=''	${x}_WRAPPER_REMOVE=''"
		echo "export ${x}_WRAPPER_INSERT=''	${x}_WRAPPER_APPEND=''"
		echo "export ${x}_WRAPPER_FILTER=''	${x}_WRAPPER_APPEND_PO=''"
	done > build/$ROCKCFG_ID/$toolsdir/lib/wrapper-config.$$
	mv build/$ROCKCFG_ID/$toolsdir/lib/wrapper-config.$$ \
	   build/$ROCKCFG_ID/$toolsdir/lib/wrapper-config

	touch build/$ROCKCFG_ID/$toolsdir/.lastupdate
fi

exit 0