#!/bin/bash
# This script replaces all binaries and libraries from rootfs/ directory
# by the ones used in currently running system, thus it updates them to
# the 'latest' (current) versions

updateit()
{
   find | grep -v "README" | grep -v "bin/init" | grep -v "bin/bash" | grep -v "bin/sh" | while read LINE; do
   if [ ! -d "$LINE" ]; then
         SOURCE=""
         LINEB=$(basename $LINE)
         if [ -e "/$LINEB" ]; then SOURCE="/$LINEB"; fi
         if [ -e "/bin/$LINEB" ]; then SOURCE="/bin/$LINEB"; fi
         if [ -e "/sbin/$LINEB" ]; then SOURCE="/sbin/$LINEB"; fi
         if [ -e "/lib/$LINEB" ]; then SOURCE="/lib/$LINEB"; fi
   
         if [ -e "/usr/$LINEB" ]; then SOURCE="/usr/$LINEB"; fi
         if [ -e "/usr/bin/$LINEB" ]; then SOURCE="/usr/bin/$LINEB"; fi
         if [ -e "/usr/sbin/$LINEB" ]; then SOURCE="/usr/sbin/$LINEB"; fi
         if [ -e "/usr/lib/$LINEB" ]; then SOURCE="/usr/lib/$LINEB"; fi
   
         if [ -e "/usr/local/$LINEB" ]; then SOURCE="/usr/local/$LINEB"; fi
         if [ -e "/usr/local/bin/$LINEB" ]; then SOURCE="/usr/local/bin/$LINEB"; fi
         if [ -e "/usr/local/sbin/$LINEB" ]; then SOURCE="/usr/local/sbin/$LINEB"; fi
         if [ -e "/usr/local/lib/$LINEB" ]; then SOURCE="/usr/local/lib/$LINEB"; fi
   
         if [ "$SOURCE" != "" ]; then
            echo "$SOURCE -> $LINE"
            cat "$SOURCE" > "$LINE"
            strip --strip-unneeded "$LINE"
         fi
      fi
   done
}

echo "Update (overwrite) all binaries and libraries in ./rootfs/ ?"
echo "Hit Ctrl+C to abort or Enter to continue..."
read junk

cd rootfs
updateit
cd ..
