#!/bin/sh #Author: Cappy of ubuntuforums.org #Version: 1.02 #Comments are above the code they are commenting #ToDo: More error checking give_error() { echo "Usage: getlibs /path/to/binary" echo "Usage: getlibs -64 amd64librarytoinstall.so" echo "Usage: getlibs -32 i386librarytoinstall.so" exit 127 } #Make sure there at least 1 argument to the program if [ $# -lt 1 ]; then give_error fi #Run only for manual library install if [ $# -gt 1 ]; then architecture=`arch` if [ "x86_64" = "$architecture" ]; then if [ "$1" = "-32" ]; then #Must download the 32-bit library download=1 elif [ "$1" = "-64" ]; then #use apt-get download=2 else give_error fi else if [ "$1" = "-32" ]; then #use apt-get download=2 else give_error fi fi #for each argument add it to the $dependencylist shift while [ "$1" ]; do if [ "$templist" != "" ]; then templist=`echo "$templist\n$1"` shift else templist=`echo "$1"` shift fi done dependencylist=`echo $templist | sed 's/.\///g' | sed s/\+/%2B/g` fi #end code for manual library install #Run only for binaries if [ $# -eq 1 ]; then if [ `ldd $1 | grep -c "not a dynamic executable"` -gt 0 ]; then echo "This file is not a binary file, it is probably a script -\n" echo "If this script needs a 32-bit library use:" echo "getlibs -32 i386librarytoinstall.so\n" echo "If this script needs a 64-bit library use:" echo "getlibs -64 amd64librarytoinstall.so" exit 127 fi binaryfile=$1 #Check to see if the binary is 32-bit or 64-bit isit32bit=`file $binaryfile | grep -c "32-bit"` architecture=`arch` if [ "x86_64" = "$architecture" ]; then if [ $isit32bit -eq 1 ]; then #Must download the 32-bit library download=1 else #use apt-get download=2 fi else if [ $isit32bit -eq 1 ]; then #use apt-get download=2 else echo "This isn't a 32-bit application:" echo `file $binaryfile` exit 127 fi fi #print program dependencies | find the ones missing | get only the library names | get rid of the ones starting with "./" | replace "+" with %2B dependencylist=`ldd $binaryfile | grep "not found" | awk '{print $1}' | grep -v \./ | sed s/\+/%2B/g` #get the first missing library out of the list | replace "+" with %2B libraryneeded=`echo $dependencylist | awk '{print $1}'` if [ "$libraryneeded" = "" ]; then echo "This application isn't missing any dependencies!" exit 127 fi fi #end code for binaries #If folder "/tmp/getlibs" exists delete it cd /tmp if [ -d "/tmp/getlibs" ]; then sudo rm -rf /tmp/getlibs fi #make new getlibs folder mkdir -m 700 getlibs cd /tmp/getlibs #Get rid of duplicate lines libraries=`echo $dependencylist | sort -u` #For each missing library libraryneeded="" for libraryneeded in $libraries; do #different arch library: if [ $download -eq 1 ]; then searchpage1="http://packages.ubuntu.com/cgi-bin/search_contents.pl?word=$libraryneeded&searchmode=searchfiles&case=insensitive&version=feisty&arch=i386" #Look up library name | look for packages that are in usr/lib and no subdirectories #| pull text out between double quotes (other than color:red) | get rid of double quotes linktopage2=`wget -q -O- "$searchpage1" | grep usr/lib/[^/]*[\s]*\< | grep -o \"[^\<]*\" | grep -v "color:" | sed s/\"//g` #Get the first package that matched packagename=`echo $linktopage2 | awk '{print $1}'` if [ "$packagename" = "" ]; then echo "No match found for 32-bit $libraryneeded" else echo "Matched library $libraryneeded to $packagename" #Find the link to the i386 download page for the library linktopage3=`wget -q -O- http://packages.ubuntu.com$packagename | grep i386 | grep download.pl | grep -o \".*\" | sed s/\"//g` #Find the link for the i386 .deb file packagedownloadlink=`wget -q -O- http://packages.ubuntu.com$linktopage3 | grep mirrors.kernel.org | grep -o http.*\.deb` sudo wget -q $packagedownloadlink #Get the filename of the deb filename=`echo $packagedownloadlink | grep -o [^/]*\.deb` #Extract the debian sudo dpkg -x $filename extracted fi fi #same arch library: if [ $download -eq 2 ]; then searchpage1="http://packages.ubuntu.com/cgi-bin/search_contents.pl?word=$libraryneeded&searchmode=searchfiles&case=insensitive&version=feisty&arch=i386" #Look up library name | look for packages that are in usr/lib and no subdirectories #| pull text out between double quotes (other than color:red) | get rid of double quotes linktopage2=`wget -q -O- "$searchpage1" | grep usr/lib/[^/]*[\s]*\< | grep -o \"[^\<]*\" | grep -v "color:" | sed s/\"//g` #Get the first package that matched packagename=`echo $linktopage2 | awk '{print $1}'` #Get the filename of the package from the end of the text filename=`echo $packagename | grep -o [^/]*$` if [ "$filename" = "" ]; then echo "No match found for $libraryneeded" else echo "Matched library $libraryneeded to $filename" aptgetlist=`echo "$aptgetlist $filename"` fi fi done #If installing different arch libraries if [ $download -eq 1 ]; then echo "The following i386 libraries will be installed:" echo `ls /tmp/getlibs/extracted/usr/lib/` echo "Continue? (y/n)" read prompt case $prompt in y|Y) ;; [yY][eE][sS]) ;; n|N) sudo rm -rf /tmp/getlibs; exit 127 ;; [nN][oO]) sudo rm -rf /tmp/getlibs; exit 127 ;; *) echo "Not understood. Aborting."; sudo rm -rf /tmp/getlibs; exit 127 esac sudo cp -R /tmp/getlibs/extracted/usr/lib/* /usr/lib32/ echo "Libraries have been installed." fi if [ "$aptgetlist" != "" ]; then sudo apt-get install $aptgetlist fi sudo rm -rf /tmp/getlibs