#!/usr/bin/env bash
# Completely uninstall linphone 4.4.6-beta.0
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

cd $SCRIPT_DIR
echo

/usr/local/linphone-desktop/4.4.6-beta.0/bin/pre-uninstall-linphone

function shouldbedeleted() {
  string=$(realpath "$1")
  # Checking path: $string
  if [[ $string == *"4.4.6-beta.0"* ]]; then
    # 0 = true
    # "Should be deleted."
    return 0 
  else
    # 1 = false
    # "Keep."
    return 1
  fi
}

# Check if root
if [ "$EUID" -ne 0 ]
  then echo "Please run as root or type: sudo uninstall-linphone and press TAB key twice to auto-fill"
  exit
fi

CHECK_IF_DEB_IS_INSTALLED=$(dpkg-query -W --showformat='${Status}' kimi-linphone-desktop-beta | grep "install ok installed")
if [ -n "$CHECK_IF_DEB_IS_INSTALLED" ]; then
    echo "linphone-desktop was installed as DEB package kimi-linphone-desktop-beta"
    sleep 2
    echo "I will try to uninstall the DEB package kimi-linphone-desktop-beta."
    sleep 2
    echo "Then I will check that everything was deleted."
    sleep 2
    echo "Uninstall command:"
    echo "   apt-get -y remove --purge kimi-linphone-desktop-beta"
    sleep 2
    apt-get -y remove --purge kimi-linphone-desktop-beta && apt-get -y autoremove
else
    echo "It looks like variable $CHECK_IF_DEB_IS_INSTALLED is empty."
    sleep 2
    echo "That means that the DEB package kimi-linphone-desktop-beta is not installed."
    sleep 2
    echo CHECK_IF_DEB_IS_INSTALLED=$CHECK_IF_DEB_IS_INSTALLED
    sleep 2
fi

Deleting files if they still exist:
    echo "rm -f linphone446-beta0"
rm -f linphone446-beta0
    echo "rm -f change-linphone446-beta0-icon"
rm -f change-linphone446-beta0-icon
    echo "rm -f uninstall-linphone446-beta0"
rm -f uninstall-linphone446-beta0
    echo "rm -f pre-uninstall-linphone446-beta0"
rm -f pre-uninstall-linphone446-beta0

for fileorlink in linphone*
do
    if $(shouldbedeleted "$fileorlink"); then
        # $fileorlink will be deleted
        # Delete command:
        rm -f "$fileorlink"
    fi
done

# To find all broken symbolic links under a given directory, run the following command to delete them:
echo
echo "Find broken symbolic links commands:"
echo "find . -xtype l -delete (run it twice)"
echo
echo "find /usr/local/linphone-desktop -xtype l -delete (run it twice)"
find /usr/local/linphone-desktop -xtype l -delete 2>/dev/null
find /usr/local/linphone-desktop -xtype l -delete 2>/dev/null

echo "cd /usr/local/bin"
cd /usr/local/bin
echo

echo
echo "find /usr/local/bin -xtype l -delete"
find /usr/local/bin -xtype l -delete 2>/dev/null
find /usr/local/bin -xtype l -delete 2>/dev/null
echo


for fileorlink in linphone*
do
    if $(shouldbedeleted "$fileorlink"); then
        # $fileorlink will be deleted
        # Delete command:
        rm -f "$fileorlink"
    fi
done

echo
echo "Deleted directory /usr/local/linphone-desktop/4.4.6-beta.0"
# If directory above, linphone-destop is empty, then it can also be deleted.
if [ -z "$(ls -A /usr/local/linphone-desktop)" ]; then
   echo
   echo "/usr/local/linphone-desktop is empty"
   echo "The directory:"
   echo "/usr/local/linphone-desktop/4.4.6-beta.0"
   echo "will also be deleted."
   echo
   echo "Delete command:"
   echo "rm -rf /usr/local/linphone-desktop/4.4.6-beta.0"
   rm -rf "/usr/local/linphone-desktop/4.4.6-beta.0"
else
   echo
   echo "/usr/local/linphone-desktop is not empty"
   echo "Keeping the directory."
fi


echo
echo
echo "[OK] Completely removed linphone-desktop 4.4.6-beta.0"
echo
exit 0
