#!/bin/sh

# make build directory and copy original files there for preflighting
rm -rf build
mkdir build
cp robots.txt *.js *.css build/
rm -f examples/*.pyc
cp -R examples build/

# minimize HTML (XXX this script is quite fragile and relies on knowledge of how I write HTML)
for f in *.html; do
  python htmlminimizer.py "$f" build/"$f"
done

# add evil tracking code
for f in build/*.html; do
  cat "$f" ga.js > build/tmp
  mv build/tmp "$f"
done

# jQuery will be served by Google AJAX Libraries API
sed -i -e "s|jquery\.js|http://www.google.com/jsapi|g" build/*.html
sed -i -e "s|//google\.|google.|g" build/dip3.js
sed -i -e "s|//}.; /\* google\..*|});|g" build/dip3.js

# minimize JS and CSS
revision=`hg log|grep changeset|cut -d":" -f3|head -1`
java -jar yuicompressor-2.4.2.jar build/dip3.js > build/$revision.js
java -jar yuicompressor-2.4.2.jar build/dip3.css > build/$revision.css
java -jar yuicompressor-2.4.2.jar build/mobile.css > build/m-$revision.css
sed -i -e "s|;}|}|g" build/$revision.css
sed -i -e "s|;}|}|g" build/m-$revision.css

# put CSS inline
css=`cat build/$revision.css`
mobilecss=`cat build/m-$revision.css`
#sed -i -e "s|<link rel=stylesheet media='only screen and (max-device-width: 480px)' href=mobile.css>|<style>@media screen and (max-device-width:480px)\{${mobilecss}\}</style>|g" -e "s|<link rel=stylesheet href=dip3.css>|<style>${css}</style>|g" -e "s|</style><style>||g" build/*.html
sed -i -e "s|<link rel=stylesheet href=dip3.css>|<style>${css}</style>|g" -e "s|</style><style>||g" build/*.html
sed -i -e "s|href=mobile.css|href=http://wearehugh.com/dip3/m-${revision}.css|g" build/*.html

# JS will be served from a separate domain
sed -i -e "s|dip3\.js|http://wearehugh.com/dip3/${revision}.js|g" build/*.html
sed -i -e "s|html5\.js|http://wearehugh.com/dip3/html5.js|g" build/*.html

# images would be served from a separate domain if we had any, which we currently don't
#sed -i -e "s|bsb.png|http://wearehugh.com/dip3/bsb.png|g" build/*.html

# minimize URLs
sed -i -e "s|=http:|=|g" build/*.html
sed -i -e "s|href=index.html|href=/|g" build/*.html

# set file permissions (hg resets these, don't know why)
chmod 644 build/*.html build/*.css build/*.js build/examples/*.py build/examples/*.txt build/*.txt

# ship it!
rsync -essh -avzP build/$revision.js build/html5.js build/m-$revision.css diveintomark.org:~/web/wearehugh.com/dip3/
rsync -essh -avzP build/*.html build/examples build/*.txt diveintomark.org:~/web/diveintopython3.org/
