#!/bin/bash

BASEDIR=/home/fiobuild/RPMS
BASEDIR_SSD=""
if [ -d "/home2/fiobuild/RPMS" ]; then BASEDIR_SSD=/home2/fiobuild/RPMS; fi
SITE=rpm.fugitol.com
SITENAME="Fugitol RPMs"

[ -z "${VERBOSE}" ] && verbosity="-q" || verbosity="-v"

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 el/6 el/5"
[ "${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" -a "${plat}" = "i386" ]; then plat="main/i386"; fi
		if [ "${dist}" = "el/6" -a "${plat}" = "x86_64" ]; then plat="main/x86_64"; fi
		if [ "${dist}" = "el/6" -a "${plat}" = "SRPMS" ]; then plat="main/SRPMS"; fi
		if [ "${dist}" = "el/6" -a "${plat}" = "debug/i386" ]; then plat="main/debug/i386"; fi
		if [ "${dist}" = "el/6" -a "${plat}" = "debug/x86_64" ]; then plat="main/debug/x86_64"; fi
		if [ "${dist}" = "el/6" -a "${plat}" = "nonfree/i386" ]; then plat="nonfree/main/i386"; fi
		if [ "${dist}" = "el/6" -a "${plat}" = "nonfree/x86_64" ]; then plat="nonfree/main/x86_64"; fi
		if [ "${dist}" = "el/6" -a "${plat}" = "nonfree/debug/i386" ]; then plat="nonfree/main/debug/i386"; fi
		if [ "${dist}" = "el/6" -a "${plat}" = "nonfree/debug/x86_64" ]; then plat="nonfree/main/debug/x86_64"; fi
		DIR=${BASEDIR}/${SITE}/$dist/$plat
		DIR_SSD=${BASEDIR_SSD}/repodata/$dist/$plat
		if [ -d "$DIR" ]; then
			echo "@@@ Rebuilding repo for ${dist} ${plat}"
			repo_update='--update --checkts'
			if [ ! -d "${BASEDIR}/cache/$dist/$plat" ]; then
				mkdir -p ${BASEDIR}/cache/$dist/$plat
				repo_update=''
			fi
			if [ -L "${DIR}/comps.xml" ]; then rm -f ${DIR}/comps.xml; fi
			cp -puL ${BASEDIR}/comps.xml ${DIR}
			output_dir=${DIR}
			if [ -n "${BASEDIR_SSD}" ]; then
				output_dir=${DIR_SSD}
				if [ ! -d "${DIR_SSD}" ]; then
					mkdir -p ${DIR_SSD}
					echo "  ### Creating repodata root ${DIR_SSD}"
				fi
				if [ ! -d "${DIR_SSD}/repodata" ]; then
					if [ -d "${DIR}/repodata" ]; then
						echo "  ### Copying existing ${DIR}/repodata to ${DIR_SSD}/repodata"
						cp -au ${DIR}/repodata ${DIR_SSD}
					fi
				fi
			fi
			echo -e "    /usr/bin/createrepo  -s sha  -d ${verbosity}  $repo_update \\ \n      -g ${BASEDIR}/comps.xml  -c ${BASEDIR}/cache/$dist/$plat \\ \n      -o ${output_dir}  $DIR \n"
			/usr/bin/createrepo -s sha -d ${verbosity} $repo_update -g ${BASEDIR}/comps.xml -c ${BASEDIR}/cache/$dist/$plat -o ${output_dir} $DIR
			if [ -n "${BASEDIR_SSD}" ]; then
				echo "  ### Updating ${DIR}/repodata with new data in ${DIR_SSD}/repodata"
				cp -au ${DIR_SSD}/repodata ${DIR}
			fi
			# 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}" = "fedora" ]; then
					DISTNAME="Fedora"
				fi
				DISTVER=`basename "${dist}"`
				ARCHNAME=`basename "${plat}"`
				output_dir=${DIR}
				if [ -n "${BASEDIR_SSD}" ]; then
					output_dir=${DIR_SSD}
					if [ ! -d "${DIR_SSD}" ]; then
						mkdir -p ${DIR_SSD}
						echo "  ### Creating repoview root ${DIR_SSD}"
					fi
					if [ ! -d "${DIR_SSD}/repoview" ]; then
						if [ -d "${DIR}/repoview" ]; then
							echo "  ### Copying existing ${DIR}/repoview to ${DIR_SSD}/repoview"
							cp -au ${DIR}/repoview ${DIR_SSD}
						fi
					fi
				fi
				echo -e "    /usr/bin/repoview -t "${SITENAME} - ${DISTNAME} ${DISTVER} - ${ARCHNAME} - $plat" \\ \n      ${verbosity}  -k ${BASEDIR}/repoview.template \\ \n      -c ${BASEDIR}/comps-repoview.xml  -u http://${SITE}/${dist}/${plat} \\ \n      -o ${output_dir}  ${DIR} \n"
				/usr/bin/repoview -t "${SITENAME} - ${DISTNAME} ${DISTVER} - ${ARCHNAME} - $plat" ${verbosity} -k ${BASEDIR}/repoview.template -c ${BASEDIR}/comps-repoview.xml -u http://${SITE}/${dist}/${plat} -o ${output_dir} ${DIR}
				if [ -n "${BASEDIR_SSD}" ]; then
					echo "  ### Updating ${DIR}/repoview with new data in ${DIR_SSD}/repoview"
					cp -au ${DIR_SSD}/repoview ${DIR}
				fi
			fi
			echo
			echo
		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
