#!/bin/bash

INSTALL1IP=smt.campuslan.de

# Variables
HTTPISOS="https://$INSTALL1IP"
AUTOYAST="https://$INSTALL1IP"
SMTSLES12="https://$INSTALL1IP"
SMTOES="https://$INSTALL1IP"

# Define HTTP ISO Sources
SLES12GADVD="$HTTPISOS/sles12-ga_x86_64 SLES12-GA-DVD"
SLES12SP1DVD="$HTTPISOS/sles12-sp1_x86_64 SLES12-SP1-DVD"

OES2018SP1DVD="$HTTPISOS/oes2018-sp1_x86_64 OES2018-SP1-DVD"
OES2018SP2DVD="$HTTPISOS/oes2018-sp2_x86_64 OES2018-SP2-DVD"
OES2018SP3DVD="$HTTPISOS/oes2018-sp3_x86_64 OES2018-SP3-DVD"

OES2023DVD="$HTTPISOS/oes2023 OES2023-DVD"



# Define HTTP SMT Sources

OES2018SP1POOL="$SMTOES/repo/\$RCE/OES2018-SP1-Pool/sle-12-x86_64/ OES2018-SP1-POOL"
OES2018SP1UPDATES="$SMTOES/repo/\$RCE/OES2018-SP1-Updates/sle-12-x86_64/ OES2018-SP1-UPDATES"

OES2018SP1SLES12SP3POOL="$SMTOES/repo/\$RCE/OES2018-SP1-SLES12-SP3-Pool/sle-12-x86_64/ OES2018-SP1-SLES12-SP3-POOL"
OES2018SP1SLES12SP3UPDATES="$SMTOES/repo/\$RCE/OES2018-SP1-SLES12-SP3-Updates/sle-12-x86_64/ OES2018-SP1-SLES12-SP3-UPDATES"

OES2018SP2POOL="$SMTOES/repo/\$RCE/OES2018-SP2-Pool/sle-12-x86_64/ OES2018-SP2-POOL"
OES2018SP2UPDATES="$SMTOES/repo/\$RCE/OES2018-SP2-Updates/sle-12-x86_64/ OES2018-SP2-UPDATES"

OES2018SP2SLES12SP5POOL="$SMTOES/repo/\$RCE/OES2018-SP2-SLES12-SP5-Pool/sle-12-x86_64/ OES2018-SP2-SLES12-SP5-POOL"
OES2018SP2SLES12SP5UPDATES="$SMTOES/repo/\$RCE/OES2018-SP2-SLES12-SP5-Updates/sle-12-x86_64/ OES2018-SP2-SLES12-SP5-UPDATES"

OES2018SP3SLES12SP5POOL="$SMTOES/repo/\$RCE/OES2018-SP3-SLES12-SP5-Pool/sle-12-x86_64/ OES2018-SP3-SLES12-SP5-POOL"
OES2018SP3SLES12SP5UPDATES="$SMTOES/repo/\$RCE/OES2018-SP3-SLES12-SP5-Updates/sle-12-x86_64/ OES2018-SP3-SLES12-SP5-UPDATES"
OES2018SP3POOL="$SMTOES/repo/\$RCE/OES2018-SP3-Pool/sle-12-x86_64/ OES2018-SP3-POOL"
OES2018SP3UPDATES="$SMTOES/repo/\$RCE/OES2018-SP3-Updates/sle-12-x86_64/ OES2018-SP3-UPDATES"

