#!/bin/bash
# Spacewalk Client Registration Script
# Version 2.0
# Created by Jeremy Davis

## Obtain the Spacewalk Proxy and Activation Key from first and second arguments. Check to make sure everything is good.
SPACEWALK_PROXY=$1
ACTIVATION_KEY=$2

if [ -z $SPACEWALK_PROXY ]
    then
        echo "Proxy server was not given as the first arqument!"
        exit 1
fi

if [ -z $ACTIVATION_KEY ]
    then
        echo "Activation Key was not given as the second arqument!"
        exit 1
fi

## Perform checks to make sure everything is ready for Spacewalk registration.
# Chec to make sure the proxy is resolvable
/usr/bin/host $SPACEWALK_PROXY &> /dev/null

# Obtain exit code of host check.
HOSTEXITCODE=$?

# Check to make sure the proxy resolved.
if [ $HOSTEXITCODE != 0 ]
    then
        echo "Proxy server is not resolvable please check DNS. Spacewalk Proxy server - $SPACEWALK_PROXY"
        exit 1
fi

# Check to see if ports are open on the proxy server.
# Port 80 check
/usr/bin/nc -zw 2 $SPACEWALK_PROXY 80 &> /dev/null

# Obtain exit code of port check.
PORTCKEXITCODE=$?

if [ $PORTCKEXITCODE != 0 ]
    then
        echo "Port 80 is not open on Spacewalk Proxy server - $SPACEWALK_PROXY"
        exit 1
fi

# Port 443 check
/usr/bin/nc -zw 2 $SPACEWALK_PROXY 443 &> /dev/null

# Obtain exit code of port check.
PORTCKEXITCODE=$?

if [ $PORTCKEXITCODE != 0 ]
    then
        echo "Port 443 is not open on Spacewalk Proxy server - $SPACEWALK_PROXY"
        exit 1
fi

# Port 5222 check
/usr/bin/nc -zw 2 $SPACEWALK_PROXY 5222 &> /dev/null

# Obtain exit code of port check.
PORTCKEXITCODE=$?

if [ $PORTCKEXITCODE != 0 ]
    then
        echo "Port 5222 is not open on Spacewalk Proxy server - $SPACEWALK_PROXY"
        exit 1
fi

# Port 5269 check
/usr/bin/nc -zw 2 $SPACEWALK_PROXY 5269 &> /dev/null

# Obtain exit code of port check.
PORTCKEXITCODE=$?

if [ $PORTCKEXITCODE != 0 ]
    then
        echo "Port 5269 is not open on Spacewalk Proxy server - $SPACEWALK_PROXY"
        exit 1
fi

## Perform checks on the system being registered to make sure Spacewalk supports the system.
# Obtain version of OS that is currently installed and assign it to the UF_OS-VER variable.
UF_OS_VER=`/bin/rpm -qa | /bin/grep -E 'centos-release-4|redhat-release-4|centos-release-5|redhat-release-5|centos-release-6|redhat-release-6'`

# Obtain exit code of OS check
OSCHECKEXITCODE=$?

if [ $OSCHECKEXITCODE != 0 ]
    then
        echo "Unable to determine OS version!"
        exit 1
fi

# Obtain UF_OS_VER and format the version to later be used during script.
case $UF_OS_VER in
    *release-4*)
        echo "This machine is a CentOS/RHEL 4 machine! Setting OS version for use during script!"
        OS_VER="4"
        ;;
    *release-5*)
        echo "This machine is a CentOS/RHEL 5 machine! Setting OS version for use during script!"
        OS_VER="5"
        ;;
    *release-6*)
        echo "This machine is a CentOS/RHEL 6 machine! Setting OS version for use during script!"
        OS_VER="6"
        ;;
    *)
        echo "This machine is NOT supported by this script! Please contact spacewalk@godaddy.com for assistance!"
        exit 1
        ;;
esac

# Obtain arch of the system currently being registered and assign it to the UF_ARCH variable.
UF_ARCH=`/bin/uname -i`

# Obtain exit code of Arch check
ARCHCHECKEXITCODE=$?

if [ $ARCHCHECKEXITCODE != 0 ]
    then
        echo "Unable to determine system architecture!"
        exit 1
