#!/bin/sh
#
# program:	copying libraries that are neccessary to work in chroot area
# author:	alex yu <yua@wu-ftpd.org>
# date:		11/30/2000 12:59AM
#

# define your chroot path
chrootdir="/var/ftp"

# define Y if you don't want this script to copy binaries for you
# if you define Y, you can ignore progs variable
# please use upper-case
skipbincopy="N"

# define programs you need in <chroot>/bin
# ignore this if you define Y in skipbincopy variable
# this script will try to copy from your system to <chroot>/bin
progs="bash compress cpio gzip ls unzip"

# define Y if you don't want this script to copy libs for you
# please use upper-case
skiplibcopy="N"

# define Y if you don't want this script to chroot the environment for you
# please use upper-case
skipldconfig="N"

# note: please ensure that you can access following binaries from your path
#       environment.
#       cp echo grep ldconfig mkdir uname sed

###########################################################################
# you don't need to modify anything from now on
###########################################################################

echo -n "Checking for rm... "
rm=`which rm`
if [ -x "$rm" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for mkdir... "
mkdir=`which mkdir`
if [ -x "$mkdir" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for cp... "
cp=`which cp`
if [ -x "$cp" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for sh... "
sh=`which sh`
if [ -x "$sh" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for kill... "
kill=`which kill`
if [ -x "$kill" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for grep... "
grep=`which grep`
if [ -x "$grep" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for cat... "
cat=`which cat`
if [ -x "$cat" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for echo... "
echo=`which echo`
if [ -x "$echo" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for ls... "
ls=`which ls`
if [ -x "$ls" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for bash... "
bash=`which bash`
if [ -x "$bash" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for wc... "
wc=`which wc`
if [ -x "$wc" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for uname... "
uname=`which uname`
if [ -x "$uname" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for sed... "
sed=`which sed`
if [ -x "$sed" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for ldd... "
ldd=`which ldd`
if [ -x "$ldd" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for cut... "
cut=`which cut`
if [ -x "$cut" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for sort... "
sort=`which sort`
if [ -x "$sort" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for uniq... "
uniq=`which uniq`
if [ -x "$uniq" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for tr... "
tr=`which tr`
if [ -x "$tr" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for chroot... "
chroot=`which chroot`
if [ -x "$chroot" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for find... "
find=`which find`
if [ -x "$find" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for ln... "
ln=`which ln`
if [ -x "$ln" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo -n "Checking for ldconfig... "
ldconfig=`which ldconfig`
if [ -x "$ldconfig" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi


if [ ! $skipbincopy = "Y" ] ; then 
  echo -n "Copying binaries... "
  mkdir -p $chrootdir/bin
  for i in $progs ; do
    "$cp" -f `which $i` $chrootdir/bin/
  done
  echo "done"
fi

if [ ! $skiplibcopy = "Y" ] ; then
  echo -n "Copying libraries... "
  if [ $("$uname" -s | "$sed" "s/.*BSD.*/BSD/") = "BSD" ] ; then
    bsd="1"
  elif [ $("$uname" -s | "$sed" 's/.*bsd.*/BSD/') = "BSD" ] ; then
    bsd="1"
  else
    bsd="0"
  fi
  "$mkdir" -p $chrootdir/lib
  if [ $bsd = "1" ] ; then
    for h in $(\
      for i in $chrootdir/bin/* ; do
        if [ -f $i ] ; then
          OLDIFS="$IFS"
          IFS="
"
          for j in $("$ldd" $i | "$grep" =) ; do
            lib=$(echo $j | "$cut" -d'>' -f2 | "$cut" -d' ' -f2)
            echo "$lib"
          done
          IFS="$OLDIFS"
        fi
      done | "$sort" | "$uniq") ; do
      "$cp" -f $h $chrootdir/lib
    done
    echo "done"
    echo "Make sure the BSD libs copied correctly.."
  else
    for i in `"$ldd" $chrootdir/bin/* 2>/dev/null | "$grep" =\> | "$sort" | "$uniq" | "$tr" -s " " | "$cut" -f 3 -d " "` ; do
      "$cp" -f $i $chrootdir/lib
    done
    echo "done"
    echo "Its up to you to ensure the libs copied to $chrootdir/lib correctly.."
  fi
fi

if [ ! $skipldconfig = "Y" ] ; then
  echo -n "Configuring dependencies... "
  "$mkdir" -p $chrootdir/etc
  echo "/var/lib" >> $chrootdir/etc/ld.so.conf
  "$cp" -f $ldconfig $chrootdir/bin
  "$chroot" $chrootdir/ /bin/ldconfig 2> /dev/null
  echo "done"
fi