#!/usr/bin/bash
echo "Closing zoom.AppImage ..."
pkill AppRun.wrapped
killall zoom-appimage
echo "wait a few seconds to terminate zoom.AppImage ..."
sleep 5s
# Remove traces of unfinished downloads
sudo rm -f /opt/appimages/*.AppImage.part*
echo "Download Zoom AppImage file to /opt/appimages/"
file=/opt/appimages/zoom.AppImage
minimumsize=90
if [ -f $file ]; then
    echo found $file
    actualsize=$(du -k $file | cut -f 1)
    if [ $actualsize -ge $minimumsize ]; then
        #echo size is over $minimumsize kilobytes
        echo appimageupdatetool $file
        sleep 1
        sudo appimageupdatetool $file
        iFiles=0
        if [ $iFiles -gt 1 ]; then
            sudo rm -f $file
            sudo mv /opt/appimages/zoom*.AppImage $file
        fi
    else
        #echo echo size is under $minimumsize kilobytes
        echo Downloading zoom.AppImage...
        sleep 1
        sudo wget -O $file https://download.opensuse.org/repositories/home:/kimi:/zoom/AppImage/zoom-latest-Architecture: x86_64.AppImage
    fi
else
    echo Downloading zoom.AppImage...
    sleep 1
    sudo mkdir -p /opt/appimages
    sudo wget -O $file https://download.opensuse.org/repositories/home:/kimi:/zoom/AppImage/zoom-latest-Architecture: x86_64.AppImage
fi
if [ -f $file ]; then
    sudo chmod 755 $file
fi
echo
echo "Done."
exit 0