fi

# Obtain UF_ARCH and format the version to later be used during script.
case $UF_ARCH in
    *i386*)
        echo "This machine has a 32bit Architecture!"
        ARCH="32bit"
        ;;
    *x86_64*)
        echo "This machine has a 64bit Architecture!"
        ARCH="64bit"
        ;;
    *)
        echo "The Architecture on this machine is NOT supported by this script! Please contact spacewalk@godaddy.com for assistance!"
        exit 1
        ;;
esac

## Backup current configurations before registration.
# If the directory dosn't exists perform the backup.
if [ ! -d "/root/sw-reg-bk/" ]
    then
# Create backup directory if it does not exists.
        /bin/mkdir -p /root/sw-reg-bk/yum-backup/

# Backup yum configuration
        /bin/cp -f /etc/yum.conf /root/sw-reg-bk/yum-backup/
        /bin/cp -f /etc/yum.repos.d/*.repo /root/sw-reg-bk/yum-backup/
fi

# Check to make sure yum.conf back was successful.
if [ ! -f "/root/sw-reg-bk/yum-backup/yum.conf" ]
    then
        echo "Yum configuration backup was not successful!"
        exit 1
fi

# Check to make sure .repo backup was successful.
REPOFILES=`ls /root/sw-reg-bk/yum-backup/*.repo 2> /dev/null | wc -w`
if [ $REPOFILES = 0 ]
    then
        echo "Yum repository configuration backup was not successful!"
        exit 1
fi

### Setup system to install Spacewalk packages.
# Remove yum repository configuration.
/bin/rm -f /etc/yum.repos.d/*.repo

# Obtain exit code of rm command.
RMEXITCODE=$?

# Check to make sure status returns 0.
if [ $RMEXITCODE != 0 ]
    then
        echo "/etc/yum.repos.d/*.repo files were not removed correctly!"
        exit 1
fi

# Remove yum configuration.
/bin/rm -f /etc/yum.conf

# Obtain exit code of rm command.
RMEXITCODE=$?

# Check to make sure status returns 0.
if [ $RMEXITCODE != 0 ]
    then
        echo "/etc/yum.conf file was not removed correctly!"
        exit 1
fi

# Add null CentOS Base YUM repository configuration files.
/bin/touch /etc/yum.repos.d/CentOS-Base.repo

# Obtain exit code of touch command.
TOUCHEXITCODE=$?

# Check to make sure status returns 0.
if [ $TOUCHEXITCODE != 0 ]
    then
        echo "The /etc/yum.repos.d/CentOS-Base.repo file was not created!"
        exit 1
fi

# Add null CentOS Base YUM repository configuration files.
/bin/touch /etc/yum.repos.d/CentOS-Media.repo

# Obtain exit code of touch command.
TOUCHEXITCODE=$?

# Check to make sure status returns 0.
if [ $TOUCHEXITCODE != 0 ]
    then
        echo "The /etc/yum.repos.d/CentOS-Media.repo file was not created!"
        exit 1
fi

# Add null CentOS Base YUM repository configuration files.
/bin/touch /etc/yum.repos.d/CentOS-Vault.repo

# Obtain exit code of touch command.
TOUCHEXITCODE=$?

# Check to make sure status returns 0.
if [ $TOUCHEXITCODE != 0 ]
    then
        echo "The /etc/yum.repos.d/CentOS-Vault.repo file was not created!"
        exit 1
fi

# Add null CentOS Base YUM repository configuration files.
/bin/touch /etc/yum.repos.d/CentOS-Debuginfo.repo

# Obtain exit code of touch command.
TOUCHEXITCODE=$?

# Check to make sure status returns 0.
if [ $TOUCHEXITCODE != 0 ]
    then
        echo "The /etc/yum.repos.d/CentOS-Debuginfo.repo file was not created!"
        exit 1
fi

#### CentOS/RHEL 4 Install function ####
function V4-INSTALL {
## Configure yum for Spacewalk install.
# Remove yum repository configuration.
/bin/rm -f /etc/yum.repos.d/*.repo

# Obtain exit code of rm command.
RMEXITCODE=$?

# Check to make sure status returns 0.
if [ $RMEXITCODE != 0 ]
    then
        echo "/etc/yum.repos.d/*.repo files were not removed correctly!"
        exit 1
fi

# Configure yum.conf with correct defaults.
# Update the /etc/yum.conf with the latest repositories to enable the server to register into Spacewalk.
cat << EOF > "/etc/yum.conf"
[main]
cachedir=/var/cache/yum
debuglevel=2
logfile=/var/log/yum.log
pkgpolicy=newest
distroverpkg=redhat-release
assumeyes=0
tolerant=1
exactarch=1
diskspacecheck=1
bootloader=1

[spacewalk-base]
name=Spacewalk base
baseurl=http://$SPACEWALK_PROXY/ks/dist/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-updates]
name=Spacewalk updates
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-centos-production-updates-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-extras]
name=Spacewalk extras
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-centos-production-extras-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-client]
name=Spacewalk client
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-spacewalk-client-prod-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-epel]
name=Spacewalk epel
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-spacewalk-epel-prod-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1
EOF

# Obtain exit code of cat command.
CATEXITCODE=$?

# Check to make sure status returns 0.
if [ $CATEXITCODE != 0 ]
    then
        echo "The /etc/yum.conf file was not created!"
        exit 1
fi

## Install Spacewalk Client packages.
# Yum install packages.
    /usr/bin/yum -y install up2date osad rhncfg* 1> /dev/null

# Obtain exit code of yum install.
    YUMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $YUMEXITCODE != 0 ]
        then
            echo "Yum was unable to install packages!"
            exit 1
    fi

# Makre sure client packages are installed correctly.
# rhn-client-tools check
    /bin/rpm -qa | grep up2date &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package up2date was not installed correctly!"
            exit 1
    fi
# osad check
    /bin/rpm -qa | grep osad &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package osad was not installed correctly!"
            exit 1
    fi

# rhncfg check
    /bin/rpm -qa | grep rhncfg &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg was not installed correctly!"
            exit 1
    fi

# rhncfg-actions check
    /bin/rpm -qa | grep rhncfg-actions &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-actions was not installed correctly!"
            exit 1
    fi

# rhncfg-client check
    /bin/rpm -qa | grep rhncfg-client &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-client was not installed correctly!"
            exit 1
    fi

# rhncfg-management check
    /bin/rpm -qa | grep rhncfg-management &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-management was not installed correctly!"
            exit 1
    fi

# Configure yum.conf with correct defaults.
cat << EOF > "/etc/yum.conf"
[main]
cachedir=/var/cache/yum
debuglevel=2
logfile=/var/log/yum.log
pkgpolicy=newest
distroverpkg=redhat-release
assumeyes=0
tolerant=1
exactarch=1
diskspacecheck=1
bootloader=1
EOF

# Obtain exit code of cat command.
    CATEXITCODE=$?

# Check to make sure status returns 0.
    if [ $CATEXITCODE != 0 ]
        then
            echo "The /etc/yum.conf file was not created!"
            exit 1
    fi

# Configure /etc/sysconfig/rhn/sources with correct defaults.
cat << EOF > "/etc/sysconfig/rhn/sources"
up2date default
EOF

# Check to make sure status returns 0.
    if [ $CATEXITCODE != 0 ]
        then
            echo "The /etc/sysconfig/rhn/sources file was not created!"
            exit 1
    fi

}

#### CentOS/RHEL 5 Install function ####
function V5-INSTALL {
## Configure yum for Spacewalk install.
# Configure yum.conf with correct defaults.
cat << EOF > /etc/yum.conf
[main]
distroverpkg=centos-release
cachedir=/var/cache/yum
logfile=/var/log/yum.log
pkgpolicy=newest
assumeyes=0
bootloader=1
debuglevel=2
diskspacecheck=1
exactarch=1
keepcache=0
metadata_expire=1
obsoletes=1
plugins=1
tolerant=1
multilib_policy=best
reposdir=/etc/yum.repos.d
EOF

# Obtain exit code of cat command.
CATEXITCODE=$?

# Check to make sure status returns 0.
if [ $CATEXITCODE != 0 ]
    then
        echo "The /etc/yum.conf file was not created!"
        exit 1
fi


# Configure yum repository configuration.
cat << EOF > /etc/yum.repos.d/spacewalk.repo
[spacewalk-base]
name=Spacewalk base
baseurl=http://$SPACEWALK_PROXY/ks/dist/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-updates]
name=Spacewalk updates
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-centos-production-updates-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-extras]
name=Spacewalk extras
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-centos-production-extras-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-client]
name=Spacewalk client
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-spacewalk-client-prod-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-epel]
name=Spacewalk epel
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-spacewalk-epel-prod-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1
EOF

# Obtain exit code of cat command.
CATEXITCODE=$?

# Check to make sure status returns 0.
if [ $CATEXITCODE != 0 ]
    then
        echo "The /etc/yum.repos.d/spacewalk.repo file was not created!"
        exit 1
fi

## Install Spacewalk Client packages.
# Yum install packages.
    /usr/bin/yum -y install rhn-client-tools rhn-check rhn-setup rhnsd m2crypto yum-rhn-plugin osad rhncfg* 1> /dev/null

# Obtain exit code of yum install.
    YUMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $YUMEXITCODE != 0 ]
        then
            echo "Yum was unable to install packages!"
            exit 1
    fi

# Makre sure client packages are installed correctly.
# rhn-client-tools check
    /bin/rpm -qa | grep rhn-client-tools &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhn-client-tools was not installed correctly!"
            exit 1
    fi

# rhn-check check
    /bin/rpm -qa | grep rhn-check &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhn-check was not installed correctly!"
            exit 1
    fi

# rhn-setup check
    /bin/rpm -qa | grep rhn-setup &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhn-setup was not installed correctly!"
            exit 1
    fi

# rhnsd check
    /bin/rpm -qa | grep rhnsd &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhnsd was not installed correctly!"
            exit 1
    fi

# m2crypto check
    /bin/rpm -qa | grep m2crypto &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package m2crypto was not installed correctly!"
            exit 1
    fi

# yum-rhn-plugin check
    /bin/rpm -qa | grep yum-rhn-plugin &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package yum-rhn-plugin was not installed correctly!"
            exit 1
    fi

# osad check
    /bin/rpm -qa | grep osad &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package osad was not installed correctly!"
            exit 1
    fi

# rhncfg check
    /bin/rpm -qa | grep rhncfg &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg was not installed correctly!"
            exit 1
    fi

# rhncfg-actions check
    /bin/rpm -qa | grep rhncfg-actions &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-actions was not installed correctly!"
            exit 1
    fi

# rhncfg-client check
    /bin/rpm -qa | grep rhncfg-client &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-client was not installed correctly!"
            exit 1
    fi

# rhncfg-management check
    /bin/rpm -qa | grep rhncfg-management &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-management was not installed correctly!"
            exit 1
    fi

# Modify /etc/yum/pluginconf.d/rhnplugin.conf to not check for gpg keys during install. This will resolve issue until we can get a signed gdg key for all packages.
    /bin/sed -i s'/gpgcheck = 1/gpgcheck = 0/'g /etc/yum/pluginconf.d/rhnplugin.conf

# Obtain exit code of sed command.
    SEDEXITCODE=$?

# Check to make sure status returns 0.
    if [ $SEDEXITCODE != 0 ]
        then
            echo "gpgcheck was not disabled correctly in /etc/yum/pluginconf.d/rhnplugin.conf!"
            exit 1
    fi
    
# Remove spacewalk.repo config.
    /bin/rm -f /etc/yum.repos.d/spacewalk.repo

# Obtain exit code of rm command.
    RMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RMEXITCODE != 0 ]
        then
            echo "/etc/yum.repos.d/spacewalk.repo file was not removed correctly!"
            exit 1
    fi
}

#### CentOS/RHEL 6 Install function ###
function V6-INSTALL {
## Configure yum for Spacewalk install.
# Configure yum.conf with correct defaults.
cat << EOF > /etc/yum.conf
[main]
distroverpkg=centos-release
cachedir=/var/cache/yum
logfile=/var/log/yum.log
pkgpolicy=newest
assumeyes=0
bootloader=1
debuglevel=2
diskspacecheck=1
exactarch=1
keepcache=1
metadata_expire=1
obsoletes=1
plugins=1
tolerant=1
reposdir=/etc/yum.repos.d
EOF

# Obtain exit code of cat command.
CATEXITCODE=$?

# Check to make sure status returns 0.
if [ $CATEXITCODE != 0 ]
    then
        echo "The /etc/yum.conf file was not created!"
        exit 1
fi


# Configure yum repository configuration.
cat << EOF > /etc/yum.repos.d/spacewalk.repo
[spacewalk-base]
name=Spacewalk base
baseurl=http://$SPACEWALK_PROXY/ks/dist/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-updates]
name=Spacewalk updates
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-centos-production-updates-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-extras]
name=Spacewalk extras
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-centos-production-extras-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-client]
name=Spacewalk client
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-spacewalk-client-prod-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1

[spacewalk-epel]
name=Spacewalk epel
baseurl=http://$SPACEWALK_PROXY/ks/dist/child/gd-spacewalk-epel-prod-v$OS_VER-$ARCH/gd-centos-production-base-v$OS_VER-$ARCH
enabled=1
EOF

# Obtain exit code of cat command.
CATEXITCODE=$?

# Check to make sure status returns 0.
if [ $CATEXITCODE != 0 ]
    then
        echo "The /etc/yum.repos.d/spacewalk.repo file was not created!"
        exit 1
fi

## Install Spacewalk Client packages.
# Yum install packages.
    /usr/bin/yum -y install rhn-client-tools rhn-check rhn-setup rhnsd m2crypto yum-rhn-plugin osad rhncfg* 1> /dev/null

# Obtain exit code of yum install.
    YUMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $YUMEXITCODE != 0 ]
        then
            echo "Yum was unable to install packages!"
            exit 1
    fi

# Makre sure client packages are installed correctly.
# rhn-client-tools check
    /bin/rpm -qa | grep rhn-client-tools &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhn-client-tools was not installed correctly!"
            exit 1
    fi

# rhn-check check
    /bin/rpm -qa | grep rhn-check &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhn-check was not installed correctly!"
            exit 1
    fi

# rhn-setup check
    /bin/rpm -qa | grep rhn-setup &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhn-setup was not installed correctly!"
            exit 1
    fi

# rhnsd check
    /bin/rpm -qa | grep rhnsd &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhnsd was not installed correctly!"
            exit 1
    fi

# m2crypto check
    /bin/rpm -qa | grep m2crypto &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package m2crypto was not installed correctly!"
            exit 1
    fi

# yum-rhn-plugin check
    /bin/rpm -qa | grep yum-rhn-plugin &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package yum-rhn-plugin was not installed correctly!"
            exit 1
    fi

# osad check
    /bin/rpm -qa | grep osad &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package osad was not installed correctly!"
            exit 1
    fi

# rhncfg check
    /bin/rpm -qa | grep rhncfg &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg was not installed correctly!"
            exit 1
    fi

# rhncfg-actions check
    /bin/rpm -qa | grep rhncfg-actions &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-actions was not installed correctly!"
            exit 1
    fi

# rhncfg-client check
    /bin/rpm -qa | grep rhncfg-client &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-client was not installed correctly!"
            exit 1
    fi

# rhncfg-management check
    /bin/rpm -qa | grep rhncfg-management &> /dev/null

# Obtain exit code of package check
    RPMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RPMEXITCODE != 0 ]
        then
            echo "The package rhncfg-management was not installed correctly!"
            exit 1
    fi

# Modify /etc/yum/pluginconf.d/rhnplugin.conf to not check for gpg keys during install. This will resolve issue until we can get a signed gdg key for all packages.
    /bin/sed -i s'/gpgcheck = 1/gpgcheck = 0/'g /etc/yum/pluginconf.d/rhnplugin.conf

# Obtain exit code of sed command.
    SEDEXITCODE=$?

# Check to make sure status returns 0.
    if [ $SEDEXITCODE != 0 ]
        then
            echo "gpgcheck was not disabled correctly in /etc/yum/pluginconf.d/rhnplugin.conf!"
            exit 1
    fi
    
# Remove spacewalk.repo config.
    /bin/rm -f /etc/yum.repos.d/spacewalk.repo

# Obtain exit code of rm command.
    RMEXITCODE=$?

# Check to make sure status returns 0.
    if [ $RMEXITCODE != 0 ]
        then
            echo "/etc/yum.repos.d/spacewalk.repo file was not removed correctly!"
            exit 1
    fi
}

# Run the actual install steps depending on the version of the OS.
case $OS_VER in
    4)
        echo "This machine is a CentOS/RHEL 4 machine! Using CentOS 4 registration steps!"
        V4-INSTALL
        ;;
    5)
        echo "This machine is a CentOS/RHEL 5 machine! Using CentOS 5 registration steps!"
        V5-INSTALL
        ;;
    6)
        echo "This machine is a CentOS/RHEL 6 machine! Using CentOS 6 registration steps!"
        V6-INSTALL
        ;;
    *)
        echo "This machine is NOT supported by this script! Please contact spacewalk@godaddy.com for assistance!"
        exit 1
        ;;
esac

## Prepare system for Spacewalk registration.
# Create /etc/sysconfig/rhn/allowed-actions/script/ if not already created.
/bin/mkdir -p /etc/sysconfig/rhn/allowed-actions/script/

# Obtain exit code of touch command.
MKDIREXITCODE=$?

# Check to make sure status returns 0.
if [ $MKDIREXITCODE != 0 ]
    then
        echo "The /etc/sysconfig/rhn/allowed-actions/script/ directory was not created!"
        exit 1
fi

# Add the run file to /etc/sysconfig/rhn/allowed-actions/script to allow remote command capabilities.
/bin/touch /etc/sysconfig/rhn/allowed-actions/script/run

# Obtain exit code of touch command.
TOUCHEXITCODE=$?

# Check to make sure status returns 0.
if [ $TOUCHEXITCODE != 0 ]
    then
        echo "The /etc/sysconfig/rhn/allowed-actions/script/run file was not created!"
        exit 1
fi

# Enable configuration management.
/usr/bin/rhn-actions-control --enable-all

# Obtain exit code of rhn-actions-control command.
RACEXITCODE=$?

# Check to make sure status returns 0.
if [ $RACEXITCODE != 0 ]
    then
        echo "Actions were not enabled!"
        exit 1
fi

# Download Spacewalk SSL Certificate.
/usr/bin/wget -q -O /usr/share/rhn/RHN-ORG-TRUSTED-SSL-CERT http://$SPACEWALK_PROXY/pub/RHN-ORG-TRUSTED-SSL-CERT

# Obtain exit code of wget command.
WGETEXITCODE=$?

# Check to make sure status returns 0.
if [ $WGETEXITCODE != 0 ]
    then
        echo "RHN SSL Cert was not downloaded correctly!"
        exit 1
fi

# Add the Spaceawlk SSL configuration to osad configuration.
/bin/sed -i s'/osa_ssl_cert = .*/osa_ssl_cert = \/usr\/share\/rhn\/RHN-ORG-TRUSTED-SSL-CERT/'g /etc/sysconfig/rhn/osad.conf

