#!/bin/sh

die () {
  echo "$1" >/dev/stderr
  Snarl_CMD snShowMessage 10 "Dive Into Python 3" "$1." "C:\Users\pilgrim\site-lisp\todochiku-icons\alert.png"
  exit 1
}

echo "started build"

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

echo "minimizing HTML"

# minimize HTML (NB: this script is quite fragile and relies on knowledge of how I write HTML)
for f in *.html; do
  python3 util/htmlminimizer.py "$f" build/"$f" || die "Failed to minimize $f"
done

# build sitemap
ls build/*.html | sed -e "s|build/|http://diveintopython3.org/|g" -e "s|/index.html|/|g" > build/sitemap.txt || die "Failed to build sitemap"

echo "adding evil tracking code"

# add Google Analytics script
for f in build/*.html; do
  cat "$f" j/ga.js > build/tmp && \
      mv build/tmp "$f" || die "Could not add tracking code to $f"
done

# minimize JS and CSS
echo "minimizing JS"
revision=`hg log|grep changeset|cut -d":" -f3|head -1`
java -jar util/yuicompressor-2.4.2.jar build/j/prettify.js > build/j/prettify.min.js && \
    java -jar util/yuicompressor-2.4.2.jar build/j/dip3.js > build/j/dip3.min.js || die "Failed to minimize JS"

# combine jQuery and our script
echo "combining JS"
cat build/j/jquery.min.js build/j/prettify.min.js build/j/dip3.min.js > build/j/$revision.js && \
    sed -i -e "s|<script src=j/jquery.js></script>||g" build/*.html && \
    sed -i -e "s|<script src=j/prettify.js></script>||g" build/*.html && \
    sed -i -e "s|<script src=j/dip3.js>|<script src=j/${revision}.js>|g" build/*.html || die "Failed to combine JS"

echo "minimizing CSS"
java -jar util/yuicompressor-2.4.2.jar build/dip3.css > build/$revision.css && \
    java -jar util/yuicompressor-2.4.2.jar build/mobile.css > build/m-$revision.css && \
    java -jar util/yuicompressor-2.4.2.jar build/print.css > build/p-$revision.css && \
    sed -i -e "s|;}|}|g" build/$revision.css && \
    sed -i -e "s|;}|}|g" build/m-$revision.css && \
    sed -i -e "s|;}|}|g" build/p-$revision.css || die "Failed to minimize CSS"

# put CSS inline
echo "inlining CSS"
css=`cat build/$revision.css`
mobilecss=`cat build/m-$revision.css`
printcss=`cat build/p-$revision.css`
sed -i -e "s|<link rel=stylesheet href=dip3.css>|<style>${css}</style>|g" -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 media=print href=print.css>|<style>@media print{${printcss}}</style>|g" -e "s|</style><style>||g" build/*.html || die "Failed to inline CSS"

# remove unused CSS properties on a page-by-page basis
echo "removing unused CSS"
for f in build/*.html; do
  python2.6 util/lesscss.py "$f" || die "Failed to remove unused CSS"
done

# minimize URLs
echo "minimizing URLs"
sed -i -e "s|=http:|=|g" build/*.html && \
    sed -i -e "s|href=index.html|href=/|g" build/*.html || die "Failed to minimize URLs"

# set file permissions (hg resets these, don't know why)
chmod 755 build/examples build/j build/i && \
    chmod 644 build/*.html build/*.css build/*.txt build/examples/* build/examples/.htaccess build/j/* build/j/.htaccess build/i/* build/i/.htaccess build/.htaccess || die "Failed to reset file permissions"

# ship it!
echo -n "publishing"
rsync -essh -a build/i/* build/i/.htaccess diveintomark.org:~/web/wearehugh.com/dip3/ && \
    echo -n "." && \
    rsync -essh -a build/j/$revision.js build/j/html5.js build/j/.htaccess diveintomark.org:~/web/diveintopython3.org/j/ && \
    echo -n "." && \
    rsync -essh -a build/examples build/*.txt build/*.html build/.htaccess diveintomark.org:~/web/diveintopython3.org/ && \
    echo "." || die "Failed to publish to remote server"

[ -n "$(which Snarl_CMD 2>/dev/null)" ] && Snarl_CMD snShowMessage 10 "Dive Into Python 3" "Published." "C:\Users\pilgrim\site-lisp\todochiku-icons\clean.png"
