#! /bin/sh

# this script finds all binaries and updates them with the ones 
# from your system, in order to maintain them more easily.
# Be careful. If you have specially made binaries (perhaps lean ones),
# they will be overwritten.

TOPDIR=`pwd`
R=roottree
PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH"



rm -rf $R/sbin
mkdir -p $R/cdrom $R/etc $R/initrd $R/lib $R/mnt $R/mnt/cdrom
mkdir -p $R/proc $R/root $R/sbin $R/tmp $R/var 
mkdir -p $R/etc/terminfo $R/etc/terminfo/l
mkdir -p $R/var/lib $R/var/log $R/var/run $R/var/state
mkdir -p $R/var/lib/nfs 
mkdir -p $R/var/log/news
touch $R/var/log/news/news.crit $R/var/log/news/news.err $R/var/log/news.notice

cd $R
ln -sf sbin bin
cd $TOPDIR

echo "Building root /sbin ..."
for file in `cat rootsbin.list` ; do
    command=`basename $file`
    fullfile=`which $command 2> /dev/null`
    if [ x$fullfile != x ] ; then
#       echo "updating $command in roottree with $fullfile"
        cp -fpd $fullfile $file
        if [ $? != 0 ] ; then
           echo "Update failed."
           exit 1
        fi
        strip $file 2>/dev/null
    else
        echo "==== Could not find $command on your system ===="
        rm -f $file
    fi
done
# Special cases
cd roottree/sbin
ln -sf halt reboot
cd $TOPDIR

# Copy all needed shared libraries
echo "Building root shared libaries ..."
rm -f `find roottree/lib/ -maxdepth 1 -type f 2>/dev/nul`
#
# Statically linked binaries create an error
# message from ldd, so we ignore those
#
for lib in `find roottree/sbin/ -type f -exec ldd {} \; | awk '{print $3}' | sort | uniq` ; do
  if [ -e $lib ] ; then
     cp -fp $lib roottree/lib/
     if [ $? != 0 ] ; then
        echo "Update failed."
        exit 1
     fi
  fi
done
strip `find roottree/lib/ -maxdepth 1 -type f` 2>/dev/null
cp -p /etc/services roottree/etc/
if [ $? != 0 ] ; then
   echo "Update failed."
   exit 1
fi

#
# Get a bunch of stuff from /etc
#
echo "Building /etc ..."
for file in `cat rootetc.list` ; do
   if [ -e $file ] ; then
      cp -fp $file roottree/etc/
   else
      echo "==== Could not find $file in /etc ===="
   fi
done
rm -rf roottree/etc/pam.d
if [ -e /etc/pam.d ] ; then
   cp -a /etc/pam.d roottree/etc/
   if [ -e /etc/pam.conf ] ; then
      cp -fp /etc/pam.conf roottree/etc/
   fi
fi

if [ -e /etc/init.d ] ; then
   rm -rf roottree/etc/init.d
   cp -aL /etc/init.d roottree/etc/
fi

if [ -e /etc/ssh ] ; then
   cp -a /etc/ssh roottree/etc/
   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
   echo "!!!!                                             !!!!"
   echo "!!!! Warning your /etc/ssh directory was copied. !!!!"
   echo "!!!! Keep this directory and the CDROM secure.   !!!!"
   echo "!!!!                                             !!!!"
   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
fi
if [ -e /etc/security ] ; then
  cp -a /etc/security roottree/etc/
fi

#
# Pull a few files needed by PAM
#
if [ -e /lib/libnss_files.so.1 ] ; then
  cp -fp /lib/libnss_files.so.1 roottree/lib/
fi
cp -fp /lib/libnss_files.so.2 roottree/lib/
cp -a /lib/security roottree/lib/
