1for cmd in xargs file; do
2	if ! command -v $cmd > /dev/null 2>&1; then
3		echo "The command '$cmd' is required by the relocation script, please install it first. Abort!"
4		exit 1
5	fi
6done
7
8# fix dynamic loader paths in all ELF SDK binaries
9# allow symlinks to be accessed via the find command too
10native_sysroot=$($SUDO_EXEC cat $env_setup_script |grep 'OECORE_NATIVE_SYSROOT='|cut -d'=' -f2|tr -d '"')
11dl_path=$($SUDO_EXEC find $native_sysroot/lib/ -maxdepth 1 -name "ld-linux*")
12if [ "$dl_path" = "" ] ; then
13	echo "SDK could not be set up. Relocate script unable to find ld-linux.so. Abort!"
14	exit 1
15fi
16executable_files=$($SUDO_EXEC find $native_sysroot -type f \
17	\( -perm -0100 -o -perm -0010 -o -perm -0001 \) -printf "'%h/%f' ")
18if [ "x$executable_files" = "x" ]; then
19   echo "SDK relocate failed, could not get executalbe files"
20   exit 1
21fi
22
23tdir=`mktemp -d`
24if [ x$tdir = x ] ; then
25   echo "SDK relocate failed, could not create a temporary directory"
26   exit 1
27fi
28cat <<EOF >> $tdir/relocate_sdk.sh
29#!/bin/sh
30for py in python python2 python3
31do
32	PYTHON=\`which \${py} 2>/dev/null\`
33	if [ \$? -eq 0 ]; then
34		break;
35	fi
36done
37
38if [ x\${PYTHON} = "x"  ]; then
39	echo "SDK could not be relocated.  No python found."
40	exit 1
41fi
42\${PYTHON} ${env_setup_script%/*}/relocate_sdk.py $target_sdk_dir $dl_path $executable_files
43EOF
44
45$SUDO_EXEC mv $tdir/relocate_sdk.sh ${env_setup_script%/*}/relocate_sdk.sh
46$SUDO_EXEC chmod 755 ${env_setup_script%/*}/relocate_sdk.sh
47rm -rf $tdir
48if [ $relocate = 1 ] ; then
49	$SUDO_EXEC ${env_setup_script%/*}/relocate_sdk.sh
50	if [ $? -ne 0 ]; then
51		echo "SDK could not be set up. Relocate script failed. Abort!"
52		exit 1
53	fi
54fi
55
56# replace @SDKPATH@ with the new prefix in all text files: configs/scripts/etc.
57# replace the host perl with SDK perl.
58for replace in "$target_sdk_dir -maxdepth 1" "$native_sysroot"; do
59	$SUDO_EXEC find $replace -type f
60done | xargs -n100 file | grep ":.*\(ASCII\|script\|source\).*text" | \
61    awk -F': ' '{printf "\"%s\"\n", $1}' | \
62    grep -Fv -e "$target_sdk_dir/environment-setup-" \
63             -e "$target_sdk_dir/relocate_sdk" \
64             -e "$target_sdk_dir/post-relocate-setup" \
65             -e "$target_sdk_dir/${0##*/}" | \
66    xargs -n100 $SUDO_EXEC sed -i \
67        -e "s:$SDK_BUILD_PATH:$target_sdk_dir:g" \
68        -e "s:^#! */usr/bin/perl.*:#! /usr/bin/env perl:g" \
69        -e "s: /usr/bin/perl: /usr/bin/env perl:g"
70
71if [ $? -ne 0 ]; then
72	echo "Failed to replace perl. Relocate script failed. Abort!"
73	exit 1
74fi
75
76# change all symlinks pointing to @SDKPATH@
77for l in $($SUDO_EXEC find $native_sysroot -type l); do
78	$SUDO_EXEC ln -sfn $(readlink $l|$SUDO_EXEC sed -e "s:$SDK_BUILD_PATH:$target_sdk_dir:") $l
79	if [ $? -ne 0 ]; then
80		echo "Failed to setup symlinks. Relocate script failed. Abort!"
81		exit 1
82    fi
83done
84
85echo done
86