#!/bin/bash

cd /home/rpmbuild/site/rpms

function getOneMock() {
case $1 in
*i)	dep=${1%i}
	;;
*x)	dep=${1%x}
	;;
*)	dep=$1
	;;
esac

rep=${2-remi}

case $dep in 
scl81el*)	dep=${dep#scl81el}
	type=enterprise
	;;
scl80el*)	dep=${dep#scl80el}
	type=enterprise
	;;
scl74el*)	dep=${dep#scl74el}
	type=enterprise
	;;
scl73el*)	dep=${dep#scl73el}
	type=enterprise
	;;
scl72el*)	dep=${dep#scl72el}
	type=enterprise
	;;
scl71el*)	dep=${dep#scl71el}
	type=enterprise
	;;
scl70el*)	dep=${dep#scl70el}
	type=enterprise
	;;
scl56el*)	dep=${dep#scl56el}
	type=enterprise
	;;
scl55el*)	dep=${dep#scl55el}
	type=enterprise
	;;
scl54el*)	dep=${dep#scl54el}
	type=enterprise
	;;
el?x55)
	dep=${1:2:1}
    if [ "$2" == "php55" -o "$2" == "test55" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php55
	else	rep=${2}55
	fi
	type=enterprise
	;;
el?x56)
	dep=${1:2:1}
    if [ "$2" == "php56" -o "$2" == "test56" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php56
	else	rep=${2}56
	fi
	type=enterprise
	;;
el?x70)
	dep=${1:2:1}
    if [ "$2" == "php70" -o "$2" == "test70" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php70
	else	rep=${2}70
	fi
	type=enterprise
	;;
el?x71)
	dep=${1:2:1}
    if [ "$2" == "php71" -o "$2" == "test71" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php71
	else	rep=${2}71
	fi
	type=enterprise
	;;
el?x72)
	dep=${1:2:1}
    if [ "$2" == "php72" -o "$2" == "test72" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php72
	else	rep=${2}72
	fi
	type=enterprise
	;;
el?x73)
	dep=${1:2:1}
    if [ "$2" == "php73" -o "$2" == "test73" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php73
	else	rep=${2}73
	fi
	type=enterprise
	;;
el?x74)
	dep=${1:2:1}
    if [ "$2" == "php74" -o "$2" == "test74" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php74
	else	rep=${2}74
	fi
	type=enterprise
	;;
el?x80)
	dep=${1:2:1}
    if [ "$2" == "php80" -o "$2" == "test80" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php80
	else	rep=${2}80
	fi
	type=enterprise
	;;
el?x81)
	dep=${1:2:1}
    if [ "$2" == "php81" -o "$2" == "test81" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php81
	else	rep=${2}81
	fi
	type=enterprise
	;;
fc??x74)
	dep=${1:2:2}
    if [ "$2" == "php74" -o "$2" == "test74" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php74
	else	rep=${2}74
	fi
	type=fedora
	;;
fc??x80)
	dep=${1:2:2}
    if [ "$2" == "php80" -o "$2" == "test80" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php80
	else	rep=${2}80
	fi
	type=fedora
	;;
fc??x81)
	dep=${1:2:2}
    if [ "$2" == "php81" -o "$2" == "test81" ]
    then	rep=$rep
	elif [ "$2" == "remi" ]
	then	rep=php81
	else	rep=${2}81
	fi
	type=fedora
	;;
el*)	dep=${dep#el}
	type=enterprise
	;;
fc*)	dep=${dep#fc}
	type=fedora
	;;
scl81fc*)	dep=${dep#scl81fc}
	type=fedora
	;;
scl80fc*)	dep=${dep#scl80fc}
	type=fedora
	;;
scl74fc*)	dep=${dep#scl74fc}
	type=fedora
	;;
scl73fc*)	dep=${dep#scl73fc}
	type=fedora
	;;
scl72fc*)	dep=${dep#scl72fc}
	type=fedora
	;;
scl71fc*)	dep=${dep#scl71fc}
	type=fedora
	;;
scl70fc*)	dep=${dep#scl70fc}
	type=fedora
	;;
scl56fc*)	dep=${dep#scl56fc}
	type=fedora
	;;
scl55fc*)	dep=${dep#scl55fc}
	type=fedora
	;;
scl54fc*)	dep=${dep#scl54fc}
	type=fedora
	;;
*)	echo Connais pas $dep
	;;
esac


if [ -d $type/$dep/$rep/x86_64 ]
then
	cible=$type/$dep/$rep/
	debug=$type/$dep/debug-$rep/
