#!/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

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 pre-uninstall-linphone and press TAB key twice to auto-fill"
  exit
fi

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:

# Find broken symbolic links commands:
# find . -xtype l -delete 2>/dev/null (run it twice)

# find . -xtype l -delete 2>/dev/null
# find . -xtype l -delete 2>/dev/null

echo "Removing broken links in /usr/local/linphone-desktop"
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 "Removing broken links in /usr/local/bin"
echo "find /usr/local/bin -xtype l -delete (run it twice)"
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"
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"
ls -al "/usr/local/linphone-desktop"
 
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 "should 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 "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
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" ]; then
        echo "User: $user"
    fi
    if [ -d "/home/$user/.local/share/icons/hicolor" ]; then
        # echo "User $user has directory /home/$user/.local/share/icons/hicolor"
        # echo "rm -f home/$user/.local/share/icons/hicolor/scalable/apps/linphone446-beta0.svg"
        rm -f "/home/$user/.local/share/icons/hicolor/scalable/apps/linphone446beta0.svg"
        echo "touch /home/$user/.local/share/icons/hicolor"
        touch "/home/$user/.local/share/icons/hicolor"
        # echo "gtk-update-icon-cache /home/$user/.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/\ser/.local/share/applications"
        # echo "rm -f home/$user/.local/share/applications/linphone446-beta0.desktop"
        rm -f "/home/$user/.local/share/share/applications/linphone446-beta0.desktop"
    fi
    if [ -d "/home/$user/.config/autostart" ]; then
        # echo "User $user has directory /home/ser/.config/autostart"
        # echo "rm -f /home/$user/.config/autostart/linphone446-beta0-minimized.desktop"
        rm -f /home/$user/.config/autostart/linphone446-beta0-minimized.desktop
        echo "Deleted /home/$user/.config/autostart/linphone446-beta0-minimized.desktop"
    fi
done
echo
echo "[OK] Completely removed all links to linphone-desktop 4.4.6-beta.0 from user's accounts."
echo
exit 0
