#!/bin/sh
set -e

BASENAME=`basename $1`

mkdir --parents bin-foreign lib-foreign 

# TODO: fix use of absolute path
cat > bin-foreign/${BASENAME} <<EOF
#!/bin/sh
LD_LIBRARY_PATH=/lib:/usr/share/olpc-radio-testing/lib-foreign /usr/share/olpc-radio-testing/lib-foreign/${BASENAME} \$*
EOF

chmod +x bin-foreign/${BASENAME} 
echo "wrapper --> bin-foreign/${BASENAME}"

cp --preserve=mode,ownership,timestamps $1 lib-foreign/
echo "$1 --> lib-foreign/${BASENAME}"

for x in `ldd $1|egrep -v "linux-gate.so|ld-linux.so"|awk '{print $3}'`; do
	echo "$x --> lib-foreign/"
	cp --preserve=mode,ownership,timestamps $x lib-foreign/
done
