#! /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>] [--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=""

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";;
        "") break ;;
        -h|--help) usage;;
        https://*) RMTNAME=${1:8};
                   REGURL=$1;;
        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

SLL_version=`cat /etc/os-release | grep "VERSION_ID" | cut -d\" -f2 | cut -d\. -f1`
if [[ ${SLL_version} > 8 ]]; then                                                                                                       
   SLL_name="SLL";                                                                                                                
   SLL_release_package="sll-release"                                                                                              
else                                                                                                                                 
   SLL_name="RES";                                                                                                                
   SLL_release_package="sles_es-release"                                                                                       
fi                                                                                                                                   

echo "detect ${SLL_name} version... ${SLL_version}"                                                                                  

echo "Disabling all repositories"
dnf config-manager --disable $(dnf repolist -q | awk '{ print $1 }' | grep -v repo)
#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
if [ -f /usr/share/redhat-release ]; then
   rm -f /usr/share/redhat-release;
fi

# on RHEL9 (not RHEL8) redhat-release is protected and cannot be updated to sll-release
if [ -f /etc/dnf/protected.d/redhat-release.conf ]; then
    rm -f /etc/dnf/protected.d/redhat-release.conf;
fi

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/${SLL_name}/${SLL_version}/x86_64/update
    $DNF config-manager --add-repo ${REGURL}/repo/SUSE/Updates/${SLL_name}-AS/${SLL_version}/x86_64/update
    $DNF install --allowerasing ${SLL_release_package}
    $DNF install SUSEConnect librepo
    $DNF config-manager --set-disabled "${RMTNAME}_repo_SUSE_Updates_${SLL_name}_${SLL_version}_x86_64_update"
    $DNF config-manager --set-disabled "${RMTNAME}_repo_SUSE_Updates_${SLL_name}-AS_${SLL_version}_x86_64_update"
fi

$CURL --silent --show-error --insecure $REGURL/tools/rmt-client-setup --output rmt-client-setup
echo "Running rmt-client-setup $PARAMS"
sh rmt-client-setup $PARAMS

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

if [[ ${SLL_version} > 8 ]]; then 
    systemctl start suseconnect-keepalive.timer
    systemctl enable suseconnect-keepalive.timer
fi
