#!/bin/bash

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Copyright 2009  Colin Coe <colin.coe@gmail.com>
#
# Portions of code reused from the Satellite/Spacewalk documentation,
# 'Mastering Regular Expressions (2nd Edition)', the RHN Satellite mailing
# list and from the Internet.

# Script to easily make a node a Satellite client

FOUND=0
OLD=0
RR="el`awk '{print $7}' /etc/redhat-release | cut -d. -f1`"
export SAT_HOST=sat.example.com
export SAT_USER=admin

export proxy=$(echo $http_proxy |sed -e "s/.*:\/\///" -e "s/\///")

python -c '
import socket, os, sys
if socket.getaddrinfo(os.environ["HOSTNAME"], None)[0][4][0] == "127.0.0.1":
   sys.exit(1)
'
if [ $? -ne 0 ]; then
        echo "$HOSTNAME reports it's IP address as $IP, resolve and re-run"
        exit 1
fi

# all enmac systems/networks have an alias for proxy in their DNS, so don't need to be specific
PROXY_PORT=3128
PROXY_HOST='proxy' # mmmmm fragile....
USE_PROXY=1

# Logic to control proxy host and port (if required) goes here

# Set the environment variable SAT_USER here.  If you are a multi-org site
# you need additional log to determine the appropriate org or sat admin
# user names.

# Example: edit to suite...

case `dnsdomainname` in
	# Org 102
        'h1' | 'h2' | 'h3')
		USE_PROXY=1
		export SAT_USER=admin102
		export business=hp
		export org=102
		if [ `hostname` = "proxy.h2" ]; then
			USE_PROXY=0
		else
			if [ `dnsdomainname` = "h2" ]; then
				PROXY_HOST='proxy.h2'
			fi
		fi
		;;
	# Org 122
        'w1' | 'w2' | 'w3')
		export org=122
		USE_PROXY=0
		export SAT_USER=admin122
		export business=wp
		;;
esac


if [ $USE_PROXY == 1 ]
then
        export PROXY="--httpproxy $PROXY_HOST --httpport $PROXY_PORT"
        export http_proxy="http://$PROXY_HOST:$PROXY_PORT"
        export proxy="$PROXY_HOST:$PROXY_PORT"
        sed -i -e 's/httpProxy=.*$/httpProxy='$proxy'/' \
        	-e 's/enableProxy=0/enableProxy=1/' /etc/sysconfig/rhn/up2date
else
        sed -i -e 's/enableProxy=.*?/enableProxy=0/' /etc/sysconfig/rhn/up2date
#        unset http_proxy ftp_proxy https_proxy proxy PROXY
fi


> /tmp/ks.sh


# Install the new Satellites SSL key
# make sure RPMs from previous attempts don't get in the way
rm -rf /tmp/rhn_rpms

# blat the previous Satellites SSL key

rpm -e rhn-org-trusted-ssl-cert 2> /dev/null

# Force re-registration to happen
rm -f /etc/sysconfig/rhn/systemid*

# Get and process the ks.cfg file
VER=`awk '{print $7}' /etc/redhat-release | cut -c1`
TYPE=`awk '{print $5}' /etc/redhat-release | tr 'A-Z' 'a-z' | tr -d ' '`
echo "VER='$VER', TYPE='$TYPE'"
[ $((`echo $TYPE | wc -c`-1)) -ne 2 ] && TYPE="s"

URL="http://${SAT_HOST}/ks/cfg/org/${org}/label/mksatclient${VER}${TYPE}-`arch`"

wget --no-cache -r -O - $URL | while read LINE; do
        echo "${LINE}" | egrep -q -- "--Begin|rhn_check" && FOUND=$((${FOUND}+1))
        if [ ${OLD} -ne 0 ]; then
                [ ${FOUND} -eq 1 ] && echo "${LINE}" >> /tmp/ks.sh
        fi
        OLD=${FOUND}
done

# Run the decimated ks.cfg
bash -x /tmp/ks.sh

if [ "${business}" = "hp" ]; then
        /usr/bin/rhn-actions-control --enable-all
fi

# At this point we should be a Satellite client
if [ $USE_PROXY = 0 ]; then
	if [ "${RR}" = "el4" ]; then
		up2date osad
	else
		yum install -y osad
	fi
	chkconfig osad off
else
	sed -i -e 's/^# enableProxy .*$/enableProxy = 1/' -e 's/^# httpProxy = .*$/httpProxy = '$proxy'/' /etc/sysconfig/rhn/osad.conf
	chkconfig osad off
fi

# Needed for init_sat_client
while [ -z "${SAT_PASS}" ]; do
	echo "Enter the password $SAT_USER"
	read -s SAT_PASS
done
export SAT_PASS

### Post install configuration
rm -f /tmp/init_sat_client*
wget --no-cache -P /tmp http://${SAT_HOST}/pub/init_sat_client
chmod 0700 /tmp/init_sat_client
chown root:root /tmp/init_sat_client
/tmp/init_sat_client

# Clean up like all good scripts
rm -f /tmp/ks.sh /tmp/init_sat_client
#unset SAT_PASS
#unset PROXY
#unset http_proxy
#unset proxy

exit 0
