#!/bin/bash
function check()
{
    local a=false
    echo -e "Verification of compression systems:\n"
    echo -e "Systems\t\t\tStatus"
    echo "----------------------------------"
    for i in "rar" "unrar" "tar" "bzip2" "gzip" "zip" "unzip" "7za"
    do
        if ! [ -x "$(command -v $i)" ]
        then
            echo -e "$i\t\t\t\e[91mNot Installed\e[0m"
            $a = true
        else
            echo -e "$i\t\t\t\e[32mInstalled\e[0m"
        fi
    done

    if $a
    then
        echo -e "\nIn order to use all the extensions install the packages: tar, bzip2, gzip, zip, unzip, 7za, rar, unrar"
    fi
    echo ""
    exit 0
}

function rarInstall(){
    if [ -e "$(command -v wget)" ]
    then
        if [[ $(uname -m) -eq "x86_64" ]]
        then
            RAR_URL="https://www.rarlab.com/rar/rarlinux-x64-5.9.1.tar.gz"
            RAR_FILE="rarlinux-x64-5.9.1.tar.gz"
        else
            RAR_URL="https://www.rarlab.com/rar/rarlinux-5.9.1.tar.gz"
            RAR_FILE="rarlinux-5.9.1.tar.gz"
        fi
        wget $RAR_URL 
        tar -zxvf $RAR_FILE
        cd rar
        cp -v rar unrar /usr/local/bin/
        cd ..
        rm -r rar $RAR_FILE
        echo "rar/unrar installed."
    else
        echo -e "\n\e[91m[Error]\e[0m - Wget is not installed, so rar could not be installed.\nPlease install wget.\n"
        read -p "Press [ENTER] to continue installing or [CTRL+C] to exit."
    fi
}

function install()
{
    echo -e "Install dependencies\n"

    if [ "$UID" != "0" ]; then 
        echo " Only root can execute this script, sorry."
        echo " Try 'sudo $0 $@'"
        exit 0
    fi
    
    if [ -e "$(command -v rar)" ]
    then
        echo "rar installed."
    else
        read -p "Do you want to install non-free software? (rar, unrar) [Y/N]: " PRIVSOFT
            case ${PRIVSOFT} in
                y|Y)
                    rarInstall;;
                n|N)
                    echo -e "\nOnly will install free software.\n";;
                *)
                    echo -e "Options: 'Y' or 'N': ";;
            esac
    fi
        
    if [ -x "$(command -v apt)" ]
    then
        echo -e "APT system detected\n"
        apt install -y tar bzip2 gzip zip unzip p7zip-full 1> /dev/null
    elif [ -n "$(grep "centos" /etc/*-release)" ]
    then
        echo -e "CentOS system detected\n"
        yum install -y -q epel-release 1> /dev/null
        rpm -U --quiet http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-8.noarch.rpm 1> /dev/null
        rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 1> /dev/null
        yum repolist 1> /dev/null
        yum install -y -q tar bzip2 gzip zip unzip p7zip 1> /dev/null
    elif [ -x "$(command -v dnf)" ]
    then
        echo -e "DNF system detected\n"
        dnf install -y -q tar bzip2 gzip zip unzip p7zip 1> /dev/null
    elif [ -x "$(command -v yum)" ]
    then
        echo -e "Yum system detected\n"
        yum install -y -q tar bzip2 gzip zip unzip p7zip 1> /dev/null
    elif [ -x "$(command -v pacman)" ]
    then
        echo -e "Pacman system detected\n"
        pacman -Sqy --noconfirm tar bzip2 gzip zip unzip p7zip 1> /dev/null
    elif [ -x "$(command -v zypper)" ]
    then
        echo -e "Zypper system detected\n"
        zypper install -y tar bzip2 gzip zip unzip p7zip-full 1> /dev/null
    else
        echo -e "System installer not detected\n"
    fi

    echo -e "\nDependencies installation finished\n"
    exit 0
}