#!/bin/bash

# update RHN classic channels
if [ X$RHN_USER == X -o X$RHN_PASS == X ]
then
  echo '$RHN_USER and $RHN_PASS must be defined'
  exit 1
fi

for i in infrastructure node jbosseap rhc
do
  old_channel=rhel-x86_64-server-6-osop-1-$i
  new_channel=rhel-x86_64-server-6-ose-1.2-$i
  if (rhn-channel --list | grep -q $old_channel)
  then
    echo "Unsubscribing $old_channel and subscribing $new_channel."
    echo "rhn-channel --add --channel $new_channel"' --user $RHN_USER --password $RHN_PASS'
    rhn-channel --add --channel $new_channel --user $RHN_USER --password $RHN_PASS || exit 1
    echo "rhn-channel --remove --channel $old_channel"' --user $RHN_USER --password $RHN_PASS'
    rhn-channel --remove --channel $old_channel --user $RHN_USER --password $RHN_PASS || exit 1
  fi
done

# for v2 we need to point to the jboss ews 2 repo not 1
old_channel=jb-ews-1-x86_64-server-6-rpm
new_channel=jb-ews-2-x86_64-server-6-rpm
if (rhn-channel --list | grep -q $old_channel)
then
  echo "Unsubscribing $old_channel and subscribing $new_channel."
  echo "rhn-channel --add --channel $new_channel"' --user $RHN_USER --password $RHN_PASS'
  rhn-channel --add --channel $new_channel --user $RHN_USER --password $RHN_PASS || exit 1
  echo "rhn-channel --remove --channel $old_channel"' --user $RHN_USER --password $RHN_PASS'
  rhn-channel --remove --channel $old_channel --user $RHN_USER --password $RHN_PASS || exit 1
fi

yum clean all

exit 0
