# Extra env variables for this snippet:
#  - SM_USER
#  - SM_PASS

LOG_FILE=/root/postinstall-subscription-manager.log
[ -n "@NO_SAVE@" ] && LOG_FILE=/dev/null

exec < /dev/tty3 > /dev/tty3
chvt 3
echo "#########################"
echo "# %subscription-manager #"
echo "#########################"
(
  SM_USER='@SM_USER@'
  SM_PASS='@SM_PASS@'
  SM_URL='@SM_URL@'

  # Sanity check.
  skip=
  [ -z "${SM_USER}" ] && skip=true
  [ -z "${SM_PASS}" ] && skip=true
  if [ -z "${SM_URL}" ] || [ "${SM_URL}" = "$(printf '@%s@' SM_URL)" ]; then
    SM_URL=subscription.rhsm.redhat.com
  fi

  if [ -z "${skip}" ]; then
    if rpm -q subscription-manager; then
     subscription-manager register \
       --serverurl="${SM_URL}:443/subscription" \
       --baseurl=https://cdn.redhat.com \
       --username="${SM_USER}" \
       --password="${SM_PASS}" \
       --auto-attach

      subscription-manager status
    fi
  else
    echo "##################################################################"
    echo "#    Kickstart snippet skipped. Please make sure the required    #"
    echo "# variables are defined and the required packages are installed. #"
    echo "##################################################################"
    echo
    echo "* Required variables:"
    echo "SM_USER(${SM_USER})"
    echo "SM_PASS(${SM_PASS})"
    echo "SM_URL(${SM_URL})"
    echo
    echo "* Required packages: (none)"
    echo
    echo "* Installed packages:"
    rpm -qa | sort
    echo
  fi
) 2>&1 | tee "${LOG_FILE}"
chvt 1

# vim:set ts=2 sw=2 et:

