#!/usr/bin/env bash

function rarInstall(){
    if [ -e "$(command -v wget)" ] || [ -e "$(command -v curl)" ]
    then
        if [[ "$(uname -m)" == "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
        if [ -e "$(command -v wget)" ]
        then
            wget -q $RAR_URL  > /dev/null
        else
            curl -s -o $RAR_FILE $RAR_URL > /dev/null
        fi
        tar -zxvf $RAR_FILE  > /dev/null
        cd rar
        cp -v rar unrar /usr/local/bin/ > /dev/null
        cd ..
        rm -r rar $RAR_FILE  > /dev/null
        complete_install "rar"
    else
        echo -e "\n\e[91m[Error]\e[0m - wget or curl 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()
{
    if [ "$UID" != "0" ]; then 
        echo " Only root can execute this script, sorry."
        echo " Try 'sudo lch $@'"
        exit 0
    fi

    if [ $# -eq 1 ]; then 
        # Poner aviso "Vamos a instalarlo todo y mucho"
        read -p "Do you want to install non-free software? (rar, unrar) [y/N]: " PRIVSOFT
        case ${PRIVSOFT} in
            y | Y | yes)
                PRIVSOFT=true
                echo -e "\nInstall all dependencies (with non-free)\n";;
            *)
                PRIVSOFT=false
                echo -e "\nOnly will install free software.\n";;
        esac
    else
        PRIVSOFT=false
    fi

    if [ -x "$(command -v apt)" ]
    then
        if [[ -e  ./lib/installers/deb.sh ]]; then . ./lib/installers/deb.sh; else . /usr/lib/lch/installers/deb.sh; fi
    elif [ -x "$(command -v yum)" ]
    then
       if [[ -e  ./lib/installers/rpm.sh ]]; then . ./lib/installers/rpm.sh; else . /usr/lib/lch/installers/rpm.sh; fi
    elif [ -x "$(command -v pacman)" ]
    then
        if [[ -e  ./lib/installers/pacman.sh ]]; then . ./lib/installers/pacman.sh; else . /usr/lib/lch/installers/pacman.sh; fi
    elif [ -x "$(command -v zypper)" ]
    then
        if [[ -e  ./lib/installers/zypper.sh ]]; then . ./lib/installers/zypper.sh; else . /usr/lib/lch/installers/zypper.sh; fi
    else
        echo -e "System installer not detected"
    fi

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