#!/bin/bash
# This script only helps to update applications that have already been installed by the user,
# but that are not found in software repositories.

# Package information
package_name="teamspeak3"
latest_package_version="3.6.1"
package_installation_directory="/opt/$package_name-updater"
executable_file="/opt/$package_name/ts3client_runscript.sh"
package_download_name="TeamSpeak3-Client-linux_amd64-$latest_package_version.run"
download_link="https://files.teamspeak-services.com/releases/client/$latest_package_version/$package_download_name"
expected_package_version=$(echo $latest_package_version | cut -d"-" -f -1 | sed "s/\.//g")
installed_package_version=$(rpm -qi $package_name-updater | grep Ver | awk '{print $3}' | sed "s/\.//g")

# If necessary, update the application installation
function update_app() {
ps -C regataos-store > /dev/null
if [ $? = 0 ]
then
    if test -e "/tmp/progressbar-store/installing-$package_name" ; then
        # Prepare the progress bar and downloading
        rm -f "/tmp/progressbar-store/progress-movement"
        echo "0%" > /tmp/progressbar-store/progress
        echo "Downloading..." > /tmp/progressbar-store/status
        chmod 777 /tmp/progressbar-store/*
        sleep 1
        echo "show progress bar" > /tmp/progressbar-store/progressbar
        chmod 777 /tmp/progressbar-store/*

        echo "/tmp/progressbar-store/$package_download_name" > /tmp/progressbar-store/file-download-size
        chmod 777 /tmp/progressbar-store/*
        echo "wget --no-check-certificate -O $package_installation_directory/$package_download_name $download_link" > /tmp/progressbar-store/get-pid
        mkdir -p $package_installation_directory/
        wget --no-check-certificate -O "$package_installation_directory/$package_download_name" "$download_link" 2>&1 | (pv -n > /tmp/progressbar-store/download-percentage)
        echo 100% > /tmp/progressbar-store/progress
        chmod 777 /tmp/progressbar-store/*
        sleep 1
        #Clear cache
        rm -f "/tmp/progressbar-store/download-percentage"
        rm -f "/tmp/progressbar-store/download-size"
        rm -f "/tmp/progressbar-store/download-speed"
        rm -f "/tmp/progressbar-store/file-size"
        rm -f "/tmp/progressbar-store/eta"

        # Install app
        #Prepare the progress bar and install
        chmod 777 /tmp/progressbar-store/*
        echo "Installing..." > /tmp/progressbar-store/status
        echo "" > /tmp/progressbar-store/progress
        echo "installing" > /tmp/progressbar-store/progress-movement

        #Install package
        cd $package_installation_directory/
        chmod +x $package_download_name
        sh $package_download_name --tar -xf 2>/dev/null
        ln -sf $package_installation_directory/ts3client_runscript.sh /usr/bin/teamspeak3

        #Fix a weird change that TS3 did which broke symlinks.
        #Check: https://forum.teamspeak.com/threads/134617-Linux-client-not-starting-when-run-though-symlink-since-3-1-8?p=454315#post454315
        sed -i "s|cd.*|cd $package_installation_directory|" $package_installation_directory/ts3client_runscript.sh

        # Fix teamspeak3 on wayland (FS#57366)
        sed -i "s|ts3client_linux_amd64|ts3client_linux_amd64 -platform xcb|" "${pkgdir}"$package_installation_directory/ts3client_runscript.sh

        #Fix permissions
        find -type d | xargs chmod 755
        find -type f | xargs chmod 644
        find -name '*.so*' | xargs chmod 755
        chmod +x ts3client* package_inst QtWebEngineProcess

    else
        #Get the app
        mkdir -p $package_installation_directory/
        wget --no-check-certificate -O "$package_installation_directory/$package_download_name" "$download_link"

        #Install package
        cd $package_installation_directory/
        chmod +x $package_download_name
        sh $package_download_name --tar -xf 2>/dev/null
        ln -sf $package_installation_directory/ts3client_runscript.sh /usr/bin/teamspeak3

        #Fix a weird change that TS3 did which broke symlinks.
        #Check: https://forum.teamspeak.com/threads/134617-Linux-client-not-starting-when-run-though-symlink-since-3-1-8?p=454315#post454315
        sed -i "s|cd.*|cd $package_installation_directory|" $package_installation_directory/ts3client_runscript.sh

        # Fix teamspeak3 on wayland (FS#57366)
        sed -i "s|ts3client_linux_amd64|ts3client_linux_amd64 -platform xcb|" "${pkgdir}"$package_installation_directory/ts3client_runscript.sh

        #Fix permissions
        find -type d | xargs chmod 755
        find -type f | xargs chmod 644
        find -name '*.so*' | xargs chmod 755
        chmod +x ts3client* package_inst QtWebEngineProcess
    fi

else
    #Get the app
    mkdir -p $package_installation_directory/
    wget --no-check-certificate -O "$package_installation_directory/$package_download_name" "$download_link"

    #Install package
    cd $package_installation_directory/
    chmod +x $package_download_name
    sh $package_download_name --tar -xf 2>/dev/null
    ln -sf $package_installation_directory/ts3client_runscript.sh /usr/bin/teamspeak3

    #Fix a weird change that TS3 did which broke symlinks.
    #Check: https://forum.teamspeak.com/threads/134617-Linux-client-not-starting-when-run-though-symlink-since-3-1-8?p=454315#post454315
    sed -i "s|cd.*|cd $package_installation_directory|" $package_installation_directory/ts3client_runscript.sh

    # Fix teamspeak3 on wayland (FS#57366)
    sed -i "s|ts3client_linux_amd64|ts3client_linux_amd64 -platform xcb|" "${pkgdir}"$package_installation_directory/ts3client_runscript.sh

    #Fix permissions
    find -type d | xargs chmod 755
    find -type f | xargs chmod 644
    find -name '*.so*' | xargs chmod 755
    chmod +x ts3client* package_inst QtWebEngineProcess
fi
}

# Check the package version
#Check the main version of the application
if [ $expected_package_version -gt $installed_package_version ]
then
    echo "Updating app"
    update_app
else
	if test ! -e "$executable_file" ; then
		echo "Installing app"
		update_app
	else
		echo "App version: $latest_package_version. The application appears to be updated. Nothing to do."
	fi
fi
