#!/bin/sh -f

#set -x

if [  $# != 1 ] ; then
  echo "build_sys_so : one argument expected."
  exit
fi

if [ `uname` != Linux ] ; then
  exit
fi

#obuild_pwd=`pwd`

obuild_dir=$1
if [ ! -d "${obuild_dir}" ] ; then
  echo "build_sys_so : directory ${obuild_dir} not found."
  exit
fi
if [ ! -d "${obuild_dir}/lib" ] ; then
  echo "build_sys_so : directory ${obuild_dir}/lib not found."
  exit
fi
if [ ! -d "${obuild_dir}/bin" ] ; then
  echo "build_sys_so : directory ${obuild_dir}/bin not found."
  exit
fi

/bin/rm -R -f "${obuild_dir}/sys"
/bin/mkdir "${obuild_dir}/sys"

obuild_dir_sys="${obuild_dir}/sys"

# Analyse .so to find extra dependancies :
LD_LIBRARY_PATH=
export LD_LIBRARY_PATH
/bin/rm -f obuild_tmp
/bin/rm -f obuild_tmp_0
find "${obuild_dir}/lib" -name '*.so' -print >> obuild_tmp_0
find "${obuild_dir}/bin" -type f -name '*' -print >> obuild_tmp_0
grep -v '.save' obuild_tmp_0 > obuild_tmp_1
while read f ; do
  ldd $f >> obuild_tmp
done < obuild_tmp_1
#sort -u obuild_tmp | grep found
grep -v 'statically linked' obuild_tmp | grep -v 'not a dynamic executable'   > obuild_tmp0
awk '{print $3;}' obuild_tmp0 > obuild_tmp1
grep -v '\./' obuild_tmp1 | grep -v 'not' > obuild_tmp2
sort -u obuild_tmp2 > obuild_tmp3
awk '{print "cp " $1 " xxx/.";}' obuild_tmp3 > obuild_tmp4
sed -e "s:xxx:\"${obuild_dir_sys}\":g" obuild_tmp4 > obuild_tmp.sh
chmod a+x obuild_tmp.sh
./obuild_tmp.sh
/bin/rm -f obuild_tmp_0 obuild_tmp_1
/bin/rm -f obuild_tmp obuild_tmp0 obuild_tmp1 
/bin/rm -f obuild_tmp2 obuild_tmp3 obuild_tmp4
/bin/rm -f obuild_tmp.sh