else 
	echo "$1 n'est pas un dépôt ($type/$dep/$rep)"
	exit 2
fi

if   [ -d /var/lib/mock/${1}/repodata ]; then
	src=/var/lib/mock/${1}

elif   [ -d /var/lib/mock/${1}i/result ]; then
	src=/var/lib/mock/${1}i/result

elif [ -d /var/lib/mock/$1/result ]; then
	src=/var/lib/mock/$1/result
else
	echo "$1 n'est pas un dossier mock"
	exit 2
fi

echo Copie de $src vers $cible
for i in $src/${3-*}.rpm
do
	j=$(basename $i)
	case $j in 
		*.src.rpm)	echo " " $i ignored
			;;
		xulrunner-last-debuginfo*)	echo " " $i ignored
			;;
		mozilla-crashreporter*)		echo " " $i ignored
			;;
		firefox-debuginfo*)		echo " " $i ignored
			;;
		thunderbird-debuginfo*)	echo " " $i ignored
			;;
		*-debuginfo-*x86_64.rpm|*-debugsource-*x86_64.rpm)
			if [ -f ${debug}x86_64/$j ]; then
				echo " " $i already there
			elif [ -d ${debug}x86_64 ]; then
				cp $i ${debug}x86_64 && echo + $i copied debuginfo x86_64
			else
				echo " " $i ignored
			fi
			;;
		*-debuginfo-*86.rpm|*-debugsource-*86.rpm)
			if [ -f ${debug}i386/$j ]; then
				echo " " $i already there
			elif [ -d ${debug}i386 ]; then
				cp $i ${debug}i386 && echo + $i copied debuginfo i386
			else
				echo " " $i ignored
			fi
			;;
		*.noarch.rpm)	
			if [ -f ${cible}x86_64/$j ]; then
				echo " " $i already there
			else
				cp $i ${cible}x86_64 && echo + $i copied x86_64
				[ -d ${cible}ppc    -a ! -f ${cible}ppc/.closed ]  \
					&& ln ${cible}x86_64/$j ${cible}ppc/$j \
					&& echo + $i linked ppc
				[ -d ${cible}ppc64  -a ! -f ${cible}ppc64/.closed ] \
					&& ln ${cible}					/$j ${cible}ppc64/$j  \
					&& echo + $i linked ppc64
				#[ -d ${cible}i386 ] \
				#	&& ln ${cible}x86_64/$j ${cible}i386/$j \
				#	&& echo + $i linked x86_64
				[ -d ${cible}armhfp ] \
					&& ln ${cible}x86_64/$j ${cible}armhfp/$j \
					&& echo + $i linked armhfp
				[ -d ${cible}aarch64 ] \
					&& ln ${cible}x86_64/$j ${cible}aarch64/$j \
					&& echo + $i linked aarch64
			fi
			;;
		*.i?86.rpm)	
			if [ -f ${cible}i386/$j ]; then
				echo " " $i already there
			else
				cp $i ${cible}i386 && echo + $i copied i686
			fi
			;;
		*.x86_64.rpm)	
			if [ -f ${cible}x86_64/$j ]; then
				echo " " $i already there
			else
				cp $i ${cible}x86_64 && echo + $i copied x86_64
			fi
			;;
		*)		echo Aucun RPM disponible
			;;
	esac
done
}

