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