OES2023SLES15SP4POOL="$SMTOES/repo/\$RCE/OES2023-SLE-Product-SLES15-SP4-Pool/sle-15-x86_64/ OES2023-SLES15-SP4-POOL"
OES2023SLES15SP4UPDATES="$SMTOES/repo/\$RCE/OES2023-SLE-Product-SLES15-SP4-Updates/sle-15-x86_64/ OES2023-SLES15-SP4-UPDATES"
OES2023SLES15SP4BASESYSTEMPOOL="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Basesystem15-SP4-Pool/sle-15-x86_64/ OES2023-SLE-Module-Basesystem15-SP4-Pool"
OES2023SLES15SP4BASESYSTEMUPDATES="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Basesystem15-SP4-Updates/sle-15-x86_64/ OES2023-SLE-Module-Basesystem15-SP4-Updates"
OES2023SLES15SP4CONTAINERSPOOL="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Containers15-SP4-Pool/sle-15-x86_64/ OES2023-SLE-Module-Containers15-SP4-Pool"
OES2023SLES15SP4CONTAINERSUPDATES="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Containers15-SP4-Updates/sle-15-x86_64/ OES2023-SLE-Module-Containers15-SP4-Updates"
OES2023SLES15SP4DESKTOPPOOL="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Desktop-Applications15-SP4-Pool/sle-15-x86_64/ OES2023-SLE-Module-Desktop-Applications15-SP4-Pool"
OES2023SLES15SP4DESKTOPUPDATES="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Desktop-Applications15-SP4-Updates/sle-15-x86_64/ OES2023-SLE-Module-Desktop-Applications15-SP4-Updates"
OES2023SLES15SP4LEGACYPOOL="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Legacy15-SP4-Pool/sle-15-x86_64/ OES2023-SLE-Module-Legacy15-SP4-Pool"
OES2023SLES15SP4LEGACYUPDATES="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Legacy15-SP4-Pool/sle-15-x86_64/ OES2023-SLE-Module-Legacy15-SP4-Updates"
OES2023SLES15SP4SERVERPOOL="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Server-Applications15-SP4-Pool/sle-15-x86_64/ OES2023-SLE-Module-Server-Applications15-SP4-Pool"
OES2023SLES15SP4SERVERUPDATES="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Server-Applications15-SP4-Updates/sle-15-x86_64/ OES2023-SLE-Module-Server-Applications15-SP4-Updates"
OES2023SLES15SP4WEBPOOL="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Web-Scripting15-SP4-Pool/sle-15-x86_64/ OES2023-SLE-Module-Web-Scripting15-SP4-Pool"
OES2023SLES15SP4WEBUPDATES="$SMTOES/repo/\$RCE/OES2023-SLE-Module-Web-Scripting15-SP4-Updates/sle-15-x86_64/ OES2023-SLE-Module-Web-Scripting15-SP4-Updates"
OES2023POOL="$SMTOES/repo/\$RCE/OES2023-Pool/sle-15-x86_64 OES2023-POOL"
OES2023UPDATES="$SMTOES/repo/\$RCE/OES2023-Updates/sle-15-x86_64 OES2023-UPDATES"


SLES12GAPOOL="$SMTSLES12/repo/SUSE/Products/SLE-SERVER/12/x86_64/product/ SLES12-GA-POOL"
SLES12GAUPDATES="$SMTSLES12/repo/SUSE/Updates/SLE-SERVER/12/x86_64/update/ SLES12-GA-UPDATES"

SLES12SP1POOL="$SMTSLES12/repo/SUSE/Products/SLE-SERVER/12-SP1/x86_64/product/ SLES12-SP1-POOL"
SLES12SP1UPDATES="$SMTSLES12/repo/SUSE/Updates/SLE-SERVER/12-SP1/x86_64/update/ SLES12-SP1-UPDATES"

SLES12SDKPOOL="$SMTSLES12/repo/SUSE/Products/SLE-SDK/12/x86_64/product/ SLES12-SDK-POOL"
SLES12SDKUPDATES="$SMTSLES12/repo/SUSE/Updates/SLE-SDK/12/x86_64/update/ SLES12-SDK-UPDATES"
SLES12SP1SDKPOOL="$SMTSLES12/repo/SUSE/Products/SLE-SDK/12-SP1/x86_64/product/ SLES12-SP1-SDK-POOL"
SLES12SP1SDKUPDATES="$SMTSLES12/repo/SUSE/Updates/SLE-SDK/12-SP1/x86_64/update/ SLES12-SP1-SDK-UPDATES"

SLES12MODULELEGACYPOOL="$SMTSLES12/repo/SUSE/Products/SLE-Module-Legacy/12/x86_64/product/ SLES12-MODULE-LEGACY-POOL"
SLES12MODULELEGACYUPDATES="$SMTSLES12/repo/SUSE/Updates/SLE-Module-Legacy/12/x86_64/update/ SLES12-MODULE-LEGACY-UPDATES"

SLES12MODULECONTAINERSPOOL="$SMTSLES12/repo/SUSE/Products/SLE-Module-Containers/12/x86_64/product/ SLES12-MODULE-CONTAINERS-POOL"
SLES12MODULECONTAINERSUPDATES="$SMTSLES12/repo/SUSE/Updates/SLE-Module-Containers/12/x86_64/update/ SLES12-MODULE-CONTAINERS-UPDATES"

SLES12MODULEWEBSCRIPTINGPOOL="$SMTSLES12/repo/SUSE/Products/SLE-Module-Web-Scripting/12/x86_64/product/ SLES12-MODULE-WEB-SCRIPTING-POOL"
SLES12MODULEWEBSCRIPTINGUPDATES="$SMTSLES12/repo/SUSE/Updates/SLE-Module-Web-Scripting/12/x86_64/update/ SLES12-MODULE-WEB-SCRIPTING-UPDATES"


function _DELETE_ALL_CHANNELS {
	# Delete all channels
	echo "zypper sd {0..20} >/dev/null 2>&1"
	zypper sd {0..20} >/dev/null 2>&1
}

function _ADD_DVD_CHANNELS() {
	# Loop through all DVD Channels
	for DVD in "$@"; do
		echo "zypper ar --refresh --no-gpgcheck --type=yast2 ${!DVD} >/dev/null 2>&1";
		zypper ar --refresh --no-gpgcheck --type=yast2 ${!DVD} >/dev/null 2>&1;
	done
}

