#!/bin/bash

if [ "$1" = "RPMPOSTSCRIPT" ] ; then
  if [ -f /usr/bin/rhn-actions-control ]
  then
    /usr/bin/rhn-actions-control --enable-deploy --enable-diff
  fi

  if [ -f /usr/sbin/steadfast-spacewalk-setup ] ; then
    SERVER=spacewalk
  else
    SERVER=updater
  fi
  cat << EOF

  IMPORTANT!!
    To complete Steadfast Spacewalk Service Setup, you must run as root:
    /usr/sbin/steadfast-$SERVER-setup

EOF
  exit 0
fi

echo $0 | egrep steadfast-spacewalk > /dev/null
if [ $? -eq 0 ] ; then
  SPACEWALKSERVER="spacewalk.steadfast.net"
  STEADFASTID="default"
  RHNACTIONS=1
else
  SPACEWALKSERVER="updater.steadfast.net"
fi

echo $0 | egrep "\-reg$" > /dev/null
if [ $? -eq 0 ] ; then
  REGISTERONLY=1
else
  REGISTERONLY=0
fi

if [ -z "$RHNACTIONS" ] ; then
  RHNACTIONS=1
fi

echo Setup running for $SPACEWALKSERVER
echo ================================================== 
echo

id | egrep "^uid=0" > /dev/null
if [ $? -ne 0 ] ; then
  echo ERROR: Script must be run as user root
  echo Run $0 as root
  exit 1
fi

if [ ! -f /etc/redhat-release ] ; then
  echo ERROR: Not a CentOS system
  exit 1
fi

egrep "^CentOS " /etc/redhat-release > /dev/null
if [ $? -ne 0 ] ; then
  echo ERROR: Not a CentOS system
  exit 1
fi

if [ $REGISTERONLY -eq 0 ] ; then
  echo
  echo Installing Spacewalk Client ...
  echo -------------------------------
  echo
  GPGKEYURL="http://mirror.steadfast.net/spacewalk/RPM-GPG-KEY-spacewalk"
  rpm --import $GPGKEYURL-2015
  cat > /etc/yum.repos.d/steadfast-spacewalk-setup.repo << EOF
[steadfast-mirror-centos-base]
name=Steadfast Mirror CentOS-\$releasever - Base
baseurl=http://mirror.steadfast.net/centos/\$releasever/os/\$basearch/
enabled=0
gpgcheck=1
gpgkey=http://mirror.steadfast.net/centos/RPM-GPG-KEY-CentOS-\$releasever

[steadfast-mirror-centos-updates]
name=Steadfast Mirror CentOS-\$releasever - Updates
baseurl=http://mirror.steadfast.net/centos/\$releasever/updates/\$basearch/
enabled=0
gpgcheck=1
gpgkey=http://mirror.steadfast.net/centos/RPM-GPG-KEY-CentOS-\$releasever

[steadfast-mirror-epel]
name=Steadfast Mirror Extra Packages for Enterprise Linux \$releasever - \$basearch
baseurl=http://mirror.steadfast.net/epel/\$releasever/\$basearch/
enabled=0
gpgcheck=1
gpgkey=http://mirror.steadfast.net/epel/RPM-GPG-KEY-EPEL-\$releasever

[steadfast-spacewalk-client]
name=Spacewalk Client Tools
baseurl=http://mirror.steadfast.net/spacewalk/latest-client/RHEL/\$releasever/\$basearch/
gpgkey=http://mirror.steadfast.net/spacewalk/RPM-GPG-KEY-spacewalk-2015
enabled=0
gpgcheck=1

[steadfast-spacewalk-client-nightly]
name=Spacewalk Client Tools
baseurl=http://mirror.steadfast.net/spacewalk/nightly-client/RHEL/\$releasever/\$basearch/
gpgkey=http://mirror.steadfast.net/spacewalk/RPM-GPG-KEY-spacewalk-2015
enabled=0
gpgcheck=0

[steadfast-mirror-centos-steadfast]
name=Steadfast-\$releasever
baseurl=http://mirror.steadfast.net/centos-steadfast/\$releasever/\$basearch/
enabled=0
gpgcheck=1
EOF
  /usr/bin/yum -y \
    --enablerepo=steadfast-mirror-centos-base \
    --enablerepo=steadfast-mirror-centos-updates \
    --enablerepo=steadfast-mirror-epel \
    --enablerepo=steadfast-spacewalk-client \
    install spacewalk-client-repo rhn-setup yum-rhn-plugin python-dmidecode \
      rhncfg rhncfg-actions rhncfg-client osad python-hashlib wget curl
  EXITCODE=$?
  rm -f /etc/yum.repos.d/steadfast-spacewalk-setup.repo
  if [ $EXITCODE -ne 0 ] ; then
    echo ERROR: Could not install spacewalk client RPMs
    echo Correct the problem and run $0 again
    exit 1
  fi
