#!/bin/bash

function centos() {

	Repo=epel
	Action "Get epel" reposync -l --newest-only --delete --downloadcomps --download-metadata  --repoid=${Repo} --download_path=/data/repo/ 2>&1 | tee /data/log/get_${Repo}.log &> /dev/null
	Action "Move SeanAps EPEL : " mkdir -p /data/repo/EPEL/el7/x86_64/Packages ; for var in $(find /data/repo/epel/ -name *rpm -type f) ; do mv $var /data/repo/EPEL/el7/x86_64/Packages; done
	Action  "Create repo epel : " cd /data/repo/EPEL/el7/x86_64/ && createrepo -v /data/repo/EPEL/el7/x86_64/ 2>&1 | tee /data/log/CreateRepo_${Repo}.log &> /dev/null
	Action "Get OS BASE : "	mkdir -p /data/repo/tmp/ && cd /data/repo/tmp/ && 	wget -r -p -k  http://mirror.centos.org/centos/7.9.2009/os/x86_64/ 2>&1 | tee /data/log/get_base.log

	for var in $(find .| grep index.html) ;do rm -f $var;done
	mkdir -p /data/repo/CentOS/7.9/os/
	mv /data/repo/tmp/mirror.centos.org/centos/7.9.2009/os/x86_64/ /data/repo/CentOS/7.9/os/

	rm -rf /data/repo/tmp/*

	Action "Get OS updates : " cd /data/repo/tmp/ && wget -r -p -k  http://mirror.centos.org/centos/7.9.2009/updates/x86_64/ 2>&1 | tee /data/log/get_updates.log  &> /dev/null
}

RunScreen (){
	Name=$1
	shift
	Data="$*"
	screen -dmS $Name ${Data}

}

function oraclelinux () {

	Rand=repo${RANDOM}
	Majeur=$(echo ${osversion} | cut -d"." -f1)
	Mineur=$(echo ${osversion} | cut -d"." -f2)

	test -L /data/repo/EPEL/x86_64 && Action "Delete Old Link" rm  /data/repo/EPEL/x86_64 
	Action "Create Dir ${osversion}" mkdir -p /data/repo/EPEL/
	RunScreen EPEL dnf reposync -g --newest-only  --exclude='*.src' -p /data/repo/EPEL/ol${Majeur} --download-metadata --repoid=ol${Majeur}_developer_EPEL
	Download /data/repo/EPEL/ EPEL
	Action "Get in ${osversion} EPEL" cd  /data/repo/EPEL/ol${Majeur}
	Action "Link SeaNaps environement EPEL" ln -s ol${Majeur}_developer_EPEL x86_64



	test -L /data/repo/OracleLinux/OL8/codeready/builder/x86_64 && Action "Delete Old Link" rm  /data/repo/OracleLinux/OL8/codeready/builder/x86_64
 	Action "Create Dir ${osversion}" mkdir -p /data/repo/OracleLinux/OL8/codeready/builder/
	RunScreen codeready dnf reposync -g --newest-only  --exclude='*.src' -p /data/repo/OracleLinux/OL8/codeready/builder --download-metadata --repoid=ol${Majeur}_codeready_builder
	Download /data/repo/OracleLinux/OL8 codeready
	Action "Get in ${osversion} codeready" cd  /data/repo/OracleLinux/OL8/codeready/builder
	Action "Link SeaNaps environement codeready" ln -s ol${Majeur}_codeready_builder x86_64




	test -L /data/repo/OracleLinux/${osversion}/appstream/x86_64 && Action "Delete Old Link" rm  /data/repo/OracleLinux/${osversion}/appstream/x86_64 
 	Action "Create Dir ${osversion}" mkdir -p /data/repo/OracleLinux/${osversion}/appstream/
	RunScreen appstream dnf reposync -g --newest-only  --exclude='*.src' -p /data/repo/OracleLinux/${osversion}/appstream/ --download-metadata --repoid=ol${Majeur}_appstream 
	Download /data/repo/OracleLinux/${osversion}/appstream/ appstream
	Action "Get in ${osversion} appstream" cd  /data/repo/OracleLinux/${osversion}/appstream/
	Action "Link SeaNaps environement appstream" ln -s ol${Majeur}_appstream x86_64


	test -L /data/repo/OracleLinux/${osversion}/baseos/latest/x86_64 && Action "Delete Old Link" rm  /data/repo/OracleLinux/${osversion}/baseos/latest/x86_64

 	Action "Create Dir ${osversion}" mkdir -p /data/repo/OracleLinux/${osversion}/baseos/latest
	RunScreen baseos dnf reposync -g --newest-only   --exclude='*.src'  -p /data/repo/OracleLinux/${osversion}/baseos/latest --download-metadata  --repoid=ol${Majeur}_baseos_latest
	#RunScreen baseos dnf reposync -g --newest-only   --exclude='*.src'  -p /data/repo/OracleLinux/${osversion}/baseos/latest --download-metadata  --repoid=ol${Majeur}_u${Mineur}_baseos_base
	Download /data/repo/OracleLinux/${osversion}/baseos baseos
	Action "Get in  ${osversion} baseos" cd /data/repo/OracleLinux/${osversion}/baseos/latest
	#Action "Link SeaNaps environement baseOS" ln -s ol${Majeur}_u${Mineur}_baseos_base x86_64
	Action "Link SeaNaps environement baseOS" ln -s ol${Majeur}_baseos_latest x86_64


}
