#!/bin/bash

echo -e "$L_START_TOOLCH"

echo -e "$L_SET_LINK"
cd $TOOLS_DIR

{

mv -v /tools/bin/{ld,ld-old}
mv -v /tools/$(gcc -dumpmachine)/bin/{ld,ld-old}
mv -v /tools/bin/{ld-new,ld}
ln -sv /tools/bin/ld /tools/$(gcc -dumpmachine)/bin/ld

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


echo -e "$L_TEST_SPR"

echo 'main(){}' > dummy.c
cc dummy.c -v -Wl,--verbose &> dummy.log
readelf -l a.out | grep ': /lib'

echo -e "$R IF is everything OK, you should see somethink like this$DEF"
echo -e "$G [Requesting program interpreter: /lib/ld-linux.so.2] for 32bit $DEF"
echo -e "$G [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2] for 64bit $DEF"
waitUser
#case $(uname -m) in
#    x86_64) compareFiles $SCRIPTS_DIR/linker.ch6.1.test64 $TOOLS_DIR/testlink.ch6.1 ||  ;;
#    i?86) compareFiles $SCRIPTS_DIR/linker.ch6.1.test $TOOLS_DIR/testlink.ch6.1 ;; 
#esac
grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log

echo -e "$R IF is everything OK, you should see somethink like this$DEF"
echo -e "$G /usr/lib/crt1.o succeeded $DEF"
echo -e "$G /usr/lib/crti.o succeeded $DEF"
echo -e "$G /usr/lib/crtn.o succeeded $DEF"
waitUser

grep -B1 '^ /usr/include' dummy.log
echo -e "$R IF is everything OK, you should see somethink like this$DEF"
echo -e "$G #include <...> search starts here: $DEF"
echo -e "$G /usr/include $DEF"
waitUser

grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'
echo -e "$R IF is everything OK, you should see somethink like this$DEF"
echo -e "$R and somethin similar with x86_64 $DEF"
echo -e "$G SEARCH_DIR("/tools/i686-pc-linux-gnu/lib") $DEF"
echo -e "$G SEARCH_DIR("/usr/lib") $DEF"
echo -e "$G SEARCH_DIR("/lib"); $DEF"
waitUser

grep "/lib.*/libc.so.6 " dummy.log
echo -e "$R IF is everything OK, you should see somethink like this$DEF"
echo -e "$R and somethin similar with x86_64 $DEF"
echo -e "$G attempt to open /lib/libc.so.6 succeeded $DEF"
waitUser

grep found dummy.log
echo -e "$R IF is everything OK, you should see somethink like this$DEF"
echo -e "$R and somethin similar with x86_64 $DEF"
echo -e "$G found ld-linux.so.2 at /lib/ld-linux.so.2 $DEF"
waitUser



} 2>&1 | tee $BUILD_DIR/LOG_LINKER_CH6.log

if [ $PIPESTATUS -ne 0 ]
	then
		echo -e "$L_FAIL_LINK"
		exit 1
fi  
echo -e "$L_RM_TFIL $G rm -v dummy.c a.out dummy.log $DEF"
rm -r $TOOLS_DIR/dummy.c $TOOLS_DIR/a.out $TOOLS_DIR/dummy.log
echo -e "$DEF $G $TOOLS_DIR/testlink.ch6.1 $TOOLS_DIR/pathtest.ch6.1 $TOOLS_DIR/corhead.ch6.1 $TOOLS_DIR/testlink.ch6.2 $TOOLS_DIR/libctest.ch6.1 $TOOLS_DIR/dynlink.ch6.1 $DEF $L_SET_KEEP"
cd $ROOT