# Obtain exit code of sed command.
SEDEXITCODE=$?

# Check to make sure status returns 0.
if [ $SEDEXITCODE != 0 ]
    then
        echo "SSL configuration was not added to osad.conf correctly!"
        exit 1
fi

# Modify /etc/sysconfig/rhn/up2date to not check for gpg keys during install. This will resolve issue until we can get a signed gdg key for all packages.
/bin/sed -i s'/useGPG=.*/useGPG=0/'g /etc/sysconfig/rhn/up2date

# Obtain exit code of sed command.
SEDEXITCODE=$?

# Check to make sure status returns 0.
if [ $SEDEXITCODE != 0 ]
    then
        echo "GPG configuration was not updated in /etc/sysconfig/rhn/up2date correctly!"
        exit 1
fi

# Modify /etc/sysconfig/rhn/up2date to not exclude kernel updates.
/bin/sed -i s'/removeSkipList=.*/removeSkipList=/'g /etc/sysconfig/rhn/up2date

# Obtain exit code of sed command.
SEDEXITCODE=$?

# Check to make sure status returns 0.
if [ $SEDEXITCODE != 0 ]
    then
        echo "removeSkipList configuration was not updated in /etc/sysconfig/rhn/up2date correctly!"
        exit 1
fi

# Modify /etc/sysconfig/rhn/up2date to not exclude kernel updates.
/bin/sed -i s'/pkgsToInstallNotUpdate=.*/pkgsToInstallNotUpdate=/'g /etc/sysconfig/rhn/up2date

