#!/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.11698"
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="$(echo $package_name)_$latest_package_version.XA_amd64.deb"
download_link="https://wdl1.pcfg.cache.wpscdn.com/wpsdl/wpsoffice/download/linux/$(echo $latest_package_version | cut -d"." -f 4-)/$package_download_name"
expected_package_version=$(echo $latest_package_version | sed "s/\.//g")
installed_package_version=$(cat $package_installation_directory/app-version.txt | sed "s/\.//g")

# If necessary, update the application installation
function update_app() {
ps -C regataos-store > /dev/null
if [ $? = 0 ]
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

    #Extract the main package
    cd $package_installation_directory/
    $command_extract_main_package $package_download_name

    #Install package
    $command_install_package $package_files_installation -C /
    update-desktop-database
    
    #Run post-installation script
    tar -vzxf control.tar.gz
    chmod +x postinst
    /bin/bash postinst configure

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

    #Extract the main package
    cd $package_installation_directory/
    $command_extract_main_package $package_download_name

    #Install package
    $command_install_package $package_files_installation -C /
    update-desktop-database
fi
}

# Check the package version
#Check the main version of the application
if test ! -e "/usr/bin/wps" ; then
    echo "Install app"
    echo "$latest_package_version" > $package_installation_directory/app-version.txt
    update_app
elif [ $expected_package_version -gt $installed_package_version ]; then
    echo "Updating app"
    echo "$latest_package_version" > $package_installation_directory/app-version.txt
    update_app
else
    echo "App version: $latest_package_version. The application appears to be updated. Nothing to do."
fi
