#!/bin/bash


pkgname="teamviewer"
arch="amd64"
deps="wget xz hicolor-icon-theme qt5-declarative qt5-quickcontrols qt5-webkit qt5-x11extras"
src="https://download.${pkgname}.com/download/linux/${pkgname}_${arch}.tar.xz"
installdir="/home/${USER}/.teamviewer"

prepare_(){
    echo "Preparing directories..."
    if [ ! -d ${installdir} ];then
        mkdir -pv ${installdir}
    fi
}

deps_(){
    echo "Installing dependencies..."
    sudo xbps-install -Sy ${deps}
}

download_(){
    echo "Downloading source file..."
    if [ ! -f /tmp/${pkgname}_${arch}.tar.xz ];then
        wget ${src} -P /tmp
    fi
}

extract_(){
    echo "Extracting archive..."
    if [ -f /tmp/${pkgname}_${arch}.tar.xz ];then
        tar -xvf /tmp/${pkgname}_${arch}.tar.xz -C ${installdir}
    fi
}

install_(){
    echo "Installing desktop file..."
    sudo cp ${installdir}/${pkgname}/${pkgname}.desktop /usr/share/applications/
    echo "Linking binaries..."
    sudo ln -s ${installdir}/${pkgname}/tv_bin/script/${pkgname} /usr/bin/${pkgname}
    sudo ln -s ${installdir}/${pkgname}/tv_bin/${pkgname}d /usr/bin/${pkgname}d
    echo
}

init_(){
    echo "Launching Teamviewer to create desktop file..."
    ${installdir}/${pkgname}/${pkgname} &
    
}

finished_(){
    echo "All finished! Look for the desktop icon in your applications menu."

}

prepare_
deps_
download_
extract_
init_
install_
finished_