# Obtain exit code of sed command.
SEDEXITCODE=$?

# Check to make sure status returns 0.
if [ $SEDEXITCODE != 0 ]
    then
        echo "pkgsToInstallNotUpdate configuration was not updated in /etc/sysconfig/rhn/up2date correctly!"
        exit 1
fi

# Modify /etc/sysconfig/rhn/up2date to not exclude kernel updates.
/bin/sed -i s'/pkgSkipList=.*/pkgSkipList=/'g /etc/sysconfig/rhn/up2date

# Obtain exit code of sed command.
SEDEXITCODE=$?

# Check to make sure status returns 0.
if [ $SEDEXITCODE != 0 ]
    then
        echo "pkgSkipList configuration was not updated in /etc/sysconfig/rhn/up2date correctly!"
        exit 1
fi


# Make sure osad starts at startup
/sbin/chkconfig osad on

# Obtain exit code of chkconfig command.
CHKCEXITCODE=$?

# Check to make sure status returns 0.
if [ $CHKCEXITCODE != 0 ]
    then
        echo "OSAD was not correctly set to load on boot!"
        exit 1
fi

# Make sure rhnsd starts at startup
/sbin/chkconfig rhnsd on

# Obtain exit code of rhnsd command.
CHKCEXITCODE=$?