function _ADD_SMT_CHANNELS() {
	# Loop through all SMT Channels
	for SMT in "$@"; do
		echo "zypper ar --refresh --no-gpgcheck --type=rpm-md ${!SMT} >/dev/null 2>&1";
		zypper ar --refresh --no-gpgcheck --type=rpm-md ${!SMT} >/dev/null 2>&1;
	done
}

# Add Repos
if [[ -z $1 ]]; then
	echo "OS needs to be defined as first option, in case of UPGRADE as second option the current channels are not deleted" 
	echo "OES:  OES2018SP3 OES2023"
	echo "SLES: SLES12SP5 SLES15SP4"
	echo "example: ./set-repos.sh OES2023 UPGRADE"
	exit 1
fi

# Delete existing repos in case "UPGRADE" is not specified
if [ "$2" == "UPGRADE" ]; then
	echo "Not deleting channels due to upgrade"
else
	_DELETE_ALL_CHANNELS
fi

if [ "$1" == "OES2018SP1" ]; then
        DVDCHANNELS="OES2018SP1DVD"
        SMTCHANNELS="OES2018SP1SLES12SP3POOL OES2018SP1SLES12SP3UPDATES OES2018SP1POOL OES2018SP1UPDATES"
        _ADD_DVD_CHANNELS $DVDCHANNELS
        _ADD_SMT_CHANNELS $SMTCHANNELS
fi

if [ "$1" == "OES2018SP2" ]; then
        DVDCHANNELS="OES2018SP2DVD"
        SMTCHANNELS="OES2018SP2SLES12SP5POOL OES2018SP2SLES12SP5UPDATES OES2018SP2POOL OES2018SP2UPDATES"
        _ADD_DVD_CHANNELS $DVDCHANNELS
        _ADD_SMT_CHANNELS $SMTCHANNELS
fi

if [ "$1" == "OES2018SP3" ]; then
        DVDCHANNELS="OES2018SP3DVD"
        SMTCHANNELS="OES2018SP3SLES12SP5POOL OES2018SP3SLES12SP5UPDATES OES2018SP3POOL OES2018SP3UPDATES"
        _ADD_DVD_CHANNELS $DVDCHANNELS
        _ADD_SMT_CHANNELS $SMTCHANNELS
fi

if [ "$1" == "OES2023" ]; then
        DVDCHANNELS="OES2023DVD"
        SMTCHANNELS="OES2023POOL OES2023UPDATES OES2023SLES15SP4POOL OES2023SLES15SP4UPDATES OES2023SLES15SP4BASESYSTEMPOOL OES2023SLES15SP4BASESYSTEMUPDATES OES2023SLES15SP4CONTAINERSPOOL OES2023SLES15SP4CONTAINERSUPDATES OES2023SLES15SP4DESKTOPPOOL OES2023SLES15SP4DESKTOPUPDATES OES2023SLES15SP4LEGACYPOOL OES2023SLES15SP4LEGACYUPDATES OES2023SLES15SP4SERVERPOOL OES2023SLES15SP4SERVERUPDATES OES2023SLES15SP4WEBPOOL OES2023SLES15SP4WEBUPDATES"
        _ADD_DVD_CHANNELS $DVDCHANNELS
        _ADD_SMT_CHANNELS $SMTCHANNELS
fi

if [ "$1" == "SLES12GA" ]; then
        DVDCHANNELS="SLES12GADVD"
        SMTCHANNELS="SLES12GAPOOL SLES12GAUPDATES SLES12SDKPOOL SLES12SDKUPDATES SLES12MODULELEGACYPOOL SLES12MODULELEGACYUPDATES SLES12MODULECONTAINERSPOOL SLES12MODULECONTAINERSUPDATES SLES12MODULEWEBSCRIPTINGPOOL SLES12MODULEWEBSCRIPTINGUPDATES"
        _ADD_DVD_CHANNELS $DVDCHANNELS
        _ADD_SMT_CHANNELS $SMTCHANNELS
fi

if [ "$1" == "SLES12SP1" ]; then
        DVDCHANNELS="SLES12SP1DVD"
        SMTCHANNELS="SLES12SP1POOL SLES12SP1UPDATES SLES12SP1SDKPOOL SLES12SP1SDKUPDATES SLES12MODULELEGACYPOOL SLES12MODULELEGACYUPDATES SLES12MODULECONTAINERSPOOL SLES12MODULECONTAINERSUPDATES SLES12MODULEWEBSCRIPTINGPOOL SLES12MODULEWEBSCRIPTINGUPDATES"
        _ADD_DVD_CHANNELS $DVDCHANNELS
        _ADD_SMT_CHANNELS $SMTCHANNELS
fi




zypper ca

exit 0


