#!/bin/bash

BASEDIR=/home/fiobuild/RPMS
SITE=rpm.fugitol.com
SITENAME="Fugitol RPMs"

if [ -z "${VERBOSE}" ]; then
	crverbosity="-q"
	rvverbosity="-q"
else
	crverbosity="-v"
	rvverbosity="-x ia64"
fi

PDIR=""
[ "x$1" = "x" ] && RPMPROC="main updates beta testing" || RPMPROC="$@"
for i in `echo ${RPMPROC}`; do
        if [ "$i" = "main" ]; then
                PDIR="${PDIR} i386 x86_64 SRPMS debug/i386 debug/x86_64 nonfree/i386 nonfree/x86_64 nonfree/debug/i386 nonfree/debug/x86_64"
        elif [ "$i" = "updates" -o "$i" = "beta" -o "$i" = "testing" ]; then
                PDIR="${PDIR} $i/i386 $i/x86_64 $i/SRPMS $i/debug/i386 $i/debug/x86_64 nonfree/$i/i386 nonfree/$i/x86_64 nonfree/$i/debug/i386 nonfree/$i/debug/x86_64"
        fi      
done 

[ "${RPMPROC}" = "testing" ] && DISTPROC="el/6" || DISTPROC="fedora/13 fedora/14 fedora/15 fedora/16 fedora/17 el/6 el/5 rhel/6 rhel/7"
#[ "${RPMPROC}" = "testing" ] && DISTPROC="el/5" || DISTPROC="fedora/13 fedora/14 el/6 el/5"
for dist in `echo "${DISTPROC}"`; do
	for plat in `echo "$PDIR"`; do
		if [ "${dist}" = "el/6" -o "${dist}" = "rhel/6" -o "${dist}" = "rhel/7" ]; then
			if [ "${plat}" = "i386" ]; then plat="main/i386"; fi
			if [ "${plat}" = "x86_64" ]; then plat="main/x86_64"; fi
			if [ "${plat}" = "SRPMS" ]; then plat="main/SRPMS"; fi
			if [ "${plat}" = "debug/i386" ]; then plat="main/debug/i386"; fi
			if [ "${plat}" = "debug/x86_64" ]; then plat="main/debug/x86_64"; fi
			if [ "${plat}" = "nonfree/i386" ]; then plat="nonfree/main/i386"; fi
			if [ "${plat}" = "nonfree/x86_64" ]; then plat="nonfree/main/x86_64"; fi
			if [ "${plat}" = "nonfree/debug/i386" ]; then plat="nonfree/main/debug/i386"; fi
			if [ "${plat}" = "nonfree/debug/x86_64" ]; then plat="nonfree/main/debug/x86_64"; fi
		fi
		DIR=${BASEDIR}/${SITE}/$dist/$plat
		if [ -d "${DIR}" ]; then
			if [ ! -e "${DIR}/.rebuildrepo" -a -d "${BASEDIR}/cache/$dist/$plat" ]; then
				echo "@@@ Repo is current, bypassing rebuild for ${dist} ${plat}"
			else
				echo "@@@ Rebuilding repo for ${dist} ${plat}"
				repo_update='--update  --checkts  --skip-stat'
				if [ ! -d "${BASEDIR}/cache/$dist/$plat" ]; then
					mkdir -p ${BASEDIR}/cache/$dist/$plat
					repo_update=''
					if [ -d "${DIR}/repodata" ]; then
						rm -rf ${DIR}/repodata
						if [ -d "${DIR}/repoview" ]; then
							rm -rf ${DIR}/repoview
						fi
					fi
				fi
				if [ -L "${DIR}/comps.xml" ]; then rm -f ${DIR}/comps.xml; fi
				cp -puL ${BASEDIR}/comps.xml ${DIR}
				if [ -d "${DIR}/repodata" ]; then
					for oldprestodelta in `ls -t ${DIR}/repodata/*prestodelta.xml.gz | tail -n +2`; do
						rm -f ${oldprestodelta}
					done
				fi
				output_dir=${DIR}
				numdeltas=0
				#oldpkgdirs="--oldpackagedirs ${DIR}"
				oldpkgdirs=""
				if [ "${plat}" = "i386" -o "${plat}" = "main/i386" ]; then
					numdeltas=1
				elif [ "${plat}" = "x86_64" -o "${plat}" = "main/x86_64" ]; then
					numdeltas=1
				elif [ "${plat}" = "updates/i386" ]; then
					numdeltas=2
					#oldpkgdirs="${oldpkgdirs}  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/main/i386"
				elif [ "${plat}" = "updates/x86_64" ]; then
					numdeltas=2
					#oldpkgdirs="${oldpkgdirs}  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/main/x86_64"
				elif [ "${plat}" = "testing/i386" ]; then
					numdeltas=1
					#oldpkgdirs="${oldpkgdirs}  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/updates/i386  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/main/i386"
				elif [ "${plat}" = "testing/x86_64" ]; then
					numdeltas=1
					#oldpkgdirs="${oldpkgdirs}  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/updates/x86_64  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/main/x86_64"
				elif [ "${plat}" = "beta/i386" ]; then
					numdeltas=1
					#oldpkgdirs="${oldpkgdirs}  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/updates/i386  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/main/i386"
				elif [ "${plat}" = "beta/x86_64" ]; then
					numdeltas=1
					#oldpkgdirs="${oldpkgdirs}  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/updates/x86_64  --oldpackagedirs ${BASEDIR}/${SITE}/${dist}/main/x86_64"
				fi
				echo -e "    /usr/bin/createrepo -s sha  --profile --workers 4  --delta-workers 4  --max-concurrent-delta-rpm-size 2147483648  --compress-type gz  --unique-md-filenames  --changelog-limit 5  --database  ${crverbosity}  $repo_update  --deltas  --max-delta-rpm-size 10000000  --num-deltas ${numdeltas} \\ \n      ${oldpkgdirs} \\ \n      -g ${BASEDIR}/comps.xml  -c ${BASEDIR}/cache/$dist/$plat \\ \n      -o ${output_dir}  $DIR \n"
				sudo nice -n -20 /usr/bin/createrepo -s sha  --profile --workers 4  --delta-workers 4  --max-concurrent-delta-rpm-size 2147483648 --compress-type gz --unique-md-filenames  --changelog-limit 5  --database  ${crverbosity}  $repo_update  --deltas  --max-delta-rpm-size 10000000  --num-deltas ${numdeltas}  ${oldpkgdirs}  -g ${BASEDIR}/comps.xml -c ${BASEDIR}/cache/$dist/$plat -o ${output_dir} $DIR
				sudo nice -n -20 gpg --yes --detach-sign --armor ${output_dir}/repodata/repomd.xml 
				# Build repoview files.
				if [ -f "/usr/bin/repoview" ]; then
					DISTDIR=`echo "${dist}" | awk -F/ '{ print $1 }'`
					DISTNAME="Other"
					if [ "${DISTDIR}" = "el" ]; then
						DISTNAME="CentOS/EL"
					elif [ "${DISTDIR}" = "rhel" ]; then
						DISTNAME="RedHat/EL"
					elif [ "${DISTDIR}" = "fedora" ]; then
						DISTNAME="Fedora"
					fi
					DISTVER=`basename "${dist}"`
					ARCHNAME=`basename "${plat}"`
					output_dir=${DIR}
					echo -e "    /usr/bin/repoview -t \"${SITENAME} - ${DISTNAME} ${DISTVER} - ${ARCHNAME} - $plat\" \\ \n      ${rvverbosity}  -k ${BASEDIR}/repoview.template \\ \n      -c ${BASEDIR}/comps-repoview.xml  -u http://${SITE}/${dist}/${plat} \\ \n      -o ${output_dir}  ${DIR} \n"
					sudo nice -n -20 /usr/bin/repoview -t "${SITENAME} - ${DISTNAME} ${DISTVER} - ${ARCHNAME} - $plat" ${rvverbosity} -k ${BASEDIR}/repoview.template -c ${BASEDIR}/comps-repoview.xml -u http://${SITE}/${dist}/${plat} -o ${output_dir} ${DIR}
				fi
				sudo chown -R fiobuild:fiobuild ${DIR} ${BASEDIR}/cache/$dist/$plat
				echo
				echo
			fi
		fi
	done