fi

if [ ! -x /usr/bin/wget ] ; then
  echo
  echo Installing wget application ...
  echo -------------------------------
  echo
  /usr/bin/yum -y install --skip-broken wget curl
fi

if [ ! -x /usr/bin/curl ] ; then
  echo
  echo Installing curl application ...
  echo -------------------------------
  echo
  /usr/bin/yum -y install curl
fi

echo
echo Configuring Spacewalk Client ...
echo --------------------------------
echo
CERTURL="http://$SPACEWALKSERVER/pub/RHN-ORG-TRUSTED-SSL-CERT"
CERTFILE="/usr/share/rhn/RHN-ORG-TRUSTED-SSL-CERT"
wget -O $CERTFILE $CERTURL
if [ ! -s $CERTFILE ] ; then
  curl $CERTURL > $CERTFILE
fi
sed -i \
 's#osa_ssl_cert = .*#osa_ssl_cert = /usr/share/rhn/RHN-ORG-TRUSTED-SSL-CERT#' \
  /etc/sysconfig/rhn/osad.conf
if [ -f /sbin/chkconfig ] ; then
  /sbin/chkconfig osad on
elif [ -f /usr/sbin/chkconfig ] ; then
  /usr/sbin/chkconfig osad on
fi

if [ "$RHNACTIONS" = "1" ] ; then
  echo
  echo Enable privileged RHN actions ...
  echo ---------------------------------
  echo
  /usr/bin/rhn-actions-control --enable-deploy --enable-diff
fi

if [ -z "$STEADFASTID" ] ; then
  echo
  echo Finding Steadfast ID
  echo -----------------------
  echo
  IDURL="https://middle.steadfast.net/uber/pxe_integration/accountID.php"
  STEADFASTID=`wget -qO - $IDURL`
  if [ $? -ne 0 ] ; then
    STEADFASTID=`curl $IDURL`
  fi
  if [ -z $STEADFASTID ] ; then
    echo Could not find the Steadfast ID
    echo Contact support@steadfast.net or 
    echo   set STEADFASTID environmental variable and run $0 again
    exit 1
  fi
else
  echo Found STEADFASTID set to $STEADFASTID
fi

echo
echo Registering Spacewalk Client ...
echo --------------------------------
echo
# ARCH=`uname -i`
# if [ $ARCH != "x86_64" ] ; then
#   ARCH="i386"
# fi
# CENTOSVER=`sed 's/^[^0-9]*//;s/[^0-9].*//' /etc/redhat-release`
/usr/sbin/rhnreg_ks -v --serverUrl=http://$SPACEWALKSERVER/XMLRPC \
  --activationkey=1-$STEADFASTID $*

echo
echo Starting Spacewalk OSA service ...
echo ----------------------------------
echo
if [ -f /sbin/service ] ; then
  /sbin/service osad start
elif [ -f /usr/sbin/service ] ; then
  /usr/sbin/service osad start
fi

echo
echo Checking YUM configuration ...
echo ------------------------------
echo

egrep "^plugins *= *1$" /etc/yum.conf > /dev/null
if [ $? -eq 0 ] ; then
  echo YUM plugins are already enabled
fi

egrep "^plugins *= *0$" /etc/yum.conf > /dev/null
if [ $? -eq 0 ] ; then
  echo YUM plugins disabled, re-enabling them
  sed -i 's/^plugins *= *0$/plugins=1/' /etc/yum.conf
fi

egrep "^plugins *=" /etc/yum.conf > /dev/null
if [ $? -ne 0 ] ; then
  echo YUM plugins option missing
  sed -i 's/^\[main\]$/&\nplugins=1/' /etc/yum.conf
  egrep "^plugins=1$" /etc/yum.conf > /dev/null
  if [ $? -eq 0 ] ; then
    echo Configuration corrected to include YUM plugins option
  else
    echo The YUM "[main]" section not correctly found
    echo Please correct the /etc/yum.conf manually
  fi
fi

echo
echo Script completed
echo ================
echo

