#!/usr/bin/bash

PACKAGES=$(zypper packages --orphaned | grep @System | cut -d '|' -f3 | xargs)

if [ -n $PACKAGES ]; then
  # Empty variable
  echo "No orphaned packages on this system."
  echo "Doing nothing."
else
  # Not empty variable
  echo "These packages are classified as orphan packages"
  echo "and can be removed with the following terminal command:"
  echo
  # Remove orphaned packages command
  # zypper packages --orphaned | grep @System | cut -d '|' -f3 | xargs echo sudo zypper remove --clean-deps
  echo "sudo zypper remove --clean-deps $PACKAGES"
  echo
  sleep 3
  echo
  echo "Issuing the command above for you now..."
  echo "You may need to type in the password for elavated privilegies"
  echo "since you are removing obsolete system packages."
  echo
  sudo zypper remove --clean-deps $PACKAGES
  echo
  echo "Hopefully the command did its job."
  echo
  echo "Done."
fi

exit 0