#!/bin/bash

################################################################################
# Script name:  bin_copy_v1.1beta.sh                                           #
# Author:       SoD                                                            #
# Contact:      SoD- @ EFNet                                                   #
# Releasedate:  2001-12-18                                                     #
#                                                                              #
# This script copies a bunch of sometimes needed binaries to glftpd's bin-dir  #
# and chmods them to 755. It also copies the libs that are needed for the      #
# binaries.                                                                    #
#                                                                              #
# Installation: -Just execute this script as root and give the path to your    #
#                glftpd-dir as argument.                                       #
#                                                                              #
# Requirements: Root access to the computer.                                   #
#                                                                              #
# Changelog:    v1.0 -> v1.1.beta:    Nescessary libs are now also copied.     #
#               Changes in settings:  None.                                    #
################################################################################

BINARIES="awk bash basename chmod chown cat cp cut date df diff dirname du echo 
expand expr fgrep find grep gzip head kill ldconfig ln ls mkdir more mount mv 
rm rmdir sed sh sleep tail touch tr unzip wc zip"

if [ $UID != 0 ]; then
    echo "You need to be root to execute this script."
    exit 1
fi
if [ -z $1 ]; then
    echo ".------------------------------------------------------------------------------."
    echo "| This script copies some useful bins and libs to you glftpd bin-directory.    |"
    echo "|                                                                              |"
    echo "| Usage:   bin_copy_v1.0.sh (path to the glftpd dir)                           |"
    echo "| Example: ./bin_copy_v1.0.sh /glftpd/                                         |"
    echo "'------------------------------------------------------------------------------'"
    exit
fi

# Make shure that the glftpd-dir exists
if [ ! -d $1 ]; then
    echo "The directory \"$1\" can't be found."
    exit 1
fi

# Make shure that the path ends with a frontslash
if [ "`echo $1 | grep -E "/$"`" ]; then
    PATHH=$1
else
    PATHH=`echo \`echo $1\`\`echo "/"\``
fi

# Check if the glftpd bin is located in $PATH/bin/
if [ ! -f ""$PATHH"bin/glftpd" ]; then
    echo "The glftpd binary can't be found in \"$PATHH"bin/"\"."
    exit 1
fi

for BINARY in $BINARIES; do
    if [ ! -f ""$PATHH"bin/$BINARY" ]; then
        if [ ! "`whereis -b $BINARY | awk '{print $2}'`" ]; then
            echo "Couldnt find the binary \"$BINARY\" on computer. Copy it manually."
        else
            echo "Copying `whereis -b $BINARY | awk '{print $2}'` to "$PATHH"bin/"
            cp `whereis -b $BINARY | awk '{print $2}'` ""$PATHH"bin/"
            chmod 755 ""$PATHH"bin/$BINARY"

            # Make a list of needed libs for $BINARY
            LIBS=`ldd \`whereis -b $BINARY | awk '{print $2}'\` | awk '{print $3}'`

            # Are any libs needed?
            if [ "LIBS" ]; then
                for LIB in $LIBS; do
                    if [ "`echo $LIB | grep "/usr/"`" ] && [ ! -f "$PATHH"usr/lib/`basename $LIB` ]; then
                        if [ `echo $LIB | cut -c 1 | grep "/"` ]; then
                            echo "Copying $LIB to ""$PATHH"usr/lib/" (needed by $BINARY)"
                            cp $LIB ""$PATHH"usr/lib/"
                            chmod 755 ""$PATHH"usr/lib/`basename $LIB`"
                        fi
                    elif [ ! -f "$PATHH"lib/`basename $LIB` ]; then
                        if [ `echo $LIB | cut -c 1 | grep "/"` ]; then
                            echo "Copying $LIB to "$PATHH"lib/ (needed by $BINARY)"
                            cp $LIB ""$PATHH"lib/"
                            chmod 755 ""$PATHH"lib/`basename $LIB`"
                        fi
                    fi
                done
            fi
        fi
    fi
done