### this file is sourced not run
PKGVER=1.0
PKGBUILD=1

### this is a dummy package to adjust LFS/CLFS toolchain
### not needed if you're not using LFS to build this (e.g. aboriginal's) 

### override download for LFS sources
# $1-src-dir $2-tarball $3-download URL
pkg_download() {
	: nothing to download
}

###
pkg_prepare() {
	: nothing to prepare		
}

###
pkg_package() {
	: nothing to package
}

###
pkg_build() {
	# adjust ld - LFS did this, CLFS doesn't
	#mv -v /tools/bin/ld /tools/bin/ld-old
	#mv -v /tools/$(gcc -dumpmachine)/bin/ld /tools/$(gcc -dumpmachine)/bin/ld-old
	#mv -v /tools/bin/ld-new /tools/bin/ld
	#ln -sf /tools/bin/ld /tools/$(gcc -dumpmachine)/bin/ld

	# LFS way of adjusting gcc 
	#gcc -dumpspecs | sed -e 's@/tools@@g'               \
    #-e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
    #-e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' >      \
    #$(dirname $(gcc --print-libgcc-file-name))/specs
	
	# adjust gcc (CLFS 3.0 way - LFS uses sed instead of perl)
	gcc -dumpspecs | \
	perl -p -e 's@/tools/lib/ld@/lib/ld@g;' \
		 -e 's@/tools/lib64/ld@/lib64/ld@g;' \
		 -e 's@\*startfile_prefix_spec:\n@$_/usr/lib/ @g;' > \
		 $(dirname $(gcc --print-libgcc-file-name))/specs	

    # tests - compile new program
    cd /tmp
	echo '#include <stdio.h>
int main(){ printf("it works !\n"); return 0; }' > dummy.c

	# 32-bit test
	echo 32-bit test
	gcc ${BUILD32} dummy.c -v -Wl,--verbose &> dummy.log
	
	# please read LFS for details of expected output
	readelf -l a.out | grep ': /lib'
	grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log
	grep -B1 '^ /usr/include' dummy.log
	grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'
	grep "/lib.*/libc.so.6 " dummy.log
	grep found dummy.log
	
	# finally try to run the program itself
	./a.out
	
	# 64-bit test
	echo 64-bit test
	rm a.out dummy.log
	gcc ${BUILD64} dummy.c -v -Wl,--verbose &> dummy.log
	
	# please read LFS for details of expected output
	readelf -l a.out | grep ': /lib'
	grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log
	grep -B1 '^ /usr/include' dummy.log
	grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'
	grep "/lib.*/libc.so.6 " dummy.log
	grep found dummy.log

	# finally try to run the program itself
	./a.out

	# sh # drop to shell to fix and test
} 