# Check to make sure status returns 0.
if [ $CHKCEXITCODE != 0 ]
    then
        echo "RHNSD was not correctly set to load on boot!"
        exit 1
fi


# Modify /etc/sysconfig/rhn/rhnsd to check-in every hour.
/bin/sed -i s'/INTERVAL=240/INTERVAL=60/'g /etc/sysconfig/rhn/rhnsd

# Obtain exit code of sed command.
SEDEXITCODE=$?

# Check to make sure status returns 0.
if [ $SEDEXITCODE != 0 ]
    then
        echo "Checking interval was not set correctly in /etc/sysconfig/rhn/rhnsd!"
        exit 1
fi

## Register server with Spacewalk using the activation key.
/usr/sbin/rhnreg_ks --force --serverUrl=http://$SPACEWALK_PROXY/XMLRPC --activationkey=$ACTIVATION_KEY

# Obtain exit code of rhnreg_ks command.
REGEXITCODE=$?

# Check to make sure status returns 0.
if [ $REGEXITCODE != 0 ]
    then
        echo "Registration has failed!"
        exit 1
fi

## Post registration steps.
# Stop OSAD
/sbin/service osad stop &> /dev/null

# Obtain exit code of service command.
SERVEXITCODE=$?

# Check to make sure status returns 0.
if [ $SERVEXITCODE != 0 ]
    then
        echo "OSAD service was not stopped correctly!"
        exit 1
fi

# Remove the /etc/sysconfig/rhn/osad-auth.conf if it exists.
/bin/rm -f /etc/sysconfig/rhn/osad-auth.conf

# Obtain exit code of rm command.
RMEXITCODE=$?

# Check to make sure status returns 0.
if [ $RMEXITCODE != 0 ]
    then
        echo "/etc/sysconfig/rhn/osad-auth.conf files were not removed correctly!"
        exit 1
fi

# Start OSAD
/sbin/service osad start &> /dev/null

# Obtain exit code of service command.
SERVEXITCODE=$?

# Check to make sure status returns 0.
if [ $SERVEXITCODE != 0 ]
    then
        echo "OSAD service was not started correctly!"
        exit 1
fi

# Start/restart rhnsd
/sbin/service rhnsd restart &> /dev/null

# Obtain exit code of restart command.
RESTEXITCODE=$?

# Check to make sure status returns 0.
if [ $RESTEXITCODE != 0 ]
    then
        echo "RHNSD service was not started correctly!"
        exit 1
fi

echo "Client has successfully registered!"

exit 0