if [ $# = 0 ]; then
	echo usage $0 nomdépôt  [ prefix ]
	exit 1
fi

opt=remi
if [ $1 = test ]; then
	opt=test
	shift
fi
if [ $1 = test70 ]; then
	opt=test70
	shift
fi
if [ $1 = test71 ]; then
	opt=test71
	shift
fi
if [ $1 = test72 ]; then
	opt=test72
	shift
fi
if [ $1 = test72 ]; then
	opt=test72
	shift
fi
if [[ $1 = php* ]]; then
	opt=$i
	shift
fi
if [[ $1 == glpi* ]]; then
	opt=$1
	shift
fi
if [[ $1 == redis* ]]; then
	opt=$1
	shift
fi
if [[ $1 == memcached ]]; then
	opt=$1
	shift
fi
if [[ $1 == composer* ]]; then
	opt=$1
	shift
fi
if [ $1 = devs -o $1 = dev ]; then
	opt=devs
	shift
fi

if [ $1 = all55 ]; then
	n=7
		getOneMock el${n}x55 php55 '*5.5*'

elif [ $1 = all56 ]; then
	n=7
		getOneMock el${n}x56 php56 '*5.6*'

elif [ $1 = all70 ]; then
	[ $opt = remi ] && opt=php70
	[ $opt = test ] && opt=test70
	n=7
		getOneMock el${n}x70 $opt '*7.0*'

elif [ $1 = all71 ]; then
	[ $opt = remi ] && opt=php71
	[ $opt = test ] && opt=test71
	n=7
		getOneMock el${n}x71 $opt '*7.1*'

elif [ $1 = all72 ]; then
	[ $opt = remi ] && opt=php72
	[ $opt = test ] && opt=test72
		getOneMock el7x72  $opt '*7.2*'
		getOneMock el8x72  $opt '*7.2*'

elif [ $1 = all73 ]; then
	[ $opt = remi ] && opt=php73
	[ $opt = test ] && opt=test73
		getOneMock el7x73  $opt '*7.3*'
		getOneMock el8x73  $opt '*7.3*'

elif [ $1 = all74 ]; then
	[ $opt = remi ] && opt=php74
	[ $opt = test ] && opt=test74
		getOneMock fc35x74 $opt '*7.4*'
		getOneMock fc36x74 $opt '*7.4*'
		getOneMock el7x74  $opt '*7.4*'
		getOneMock el8x74  $opt '*7.4*'
		getOneMock el9x74  $opt '*7.4*'

elif [ $1 = all80 ]; then
	[ $opt = remi ] && opt=php80
	[ $opt = test ] && opt=test80
		getOneMock fc34x80 $opt '*8.0*'
		getOneMock fc36x80 $opt '*8.0*'
		getOneMock el7x80  $opt '*8.0*'
		getOneMock el8x80  $opt '*8.0*'
		getOneMock el9x80  $opt '*8.0*'

elif [ $1 = all81 ]; then
	[ $opt = remi ] && opt=php81
	[ $opt = test ] && opt=test81
		getOneMock fc34x81 $opt '*8.1*'
		getOneMock fc35x81 $opt '*8.1*'
		getOneMock fc36x81 $opt '*8.1*'
		getOneMock el7x81  $opt '*8.1*'
		getOneMock el8x81  $opt '*8.1*'
		getOneMock el9x81  $opt '*8.1*'

elif [ $1 = all54 ]; then
	n=7
		getOneMock el${n}x remi '*5.4*'

elif [ $1 = for56 ]; then
	for i in 7
	do
		getOneMock el${i}x test
	done

elif [ $1 = for55 ]; then
	for i in 7
	do
		getOneMock el${i}x test
	done

elif [ $1 = allx ]; then
	for i in 34 35 36
	do
		getOneMock fc${i}x $opt
	done
	for i in 7 8 9
	do
		getOneMock el${i}x $opt
	done

elif [ $1 = scl54 ]; then
	for i in 7
	do
		getOneMock scl54el${i}x $opt
	done

elif [ $1 = scl55 ]; then
	for i in 7
	do
		getOneMock scl55el${i}x $opt
	done

elif [ $1 = scl56 ]; then
	for i in 34 35
	do
		getOneMock scl56fc${i}x $opt
	done
	for i in 7 8
	do
		getOneMock scl56el${i}x $opt
	done

elif [ $1 = scl70 ]; then
	for i in 34 35
	do
		getOneMock scl70fc${i}x $opt
	done
	for i in 7 8
	do
		getOneMock scl70el${i}x $opt
	done

elif [ $1 = scl71 ]; then
	for i in 34 35
	do
		getOneMock scl71fc${i}x $opt
	done
	for i in 7 8
	do
		getOneMock scl71el${i}x $opt
	done

elif [ $1 = scl81 ]; then
	for i in 34 35 36
	do
		getOneMock scl81fc${i}x $opt
	done
	for i in 7 8 9
	do
		getOneMock scl81el${i}x $opt
	done

elif [ $1 = scl80 ]; then
	for i in 34 35 36
	do
		getOneMock scl80fc${i}x $opt
	done
	for i in 7 8 9
	do
		getOneMock scl80el${i}x $opt
	done

elif [ $1 = scl74 ]; then
	for i in 34 35 36
	do
		getOneMock scl74fc${i}x $opt
	done
	for i in 7 8 9
	do
		getOneMock scl74el${i}x $opt
	done

elif [ $1 = scl73 ]; then
	for i in 34 35
	do
		getOneMock scl73fc${i}x $opt
	done
	for i in 7 8
	do
		getOneMock scl73el${i}x $opt
	done

elif [ $1 = scl72 ]; then
	for i in 34 35
	do
		getOneMock scl72fc${i}x $opt
	done
	for i in 7 8
	do
		getOneMock scl72el${i}x $opt
	done

else
	for i in $*
	do
		getOneMock $i $opt
	done
fi