done

cd ${BASEDIR}/${SITE}
find . -type f | awk '{ print "<a href=\""$1"\">"$1"</a><br />" }' > filelist.html
echo "Generated: `date`" >> filelist.html

echo '<?xml version="1.0" encoding="UTF-8"?>' > sitemap.xml
echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' >> sitemap.xml
# Get index.html
find -mindepth 1 -maxdepth 1 -name 'index.html' -exec ls -l --time-style=long-iso {} \;| sed -e 's/\.\///' | awk '{ print "  <url><priority>0.8</priority><changefreq>monthly</changefreq><lastmod>"$6"T"$7":00-05:00</lastmod><loc>http://rpm.fugitol.com/</loc></url>" }' >> sitemap.xml
# Get Levels 1-4
find -mindepth 1 -maxdepth 4 -name '*.html' -exec ls -l --time-style=long-iso {} \;| grep -v nonfree | sed -e 's/\.\///' | awk '{ print "  <url><priority>0.8</priority><changefreq>monthly</changefreq><lastmod>"$6"T"$7":00-05:00</lastmod><loc>http://rpm.fugitol.com/"$8"</loc></url>" }' >> sitemap.xml
# Get level 5
find -mindepth 5 -maxdepth 5 -name '*.html' -exec ls -l --time-style=long-iso {} \;| grep -v nonfree | sed -e 's/\.\///' | awk '{ print "  <url><priority>0.6</priority><changefreq>weekly</changefreq><lastmod>"$6"T"$7":00-05:00</lastmod><loc>http://rpm.fugitol.com/"$8"</loc></url>" }' >> sitemap.xml
# Get level 6
find -mindepth 6 -maxdepth 6 -name '*.html' -exec ls -l --time-style=long-iso {} \;| grep -v nonfree | sed -e 's/\.\///' | awk '{ print "  <url><priority>0.4</priority><changefreq>daily</changefreq><lastmod>"$6"T"$7":00-05:00</lastmod><loc>http://rpm.fugitol.com/"$8"</loc></url>" }' >> sitemap.xml
# Get level 7+
find -mindepth 7 -name '*.html' -exec ls -l --time-style=long-iso {} \;| grep -v nonfree | sed -e 's/\.\///' | awk '{ print "  <url><priority>0.4</priority><changefreq>daily</changefreq><lastmod>"$6"T"$7":00-05:00</lastmod><loc>http://rpm.fugitol.com/"$8"</loc></url>" }' >> sitemap.xml

echo '</urlset>' >> sitemap.xml

gzip -c sitemap.xml > sitemap.xml.gz
