#!/bin/bash
#########################################################################
#Copyright 2015 Anton Midyukov    <antohami@altlinux.org>               #
#                                                                       #
#This program is free software; you can redistribute it and/or modify   #
#it under the terms of the GNU General Public License as published by   #
#the Free Software Foundation; either version 3 of the License, or      #
#(at your option) any later version.                                    #
#                                                                       #
#This program is distributed in the hope that it will be useful,        #
#but WITHOUT ANY WARRANTY; without even the implied warranty of         #
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
#GNU General Public License for more details.                           #
#                                                                       #
#You should have received a copy of the GNU General Public License      #
#along with this program; if not, write to the Free Software            #
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             #
#MA 02110-1301, USA.                                                    #
#########################################################################

export TEXTDOMAIN="ldd-requires" #name_file .mo
export TEXTDOMAINDIR="/usr/share/locale"
export OUTPUT_CHARSET=UTF-8
. gettext.sh

find_requires () {
    #создаём массив ldd_list, в который помещаем 
    #список недостающих библиотек
    ldd_list=( $(ldd $1 | grep "not found" | uniq | cut -d " " -f1) )
    #Если список не пуст
    if [[ ${#ldd_list[*]} != 0 ]]; then
        echo -e `gettext 'The list of missing libraries:'`
        echo -e ${ldd_list[@]}
        echo -e `gettext "Is search packages"`
        echo -e `gettext "It may take a long time."`
        #циклически ищем каждую библиотеку и добавляем
        #в массив название пакетов их содержащих
        for line in ${ldd_list[@]}; do
            package_found=`apt-cache search "$line"| grep $2 i586 | grep -v debuginfo | cut -d " " -f1`
            if [[ $package_found != '' ]]; then
                echo $package_found
                packages_list=( "${packages_list[@]}" "$package_found" ) 
            else
                no_packages_list=( "${no_packages_list[@]}" "$line" )
            fi
        done       
        #Если список найденных пакетов не пуст
        if [[ ${#packages_list[*]} != 0 ]]; then
            echo -e `gettext 'Please install packages'`
            echo -e "apt-get install ${packages_list[@]}"
        else    
            echo -e `gettext 'Packages not find'`
        fi
        if [[ ${#no_packages_list[*]} != 0 ]]; then
            echo -e `gettext 'The following library is not in the repository:'`
            echo -e "${no_packages_list[@]}"
        fi
    else
        echo -e `gettext 'All libraries are already installed'`
    fi
}
#Если нет аргументов
if [ -z "$1" ]; then
    echo -e `gettext 'Missing arguments'`
elif [[ `file $1` =~ "ELF" ]]; then
    file_out=`file $1`
    for filearch in "386" "x86-64" "ARM" "other"; do
        if [[ $file_out =~ $filearch ]]; then
            break
        fi
    done
    case $(arch) in
    "x86_64")
        sysarch="x86-64";;
    "i686"|"i586"|"i486"|"i386")
        sysarch="386";;
    "arm")
        sysarch="ARM";;
    esac
    #Архитектура бинарика соответствует ОС
    if [[ $filearch = $sysarch ]]; then
        echo -e `gettext 'Architecture binary file corresponds to the OS'`
        find_requires $1 "-v"
    #32 битный бинарик и 64 битная система
    elif [[ $filearch = 386 && $sysarch = x86-64 ]]; then
        echo -e `gettext '32-bit file'`
        echo -e `gettext 'You have a 64 bit OS'`
        find_requires $1
    else
        echo -e `gettext 'You cannot run file'`
    fi
else
    echo -e "$1 `gettext 'not a dynamic executable'`"
fi
