#!/bin/csh -f
#projde vsecky podrizene podresare a vztvori y nich sitemap pro alpeh.osu.cz/. Matyas Bajger 20120525
date
echo "Initializing... (deleteing old sitempas and counting web files)"
foreach ocista ( `ls sitemap*.xml ` )
  rm $ocista
end
@ sitemap_no = 0
@ i=1
@ ii=1
@ fileCount = `ls * -lr | wc -l`
set sitemap_filename='sitemap00.xml'
echo "generating sitemap to file $sitemap_filename"
echo '<?xml version="1.0" encoding="UTF-8"?>' >$sitemap_filename
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' >>$sitemap_filename
foreach filename ( `find . ` )
   @ perc = ( ($ii * 100) / $fileCount )
   echo "processing $filename ( $perc% done, output $sitemap_filename )"
   echo $filename | sed 's/^\./<url><loc>http:\/\/katalog.osu.cz\/publish/' | sed 's/ *$/<\/loc><changefreq>daily<\/changefreq><priority>1.00<\/priority><\/url>/' >>$sitemap_filename
   @ i++
   @ ii++
   if ( $i > 4999 ) then
      echo '</urlset>' >>$sitemap_filename
      if ( $sitemap_no < 10 ) then
         set sitemap_no2 = "0$sitemap_no"
      else
         set sitemap_no2 = $sitemap_no
      endif
      set sitemap_filename = "sitemap$sitemap_no2.xml"
      echo "generating sitemap to file $sitemap_filename"
      echo '<?xml version="1.0" encoding="UTF-8"?>' >$sitemap_filename
      echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' >>$sitemap_filename
      @ i=1
      @ sitemap_no++
   endif
end
echo '</urlset>' >>$sitemap_filename

echo "------------------"; date
echo 'creating Sitemap Index'
grep '^Sitemap' -v $httpd_root/htdocs/robots.txt >$httpd_root/htdocs/robots.tmp
mv $httpd_root/htdocs/robots.tmp $httpd_root/htdocs/robots.txt
set sitemapIndex='Sitemap_index.xml'
echo '<?xml version="1.0" encoding="UTF-8"?>' >$sitemapIndex
echo '<sitemapindex xmlns="http://www.google.com/schemas/sitemap/0.84">' >$sitemapIndex
foreach sitemapFile ( `ls sitemap*.xml`)
   echo "<sitemap><loc>http://katalog.osu.cz/publish/$sitemapFile</loc></sitemap>" >>$sitemapIndex
   echo "Sitemap: http://katalog.osu.cz/publish/$sitemapFile"  >>$httpd_root/htdocs/robots.txt
   set bingURL='http://www.bing.com/webmaster/ping.aspx?siteMap=http%3A%2F%2Fkatalog.osu.cz%2Fpublish%2F'$sitemapFile
   curl -s "$bingURL"
end
echo '</sitemapindex>' >>$sitemapIndex

