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

cd $SCRIPT_DIR
echo

function shouldbedeleted() {
  string=$(realpath "$1")
  # Checking path: $string
  if [[ $string == *"5.1.2"* ]]; 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 for auto-fill-in"
  exit
fi

CHECK_IF_RPM_IS_INSTALLED=`rpm -qa kimi-linphone-desktop-5.1.2`
if [ -n  ]; then
    echo "linphone-desktop was installed as RPM package kimi-linphone-desktop-5.1.2"
    sleep 2
    echo "I will try to uninstall the RPM package kimi-linphone-desktop-5.1.2."
    sleep 2
    echo "Then I will check that everything was deleted."
    sleep 2
    echo "Uninstall command:"
    echo "   dnf -y remove kimi-linphone-desktop-5.1.2"
    sleep 2
    dnf -y remove kimi-linphone-desktop-5.1.2
else
    echo "It looks like variable $CHECK_IF_RPM_IS_INSTALLED is empty."
    sleep 2
    echo "That means that the rpm package kimi-linphone-desktop-5.1.2 is not longer installed."
    sleep 2
    sleep 2
fi

#Deleting files if they still exist:
    echo "rm -f linphone512"
rm -f linphone512
    echo "rm -f change-linphone512-icon"
rm -f change-linphone512-icon
    echo "rm -f uninstall-linphone512"
rm -f uninstall-linphone512

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
find . -xtype l -delete 2>/dev/null
find . -xtype l -delete 2>/dev/null
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"
    sleep 2
cd /usr/local/bin
echo

echo
echo "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/5.1.2"
echo "If directory above, linphone-destop is empty, then it can also be deleted."
echo
echo "Directory list command:"
echo "ls -al /usr/local/linphone-desktop/5.1.2/../"
ls -al "/usr/local/linphone-desktop/5.1.2/../"
 
if [ -z "$(ls -A /usr/local/linphone-desktop/5.1.2/../)" ]; then
   echo
   echo "/usr/local/linphone-desktop/5.1.2/../ is empty"
   echo "The directory:"
   echo "/usr/local/linphone-desktop/5.1.2"
   echo "should also be deleted."
   echo
   echo "Delete command:"
   echo "rm -rf /usr/local/linphone-desktop/5.1.2"
   rm -rf "/usr/local/linphone-desktop/5.1.2"
else
   echo
   echo "/usr/local/linphone-desktop/5.1.2/../ is not empty"
   echo "Keeping the directory."
fi


echo
echo "Broken symbolic links:"
echo "find /usr/local/bin -xtype l -delete (run it twice)"
echo
find /usr/local/bin -xtype l -delete 2>/dev/null
find /usr/local/bin -xtype l -delete 2>/dev/null

# Users on the system
USERS=$(awk -F':' '{ print $1}' /etc/passwd)
echo "These users seems to have access to linphone:"
echo $USERS
sleep 2
echo "I will now remove the links to linphone for:"
echo $USERS
sleep 2
#for username in $(awk -F':' '{ print $1}' /etc/passwd); do
for user in $USERS; do
    #echo User: $user
    if [ -d "/home/$user/.local/share/icons/hicolor" ]; then
        echo "User $user\ has directory /home/$user/.local/share/icons/hicolor"
        echo "rm -f home//.local/share/icons/hicolor/scalable/apps/linphone512.svg"
        rm -v -f "/home/$user/.local/share/icons/hicolor/scalable/apps/linphone512.svg"
        echo "touch /home/$user/.local/share/icons/hicolor"
        touch "/home/$user/.local/share/icons/hicolor"
        echo "gtk-update-icon-cache /home//.local/share/icons/hicolor > /dev/null"
        gtk-update-icon-cache "/home/$user/.local/share/icons/hicolor" > /dev/null       
    fi
    if [ -d "/home/$user/.local/share/applications" ]; then
        echo "User $user\ has directory /home/$user/.local/share/applications"
        echo "rm -f home//.local/share/applications/linphone512.desktop"
        rm -v -f "/home/$user/.local/share/applications/linphone512.desktop"
    fi
    if [ -d "/home/$user/.config/autostart" ]; then
        echo "User $user has directory /home//.config/autostart"
        echo "rm -f /home/$user/.config/autostart/linphone512-minimized.desktop"
        rm -f /home/$user/.config/autostart/linphone512-minimized.desktop
        echo "Deleted /home//.config/autostart/linphone512-minimized.desktop"
    fi
done
echo
echo "[OK] Completely removed linphone-desktop 5.1.2"
echo
exit 0
