# Make sure we start in the correct directory
if ! [[ -d ../.git ]]; then
  echo "Call this script from the docs dir."
  exit
fi

installed_ver=`python -c "import scikits.image; print scikits.image.__version__"`
if [ $installed_ver = "unbuilt-dev" ]; then
   echo "Please build the scikit before generating the docs."
   exit 1
fi

# Get current branch
ref=$(git symbolic-ref HEAD 2> /dev/null)
branch=${ref#refs/heads/}
wd=`pwd`

make clean
make html
rm -rf /tmp/scikits.image.docs
cp -r build/html /tmp/scikits.image.docs
cd ..

# This file changes upon build, so we have to back it up,
# otherwise git won't switch branches
ignore_files="scikits/image/version.py
              scikits/image/opencv/opencv_cv.so
              scikits/image/opencv/opencv_backend.so
              scikits/image/graph/spath.so"
mkdir -p /tmp/_scikits_image_backup
for f in $ignore_files; do
  cp $f /tmp/_scikits_image_backup
done
git co scikits/image/version.py

git co gh-pages || exit
rm -rf ./*
cp -r /tmp/scikits.image.docs/* .

# github doesn't like directories starting with "_"
sed -i "" 's/_static/static/g' `find . -name "*.html"`
sed -i "" 's/_images/images/g' `find . -name "*.html"`
mv _static static
mv _images images
for f in `find . | grep "./" | grep -v ".git"`; do
   git add $f
done

echo "--------------------------------------"
git status
echo "--------------------------------------"

echo "***"
echo "*** These changes are about to be committed.  Press Ctrl-C to abort."
echo "*** Press enter to continue."
read

git commit -m "Update docs."
git push origin gh-pages
git co $branch

for f in $ignore_files; do
  cp /tmp/_scikits_image_backup/`basename $f` $f
done

