#!/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="wps-office"
latest_package_version="11.1.0.9505"
package_installation_directory="/opt/kingsoft"
command_extract_main_package="ar -x"
command_install_package="tar xf"
package_files_installation="data.tar.xz"
package_download_name="$package_name-$latest_package_version.XA_amd64.deb"
download_link="http://wdl1.pcfg.cache.wpscdn.com/wpsdl/wpsoffice/download/linux/9505/$package_download_name"
expected_package_version=$(echo $latest_package_version | sed "s/\.//" | sed "s/\.//" | sed "s/\.//")
installed_package_version=$(rpm -qi $package_name | grep Ver | awk '{print $3}' | sed "s/\.//" | sed "s/\.//" | sed "s/\.//" | sed "s/\.XA//")

# If necessary, update the application installation
function update_app() {
    #Get the app
    mkdir -p $package_installation_directory/
    wget --no-check-certificate -O $package_installation_directory/$package_download_name $download_link
    cd $package_installation_directory/

    #Extract the main package
    $command_extract_main_package $package_download_name

    #Install package
    $command_install_package $package_files_installation -C /

    #Update app menu icon cache
    update-desktop-database
}

# 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
    echo "App version: $latest_package_version. The application appears to be updated. Nothing to do."
fi
