#!/bin/bash

#DESC Register system with TF's Redhat insights account.
# TF POS Linux insights registration.

yum -y install scap-security-guide*

# verify root
ID=`/usr/bin/id -u`
[ $ID -ne 0 ] && echo "You must be root to run $0." && exit 1

# cert fix
[ -f /etc/pki/consumer/cert.pem ] && mv /etc/pki/consumer/cert.pem /etc/pki/consumer/cert.pem.bak
[ -f /etc/pki/consumer/key.pem ] && mv /etc/pki/consumer/key.pem /etc/pki/consumer/key.pem.bak

# install insights rpm.
yum -y install insights-client
[ $? -ne 0 ] && echo "Could not install insights client." && exit 2

# make config changes to conf.
sed -i 's/\#authmethod=BASIC/authmethod=BASIC/g' /etc/insights-client/insights-client.conf
sed -i 's/\#username=/username=tflinuxpos/g' /etc/insights-client/insights-client.conf
sed -i 's/\#password=/password=erpoithxciujhnbmxhgkvhasw/g' /etc/insights-client/insights-client.conf
[ $? -ne 0 ] && echo "Could not update conf file." && exit 3

# register.
insights-client --unregister
insights-client --register
insights-client --compliance
insights-client --enable-schedule
insights-client --checkin
[ $? -ne 0 ] && echo "Error with registration." && exit 4

# move off redhat.repo if exists.
[ -f /etc/yum/repos.d/redhat.repo ] && mv /etc/yum.repos.d/redhat.repo ~

exit 0
