# Function to handle post-installation tasks
post_install() {
  # Enable Systemd for User
  # Check if the directory for user-specific systemd services exists
  if [ ! -d "/etc/systemd/user/default.target.wants/" ]; then
    # Create the directory if it doesn't exist
    mkdir -p /etc/systemd/user/default.target.wants/
  fi
  # Check if the symbolic link for the 'render-menu-options-user.path' exists
  if [ ! -e "/etc/systemd/user/default.target.wants/render-menu-options-user.path" ]; then
    # Create the symbolic link if it doesn't exist
    ln -s /usr/lib/systemd/user/render-menu-options-user.path /etc/systemd/user/default.target.wants/render-menu-options-user.path
  fi

  # Check if the symbolic link for the 'render-menu-options-user.service' exists
  if [ -e "/etc/systemd/user/default.target.wants/render-menu-options-user.service" ]; then
    rm /etc/systemd/user/default.target.wants/render-menu-options-user.service
  fi

  # Start the 'render-menu-options' for user HOME folder
  for user in $(awk -F':' '{ if ($3 >= 1000 && $1 != "nobody") print $1 }' /etc/passwd); do
    if [ -e /home/"${user}"/.local/share/applications ];then
        /usr/share/render-menu-options/render-menu-options.sh "/home/"${user}"/.local/share/applications/"
    fi
  done

  # Enable and start the 'render-menu-options' systemd path unit
  systemctl enable render-menu-options.path
  # Enable and start the 'render-menu-options-flatpak' systemd path unit
  systemctl enable render-menu-options-flatpak.path
  # Start the 'render-menu-options' systemd service unit
  systemctl start render-menu-options.service &
  # Start the 'render-menu-options-flatpak' systemd service unit
  systemctl start render-menu-options-flatpak.service &
}

# Function to handle post-upgrade tasks, which simply calls post_install
post_upgrade() {
  post_install
}

pre_remove() {
  rm /etc/systemd/user/default.target.wants/render-menu-options-user.path
  rm /etc/systemd/user/default.target.wants/render-menu-options-user.service
  systemctl disable --now render-menu-options.path
  systemctl disable --now render-menu-options-flatpak.path
}

post_remove() {
  IFS=$'\n'

  folderType=(
  /usr/share/applications
  )
  #if the flatpak directory exists add it to $folderType
  if [ -d /var/lib/flatpak/exports/share/applications ];then
    folderType+=(/var/lib/flatpak/exports/share/applications)
  fi
  #add users to $folderType
  for user in $(awk -F':' '{ if ($3 >= 1000 && $1 != "nobody") print $1 }' /etc/passwd); do
    if [ -e /home/"${user}"/.local/share/applications ];then
      folderType+=(/home/"${user}"/.local/share/applications)
    fi
  done

  RenderType=(
  SoftwareRender
  NvidiaRender
  IntegratedRender
  AmdRender
  )

  for render in ${RenderType[@]}; do
    for folder in ${folderType[@]}; do
      Applications=$(grep -sl "Action $render" $folder/*.desktop)
      for App in ${Applications[@]}; do
        #remove app from action=
        sed -i "/Actions=/s/$render;//g" $App
        #remove Desktop Action
        sed -i "/Desktop Action $render/,/^$/d" $App
      done
      update-desktop-database -q $folder
    done
  done
}
