#not yet used.
MAJ=5.5
VER=5.5.62
REL=1

CWD=`pwd`

#https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
#https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.20-1.el6.x86_64.rpm

WGET_OPTION=--no-check-certificate

#must alrady be cd to the current folder to be saved to.
get_file()
{
	URL=$1
	OUT=$2
	#MD5_URL=$3
	OUT_MD5=$OUT.md5

	#first get the MD5 so we can check if a download is even needed.
	if [ "$MD5_URL" != "" ]; then
		wget $WGET_OPTION -O $OUT_MD5 $MD5_URL

		if [ ! -s $OUT_MD5 ]; then
			echo "";
			echo "*** MD5 DOWNLOAD FAILURE from $MD5_URL!! ***";
			echo "";
			exit 1;
		fi

		if [ -s $OUT ]; then

			md5sum -c $OUT_MD5
			if [ "$?" -eq 0 ]; then
				echo "MD5 on $OUT passes.";
				return;
			fi
		fi
	elif [ -s $OUT ] && [ -s $OUT_MD5 ]; then
		md5sum -c $OUT_MD5
		if [ "$?" -eq 0 ]; then
			echo "MD5 on $OUT passes.";
			return;
		fi
	fi

	#either there was no md5, no file yet, or it failed.
	#grab the file.
	wget $WGET_OPTION -O $OUT $URL

	if [ "$MD5_URL" != "" ] && [ -s $OUT ]; then
		md5sum -c $OUT_MD5
		if [ "$?" -ne 0 ]; then
			echo "";
			echo "*** MD5 FAIL ON $OUT ***";
			echo "       ABORT!!          ";
			echo "";
			exit 1;
		fi
	fi

	if [ "$MD5_URL" = "" ] && [ -s $OUT ]; then
		md5sum $OUT > $OUT_MD5
	fi

}

grab()
{
	OS=$1
	BIT=$2
	RPM_LIST=${3}.txt

	#https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-1.el6.${BIT}.rpm-bundle.tar
	#https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.20-1.el6.${BIT}.rpm-bundle.tar.md5

	#https://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-5.6.38-1.el6.x86_64.rpm-bundle.tar

	F=mysql-${VER}-${REL}.${OS}.${BIT}.rpm-bundle.tar

	U=https://dev.mysql.com/get/Downloads/MySQL-${MAJ}/$F
	O=$F
	M=https://cdn.mysql.com/Downloads/MySQL-${MAJ}/$F.md5

	#wget $WGET_OPTION -O 64-bit/${MAJ}/$VER/$F https://dev.mysql.com/get/Downloads/MySQL-${MAJ}/$F
	get_file $U $O $M

	#if md5 and md5 failed, it wouldn't get this far.

	tar xvf $O --no-same-owner

	ls *${OS}.${BIT}.rpm > ${RPM_LIST}
}

doDebian()
{
	cd ${CWD}/${MAJ}/${VER}/64-bit
	F=mysql-${VER}-linux-glibc2.12-x86_64.tar.gz
	U=https://dev.mysql.com/get/Downloads/MySQL-${MAJ}/$F
	M=https://cdn.mysql.com//Downloads/MySQL-${MAJ}/$F.md5
	O=$F
	get_file $U $O $M

	cd $CWD/debian/64-bit
	ln -s ../../${MAJ}/${VER}/64-bit/$F

	cd ${CWD}/${MAJ}/${VER}/32-bit
	F=mysql-${VER}-linux-glibc2.12-i686.tar.gz
	U=https://dev.mysql.com/get/Downloads/MySQL-${MAJ}/$F
	O=$F
	get_file $U $O $M
}

doMain()
{
	cd ${CWD}/${MAJ}/${VER}/64-bit
	grab el6 x86_64 centos6-64
	grab el7 x86_64 centos7-64

	cd ${CWD}/${MAJ}/${VER}/32-bit
	grab el6 i686 centos6-32
}


##########################################################################

mkdir -p ${MAJ}/${VER}/32-bit
mkdir -p ${MAJ}/${VER}/64-bit

doMain
doDebian

#Source code
cd $CWD/${MAJ}/$VER
#http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.8.tar.gz/from/http://mysql.mirror.rafal.ca/
#https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.20.tar.gz.md5
F=mysql-${VER}.tar.gz
U=https://dev.mysql.com/get/Downloads/MySQL-${MAJ}/$F
O=$F
M=https://cdn.mysql.com//Downloads/MySQL-${MAJ}/${F}.md5

#wget $WGET_OPTION -O mysql-${VER}.tar.gz http://dev.mysql.com/get/Downloads/MySQL-${MAJ}/mysql-${VER}.tar.gz/from/${FROM_SERVER}
get_file $U $O $M

cd $CWD
ln -sf ${MAJ}/${VER}/mysql-${VER}.tar.gz .

#to keep it backwards compat, for now:
rm -f ./${VER}
rm -f ./64-bit/${VER}
ln -sf ${MAJ}/${VER}/32-bit ./${VER}
ln -sf ../${MAJ}/${VER}/64-bit ./64-bit/${VER}

exit 0;

