#! /bin/sh
#
# rmt-client-setup-res: client use rmt-client-setup script to register with rmt.
# But This script assumes SUSEConnect is already installed on the system.
# That is not true for RHEL, CentOS or Fedora, so this tiny wrapper script just sets up repositories on rmt,
# installs SUSEConnect and its dependencies and downloads and calls rmt-client-setup script
# (maintained by zpetrova@suse.com)

SUSECONNECT=/usr/bin/SUSEConnect
RPM=/usr/bin/rpm
DNF=/usr/bin/dnf
CURL=/usr/bin/curl

PARAMS=$@

function usage()
{
    cat << EOT >&2

   $1

   $0 script installs SUSEConnect and its dependencies and calls rmt-client-setup script that registers to rmt

  Usage: $0 <registration URL> [--regcert <url>] [--regdata <filename>] [--de-register]
  Usage: $0 --host <hostname of the RMT server> [--regcert <url>] [--regdata <filename>] [--de-register]
  Usage: $0 --host <hostname of the RMT server> [--fingerprint <fingerprint of server cert>] [--yes] [--regdata <filename>] [--de-register]
         configures a SLE client to register against a different registration server

  Example: $0 https://rmt.example.com/
  Example: $0 --host rmt.example.com --regcert http://rmt.example.com/certs/rmt.crt

EOT

    exit 1
}

# We need only REGURL and RMTNAME, all other parameters are just passed to rmt-client-setup script
REGURL=""
RMTNAME=""
AUTOACCEPT=""

while true ; do
    case "$1" in
        --fingerprint | --regcert | --regdata)
             test -z "$2" && usage "Option $1 needs an argument";
             shift;;
        --host) test -z "$2" && usage "Option $1 needs an argument"
             eval RMTNAME=\$2
             REGURL="http://${RMTNAME}";
             shift;;
        --de-register) DE_REGISTER="Y";;
        --yes) AUTOACCEPT="--assumeyes";;
        "") break ;;
        -h|--help) usage;;
        https://*) RMTNAME=${1:8};
                   REGURL="http://${RMTNAME}";;
        http://*) REGURL=$1;
                  RMTNAME=${REGURL:7};;
        *) usage "Unknown option $1";;
    esac
    shift
done

if [ `id -u` != 0 ]; then
    echo "You must be root. Abort."
    exit 1;
fi

if [ -z "$REGURL" ]; then
    echo "Missing registration URL. Abort."
    usage
fi

if [ ! -x $RPM ]; then
    echo "rpm command not found. Abort."
    exit 1
fi

if [ ! -x $CURL ]; then
    echo "curl command not found. Abort."
    exit 1
fi

echo "Importing repomd.xml.key"
$RPM --import ${REGURL}/repo/SUSE/Updates/RES/8/x86_64/update/repodata/repomd.xml.key

echo "Disabling all repositories"                                                                                                    
sed -i 's/^enabled=1/enabled=0/' /etc/yum.repos.d/*                                                                  

# on Centos /usr/share/redhat-release is a file, on RHEL and RES it is a directory
# so this is CentOS only workaround
rm -f /usr/share/redhat-release

if [ ! -x $SUSECONNECT ]; then
    echo "Downloading SUSEConnect"

    if [ ! -x $DNF ]; then
        echo "dnf command not found. Abort."
        exit 1
    fi

    $DNF config-manager --add-repo ${REGURL}/repo/SUSE/Updates/RES/8/x86_64/update
    $DNF config-manager --add-repo ${REGURL}/repo/SUSE/Updates/RES-AS/8/x86_64/update
    $DNF install ${AUTOACCEPT} --allowerasing sles_es-release
    $DNF install ${AUTOACCEPT} SUSEConnect librepo
    $DNF config-manager --set-disabled "${RMTNAME}_repo_SUSE_Updates_RES_8_x86_64_update"
    $DNF config-manager --set-disabled "${RMTNAME}_repo_SUSE_Updates_RES-AS_8_x86_64_update"
fi

$CURL -s -S $REGURL/tools/rmt-client-setup --output rmt-client-setup
echo "Running rmt-client-setup $PARAMS"
sh rmt-client-setup $PARAMS
